From 18c529984bf88da3d7427de8f063515126211f87 Mon Sep 17 00:00:00 2001 From: TT Date: Sat, 5 Oct 2019 19:08:20 +0900 Subject: [PATCH 1/5] chore: use table to adjust gain related to freq --- main.c | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/main.c b/main.c index 173c33c..58e4c46 100644 --- a/main.c +++ b/main.c @@ -257,36 +257,35 @@ static void cmd_reset(BaseSequentialStream *chp, int argc, char *argv[]) ; } +const int8_t gain_table[] = { + 0, // 0 ~ 300MHz + 40, // 300 ~ 600MHz + 50, // 600 ~ 900MHz + 75, // 900 ~ 1200MHz + 85, // 1200 ~ 1400MHz + 95 // 1400MHz ~ +}; + +static int +adjust_gain(int newfreq) +{ + int delay = 0; + int new_order = newfreq / FREQ_HARMONICS; + int old_order = frequency / FREQ_HARMONICS; + if (new_order != old_order) { + tlv320aic3204_set_gain(gain_table[new_order], gain_table[new_order]); + delay += 10; + } + return delay; +} + int set_frequency(int freq) { int delay = 0; if (frequency == freq) return delay; - if (freq > 1400000000 && frequency <= 1400000000) { - tlv320aic3204_set_gain(95, 95); - delay += 10; - } else - if (freq > 1200000000 && frequency <= 1200000000) { - tlv320aic3204_set_gain(85, 85); - delay += 10; - } else - if (freq > 900000000 && frequency <= 900000000) { - tlv320aic3204_set_gain(75, 75); - delay += 10; - } else - if (freq > 600000000 && frequency <= 600000000) { - tlv320aic3204_set_gain(50, 50); - delay += 10; - } else - if (freq > FREQ_HARMONICS && frequency <= FREQ_HARMONICS) { - tlv320aic3204_set_gain(40, 40); - delay += 10; - } else - if (freq <= FREQ_HARMONICS && frequency > FREQ_HARMONICS) { - tlv320aic3204_set_gain(0, 0); - delay += 10; - } + delay += adjust_gain(freq); int8_t ds = drive_strength; if (ds == DRIVE_STRENGTH_AUTO) { From 94659a22ba90955a8eb82657bceaff6daa7a96c6 Mon Sep 17 00:00:00 2001 From: TT Date: Sat, 5 Oct 2019 19:56:38 +0900 Subject: [PATCH 2/5] feat: add threshold command --- main.c | 36 +++++++++++++++++++++++++----------- nanovna.h | 1 + si5351.c | 5 +++-- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/main.c b/main.c index 58e4c46..e4f281e 100644 --- a/main.c +++ b/main.c @@ -43,7 +43,8 @@ bool sweep(bool break_on_operation); static MUTEX_DECL(mutex); #define DRIVE_STRENGTH_AUTO (-1) -#define FREQ_HARMONICS 300000000 +//#define FREQ_HARMONICS 300000000 +#define FREQ_HARMONICS (config.harmonic_freq_threshold) int32_t frequency_offset = 5000; int32_t frequency = 10000000; @@ -354,6 +355,18 @@ static void cmd_dac(BaseSequentialStream *chp, int argc, char *argv[]) dacPutChannelX(&DACD2, 0, value); } +static void cmd_threshold(BaseSequentialStream *chp, int argc, char *argv[]) +{ + int value; + if (argc != 1) { + chprintf(chp, "usage: threshold {frequency in harmonic mode}\r\n"); + chprintf(chp, "current: %d\r\n", config.harmonic_freq_threshold); + return; + } + value = atoi(argv[0]); + config.harmonic_freq_threshold = value; +} + static void cmd_saveconfig(BaseSequentialStream *chp, int argc, char *argv[]) { (void)argc; @@ -590,16 +603,16 @@ float cal_data[5][101][2]; #endif config_t config = { - /* magic */ CONFIG_MAGIC, - /* dac_value */ 1922, - /* grid_color */ 0x1084, - /* menu_normal_color */ 0xffff, - /* menu_active_color */ 0x7777, - /* trace_colors[4] */ { RGB565(0,255,255), RGB565(255,0,40), RGB565(0,0,255), RGB565(50,255,0) }, - ///* touch_cal[4] */ { 620, 600, 160, 190 }, - /* touch_cal[4] */ { 693, 605, 124, 171 }, - /* default_loadcal */ 0, - /* checksum */ 0 + .magic = CONFIG_MAGIC, + .dac_value = 1922, + .grid_color = 0x1084, + .menu_normal_color = 0xffff, + .menu_active_color = 0x7777, + .trace_color = { RGB565(0,255,255), RGB565(255,0,40), RGB565(0,0,255), RGB565(50,255,0) }, + .touch_cal = { 693, 605, 124, 171 }, //{ 620, 600, 160, 190 }, + .default_loadcal = 0, + .harmonic_freq_threshold = 300000000, + .checksum = 0 }; properties_t current_props = { @@ -1981,6 +1994,7 @@ static const ShellCommand commands[] = { "capture", cmd_capture }, { "vbat", cmd_vbat }, { "transform", cmd_transform }, + { "threshold", cmd_threshold }, { NULL, NULL } }; diff --git a/nanovna.h b/nanovna.h index ef4425c..19dcc92 100644 --- a/nanovna.h +++ b/nanovna.h @@ -195,6 +195,7 @@ typedef struct { uint16_t trace_color[TRACES_MAX]; int16_t touch_cal[4]; int8_t default_loadcal; + int32_t harmonic_freq_threshold; int32_t checksum; } config_t; diff --git a/si5351.c b/si5351.c index ebd4aa9..9dae5c2 100644 --- a/si5351.c +++ b/si5351.c @@ -18,6 +18,7 @@ * Boston, MA 02110-1301, USA. */ #include "hal.h" +#include "nanovna.h" #include "si5351.h" #define SI5351_I2C_ADDR (0x60<<1) @@ -309,10 +310,10 @@ si5351_set_frequency_with_offset(int freq, int offset, uint8_t drive_strength) int delay = 3; uint32_t ofreq = freq + offset; uint32_t rdiv = SI5351_R_DIV_1; - if (freq > 900000000) { + if (freq > config.harmonic_freq_threshold * 3) { freq /= 5; ofreq /= 7; - } else if (freq > 300000000) { + } else if (freq > config.harmonic_freq_threshold) { freq /= 3; ofreq /= 5; } From 3a6de231c6782655bfe7a8a5bdb1d97a744c2958 Mon Sep 17 00:00:00 2001 From: TT Date: Sat, 5 Oct 2019 21:11:19 +0900 Subject: [PATCH 3/5] fix: remove glitch on interpolation --- main.c | 9 ++++++++- si5351.c | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index e4f281e..4345369 100644 --- a/main.c +++ b/main.c @@ -43,8 +43,8 @@ bool sweep(bool break_on_operation); static MUTEX_DECL(mutex); #define DRIVE_STRENGTH_AUTO (-1) -//#define FREQ_HARMONICS 300000000 #define FREQ_HARMONICS (config.harmonic_freq_threshold) +#define IS_HARMONIC_MODE(f) ((f) > FREQ_HARMONICS) int32_t frequency_offset = 5000; int32_t frequency = 10000000; @@ -1292,6 +1292,13 @@ cal_interpolate(int s) // found f between freqs at j and j+1 float k1 = (float)(f - src->_frequencies[j]) / (src->_frequencies[j+1] - src->_frequencies[j]); + + // avoid glitch between freqs in different harmonics mode + if (IS_HARMONIC_MODE(src->_frequencies[j]) != IS_HARMONIC_MODE(src->_frequencies[j+1])) { + // assume f[j] < f[j+1] + k1 = IS_HARMONIC_MODE(f) ? 1.0 : 0.0; + } + float k0 = 1.0 - k1; for (eterm = 0; eterm < 5; eterm++) { cal_data[eterm][i][0] = src->_cal_data[eterm][j][0] * k0 + src->_cal_data[eterm][j+1][0] * k1; diff --git a/si5351.c b/si5351.c index 9dae5c2..6e66e8a 100644 --- a/si5351.c +++ b/si5351.c @@ -310,10 +310,10 @@ si5351_set_frequency_with_offset(int freq, int offset, uint8_t drive_strength) int delay = 3; uint32_t ofreq = freq + offset; uint32_t rdiv = SI5351_R_DIV_1; - if (freq > config.harmonic_freq_threshold * 3) { + if (freq >= config.harmonic_freq_threshold * 3) { freq /= 5; ofreq /= 7; - } else if (freq > config.harmonic_freq_threshold) { + } else if (freq >= config.harmonic_freq_threshold) { freq /= 3; ofreq /= 5; } From bfd45c715d9b87a6a5c331ae45d94bd3ddaa9483 Mon Sep 17 00:00:00 2001 From: TT Date: Sat, 5 Oct 2019 21:48:00 +0900 Subject: [PATCH 4/5] fix: fix digit loss of sweep frequency --- main.c | 13 ++++++------- nanovna.h | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/main.c b/main.c index 4345369..e023e7a 100644 --- a/main.c +++ b/main.c @@ -827,16 +827,15 @@ freq_mode_centerspan(void) #define STOP_MAX 1500000000 void -set_sweep_frequency(int type, float frequency) +set_sweep_frequency(int type, uint32_t freq) { - int32_t freq = frequency; int cal_applied = cal_status & CALSTAT_APPLY; switch (type) { case ST_START: freq_mode_startstop(); - if (frequency < START_MIN) + if (freq < START_MIN) freq = START_MIN; - if (frequency > STOP_MAX) + if (freq > STOP_MAX) freq = STOP_MAX; if (frequency0 != freq) { ensure_edit_config(); @@ -849,9 +848,9 @@ set_sweep_frequency(int type, float frequency) break; case ST_STOP: freq_mode_startstop(); - if (frequency > STOP_MAX) + if (freq > STOP_MAX) freq = STOP_MAX; - if (frequency < START_MIN) + if (freq < START_MIN) freq = START_MIN; if (frequency1 != freq) { ensure_edit_config(); @@ -903,7 +902,7 @@ set_sweep_frequency(int type, float frequency) freq_mode_centerspan(); if (frequency0 != freq || frequency1 != 0) { ensure_edit_config(); - frequency0 = frequency; + frequency0 = freq; frequency1 = 0; update_frequencies(); } diff --git a/nanovna.h b/nanovna.h index 19dcc92..8fbe96c 100644 --- a/nanovna.h +++ b/nanovna.h @@ -71,7 +71,7 @@ enum { ST_START, ST_STOP, ST_CENTER, ST_SPAN, ST_CW }; -void set_sweep_frequency(int type, float frequency); +void set_sweep_frequency(int type, uint32_t frequency); uint32_t get_sweep_frequency(int type); float my_atof(const char *p); From f7e712798a5cd50961a401bc79d0ca6fd7e06aba Mon Sep 17 00:00:00 2001 From: TT Date: Sun, 6 Oct 2019 07:56:25 +0900 Subject: [PATCH 5/5] chore: remove warnings --- main.c | 17 +++++++++++------ nanovna.h | 9 ++++++--- plot.c | 11 +++++------ si5351.c | 2 +- ui.c | 1 + 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/main.c b/main.c index e023e7a..acd7c1a 100644 --- a/main.c +++ b/main.c @@ -34,9 +34,11 @@ #define ENABLED_DUMP -static void apply_error_term(void); static void apply_error_term_at(int i); +static void apply_edelay_at(int i); static void cal_interpolate(int s); +void update_frequencies(void); +void set_frequencies(uint32_t start, uint32_t stop, int16_t points); bool sweep(bool break_on_operation); @@ -47,7 +49,7 @@ static MUTEX_DECL(mutex); #define IS_HARMONIC_MODE(f) ((f) > FREQ_HARMONICS) int32_t frequency_offset = 5000; -int32_t frequency = 10000000; +uint32_t frequency = 10000000; int8_t drive_strength = DRIVE_STRENGTH_AUTO; int8_t sweep_enabled = TRUE; int8_t sweep_once = FALSE; @@ -146,7 +148,7 @@ transform_domain(void) // and calculate ifft for time domain float* tmp = (float*)spi_buffer; - uint8_t window_size, offset; + uint8_t window_size = 101, offset = 0; uint8_t is_lowpass = FALSE; switch (domain_mode & TD_FUNC) { case TD_FUNC_BANDPASS: @@ -280,7 +282,7 @@ adjust_gain(int newfreq) return delay; } -int set_frequency(int freq) +int set_frequency(uint32_t freq) { int delay = 0; if (frequency == freq) @@ -827,7 +829,7 @@ freq_mode_centerspan(void) #define STOP_MAX 1500000000 void -set_sweep_frequency(int type, uint32_t freq) +set_sweep_frequency(int type, int32_t freq) { int cal_applied = cal_status & CALSTAT_APPLY; switch (type) { @@ -1110,6 +1112,7 @@ eterm_calc_et(void) cal_status |= CALSTAT_ET; } +#if 0 void apply_error_term(void) { int i; @@ -1141,6 +1144,7 @@ void apply_error_term(void) measured[1][i][1] = s21ai; } } +#endif void apply_error_term_at(int i) { @@ -1171,7 +1175,7 @@ void apply_error_term_at(int i) measured[1][i][1] = s21ai; } -void apply_edelay_at(int i) +static void apply_edelay_at(int i) { float w = 2 * M_PI * electrical_delay * frequencies[i] * 1E-12; float s = sin(w); @@ -2127,5 +2131,6 @@ void HardFault_Handler(void) void hard_fault_handler_c(uint32_t* sp) { + (void)sp; while (true) {} } diff --git a/nanovna.h b/nanovna.h index 8fbe96c..809921e 100644 --- a/nanovna.h +++ b/nanovna.h @@ -71,7 +71,7 @@ enum { ST_START, ST_STOP, ST_CENTER, ST_SPAN, ST_CW }; -void set_sweep_frequency(int type, uint32_t frequency); +void set_sweep_frequency(int type, int32_t frequency); uint32_t get_sweep_frequency(int type); float my_atof(const char *p); @@ -106,7 +106,7 @@ void calculate_gamma(float *gamma); void fetch_amplitude(float *gamma); void fetch_amplitude_ref(float *gamma); -int si5351_set_frequency_with_offset(int freq, int offset, uint8_t drive_strength); +int si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_strength); /* @@ -195,7 +195,7 @@ typedef struct { uint16_t trace_color[TRACES_MAX]; int16_t touch_cal[4]; int8_t default_loadcal; - int32_t harmonic_freq_threshold; + uint32_t harmonic_freq_threshold; int32_t checksum; } config_t; @@ -237,6 +237,7 @@ void redraw_marker(int marker, int update_info); void trace_get_info(int t, char *buf, int len); void plot_into_index(float measured[2][101][2]); void force_set_markmap(void); +void draw_frequencies(void); void draw_all(bool flush); void draw_cal_status(void); @@ -361,6 +362,8 @@ void ui_hide(void); extern uint8_t operation_requested; +void touch_start_watchdog(void); +void touch_position(int *x, int *y); void handle_touch_interrupt(void); #define TOUCH_THRESHOLD 2000 diff --git a/plot.c b/plot.c index ad43241..234ca94 100644 --- a/plot.c +++ b/plot.c @@ -8,7 +8,6 @@ #define SWAP(x,y) do { int z=x; x = y; y = z; } while(0) static void cell_draw_marker_info(int m, int n, int w, int h); -void draw_frequencies(void); void frequency_string(char *buf, size_t len, int32_t freq); void markmap_all_markers(void); @@ -614,7 +613,7 @@ gamma2imp(char *buf, int len, const float coeff[2], uint32_t frequency) } static void -gamma2resistance(char *buf, int len, const float coeff[2], uint32_t frequency) +gamma2resistance(char *buf, int len, const float coeff[2]) { float z0 = 50; float d = z0 / ((1-coeff[0])*(1-coeff[0])+coeff[1]*coeff[1]); @@ -623,7 +622,7 @@ gamma2resistance(char *buf, int len, const float coeff[2], uint32_t frequency) } static void -gamma2reactance(char *buf, int len, const float coeff[2], uint32_t frequency) +gamma2reactance(char *buf, int len, const float coeff[2]) { float z0 = 50; float d = z0 / ((1-coeff[0])*(1-coeff[0])+coeff[1]*coeff[1]); @@ -665,10 +664,10 @@ trace_get_value_string(int t, char *buf, int len, float coeff[2], uint32_t frequ chsnprintf(buf, len, "%.2fj", coeff[1]); break; case TRC_R: - gamma2resistance(buf, len, coeff, frequency); + gamma2resistance(buf, len, coeff); break; case TRC_X: - gamma2reactance(buf, len, coeff, frequency); + gamma2reactance(buf, len, coeff); break; //case TRC_ADMIT: case TRC_POLAR: @@ -744,7 +743,7 @@ clear_markmap(void) memset(markmap[current_mappage], 0, sizeof markmap[current_mappage]); } -void inline +inline void force_set_markmap(void) { memset(markmap[current_mappage], 0xff, sizeof markmap[current_mappage]); diff --git a/si5351.c b/si5351.c index 6e66e8a..49de637 100644 --- a/si5351.c +++ b/si5351.c @@ -304,7 +304,7 @@ int current_band = -1; */ #define CLK2_FREQUENCY 8000000L int -si5351_set_frequency_with_offset(int freq, int offset, uint8_t drive_strength) +si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_strength) { int band; int delay = 3; diff --git a/ui.c b/ui.c index 8cccb6d..80147b9 100644 --- a/ui.c +++ b/ui.c @@ -20,6 +20,7 @@ #include "ch.h" #include "hal.h" +#include "chprintf.h" #include "nanovna.h" #include #include