diff --git a/main.c b/main.c index 6c8c872..58c417d 100644 --- a/main.c +++ b/main.c @@ -47,7 +47,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; @@ -268,36 +269,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) { @@ -366,6 +366,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; @@ -602,16 +614,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_color = /*[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 = { @@ -820,16 +832,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(); @@ -842,9 +853,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(); @@ -896,7 +907,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(); } @@ -1285,6 +1296,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; @@ -1987,6 +2005,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 9197bea..4221e2a 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); @@ -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 c2d61de..6e57ffa 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) @@ -335,10 +336,10 @@ int si5351_set_frequency_with_offset(int freq, int offset, uint8_t drive_strengt 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; }