From 922b66abdba87324d2476be8dddec8c399ec9c64 Mon Sep 17 00:00:00 2001 From: DiSlord Date: Sat, 14 Mar 2020 15:18:14 +0300 Subject: [PATCH] Move offset variable to si5351.c (better use it as independent library) Define and move constants in nanovna.h, and use it Fix command 'marker' - display marker freq (not current freq) --- fft.h | 4 ++-- main.c | 30 ++++++++++++------------------ nanovna.h | 14 +++++++++++--- plot.c | 10 +++++----- si5351.c | 20 ++++++++++++++------ si5351.h | 6 +++++- 6 files changed, 49 insertions(+), 35 deletions(-) diff --git a/fft.h b/fft.h index 0c367b7..dbaa32f 100644 --- a/fft.h +++ b/fft.h @@ -68,8 +68,8 @@ static void fft256(float array[][2], const uint8_t dir) { uint16_t j, k; for (j = i, k = 0; j < i + halfsize; j++, k += tablestep) { uint16_t l = j + halfsize; - float tpre = array[l][real] * cos(2 * M_PI * k / 256) + array[l][imag] * sin(2 * M_PI * k / 256); - float tpim = -array[l][real] * sin(2 * M_PI * k / 256) + array[l][imag] * cos(2 * M_PI * k / 256); + float tpre = array[l][real] * cos(2 * VNA_PI * k / 256) + array[l][imag] * sin(2 * VNA_PI * k / 256); + float tpim = -array[l][real] * sin(2 * VNA_PI * k / 256) + array[l][imag] * cos(2 * VNA_PI * k / 256); array[l][real] = array[j][real] - tpre; array[l][imag] = array[j][imag] - tpim; array[j][real] += tpre; diff --git a/main.c b/main.c index e903394..3748579 100644 --- a/main.c +++ b/main.c @@ -72,8 +72,8 @@ static volatile vna_shellcmd_t shell_function = 0; 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, uint16_t points); +static void update_frequencies(void); +static void set_frequencies(uint32_t start, uint32_t stop, uint16_t points); static bool sweep(bool break_on_operation); static void transform_domain(void); @@ -83,8 +83,6 @@ static void transform_domain(void); // 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; int8_t sweep_mode = SWEEP_ENABLE; volatile uint8_t redraw_request = 0; // contains REDRAW_XXX flags @@ -330,7 +328,7 @@ static int adjust_gain(uint32_t newfreq) { int new_order = newfreq / FREQ_HARMONICS; - int old_order = frequency / FREQ_HARMONICS; + int old_order = si5351_getFrequency() / FREQ_HARMONICS; if (new_order != old_order) { tlv320aic3204_set_gain(gain_table[new_order], gain_table[new_order]); return DELAY_GAIN_CHANGE; @@ -345,9 +343,7 @@ int set_frequency(uint32_t freq) if (ds == DRIVE_STRENGTH_AUTO) { ds = freq > FREQ_HARMONICS ? SI5351_CLK_DRIVE_STRENGTH_8MA : SI5351_CLK_DRIVE_STRENGTH_2MA; } - delay += si5351_set_frequency_with_offset(freq, frequency_offset, ds); - - frequency = freq; + delay += si5351_set_frequency_with_offset(freq, ds); return delay; } @@ -468,8 +464,7 @@ VNA_SHELL_FUNCTION(cmd_offset) shell_printf("usage: offset {frequency offset(Hz)}\r\n"); return; } - frequency_offset = my_atoui(argv[0]); - set_frequency(frequency); + si5351_set_frequency_offset(my_atoi(argv[0])); } VNA_SHELL_FUNCTION(cmd_freq) @@ -493,7 +488,7 @@ VNA_SHELL_FUNCTION(cmd_power) return; } drive_strength = my_atoi(argv[0]); - set_frequency(frequency); +// set_frequency(frequency); } #ifdef ENABLE_TIME_COMMAND @@ -901,7 +896,7 @@ update_marker_index(void) } } -void +static void set_frequencies(uint32_t start, uint32_t stop, uint16_t points) { uint32_t i; @@ -923,7 +918,7 @@ set_frequencies(uint32_t start, uint32_t stop, uint16_t points) frequencies[i] = 0; } -void +static void update_frequencies(void) { uint32_t start, stop; @@ -1100,7 +1095,7 @@ adjust_ed(void) // prepare 1/s11ao to avoid dividing complex float c = 1000e-15; float z0 = 50; - //float z = 2 * M_PI * frequencies[i] * c * z0; + //float z = 2 * VNA_PI * frequencies[i] * c * z0; float z = 0.02; cal_data[ETERM_ED][i][0] += z; } @@ -1118,7 +1113,7 @@ eterm_calc_es(void) float c = 50e-15; //float c = 1.707e-12; float z0 = 50; - float z = 2 * M_PI * frequencies[i] * c * z0; + float z = 2 * VNA_PI * frequencies[i] * c * z0; float sq = 1 + z*z; float s11aor = (1 - z*z) / sq; float s11aoi = 2*z / sq; @@ -1254,7 +1249,7 @@ static void apply_error_term_at(int i) static void apply_edelay_at(int i) { - float w = 2 * M_PI * electrical_delay * frequencies[i] * 1E-12; + float w = 2 * VNA_PI * electrical_delay * frequencies[i] * 1E-12; float s = sin(w); float c = cos(w); float real = measured[0][i][0]; @@ -1667,7 +1662,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, frequency); + shell_printf("%d %d %d\r\n", t+1, markers[t].index, markers[t].frequency); active_marker = t; // select active marker markers[t].enabled = TRUE; @@ -1893,7 +1888,6 @@ VNA_SHELL_FUNCTION(cmd_stat) // shell_printf("awd: %d\r\n", awd_count); } - #ifndef VERSION #define VERSION "unknown" #endif diff --git a/nanovna.h b/nanovna.h index 0fbab6d..5fddc78 100644 --- a/nanovna.h +++ b/nanovna.h @@ -25,9 +25,17 @@ /* * main.c */ -#define START_MIN 50000 -#define STOP_MAX 2700000000U -#define SPEED_OF_LIGHT 299792458 + +// Minimum frequency set +#define START_MIN 50000 +// Maximum frequency set +#define STOP_MAX 2700000000U +// Frequency offset (sin_cos table in dsp.c generated for this offset, if change need create new table) +#define FREQUENCY_OFFSET 5000 +// Speed of light const +#define SPEED_OF_LIGHT 299792458 +// pi const +#define VNA_PI 3.14159265358979323846 #define POINTS_COUNT 101 extern float measured[2][POINTS_COUNT][2]; diff --git a/plot.c b/plot.c index 1ebf2b2..fd2f8cf 100644 --- a/plot.c +++ b/plot.c @@ -425,7 +425,7 @@ logmag(const float *v) static float phase(const float *v) { - return 2 * atan2f(v[1], v[0]) / M_PI * 90; + return 2 * atan2f(v[1], v[0]) / VNA_PI * 90; } /* @@ -438,9 +438,9 @@ groupdelay(const float *v, const float *w, float deltaf) // atan(w)-atan(v) = atan((w-v)/(1+wv)) float r = w[0]*v[1] - w[1]*v[0]; float i = w[0]*v[0] + w[1]*v[1]; - return atan2f(r, i) / (2 * M_PI * deltaf); + return atan2f(r, i) / (2 * VNA_PI * deltaf); #else - return (atan2f(w[0], w[1]) - atan2f(v[0], v[1])) / (2 * M_PI * deltaf); + return (atan2f(w[0], w[1]) - atan2f(v[0], v[1])) / (2 * VNA_PI * deltaf); #endif } @@ -605,11 +605,11 @@ format_smith_value(char *buf, int len, const float coeff[2], uint32_t frequency) case MS_RLC: if (zi < 0){// Capacity prefix = 'F'; - value = -1 / (2 * M_PI * frequency * zi); + value = -1 / (2 * VNA_PI * frequency * zi); } else { prefix = 'H'; - value = zi / (2 * M_PI * frequency); + value = zi / (2 * VNA_PI * frequency); } plot_printf(buf, len, "%F"S_OHM" %F%c", zr, value, prefix); break; diff --git a/si5351.c b/si5351.c index 7cd37da..fefffb0 100644 --- a/si5351.c +++ b/si5351.c @@ -36,8 +36,9 @@ // I2C address on bus (only 0x60 for Si5351A in 10-Pin MSOP) #define SI5351_I2C_ADDR 0x60 -static uint8_t current_band = 0; -static uint32_t current_freq = 0; +static uint8_t current_band = 0; +static uint32_t current_freq = 0; +static int32_t current_offset = FREQUENCY_OFFSET; // Minimum value is 2, freq change apply at next dsp measure, and need skip it #define DELAY_NORMAL 2 @@ -50,6 +51,13 @@ static uint32_t current_freq = 0; // Delay after set new PLL values, and send reset (on band 1 unstable if less then 900, on 4000-5000 no amplitude spike on change) #define DELAY_RESET_PLL 5000 +uint32_t si5351_getFrequency(void) {return current_freq;} + +void si5351_set_frequency_offset(int32_t offset) { + current_offset = offset; + current_freq = 0; // reset freq, for +} + static void si5351_bulk_write(const uint8_t *buf, int len) { @@ -354,15 +362,17 @@ static inline uint8_t si5351_getBand(uint32_t freq){ * CLK2: fixed 8MHz */ int -si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_strength){ +si5351_set_frequency_with_offset(uint32_t freq, uint8_t drive_strength){ uint8_t band; int delay = DELAY_NORMAL; if (freq == current_freq) return delay; - uint32_t ofreq = freq + offset; + uint32_t ofreq = freq + current_offset; uint32_t mul = 1, omul = 1; uint32_t rdiv = SI5351_R_DIV_1; uint32_t fdiv; + // Fix possible uncorrect input + drive_strength&=SI5351_CLK_DRIVE_STRENGTH_MASK; current_freq = freq; if (freq >= config.harmonic_freq_threshold * 7U) { mul = 9; @@ -386,7 +396,6 @@ si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_streng freq<<= 3; ofreq<<= 3; } - band = si5351_getBand(freq/mul); switch (band) { case 1: @@ -420,7 +429,6 @@ si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_streng 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) { si5351_reset_pll(SI5351_PLL_RESET_A|SI5351_PLL_RESET_B); current_band = band; diff --git a/si5351.h b/si5351.h index 6b5bdf5..42a7d99 100644 --- a/si5351.h +++ b/si5351.h @@ -72,4 +72,8 @@ void si5351_init(void); void si5351_disable_output(void); void si5351_enable_output(void); //void si5351_set_frequency(int channel, uint32_t freq, uint8_t drive_strength); -int si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_strength); + +void si5351_set_frequency_offset(int32_t offset); +int si5351_set_frequency_with_offset(uint32_t freq, uint8_t drive_strength); +uint32_t si5351_getFrequency(void); +