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)
This commit is contained in:
DiSlord 2020-03-14 15:18:14 +03:00
parent 88617a31fe
commit 922b66abdb
6 changed files with 49 additions and 35 deletions

4
fft.h
View file

@ -68,8 +68,8 @@ static void fft256(float array[][2], const uint8_t dir) {
uint16_t j, k; uint16_t j, k;
for (j = i, k = 0; j < i + halfsize; j++, k += tablestep) { for (j = i, k = 0; j < i + halfsize; j++, k += tablestep) {
uint16_t l = j + halfsize; 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 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 * M_PI * k / 256) + array[l][imag] * cos(2 * M_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][real] = array[j][real] - tpre;
array[l][imag] = array[j][imag] - tpim; array[l][imag] = array[j][imag] - tpim;
array[j][real] += tpre; array[j][real] += tpre;

30
main.c
View file

@ -72,8 +72,8 @@ static volatile vna_shellcmd_t shell_function = 0;
static void apply_error_term_at(int i); static void apply_error_term_at(int i);
static void apply_edelay_at(int i); static void apply_edelay_at(int i);
static void cal_interpolate(int s); static void cal_interpolate(int s);
void update_frequencies(void); static void update_frequencies(void);
void set_frequencies(uint32_t start, uint32_t stop, uint16_t points); static void set_frequencies(uint32_t start, uint32_t stop, uint16_t points);
static bool sweep(bool break_on_operation); static bool sweep(bool break_on_operation);
static void transform_domain(void); static void transform_domain(void);
@ -83,8 +83,6 @@ static void transform_domain(void);
// Obsolete, always use interpolate // Obsolete, always use interpolate
#define cal_auto_interpolate TRUE #define cal_auto_interpolate TRUE
static int32_t frequency_offset = 5000;
static uint32_t frequency = 10000000;
static int8_t drive_strength = DRIVE_STRENGTH_AUTO; static int8_t drive_strength = DRIVE_STRENGTH_AUTO;
int8_t sweep_mode = SWEEP_ENABLE; int8_t sweep_mode = SWEEP_ENABLE;
volatile uint8_t redraw_request = 0; // contains REDRAW_XXX flags volatile uint8_t redraw_request = 0; // contains REDRAW_XXX flags
@ -330,7 +328,7 @@ static int
adjust_gain(uint32_t newfreq) adjust_gain(uint32_t newfreq)
{ {
int new_order = newfreq / FREQ_HARMONICS; int new_order = newfreq / FREQ_HARMONICS;
int old_order = frequency / FREQ_HARMONICS; int old_order = si5351_getFrequency() / FREQ_HARMONICS;
if (new_order != old_order) { if (new_order != old_order) {
tlv320aic3204_set_gain(gain_table[new_order], gain_table[new_order]); tlv320aic3204_set_gain(gain_table[new_order], gain_table[new_order]);
return DELAY_GAIN_CHANGE; return DELAY_GAIN_CHANGE;
@ -345,9 +343,7 @@ int set_frequency(uint32_t freq)
if (ds == DRIVE_STRENGTH_AUTO) { if (ds == DRIVE_STRENGTH_AUTO) {
ds = freq > FREQ_HARMONICS ? SI5351_CLK_DRIVE_STRENGTH_8MA : SI5351_CLK_DRIVE_STRENGTH_2MA; ds = freq > FREQ_HARMONICS ? SI5351_CLK_DRIVE_STRENGTH_8MA : SI5351_CLK_DRIVE_STRENGTH_2MA;
} }
delay += si5351_set_frequency_with_offset(freq, frequency_offset, ds); delay += si5351_set_frequency_with_offset(freq, ds);
frequency = freq;
return delay; return delay;
} }
@ -468,8 +464,7 @@ VNA_SHELL_FUNCTION(cmd_offset)
shell_printf("usage: offset {frequency offset(Hz)}\r\n"); shell_printf("usage: offset {frequency offset(Hz)}\r\n");
return; return;
} }
frequency_offset = my_atoui(argv[0]); si5351_set_frequency_offset(my_atoi(argv[0]));
set_frequency(frequency);
} }
VNA_SHELL_FUNCTION(cmd_freq) VNA_SHELL_FUNCTION(cmd_freq)
@ -493,7 +488,7 @@ VNA_SHELL_FUNCTION(cmd_power)
return; return;
} }
drive_strength = my_atoi(argv[0]); drive_strength = my_atoi(argv[0]);
set_frequency(frequency); // set_frequency(frequency);
} }
#ifdef ENABLE_TIME_COMMAND #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) set_frequencies(uint32_t start, uint32_t stop, uint16_t points)
{ {
uint32_t i; uint32_t i;
@ -923,7 +918,7 @@ set_frequencies(uint32_t start, uint32_t stop, uint16_t points)
frequencies[i] = 0; frequencies[i] = 0;
} }
void static void
update_frequencies(void) update_frequencies(void)
{ {
uint32_t start, stop; uint32_t start, stop;
@ -1100,7 +1095,7 @@ adjust_ed(void)
// prepare 1/s11ao to avoid dividing complex // prepare 1/s11ao to avoid dividing complex
float c = 1000e-15; float c = 1000e-15;
float z0 = 50; 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; float z = 0.02;
cal_data[ETERM_ED][i][0] += z; cal_data[ETERM_ED][i][0] += z;
} }
@ -1118,7 +1113,7 @@ eterm_calc_es(void)
float c = 50e-15; float c = 50e-15;
//float c = 1.707e-12; //float c = 1.707e-12;
float z0 = 50; 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 sq = 1 + z*z;
float s11aor = (1 - z*z) / sq; float s11aor = (1 - z*z) / sq;
float s11aoi = 2*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) 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 s = sin(w);
float c = cos(w); float c = cos(w);
float real = measured[0][i][0]; float real = measured[0][i][0];
@ -1667,7 +1662,7 @@ VNA_SHELL_FUNCTION(cmd_marker)
if (t < 0 || t >= MARKERS_MAX) if (t < 0 || t >= MARKERS_MAX)
goto usage; goto usage;
if (argc == 1) { 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; active_marker = t;
// select active marker // select active marker
markers[t].enabled = TRUE; markers[t].enabled = TRUE;
@ -1893,7 +1888,6 @@ VNA_SHELL_FUNCTION(cmd_stat)
// shell_printf("awd: %d\r\n", awd_count); // shell_printf("awd: %d\r\n", awd_count);
} }
#ifndef VERSION #ifndef VERSION
#define VERSION "unknown" #define VERSION "unknown"
#endif #endif

