From ba165a251e469225fd4ed4e38484d6dbbac75b42 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Sun, 30 May 2021 08:23:25 +0200 Subject: [PATCH 1/3] removes auto loading of config.ini --- main.c | 22 ++++++++++++++-------- ui_sa.c | 4 ++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/main.c b/main.c index 9bb3b95..62250fb 100644 --- a/main.c +++ b/main.c @@ -118,6 +118,10 @@ static THD_FUNCTION(Thread1, arg) (void)arg; chRegSetThreadName("sweep"); +#ifdef __SD_CARD_LOAD__ + sd_card_load_config("autoload.ini"); +#endif + #ifndef TINYSA4 ui_process(); #endif @@ -241,10 +245,12 @@ toggle_sweep(void) int shell_printf(const char *fmt, ...) { va_list ap; - int formatted_bytes; - va_start(ap, fmt); - formatted_bytes = chvprintf(shell_stream, fmt, ap); - va_end(ap); + int formatted_bytes = 0; + if (shell_stream) { + va_start(ap, fmt); + formatted_bytes = chvprintf(shell_stream, fmt, ap); + va_end(ap); + } return formatted_bytes; } @@ -2024,6 +2030,9 @@ void sd_card_load_config(char *filename){ if (f_open(fs_file, filename, FA_OPEN_EXISTING | FA_READ) != FR_OK) return; + BaseSequentialStream *old_shell_stream = shell_stream; + shell_stream = (BaseSequentialStream *)NULL; + char *buf = (char *)spi_buffer; UINT size = 0; @@ -2052,6 +2061,7 @@ void sd_card_load_config(char *filename){ } } f_close(fs_file); + shell_stream = old_shell_stream; return; } #endif @@ -2272,10 +2282,6 @@ int main(void) chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO-1, Thread1, NULL); -#ifdef __SD_CARD_LOAD__ - sd_card_load_config("config.ini"); -#endif - while (1) { // if (SDU1.config->usbp->state == USB_ACTIVE) { if (shell_check_connect()) { diff --git a/ui_sa.c b/ui_sa.c index 06ce952..4e48454 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -790,7 +790,7 @@ static UI_FUNCTION_ADV_CALLBACK(menu_store_preset_acb) UI_FUNCTION_CALLBACK(menu_load_config_cb) { (void)item; - sd_card_load_config((data?"config.ini":"setting.ini")); + sd_card_load_config("config.ini"); ui_mode_normal(); } #endif @@ -2518,7 +2518,7 @@ static const menuitem_t menu_settings4[] = #endif #ifdef __SD_CARD_LOAD__ { MT_CALLBACK, 0 , "LOAD\nCONFIG.INI", menu_load_config_cb}, - { MT_CALLBACK, 1 , "LOAD\nSETTING.INI", menu_load_config_cb}, +// { MT_CALLBACK, 1 , "LOAD\nSETTING.INI", menu_load_config_cb}, #endif { MT_CALLBACK, 0 , "CLEAR\nCONFIG", menu_clearconfig_cb}, #ifdef __HARMONIC__ From 5f13d6e0977ba045b2d80b0c926d7fea1c733173 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Sun, 30 May 2021 11:53:05 +0200 Subject: [PATCH 2/3] Separate NG gain from external amp --- nanovna.h | 1 + plot.c | 8 ++++---- sa_core.c | 6 +++--- si4468.c | 16 ++++++++-------- ui_sa.c | 13 +++++++++---- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/nanovna.h b/nanovna.h index 647fa53..4f84bd0 100644 --- a/nanovna.h +++ b/nanovna.h @@ -1190,6 +1190,7 @@ void clear_all_config_prop_data(void); extern void ui_init(void); extern void ui_process(void); int current_menu_is_form(void); +extern float nf_gain; void ui_mode_normal(void); void ui_mode_menu(void); diff --git a/plot.c b/plot.c index 83bd992..5a63cec 100644 --- a/plot.c +++ b/plot.c @@ -1602,10 +1602,10 @@ static void cell_draw_marker_info(int x0, int y0) aNP = marker_to_value(0); #endif float mNF = aNP - logf(actual_rbw_x10*100.0) * (10.0/logf(10.0)) + 173.93 + SI4463_noise_correction_x10/10.0; // measured noise figure at 20C - if (setting.external_gain != 0) { - float mnf = expf(mNF/10 * logf(10)); // measure noise factor + if (nf_gain != 0) { + float mnf = expf((mNF - nf_gain)/10 * logf(10)); // measure noise factor float tnf = expf(config.noise_figure/10 * logf(10)); // tinySA noise factor - float amp_gain = expf(setting.external_gain/10 * logf(10)); + float amp_gain = expf(nf_gain/10 * logf(10)); float anf = mnf - (tnf - 1.0)/amp_gain; mNF = 10*logf(anf)/logf(10); } @@ -1617,7 +1617,7 @@ static void cell_draw_marker_info(int x0, int y0) // j = 1; int xpos = 1 + (j%2)*(WIDTH/2) + CELLOFFSETX - x0; int ypos = 1 + (j/2)*(16) - y0; - cell_printf(xpos, ypos, FONT_b"NF: %4.1f", mNF); + cell_printf(xpos, ypos, FONT_b"GAIN: %4.1fdB NF: %4.1f", nf_gain, mNF); break; #endif } else diff --git a/sa_core.c b/sa_core.c index c73c695..a401b37 100644 --- a/sa_core.c +++ b/sa_core.c @@ -4780,7 +4780,7 @@ enum { #ifdef TINYSA4 //#define CAL_LEVEL -23.5 -#define CAL_LEVEL -23 +#define CAL_LEVEL -23.1 #else #define CAL_LEVEL (has_esd ? -26.2 : -25) #endif @@ -4833,8 +4833,8 @@ const test_case_t test_case [] = #define TEST_SPUR 23 TEST_CASE_STRUCT(TC_BELOW, TP_SILENT, 144, 8, -95, 0, 0), // 22 Measure 48MHz spur #define TEST_LEVEL 24 - TEST_CASE_STRUCT(TC_LEVEL, TP_30MHZ, 30, 0, CAL_LEVEL, 145, -55), // 23 Measure level - TEST_CASE_STRUCT(TC_LEVEL, TP_30MHZ_LNA, 30, 0, CAL_LEVEL, 145, -55), // 23 Measure level + TEST_CASE_STRUCT(TC_LEVEL, TP_30MHZ, 30.01, 0, CAL_LEVEL, 145, -55), // 23 Measure level + TEST_CASE_STRUCT(TC_LEVEL, TP_30MHZ_LNA, 30.01, 0, CAL_LEVEL, 145, -55), // 23 Measure level TEST_CASE_STRUCT(TC_LEVEL, TPH_30MHZ, 150, 0, CAL_LEVEL-30, 145, -55), // 23 Measure level }; diff --git a/si4468.c b/si4468.c index cca7a1a..8da173b 100644 --- a/si4468.c +++ b/si4468.c @@ -1524,14 +1524,14 @@ static const RBW_t RBW_choices[] = { // BW register corr freq {SI4463_RBW_02kHz, 18,3,42}, - {SI4463_RBW_1kHz, 18,10,22}, - {SI4463_RBW_3kHz, 12,30,21}, - {SI4463_RBW_10kHz, 7,100,20}, - {SI4463_RBW_30kHz, 10,300,20}, - {SI4463_RBW_100kHz,2,1000,26}, - {SI4463_RBW_300kHz,2,3000,19}, - {SI4463_RBW_600kHz,3,6000,18}, - {SI4463_RBW_850kHz,12,8500,15}, + {SI4463_RBW_1kHz, 18,10,29}, + {SI4463_RBW_3kHz, 12,30,28}, + {SI4463_RBW_10kHz, 7,100,26}, + {SI4463_RBW_30kHz, 10,300,22}, + {SI4463_RBW_100kHz,2,1000,19}, + {SI4463_RBW_300kHz,2,3000,7}, + {SI4463_RBW_600kHz,3,6000,-1}, + {SI4463_RBW_850kHz,12,8500,-9}, }; const uint8_t SI4432_RBW_count = ((int)(sizeof(RBW_choices)/sizeof(RBW_t))); diff --git a/ui_sa.c b/ui_sa.c index 4e48454..23c9c30 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -1151,6 +1151,7 @@ static UI_FUNCTION_CALLBACK(menu_clearconfig_cb) ui_mode_normal(); } +float nf_gain; const char * const averageText[] = { "OFF", "MIN", "MAX", "MAXD", " A 4", "A 16", "AVER", "QUASI", "DECONV"}; static UI_FUNCTION_ADV_CALLBACK(menu_measure_acb) @@ -1348,13 +1349,17 @@ static UI_FUNCTION_ADV_CALLBACK(menu_measure_acb) markers[0].mtype = M_NOISE; // Not tracking set_extra_lna(true); kp_help_text = "Amplifier Gain "; + float old_gain = setting.external_gain; ui_mode_keypad(KM_EXT_GAIN); - kp_help_text = "Noise frequency"; + nf_gain = setting.external_gain; + setting.external_gain = old_gain; + kp_help_text = "Noise center frequency"; ui_mode_keypad(KM_CENTER); set_marker_frequency(0, uistat.value); -// kp_help_text = "Noise width"; -// ui_mode_keypad(KM_SPAN); - set_sweep_frequency(ST_SPAN, 0); + kp_help_text = "Noise span"; + ui_mode_keypad(KM_SPAN); + set_RBW(get_sweep_frequency(ST_SPAN)/100 / 100); +// set_sweep_frequency(ST_SPAN, 0); set_average(AV_100); break; #endif From b6822ab881205192c7d30697ef3d0c7676c15a90 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Sun, 30 May 2021 17:34:30 +0200 Subject: [PATCH 3/3] Improved selftest 7 --- nanovna.h | 1 + sa_core.c | 58 +++++++++++++++++++++++++++++++++++-------------------- si4468.c | 27 +++++++++++++++----------- 3 files changed, 54 insertions(+), 32 deletions(-) diff --git a/nanovna.h b/nanovna.h index 4f84bd0..a35c5e3 100644 --- a/nanovna.h +++ b/nanovna.h @@ -1446,6 +1446,7 @@ extern bool ADF4351_frequency_changed; extern bool SI4463_frequency_changed; extern bool SI4463_offset_changed; extern int16_t SI4463_noise_correction_x10; +void switch_SI4463_RSSI_correction(bool); extern int old_R; extern float Si446x_get_temp(void); #define ENBW_Hz SI4463_ENBW_Hz diff --git a/sa_core.c b/sa_core.c index a401b37..9264147 100644 --- a/sa_core.c +++ b/sa_core.c @@ -4833,8 +4833,8 @@ const test_case_t test_case [] = #define TEST_SPUR 23 TEST_CASE_STRUCT(TC_BELOW, TP_SILENT, 144, 8, -95, 0, 0), // 22 Measure 48MHz spur #define TEST_LEVEL 24 - TEST_CASE_STRUCT(TC_LEVEL, TP_30MHZ, 30.01, 0, CAL_LEVEL, 145, -55), // 23 Measure level - TEST_CASE_STRUCT(TC_LEVEL, TP_30MHZ_LNA, 30.01, 0, CAL_LEVEL, 145, -55), // 23 Measure level + TEST_CASE_STRUCT(TC_LEVEL, TP_30MHZ, 30.000, 0, CAL_LEVEL, 145, -55), // 23 Measure level + TEST_CASE_STRUCT(TC_LEVEL, TP_30MHZ_LNA, 30.000, 0, CAL_LEVEL, 145, -55), // 23 Measure level TEST_CASE_STRUCT(TC_LEVEL, TPH_30MHZ, 150, 0, CAL_LEVEL-30, 145, -55), // 23 Measure level }; @@ -5657,11 +5657,16 @@ quit: shell_printf("%d: %9.3q\n\r",i, peakFreq); test_validate(TEST_SPUR); // Validate test } - } else if (false && test == 7) { // RBW level test +#ifdef TINYSA4 + } else if (test == 7) { // RBW level test in_selftest = true; ui_mode_normal(); + set_scale(2); + set_reflevel(-22); shell_printf("\n\r"); - float first_level=0; + float first_level=-23.2; + setting.R = 3; + switch_SI4463_RSSI_correction(false); for (int j= SI4432_RBW_count-1; j >= 0; j-- ) { if (setting.test_argument != 0) j = setting.test_argument; @@ -5671,29 +5676,40 @@ quit: setting.spur_removal = S_ON; test_acquire(TEST_LEVEL); // Acquire test test_validate(TEST_LEVEL); // Validate test - if (j == SI4432_RBW_count-1) - first_level = peakLevel; - shell_printf("RBW = %7.1fk, level = %6.2f, corr = %6.2f\n\r",actual_rbw_x10/10.0 , peakLevel, (first_level - peakLevel + 1.5)*10.0 ); + // if (j == SI4432_RBW_count-1) + // first_level = peakLevel; + shell_printf("RBW = %7.1fk, level = %6.2f, corr = %6.2f\n\r",actual_rbw_x10/10.0 , peakLevel, (first_level - peakLevel)*10.0 ); if (setting.test_argument != 0) break; } -#if 0 // Does not center on frequency!!!!! - shell_printf("\n\r"); - for (int j= SI4432_RBW_count-1; j >= 0; j-- ) { - if (setting.test_argument != 0) - j = setting.test_argument; - test_prepare(TEST_LEVEL+2); - setting.rbw_x10 = force_rbw(j); - test_acquire(TEST_LEVEL+2); // Acquire test - test_validate(TEST_LEVEL+2); // Validate test - if (j == SI4432_RBW_count-1) - first_level = peakLevel; - shell_printf("RBW = %7.1fk, level = %6.2f, corr = %6.2f\n\r",actual_rbw_x10/10.0 , peakLevel, (first_level - peakLevel + 1.5)*10.0 ); - if (setting.test_argument != 0) - break; +#if 1 // Does not center on frequency!!!!! + + for (int k = 0; k< 4; k++) { + shell_printf("\n\r%d ", k); + for (int j= SI4432_RBW_count-1; j >= 0; j-- ) { + if (setting.test_argument != 0) + j = setting.test_argument; + test_prepare(TEST_RBW); +// setting.step_delay_mode = SD_PRECISE; + set_repeat(5); + setting.rbw_x10 = force_rbw(j); + set_sweep_frequency(ST_SPAN, (freq_t)(setting.rbw_x10 * (1000 << k))); + test_acquire(TEST_RBW); // Acquire test + test_validate(TEST_RBW); // Validate test +// if (j == SI4432_RBW_count-1) +// first_level = peakLevel; +// shell_printf("RBW = %7.1fk, level = %6.2f, corr = %6.2f\n\r",actual_rbw_x10/10.0 , peakLevel, (first_level - peakLevel)*10.0 ); + shell_printf("%6.2f ", (first_level - peakLevel)*10.0 ); + if (setting.test_argument != 0) + break; + } } #endif + shell_printf("\n\r"); + setting.R = 0; + switch_SI4463_RSSI_correction(true); reset_settings(M_LOW); +#endif } show_test_info = FALSE; diff --git a/si4468.c b/si4468.c index 8da173b..3a4c478 100644 --- a/si4468.c +++ b/si4468.c @@ -1360,7 +1360,7 @@ int16_t Si446x_RSSI(void) // SI4463_WAIT_CTS; // Wait for CTS do{ // if (MODE_INPUT(setting.mode) && RSSI_R -#define SAMPLE_COUNT 1 +#define SAMPLE_COUNT 3 int j = SAMPLE_COUNT; //setting.repeat; int RSSI_RAW_ARRAY[3]; do{ @@ -1523,20 +1523,21 @@ typedef struct { static const RBW_t RBW_choices[] = { // BW register corr freq - {SI4463_RBW_02kHz, 18,3,42}, - {SI4463_RBW_1kHz, 18,10,29}, - {SI4463_RBW_3kHz, 12,30,28}, - {SI4463_RBW_10kHz, 7,100,26}, - {SI4463_RBW_30kHz, 10,300,22}, - {SI4463_RBW_100kHz,2,1000,19}, - {SI4463_RBW_300kHz,2,3000,7}, - {SI4463_RBW_600kHz,3,6000,-1}, - {SI4463_RBW_850kHz,12,8500,-9}, + {SI4463_RBW_02kHz, 16,3,42}, + {SI4463_RBW_1kHz, 20,10,29}, + {SI4463_RBW_3kHz, 15,30,28}, + {SI4463_RBW_10kHz, 5,100,26}, + {SI4463_RBW_30kHz, 11,300,22}, + {SI4463_RBW_100kHz,9,1000,19}, + {SI4463_RBW_300kHz,8,3000,5}, + {SI4463_RBW_600kHz,9,6000,-1}, + {SI4463_RBW_850kHz,19,8500,-9}, }; const uint8_t SI4432_RBW_count = ((int)(sizeof(RBW_choices)/sizeof(RBW_t))); static pureRSSI_t SI4463_RSSI_correction = float_TO_PURE_RSSI(-120); +bool SI4463_RSSI_correction_enabled = true; int16_t SI4463_noise_correction_x10; static int prev_band = -1; @@ -1544,6 +1545,10 @@ pureRSSI_t getSI4463_RSSI_correction(void){ return SI4463_RSSI_correction; }; +void switch_SI4463_RSSI_correction(bool enabled){ + SI4463_RSSI_correction_enabled = enabled; +}; + uint16_t force_rbw(int f) { @@ -1562,7 +1567,7 @@ uint16_t force_rbw(int f) // SI4463_wait_for_cts(); set_RSSI_comp(); // prev_band = -1; - SI4463_RSSI_correction = float_TO_PURE_RSSI(RBW_choices[f].RSSI_correction_x_10 - 1200)/10; // Set RSSI correction + SI4463_RSSI_correction = ( SI4463_RSSI_correction_enabled ? float_TO_PURE_RSSI(RBW_choices[f].RSSI_correction_x_10 - 1200)/10 : float_TO_PURE_RSSI(-120) ) ; // Set RSSI correction SI4463_noise_correction_x10 = RBW_choices[f].noise_correction_x10; return RBW_choices[f].RBWx10; // RBW achieved by SI4463 in kHz * 10 }