From 12d53738bcaabb032af4673de513a2f2eb214154 Mon Sep 17 00:00:00 2001 From: DiSlord Date: Sat, 7 Mar 2020 17:19:43 +0300 Subject: [PATCH] Revert some changes: Start/stop generation feature (unstable on segment scan from CPU) Calibration on paused sweep (need more stack, need find better solution) Variable use optimization --- Makefile | 4 +-- main.c | 100 +++++++++++++++++++++++++++++++++--------------------- nanovna.h | 8 +---- si5351.c | 19 ++++++----- ui.c | 2 +- 5 files changed, 77 insertions(+), 56 deletions(-) diff --git a/Makefile b/Makefile index fc18d97..bd17844 100644 --- a/Makefile +++ b/Makefile @@ -64,13 +64,13 @@ endif # Stack size to be allocated to the Cortex-M process stack. This stack is # the stack used by the main() thread. ifeq ($(USE_PROCESS_STACKSIZE),) - USE_PROCESS_STACKSIZE = 0x300 + USE_PROCESS_STACKSIZE = 0x200 endif # Stack size to the allocated to the Cortex-M main/exceptions stack. This # stack is used for processing interrupts and exceptions. ifeq ($(USE_EXCEPTIONS_STACKSIZE),) - USE_EXCEPTIONS_STACKSIZE = 0x100 + USE_EXCEPTIONS_STACKSIZE = 0x200 endif # diff --git a/main.c b/main.c index 02ef10d..bb1a4bb 100644 --- a/main.c +++ b/main.c @@ -69,14 +69,17 @@ static void transform_domain(void); static MUTEX_DECL(mutex); -// Obsolete enable/disable calibration interpolation (always on) -#define cal_auto_interpolate TRUE +#define DRIVE_STRENGTH_AUTO (-1) +#define FREQ_HARMONICS (config.harmonic_freq_threshold) +#define IS_HARMONIC_MODE(f) ((f) > FREQ_HARMONICS) +// Obsolete, always use interpolate +#define cal_auto_interpolate TRUE static int32_t frequency_offset = 5000; static uint32_t frequency = 10000000; static int8_t drive_strength = DRIVE_STRENGTH_AUTO; -volatile int8_t sweep_mode = SWEEP_MODE_ENABLED; - +int8_t sweep_enabled = TRUE; +volatile int8_t sweep_once = FALSE; volatile uint8_t redraw_request = 0; // contains REDRAW_XXX flags int16_t vbat = 0; @@ -88,19 +91,19 @@ static THD_FUNCTION(Thread1, arg) while (1) { bool completed = false; - if (sweep_mode&(SWEEP_MODE_ENABLED|SWEEP_MODE_RUN_ONCE)) { + if (sweep_enabled || sweep_once) { chMtxLock(&mutex); completed = sweep(true); - sweep_mode&=~SWEEP_MODE_RUN_ONCE; + sweep_once = FALSE; chMtxUnlock(&mutex); } else { - si5351_disable_output(); __WFI(); } chMtxLock(&mutex); ui_process(); - if (sweep_mode&SWEEP_MODE_ENABLED) { + + if (sweep_enabled) { if (vbat != -1) { adc_stop(ADC1); vbat = adc_vbat_read(ADC1); @@ -130,10 +133,23 @@ static THD_FUNCTION(Thread1, arg) } } -static inline void run_once_sweep(void) {sweep_mode|=SWEEP_MODE_RUN_ONCE;} -static inline void pause_sweep(void) {sweep_mode&=~SWEEP_MODE_ENABLED;} -static inline void resume_sweep(void){sweep_mode|= SWEEP_MODE_ENABLED;} -void toggle_sweep(void){sweep_mode^= SWEEP_MODE_ENABLED;} +static inline void +pause_sweep(void) +{ + sweep_enabled = FALSE; +} + +static inline void +resume_sweep(void) +{ + sweep_enabled = TRUE; +} + +void +toggle_sweep(void) +{ + sweep_enabled = !sweep_enabled; +} static float bessel0(float x) { @@ -723,8 +739,7 @@ static const marker_t def_markers[MARKERS_MAX] = { // Load propeties default settings void loadDefaultProps(void){ -//Magic add on caldata_save -//current_props.magic = CONFIG_MAGIC; + current_props.magic = CONFIG_MAGIC; current_props._frequency0 = 50000; // start = 50kHz current_props._frequency1 = 900000000; // end = 900MHz current_props._sweep_points = POINTS_COUNT; @@ -740,8 +755,6 @@ void loadDefaultProps(void){ current_props._active_marker = 0; current_props._domain_mode = 0; current_props._marker_smith_format = MS_RLC; -//Checksum add on caldata_save -//current_props.checksum = 0; } void @@ -759,23 +772,20 @@ ensure_edit_config(void) #define DELAY_CHANNEL_CHANGE 2 // main loop for measurement -static bool sweep(bool break_on_operation) +bool sweep(bool break_on_operation) { int i, delay; // blink LED while scanning palClearPad(GPIOC, GPIOC_LED); - si5351_enable_output(); - wait_dsp(1); // Wait for get optimal timings for (i = 0; i < sweep_points; i++) { // 5300 delay = set_frequency(frequencies[i]); // 700 tlv320aic3204_select(0); // 60 CH0:REFLECT - wait_dsp(delay); // 1900 // calculate reflection coefficient (*sample_func)(measured[0][i]); // 60 tlv320aic3204_select(1); // 60 CH1:TRANSMISSION - wait_dsp(DELAY_CHANNEL_CHANGE); // 1800 + wait_dsp(DELAY_CHANNEL_CHANGE); // 1700 // calculate transmission coefficient (*sample_func)(measured[1][i]); // 60 // ======== 170 =========== @@ -786,9 +796,8 @@ static bool sweep(bool break_on_operation) apply_edelay_at(i); // back to toplevel to handle ui operation - if (operation_requested && break_on_operation){ + if (operation_requested && break_on_operation) return false; - } } // blink LED while scanning palSetPad(GPIOC, GPIOC_LED); @@ -825,11 +834,11 @@ VNA_SHELL_FUNCTION(cmd_scan) if (cal_auto_interpolate && (cal_status & CALSTAT_APPLY)) cal_interpolate(lastsaveid); - run_once_sweep(); + sweep_once = TRUE; chMtxUnlock(&mutex); // wait finishing sweep - while (sweep_mode&SWEEP_MODE_RUN_ONCE) + while (sweep_once) chThdSleepMilliseconds(10); } @@ -1270,21 +1279,36 @@ void cal_collect(int type) { ensure_edit_config(); - int dst, src; + switch (type) { - case CAL_LOAD: cal_status|= CALSTAT_LOAD; dst = CAL_LOAD; src = 0; break; - case CAL_OPEN: cal_status|= CALSTAT_OPEN; dst = CAL_OPEN; src = 0; cal_status&= ~(CALSTAT_ES|CALSTAT_APPLY); break; - case CAL_SHORT: cal_status|= CALSTAT_SHORT; dst = CAL_SHORT; src = 0; cal_status&= ~(CALSTAT_ER|CALSTAT_APPLY); break; - case CAL_THRU: cal_status|= CALSTAT_THRU; dst = CAL_THRU; src = 1; break; - case CAL_ISOLN: cal_status|= CALSTAT_ISOLN; dst = CAL_ISOLN; src = 1; break; - default: - return; + case CAL_LOAD: + cal_status |= CALSTAT_LOAD; + memcpy(cal_data[CAL_LOAD], measured[0], sizeof measured[0]); + break; + + case CAL_OPEN: + cal_status |= CALSTAT_OPEN; + cal_status &= ~(CALSTAT_ES|CALSTAT_APPLY); + memcpy(cal_data[CAL_OPEN], measured[0], sizeof measured[0]); + break; + + case CAL_SHORT: + cal_status |= CALSTAT_SHORT; + cal_status &= ~(CALSTAT_ER|CALSTAT_APPLY); + memcpy(cal_data[CAL_SHORT], measured[0], sizeof measured[0]); + break; + + case CAL_THRU: + cal_status |= CALSTAT_THRU; + memcpy(cal_data[CAL_THRU], measured[1], sizeof measured[0]); + break; + + case CAL_ISOLN: + cal_status |= CALSTAT_ISOLN; + memcpy(cal_data[CAL_ISOLN], measured[1], sizeof measured[0]); + break; } - // Made sweep operation for collect calibration data - sweep(false); - // Copy calibration data - memcpy(cal_data[dst], measured[src], sizeof measured[0]); - redraw_request|= REDRAW_CAL_STATUS; + redraw_request |= REDRAW_CAL_STATUS; } void diff --git a/nanovna.h b/nanovna.h index 72753d1..b79d4c8 100644 --- a/nanovna.h +++ b/nanovna.h @@ -86,9 +86,7 @@ double my_atof(const char *p); void toggle_sweep(void); void loadDefaultProps(void); -#define SWEEP_MODE_ENABLED 0x01 -#define SWEEP_MODE_RUN_ONCE 0x02 -extern volatile int8_t sweep_mode; +extern int8_t sweep_enabled; /* * dsp.c @@ -225,10 +223,6 @@ typedef struct config { extern config_t config; -#define DRIVE_STRENGTH_AUTO (-1) -#define FREQ_HARMONICS (config.harmonic_freq_threshold) -#define IS_HARMONIC_MODE(f) ((f) > FREQ_HARMONICS) - //extern trace_t trace[TRACES_MAX]; void set_trace_type(int t, int type); diff --git a/si5351.c b/si5351.c index a08bfbe..6a8ad49 100644 --- a/si5351.c +++ b/si5351.c @@ -78,8 +78,8 @@ const uint8_t si5351_configs[] = { // setup multisynth (832MHz / 104 = 8MHz, 104/2-2=50) 9, SI5351_REG_58_MULTISYNTH2, /*P3*/0, 1, /*P1*/0, 50, 0, /*P2|P3*/0, 0, 0, 2, SI5351_REG_18_CLK2_CONTROL, SI5351_CLK_DRIVE_STRENGTH_2MA | SI5351_CLK_INPUT_MULTISYNTH_N | SI5351_CLK_INTEGER_MODE, - 2, SI5351_REG_3_OUTPUT_ENABLE_CONTROL, 0, #endif + 2, SI5351_REG_3_OUTPUT_ENABLE_CONTROL, ~(SI5351_CLK0_EN|SI5351_CLK1_EN|SI5351_CLK2_EN), 0 // sentinel }; @@ -118,7 +118,7 @@ static void si5351_reset_pll(uint8_t mask) { // Writing a 1<<5 will reset PLLA, 1<<7 reset PLLB, this is a self clearing bits. // !!! Need delay before reset PLL for apply PLL freq changes before - chThdSleepMicroseconds(200); + chThdSleepMicroseconds(400); si5351_write(SI5351_REG_177_PLL_RESET, mask | 0x0C); } @@ -126,6 +126,7 @@ void si5351_disable_output(void) { si5351_write(SI5351_REG_3_OUTPUT_ENABLE_CONTROL, 0xFF); si5351_bulk_write(disable_output, sizeof(disable_output)); + current_band = 0; } void si5351_enable_output(void) @@ -338,7 +339,8 @@ static inline uint8_t si5351_getBand(uint32_t freq){ // Additional delay for band 1 (remove unstable generation at begin) #define DELAY_BAND_1 1 // Band changes need additional delay after reset PLL -#define DELAY_BANDCHANGE 2 +#define DELAY_BANDCHANGE_1 3 +#define DELAY_BANDCHANGE_2 3 /* * Maximum supported frequency = FREQ_HARMONICS * 9U @@ -358,16 +360,16 @@ si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_streng uint32_t rdiv = SI5351_R_DIV_1; uint32_t fdiv; current_freq = freq; - if (freq >= FREQ_HARMONICS * 7U) { + if (freq >= config.harmonic_freq_threshold * 7U) { mul = 9; omul = 11; - } else if (freq >= FREQ_HARMONICS * 5U) { + } else if (freq >= config.harmonic_freq_threshold * 5U) { mul = 7; omul = 9; - } else if (freq >= FREQ_HARMONICS * 3U) { + } else if (freq >= config.harmonic_freq_threshold * 3U) { mul = 5; omul = 7; - } else if (freq >= FREQ_HARMONICS) { + } else if (freq >= config.harmonic_freq_threshold) { mul = 3; omul = 5; } @@ -388,6 +390,7 @@ si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_streng if (current_band != 1){ si5351_setupPLL(SI5351_REG_PLL_A, PLL_N, 0, 1); si5351_set_frequency_fixedpll(2, XTALFREQ * PLL_N, CLK2_FREQUENCY, SI5351_R_DIV_1, SI5351_CLK_DRIVE_STRENGTH_2MA|SI5351_CLK_PLL_SELECT_A); + delay+=DELAY_BANDCHANGE_1; } // 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); @@ -401,6 +404,7 @@ si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_streng 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); + delay+=DELAY_BANDCHANGE_2; } // Calculate and set CH0 and CH1 PLL freq si5351_setupPLL_freq(SI5351_REG_PLL_A, ofreq, fdiv, omul);// set PLLA freq = (ofreq/omul)*fdiv @@ -413,7 +417,6 @@ si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_streng if (current_band != band) { si5351_reset_pll(SI5351_PLL_RESET_A|SI5351_PLL_RESET_B); current_band = band; - delay+=DELAY_BANDCHANGE; } return delay; } diff --git a/ui.c b/ui.c index b73ef29..eaffa8d 100644 --- a/ui.c +++ b/ui.c @@ -1358,7 +1358,7 @@ menu_item_modify_attribute(const menuitem_t *menu, int item, *fg = config.menu_normal_color; } } else if (menu == menu_stimulus) { - if (item == 5 /* PAUSE */ && !(sweep_mode&SWEEP_MODE_ENABLED)) { + if (item == 5 /* PAUSE */ && !sweep_enabled) { *bg = DEFAULT_MENU_TEXT_COLOR; *fg = config.menu_normal_color; }