View file

@ -25,9 +25,17 @@
/* /*
* main.c * main.c
*/ */
#define START_MIN 50000
#define STOP_MAX 2700000000U // Minimum frequency set
#define SPEED_OF_LIGHT 299792458 #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 #define POINTS_COUNT 101
extern float measured[2][POINTS_COUNT][2]; extern float measured[2][POINTS_COUNT][2];

10
plot.c
View file

@ -425,7 +425,7 @@ logmag(const float *v)
static float static float
phase(const float *v) 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)) // atan(w)-atan(v) = atan((w-v)/(1+wv))
float r = w[0]*v[1] - w[1]*v[0]; float r = w[0]*v[1] - w[1]*v[0];
float i = w[0]*v[0] + w[1]*v[1]; 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 #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 #endif
} }
@ -605,11 +605,11 @@ format_smith_value(char *buf, int len, const float coeff[2], uint32_t frequency)
case MS_RLC: case MS_RLC:
if (zi < 0){// Capacity if (zi < 0){// Capacity
prefix = 'F'; prefix = 'F';
value = -1 / (2 * M_PI * frequency * zi); value = -1 / (2 * VNA_PI * frequency * zi);
} }
else { else {
prefix = 'H'; 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); plot_printf(buf, len, "%F"S_OHM" %F%c", zr, value, prefix);
break; break;

View file

@ -36,8 +36,9 @@
// I2C address on bus (only 0x60 for Si5351A in 10-Pin MSOP) // I2C address on bus (only 0x60 for Si5351A in 10-Pin MSOP)
#define SI5351_I2C_ADDR 0x60 #define SI5351_I2C_ADDR 0x60
static uint8_t current_band = 0; static uint8_t current_band = 0;
static uint32_t current_freq = 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 // Minimum value is 2, freq change apply at next dsp measure, and need skip it
#define DELAY_NORMAL 2 #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) // 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 #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 static void
si5351_bulk_write(const uint8_t *buf, int len) 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 * CLK2: fixed 8MHz
*/ */
int 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; uint8_t band;
int delay = DELAY_NORMAL; int delay = DELAY_NORMAL;
if (freq == current_freq) if (freq == current_freq)
return delay; return delay;
uint32_t ofreq = freq + offset; uint32_t ofreq = freq + current_offset;
uint32_t mul = 1, omul = 1; uint32_t mul = 1, omul = 1;
uint32_t rdiv = SI5351_R_DIV_1; uint32_t rdiv = SI5351_R_DIV_1;
uint32_t fdiv; uint32_t fdiv;
// Fix possible uncorrect input
drive_strength&=SI5351_CLK_DRIVE_STRENGTH_MASK;
current_freq = freq; current_freq = freq;
if (freq >= config.harmonic_freq_threshold * 7U) { if (freq >= config.harmonic_freq_threshold * 7U) {
mul = 9; mul = 9;
@ -386,7 +396,6 @@ si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_streng
freq<<= 3; freq<<= 3;
ofreq<<= 3; ofreq<<= 3;
} }
band = si5351_getBand(freq/mul); band = si5351_getBand(freq/mul);
switch (band) { switch (band) {
case 1: 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); 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; break;
} }
if (current_band != band) { if (current_band != band) {
si5351_reset_pll(SI5351_PLL_RESET_A|SI5351_PLL_RESET_B); si5351_reset_pll(SI5351_PLL_RESET_A|SI5351_PLL_RESET_B);
current_band = band; current_band = band;

View file

@ -72,4 +72,8 @@ void si5351_init(void);
void si5351_disable_output(void); void si5351_disable_output(void);
void si5351_enable_output(void); void si5351_enable_output(void);
//void si5351_set_frequency(int channel, uint32_t freq, uint8_t drive_strength); //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);