From 7fb1895f752d0dcbca6bcedbb712c72496f32cf4 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Sun, 25 Apr 2021 13:59:19 +0200 Subject: [PATCH] Improved level sweep, RSSI, mute on ADF step --- main.c | 2 +- nanovna.h | 4 ++++ sa_cmd.c | 11 +++++++---- sa_core.c | 43 ++++++++++++++++++++++++++++++++++++------- si4468.c | 50 ++++++++++++++++++++++++++++++++++++-------------- ui_sa.c | 8 ++++++++ 6 files changed, 92 insertions(+), 26 deletions(-) diff --git a/main.c b/main.c index d458f83..707e9d9 100644 --- a/main.c +++ b/main.c @@ -2509,7 +2509,7 @@ static const VNAShellCommand commands[] = #endif { "e", cmd_e, CMD_WAIT_MUTEX }, { "s", cmd_s, CMD_WAIT_MUTEX }, - { "m", cmd_m, CMD_WAIT_MUTEX }, + { "m", cmd_m, 0 }, { "p", cmd_p, CMD_WAIT_MUTEX }, { "w", cmd_w, CMD_WAIT_MUTEX }, { "o", cmd_o, CMD_WAIT_MUTEX }, diff --git a/nanovna.h b/nanovna.h index d55dc06..1ef3568 100644 --- a/nanovna.h +++ b/nanovna.h @@ -80,6 +80,9 @@ #define __HARMONIC__ #define __VBW__ #define __SWEEP_RESTART__ +#define DB_PER_DEGREE_BELOW 0.056 +#define DB_PER_DEGREE_ABOVE 0.069 +#define CENTER_TEMPERATURE 34.0 #else #endif @@ -305,6 +308,7 @@ extern int test_output; extern int test_output_switch; extern int test_output_drive; extern int test_output_attenuate; +extern bool level_error; #else extern const int8_t drive_dBm []; #endif diff --git a/sa_cmd.c b/sa_cmd.c index d04cfdf..cdd3017 100644 --- a/sa_cmd.c +++ b/sa_cmd.c @@ -808,7 +808,9 @@ VNA_SHELL_FUNCTION(cmd_m) // update_rbw(); chThdSleepMilliseconds(10); sweep_mode = SWEEP_REMOTE; -// update_rbw(); + while (sweep_mode != 0) + chThdSleepMilliseconds(10); + // update_rbw(); } VNA_SHELL_FUNCTION(cmd_p) @@ -1000,7 +1002,7 @@ VNA_SHELL_FUNCTION(cmd_q) switch (m) { case -1: goto usage; case 0: test_output_switch = true; break; - case 1: test_output_drive = MAX_DRIVE - atoi(argv[i++]); argc--; break; + case 1: test_output_drive = atoi(argv[i++]); argc--; break; case 2: test_output_attenuate = atoi(argv[i++]); argc--; break; } goto again; @@ -1009,8 +1011,9 @@ VNA_SHELL_FUNCTION(cmd_q) extern float Si446x_get_temp(void); VNA_SHELL_FUNCTION(cmd_k) { - float value; - shell_printf("temperature: %f\r\n", Si446x_get_temp()); + (void)argc; + (void)argv; + shell_printf("%.2f\r\n", Si446x_get_temp()); } diff --git a/sa_core.c b/sa_core.c index 8207c5c..33fc451 100644 --- a/sa_core.c +++ b/sa_core.c @@ -285,7 +285,7 @@ void reset_settings(int m) break; case M_GENLOW: #ifdef TINYSA4 - setting.rx_drive=MAX_DRIVE; + setting.rx_drive= MAX_DRIVE; setting.lo_drive=1; #else // setting.rx_drive=8; @@ -2513,6 +2513,7 @@ int test_output_switch = false; int test_output_drive = 0; int test_output_attenuate = 0; int start_temperature = 0; +bool level_error = false; #endif pureRSSI_t perform(bool break_on_operation, int i, freq_t f, int tracking) // Measure the RSSI for one frequency, used from sweep and other measurement routines. Must do all HW setup @@ -2629,7 +2630,13 @@ pureRSSI_t perform(bool break_on_operation, int i, freq_t f, int tracking) / correct_RSSI_freq = get_frequency_correction(f); a += PURE_TO_float(correct_RSSI_freq); #ifdef TINYSA4 - a += (Si446x_get_temp() - 34.0) * 0.0433; // Temperature correction + { + float dt = Si446x_get_temp() - CENTER_TEMPERATURE; + if (dt > 0) + a += dt * DB_PER_DEGREE_ABOVE; // Temperature correction + else + a += dt * DB_PER_DEGREE_BELOW; // Temperature correction + } a += 3.0; // Always 3dB in attenuator #endif if (a != old_a) { @@ -2671,7 +2678,7 @@ pureRSSI_t perform(bool break_on_operation, int i, freq_t f, int tracking) / while (a - BELOW_MAX_DRIVE(d) > 0 && d < MAX_DRIVE) { // Increase if needed d++; } - while (a - BELOW_MAX_DRIVE(d) < - 31 && d > LOWEST_LEVEL) { // reduce till it fits attenuator + while (a - BELOW_MAX_DRIVE(d) < - 28 && d > LOWEST_LEVEL) { // reduce till it fits attenuator (31 - 3) d--; } a -= BELOW_MAX_DRIVE(d); @@ -2685,8 +2692,18 @@ pureRSSI_t perform(bool break_on_operation, int i, freq_t f, int tracking) / #ifdef TINYSA4 a -= 3.0; // Always at least 3dB attenuation #endif - if (a > 0) + if (a > 0) { a = 0; +#ifdef TINYSA4 + if (!level_error) redraw_request |= REDRAW_CAL_STATUS; + level_error = true; +#endif + } else { +#ifdef TINYSA4 + if (level_error) redraw_request |= REDRAW_CAL_STATUS; + level_error = false; +#endif + } if (a < -31.5) a = -31.5; a = -a - 0.25; // Rounding @@ -2698,10 +2715,22 @@ pureRSSI_t perform(bool break_on_operation, int i, freq_t f, int tracking) / } } else if (setting.mode == M_GENHIGH) { +#ifdef TINYSA4 + if (test_output) { + enable_rx_output(!test_output_switch); + SI4463_set_output_level(test_output_drive); + } else +#endif + { float a = setting.level - level_max(); #ifdef TINYSA4 - if (!config.high_out_adf4350) - a += (Si446x_get_temp() - 34.0) * 0.0433; // Temperature correction + if (!config.high_out_adf4350) { + float dt = Si446x_get_temp() - CENTER_TEMPERATURE; + if (dt > 0) + a += dt * DB_PER_DEGREE_ABOVE; // Temperature correction + else + a += dt * DB_PER_DEGREE_BELOW; // Temperature correction + } #endif if (a <= -SWITCH_ATTENUATION) { setting.atten_step = true; @@ -2745,7 +2774,7 @@ pureRSSI_t perform(bool break_on_operation, int i, freq_t f, int tracking) / else SI4463_set_output_level(d); #endif - + } } } #ifdef __SI4432__ diff --git a/si4468.c b/si4468.c index b0cc0d2..85fcddb 100644 --- a/si4468.c +++ b/si4468.c @@ -108,7 +108,7 @@ void stop_SI4432_SPI_mode(void){ static void shiftOut(uint8_t val) { #ifdef USE_HARDWARE_SPI_MODE -// while (SPI_TX_IS_NOT_EMPTY(SI4432_SPI)); + while (SPI_TX_IS_NOT_EMPTY(SI4432_SPI)); SPI_WRITE_8BIT(SI4432_SPI, val); while (SPI_IS_BUSY(SI4432_SPI)) // drop rx and wait tx (void)SPI_READ_8BIT(SI4432_SPI); @@ -130,8 +130,8 @@ static uint8_t shiftIn(void) #ifdef USE_HARDWARE_SPI_MODE // while (SPI_TX_IS_NOT_EMPTY(SI4432_SPI)); SPI_WRITE_8BIT(SI4432_SPI, 0xFF); - while (SPI_IS_BUSY(SI4432_SPI)) // drop rx and wait tx - while (SPI_RX_IS_EMPTY(SI4432_SPI)); //wait rx data in buffer + while (SPI_IS_BUSY(SI4432_SPI) || SPI_RX_IS_EMPTY(SI4432_SPI)) ; // drop rx and wait tx +// while (); //wait rx data in buffer return SPI_READ_8BIT(SI4432_SPI); #else uint32_t value = 0; @@ -703,6 +703,8 @@ uint64_t ADF4351_prepare_frequency(int channel, uint64_t freq) // freq / 10Hz // my_microsecond_delay(10); // } + bitWrite (registers[4], 10, 1); // Mute till lock detect + registers[0] = 0; registers[0] = INTA << 15; // OK registers[0] = registers[0] + (FRAC << 3); @@ -868,7 +870,7 @@ static uint8_t SI4463_wait_response(void* buff, uint8_t len, uint8_t use_timeout return 1; } -#define SI_FAST_SPEED SPI_BR_DIV2 +#define SI_FAST_SPEED SPI_BR_DIV4 void SI4463_do_api(void* data, uint8_t len, void* out, uint8_t outLen) { @@ -883,8 +885,10 @@ void SI4463_do_api(void* data, uint8_t len, void* out, uint8_t outLen) __disable_irq(); SI_CS_LOW; + while(SPI_RX_IS_NOT_EMPTY(SI4432_SPI)) (void)SPI_READ_8BIT(SI4432_SPI); // Remove lingering bytes + for(uint8_t i=0;i old_t) { + if (t < old_t +100 && t + 100 > old_t) { // avoid oscillation t = (t + old_t) >> 1; } old_t = t; @@ -1397,6 +1416,7 @@ again: data[0] = SI446X_CMD_GET_MODEM_STATUS; data[1] = 0xFF; SI4463_do_api(data, 1, data, 3); // TODO no clear of interrups +#if 0 if (data[2] == 0) { if (i > 0) data[2] = age[i-1]; @@ -1404,6 +1424,7 @@ again: goto again; } if (data[2] == 255) goto again; +#endif if (i >= 0) age[i]=(char)data[2]; // Skip first RSSI #else @@ -1513,7 +1534,7 @@ int16_t Si446x_RSSI(void) int32_t RSSI_RAW = 0; do{ // if (MODE_INPUT(setting.mode) && RSSI_R - uint8_t data[3] = { + uint8_t data[4] = { SI446X_CMD_GET_MODEM_STATUS, 0xFF }; @@ -1527,14 +1548,15 @@ int16_t Si446x_RSSI(void) // __disable_irq(); data[0] = SI446X_CMD_GET_MODEM_STATUS; data[1] = 0xFF; - SI4463_do_api(data, 2, data, 3); // TODO no clear of interrupts + SI4463_do_api(data, 2, data, 4); // TODO no clear of interrupts // __enable_irq(); #else data[2] = getFRR(SI446X_CMD_READ_FRR_A); #endif - if (data[2] == 255) { + if (data[0] == 255) { my_microsecond_delay(10); goto again; +// data[2] = data[3]; } #if 0 if (data[2] == 0) { diff --git a/ui_sa.c b/ui_sa.c index 28da2a2..65bae12 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -3114,6 +3114,14 @@ redraw_cal_status: ili9341_fill(0, 0, OFFSETX, LCD_HEIGHT); max_quick_menu = 0; if (MODE_OUTPUT(setting.mode)) { // No cal status during output +#ifdef TINYSA4 + if (level_error) { + color = LCD_BRIGHT_COLOR_RED; + ili9341_set_foreground(color); + ili9341_drawstring("LEVEL", 0 , 80); + ili9341_drawstring("ERROR", 0 , 90); + } +#endif return; }