diff --git a/main.c b/main.c index b9d3c4b..71c7f9f 100644 --- a/main.c +++ b/main.c @@ -81,6 +81,8 @@ static volatile vna_shellcmd_t shell_function = 0; //#define ENABLE_GAIN_COMMAND // Enable port command, used for debug //#define ENABLE_PORT_COMMAND +// Enable si5351 timing command, used for debug +#define ENABLE_SI5351_TIMINGS static void apply_CH0_error_term_at(int i); static void apply_CH1_error_term_at(int i); @@ -363,6 +365,25 @@ adjust_gain(uint32_t newfreq) return 0; } +#ifdef ENABLE_GAIN_COMMAND +VNA_SHELL_FUNCTION(cmd_gain) +{ + int rvalue = 0; + int lvalue = 0; + if (argc < 1 && argc > 3) { + shell_printf("usage: gain idx {lgain(0-95)} [rgain(0-95)]\r\n"); + return; + } + int idx = my_atoui(argv[0]); + lvalue = rvalue = my_atoui(argv[1]); + if (argc == 3) + rvalue = my_atoui(argv[2]); + tlv320aic3204_set_gain(lvalue, rvalue); + gain_table[idx][0] = lvalue; + gain_table[idx][1] = rvalue; +} +#endif + int set_frequency(uint32_t freq) { int delay = adjust_gain(freq); @@ -836,7 +857,7 @@ static const I2SConfig i2sconfig = { }; #define DSP_START(delay) {wait_count = delay + config.bandwidth;} -#define DSP_WAIT_READY while (wait_count) {if (operation_requested && break_on_operation) return false; __WFI();} +#define DSP_WAIT_READY while (wait_count) {__WFI();} #define DSP_WAIT while (wait_count) {__WFI();} #define RESET_SWEEP {p_sweep = 0;} #define DELAY_CHANNEL_CHANGE 2 @@ -863,7 +884,7 @@ bool sweep(bool break_on_operation, uint16_t sweep_mode) if (p_sweep>=sweep_points || break_on_operation == false) RESET_SWEEP; if (break_on_operation && sweep_mode == 0) return false; - + uint16_t start_sweep = p_sweep; // blink LED while scanning palClearPad(GPIOC, GPIOC_LED); // Power stabilization after LED off, before measure @@ -880,7 +901,7 @@ bool sweep(bool break_on_operation, uint16_t sweep_mode) //================================================ DSP_WAIT_READY; (*sample_func)(measured[0][p_sweep]); // calculate reflection coefficient - if (cal_status & CALSTAT_APPLY) + if (!APPLY_CALIBRATION_AFTER_SWEEP && cal_status & CALSTAT_APPLY) apply_CH0_error_term_at(p_sweep); } if (sweep_mode & SWEEP_CH1_MEASURE){ @@ -891,16 +912,23 @@ bool sweep(bool break_on_operation, uint16_t sweep_mode) //================================================ DSP_WAIT_READY; (*sample_func)(measured[1][p_sweep]); // calculate transmission coefficient - if (cal_status & CALSTAT_APPLY) + if (!APPLY_CALIBRATION_AFTER_SWEEP && cal_status & CALSTAT_APPLY) apply_CH1_error_term_at(p_sweep); } + if (operation_requested && break_on_operation) break; st_delay = 0; // Display SPI made noise on measurement (can see in CW mode) // ili9341_fill(OFFSETX+CELLOFFSETX, OFFSETY, (p_sweep * WIDTH)/(sweep_points-1), 1, RGB565(0,0,255)); } + if (APPLY_CALIBRATION_AFTER_SWEEP && (cal_status & CALSTAT_APPLY)){ + for (;start_sweep<=p_sweep;start_sweep++){ + if (sweep_mode & SWEEP_CH0_MEASURE) apply_CH0_error_term_at(start_sweep); + if (sweep_mode & SWEEP_CH1_MEASURE) apply_CH1_error_term_at(start_sweep); + } + } // blink LED while scanning palSetPad(GPIOC, GPIOC_LED); - return true; + return p_sweep == sweep_points; } uint32_t get_bandwidth_frequency(void){ @@ -1840,7 +1868,7 @@ VNA_SHELL_FUNCTION(cmd_marker) if (argc == 0) { for (t = 0; t < MARKERS_MAX; t++) { if (markers[t].enabled) { - shell_printf("%d %d %d\r\n", t+1, markers[t].index, markers[t].frequency); + shell_printf("%d %d %u\r\n", t+1, markers[t].index, markers[t].frequency); } } return; @@ -1858,7 +1886,7 @@ VNA_SHELL_FUNCTION(cmd_marker) if (t < 0 || t >= MARKERS_MAX) goto usage; if (argc == 1) { - shell_printf("%d %d %d\r\n", t+1, markers[t].index, markers[t].frequency); + shell_printf("%d %d %u\r\n", t+1, markers[t].index, markers[t].frequency); active_marker = t; // select active marker markers[t].enabled = TRUE; @@ -2041,25 +2069,6 @@ VNA_SHELL_FUNCTION(cmd_test) } #endif -#ifdef ENABLE_GAIN_COMMAND -VNA_SHELL_FUNCTION(cmd_gain) -{ - int rvalue = 0; - int lvalue = 0; - if (argc < 1 && argc > 3) { - shell_printf("usage: gain idx {lgain(0-95)} [rgain(0-95)]\r\n"); - return; - } - int idx = my_atoui(argv[0]); - lvalue = rvalue = my_atoui(argv[1]); - if (argc == 3) - rvalue = my_atoui(argv[2]); - tlv320aic3204_set_gain(lvalue, rvalue); - gain_table[idx][0] = lvalue; - gain_table[idx][1] = rvalue; -} -#endif - #ifdef ENABLE_PORT_COMMAND VNA_SHELL_FUNCTION(cmd_port) { @@ -2161,6 +2170,22 @@ VNA_SHELL_FUNCTION(cmd_vbat_offset) } #endif +#ifdef ENABLE_SI5351_TIMINGS +VNA_SHELL_FUNCTION(cmd_si5351time) +{ + (void)argc; +// si5351_set_timing(my_atoui(argv[0]), my_atoui(argv[1])); + + uint32_t tim = STM32_TIMINGR_PRESC(0U) | + STM32_TIMINGR_SCLDEL(my_atoui(argv[0])) | STM32_TIMINGR_SDADEL(my_atoui(argv[1])) | + STM32_TIMINGR_SCLH(my_atoui(argv[2])) | STM32_TIMINGR_SCLL(my_atoui(argv[3])); + I2CD1.i2c->CR1 &=~I2C_CR1_PE; + I2CD1.i2c->TIMINGR = tim; + I2CD1.i2c->CR1 |= I2C_CR1_PE; + +} +#endif + #ifdef ENABLE_INFO_COMMAND VNA_SHELL_FUNCTION(cmd_info) { @@ -2356,6 +2381,9 @@ static const VNAShellCommand commands[] = #endif #ifdef ENABLE_THREADS_COMMAND {"threads" , cmd_threads , 0}, +#endif +#ifdef ENABLE_SI5351_TIMINGS + {"t" , cmd_si5351time , CMD_WAIT_MUTEX}, #endif {NULL , NULL , 0} }; diff --git a/nanovna.h b/nanovna.h index fdca888..ff58c73 100644 --- a/nanovna.h +++ b/nanovna.h @@ -41,6 +41,9 @@ #define FREQUENCY_OFFSET 8000 // Frequency offset for 48k ADC (sin_cos table in dsp.c generated for 3k, 4k, 5k, 6k, if change need create new table ) //#define FREQUENCY_OFFSET 5000 +// Apply calibration after made sweep, if set to 1, calibration move out from sweep cycle +// On fast CPU it slow down sweep, on slow CPU can made faster (not need wait additional measure time) +#define APPLY_CALIBRATION_AFTER_SWEEP 0 // Use real time build table (undef for use constant) //#define USE_VARIABLE_OFFSET // Speed of light const @@ -146,7 +149,14 @@ extern const char *info_about[]; // Bandwidth depend from AUDIO_SAMPLES_COUNT and audio ADC frequency // for AUDIO_SAMPLES_COUNT = 48 and ADC = 96kHz one measure give 96000/48=2000Hz // define additional measure count -#if AUDIO_ADC_FREQ/AUDIO_SAMPLES_COUNT == 2000 +#if AUDIO_ADC_FREQ/AUDIO_SAMPLES_COUNT == 4000 +#define BANDWIDTH_4000 ( 1 - 1) +#define BANDWIDTH_2000 ( 2 - 1) +#define BANDWIDTH_1000 ( 4 - 1) +#define BANDWIDTH_333 ( 12 - 1) +#define BANDWIDTH_100 ( 40 - 1) +#define BANDWIDTH_30 (132 - 1) +#elif AUDIO_ADC_FREQ/AUDIO_SAMPLES_COUNT == 2000 #define BANDWIDTH_2000 ( 1 - 1) #define BANDWIDTH_1000 ( 2 - 1) #define BANDWIDTH_333 ( 6 - 1) diff --git a/si5351.c b/si5351.c index 46310d7..0e692db 100644 --- a/si5351.c +++ b/si5351.c @@ -472,7 +472,8 @@ si5351_set_frequency(uint32_t freq, uint8_t drive_strength) if (current_band != band) { si5351_reset_pll(SI5351_PLL_RESET_A | SI5351_PLL_RESET_B); // Possibly not need add delay now - chThdSleepMicroseconds(DELAY_RESET_PLL_BEFORE); + if (DELAY_RESET_PLL_BEFORE) + chThdSleepMicroseconds(DELAY_RESET_PLL_BEFORE); } static const uint8_t band_setting[] = {1, PLL_N_1, PLL_N_2, 8, 6, 4}; switch (band) { @@ -483,18 +484,14 @@ si5351_set_frequency(uint32_t freq, uint8_t drive_strength) if (current_band != band) { si5351_setupPLL(SI5351_REG_PLL_A, pll_n, 0, 1); si5351_setupPLL(SI5351_REG_PLL_B, PLL_N_2, 0, 1); - si5351_set_frequency_fixedpll( - 2, XTALFREQ * PLL_N_2, CLK2_FREQUENCY, SI5351_R_DIV_1, - SI5351_CLK_DRIVE_STRENGTH_2MA | SI5351_CLK_PLL_SELECT_B); + si5351_set_frequency_fixedpll(2, XTALFREQ * PLL_N_2, CLK2_FREQUENCY, SI5351_R_DIV_1, SI5351_CLK_DRIVE_STRENGTH_2MA | SI5351_CLK_PLL_SELECT_B); delay = DELAY_BANDCHANGE_1_2; } else { delay = DELAY_BAND_1_2; } // Calculate and set CH0 and CH1 divider - si5351_set_frequency_fixedpll(0, (uint64_t)omul * XTALFREQ * pll_n, ofreq, rdiv, - drive_strength | SI5351_CLK_PLL_SELECT_A); - si5351_set_frequency_fixedpll(1, (uint64_t)mul * XTALFREQ * pll_n, freq, rdiv, - drive_strength | SI5351_CLK_PLL_SELECT_A); + si5351_set_frequency_fixedpll(0, (uint64_t)omul * XTALFREQ * pll_n, ofreq, rdiv, drive_strength | SI5351_CLK_PLL_SELECT_A); + si5351_set_frequency_fixedpll(1, (uint64_t) mul * XTALFREQ * pll_n, freq, rdiv, drive_strength | SI5351_CLK_PLL_SELECT_A); break; case 3: // fdiv = 8, f 100-130 PLL 800-1040 case 4: // fdiv = 6, f 130-170 PLL 780-1050 @@ -503,29 +500,24 @@ si5351_set_frequency(uint32_t freq, uint8_t drive_strength) // Setup CH0 and CH1 constant fdiv divider at change if (current_band != band) { - si5351_setupMultisynth(0, fdiv, 0, 1, SI5351_R_DIV_1, - drive_strength | SI5351_CLK_PLL_SELECT_A); - si5351_setupMultisynth(1, fdiv, 0, 1, SI5351_R_DIV_1, - drive_strength | SI5351_CLK_PLL_SELECT_B); + si5351_setupMultisynth(0, fdiv, 0, 1, SI5351_R_DIV_1, drive_strength | SI5351_CLK_PLL_SELECT_A); + si5351_setupMultisynth(1, fdiv, 0, 1, SI5351_R_DIV_1, drive_strength | SI5351_CLK_PLL_SELECT_B); delay= DELAY_BANDCHANGE_3_4; } else { delay= DELAY_BAND_3_4; } // Calculate and set CH0 and CH1 PLL freq - si5351_setupPLL_freq(SI5351_REG_PLL_A, ofreq, fdiv, - omul); // set PLLA freq = (ofreq/omul)*fdiv - si5351_setupPLL_freq(SI5351_REG_PLL_B, freq, fdiv, - mul); // set PLLB freq = ( freq/ mul)*fdiv + si5351_setupPLL_freq(SI5351_REG_PLL_A, ofreq, fdiv, omul); // set PLLA freq = (ofreq/omul)*fdiv + si5351_setupPLL_freq(SI5351_REG_PLL_B, freq, fdiv, mul); // set PLLB freq = ( freq/ mul)*fdiv // Calculate CH2 freq = CLK2_FREQUENCY, depend from calculated before CH1 PLLB = (freq/mul)*fdiv - si5351_set_frequency_fixedpll( - 2, (uint64_t)freq * fdiv, CLK2_FREQUENCY * mul, SI5351_R_DIV_1, - SI5351_CLK_DRIVE_STRENGTH_2MA | SI5351_CLK_PLL_SELECT_B); + si5351_set_frequency_fixedpll(2, (uint64_t)freq * fdiv, CLK2_FREQUENCY * mul, SI5351_R_DIV_1, SI5351_CLK_DRIVE_STRENGTH_2MA | SI5351_CLK_PLL_SELECT_B); break; } if (current_band != band) { // Possibly not need add delay now - chThdSleepMicroseconds(DELAY_RESET_PLL_AFTER); + if (DELAY_RESET_PLL_AFTER) + chThdSleepMicroseconds(DELAY_RESET_PLL_AFTER); si5351_reset_pll(SI5351_PLL_RESET_A|SI5351_PLL_RESET_B); current_band = band; } diff --git a/ui.c b/ui.c index 382c3c7..ad33e6e 100644 --- a/ui.c +++ b/ui.c @@ -963,14 +963,14 @@ static const char s1_file_header[] = "# Hz S RI R 50\r\n"; static const char s1_file_param[] = - "%10d % f % f\r\n"; + "%10u % f % f\r\n"; static const char s2_file_header[] = "!File created by NanoVNA\r\n"\ "# Hz S RI R 50\r\n"; static const char s2_file_param[] = - "%10d % f % f % f % f 0 0 0 0\r\n"; + "%10u % f % f % f % f 0 0 0 0\r\n"; static UI_FUNCTION_CALLBACK(menu_sdcard_cb) { @@ -1144,6 +1144,9 @@ const menuitem_t menu_transform[] = { }; const menuitem_t menu_bandwidth[] = { +#ifdef BANDWIDTH_4000 + { MT_ADV_CALLBACK, BANDWIDTH_4000, "4 kHz", menu_bandwidth_acb }, +#endif #ifdef BANDWIDTH_2000 { MT_ADV_CALLBACK, BANDWIDTH_2000, "2 kHz", menu_bandwidth_acb }, #endif @@ -1151,7 +1154,9 @@ const menuitem_t menu_bandwidth[] = { { MT_ADV_CALLBACK, BANDWIDTH_333, "333 Hz", menu_bandwidth_acb }, { MT_ADV_CALLBACK, BANDWIDTH_100, "100 Hz", menu_bandwidth_acb }, { MT_ADV_CALLBACK, BANDWIDTH_30, "30 Hz", menu_bandwidth_acb }, +#ifdef BANDWIDTH_10 { MT_ADV_CALLBACK, BANDWIDTH_10, "10 Hz", menu_bandwidth_acb }, +#endif { MT_CANCEL, 255, S_LARROW" BACK", NULL }, { MT_NONE, 0, NULL, NULL } // sentinel };