diff --git a/NANOVNA_STM32_F303/adc.c b/NANOVNA_STM32_F303/adc.c index be96195..b6444e6 100644 --- a/NANOVNA_STM32_F303/adc.c +++ b/NANOVNA_STM32_F303/adc.c @@ -105,8 +105,6 @@ uint16_t adc_single_read(uint32_t chsel) uint16_t adc1_single_read(uint32_t chsel) { /* ADC setup */ -// adcStart(&ADCD2, NULL); - adcgrpcfgXY.sqr[0] = ADC_SQR1_SQ1_N(ADC_CHANNEL_IN1); adcConvert(&ADCD1, &adcgrpcfgVersion, samples, 1); return(samples[0]); } diff --git a/main.c b/main.c index 5f821c4..c72bfed 100644 --- a/main.c +++ b/main.c @@ -145,7 +145,7 @@ static THD_FUNCTION(Thread1, arg) #ifdef __LISTEN__ } else if (sweep_mode & SWEEP_LISTEN) { if (markers[active_marker].enabled == M_ENABLED) { - perform(false,0,frequencies[markers[active_marker].index], false); + perform(false, 0, getFrequency(markers[active_marker].index), false); SI4432_Listen(MODE_SELECT(setting.mode)); } #endif @@ -194,9 +194,7 @@ static THD_FUNCTION(Thread1, arg) if (uistat.marker_tracking) { int i = marker_search_max(active_marker); if (i != -1 && active_marker != MARKER_INVALID) { - markers[active_marker].index = i; - markers[active_marker].frequency = frequencies[i]; - + set_marker_index(active_marker, i); redraw_request |= REDRAW_MARKER; } } @@ -995,7 +993,7 @@ VNA_SHELL_FUNCTION(cmd_scan) uint16_t mask = my_atoui(argv[3]); if (mask) { for (i = 0; i < sweep_points; i++) { - if (mask & 1) shell_printf("%U ", frequencies[i]); + if (mask & 1) shell_printf("%U ", getFrequency(i)); if (mask & 2) shell_printf("%f %f ", value(measured[TRACE_ACTUAL][i]), 0.0); if (mask & 4) shell_printf("%f %f ", value(measured[TRACE_STORED][i]), 0.0); if (mask & 8) shell_printf("%f %f ", value(measured[TRACE_TEMP][i]), 0.0); @@ -1066,7 +1064,7 @@ VNA_SHELL_FUNCTION(cmd_hop) #endif static void -update_marker_index(void) +update_markers_index(void) { int m, idx; freq_t fstart = get_sweep_frequency(ST_START); @@ -1083,8 +1081,8 @@ update_marker_index(void) else { // Search frequency index for marker frequency #if 1 for (idx = 1; idx < sweep_points; idx++) { - if (frequencies[idx] <= f) continue; - if (f < (frequencies[idx-1]/2 + frequencies[idx]/2)) idx--; // Correct closest idx + if (getFrequency(idx) <= f) continue; + if (f < (getFrequency(idx-1)/2 + getFrequency(idx)/2)) idx--; // Correct closest idx break; } #else @@ -1092,20 +1090,27 @@ update_marker_index(void) idx = r * (sweep_points-1); #endif } - markers[m].index = idx; - markers[m].frequency = frequencies[idx]; + set_marker_index(m, idx); } } +void +set_marker_index(int m, int16_t idx) +{ + if ((uint32_t)m >= MARKERS_MAX || (uint16_t)idx >= sweep_points) return; + markers[m].index = idx; + markers[m].frequency = getFrequency(idx); +} + void set_marker_frequency(int m, freq_t f) { if (m == MARKER_INVALID || !markers[m].enabled) return; int i = 1; markers[m].mtype &= ~M_TRACKING; - freq_t s = (frequencies[1] - frequencies[0])/2; + freq_t s = (getFrequency(1) - getFrequency(0))/2; while (i< sweep_points - 2){ - if (frequencies[i]-s <= f && f < frequencies[i+1]-s) { // Avoid rounding error in s!!!!!!! + if (getFrequency(i)-s <= f && f < getFrequency(i+1)-s) { // Avoid rounding error in s!!!!!!! markers[m].index = i; markers[m].frequency = f; return; @@ -1126,6 +1131,11 @@ void set_marker_time(int m, float f) markers[m].frequency = 0; } +/* + * Frequency list functions + */ +#ifdef __USE_FREQ_TABLE__ +static freq_t frequencies[POINTS_COUNT]; static void set_frequencies(freq_t start, freq_t stop, uint16_t points) { @@ -1137,11 +1147,7 @@ set_frequencies(freq_t start, freq_t stop, uint16_t points) freq_t f = start, df = step>>1; for (i = 0; i <= step; i++, f+=delta) { frequencies[i] = f; - df+=error; - if (df >=step) { - f++; - df -= step; - } + if ((df+=error) >= step) {f++; df-= step;} } // disable at out of sweep range for (; i < POINTS_COUNT; i++) @@ -1149,6 +1155,27 @@ set_frequencies(freq_t start, freq_t stop, uint16_t points) setting.frequency_step = delta; dirty = true; } +freq_t getFrequency(uint16_t idx) {return frequencies[idx];} +#else +static freq_t _f_start; +static freq_t _f_delta; +static freq_t _f_error; +static uint16_t _f_count; + +static void +set_frequencies(freq_t start, freq_t stop, uint16_t points) +{ + freq_t span = stop - start; + _f_start = start; + _f_count = (points - 1); + _f_delta = span / _f_count; + _f_error = span % _f_count; + setting.frequency_step = _f_delta; + dirty = true; +} +freq_t getFrequency(uint16_t idx) {return _f_start + _f_delta * idx + (_f_count / 2 + _f_error * idx) / _f_count;} +#endif + void update_frequencies(void) @@ -1160,7 +1187,7 @@ update_frequencies(void) set_frequencies(start, stop, sweep_points); // operation_requested|= OP_FREQCHANGE; - update_marker_index(); + update_markers_index(); // set grid layout update_grid(); @@ -1469,8 +1496,7 @@ VNA_SHELL_FUNCTION(cmd_marker) case 2: markers[t].enabled = TRUE; active_marker = t; int i = marker_search_max(active_marker); if (i == -1) i = 0; - markers[active_marker].index = i; - markers[active_marker].frequency = frequencies[i]; + set_marker_index(active_marker, i); goto display_marker; default: // select active marker and move to index or frequency @@ -1480,10 +1506,8 @@ VNA_SHELL_FUNCTION(cmd_marker) active_marker = t; if (value > sweep_points) set_marker_frequency(active_marker, value); - else { - markers[t].index = value; - markers[t].frequency = frequencies[value]; - } + else + set_marker_index(t, value); return; } usage: @@ -1522,10 +1546,8 @@ VNA_SHELL_FUNCTION(cmd_frequencies) int i; (void)argc; (void)argv; - for (i = 0; i < sweep_points; i++) { - if (frequencies[i] != 0) - shell_printf("%U\r\n", frequencies[i]); - } + for (i = 0; i < sweep_points; i++) + shell_printf("%U\r\n", getFrequency(i)); } VNA_SHELL_FUNCTION(cmd_test) diff --git a/nanovna.h b/nanovna.h index c49e39d..bd4d319 100644 --- a/nanovna.h +++ b/nanovna.h @@ -96,6 +96,7 @@ #else #define __HARMONIC__ #endif +//#define __USE_FREQ_TABLE__ // Enable use table for frequency list #ifdef TINYSA3 #define VARIANT(X,Y) (X) @@ -204,6 +205,7 @@ void send_buffer(uint8_t * buf, int s); #endif void set_marker_frequency(int m, freq_t f); void set_marker_time(int m, float f); +void set_marker_index(int m, int16_t idx); void toggle_sweep(void); void toggle_mute(void); void toggle_pulse(void); @@ -1068,7 +1070,6 @@ enum {W_OFF, W_SMALL, W_BIG}; #define REPEAT_TIME 111 // Time per extra repeat in uS #define MEASURE_TIME 127 // Time per single point measurement with vbwstep =1 without step delay in uS -extern freq_t frequencies[POINTS_COUNT]; extern const float unit_scale_value[]; extern const char unit_scale_text[]; #ifdef TINYSA4 @@ -1156,6 +1157,8 @@ extern int16_t lastsaveid; //extern properties_t current_props; +freq_t getFrequency(uint16_t idx); + //#define frequency0 current_props._frequency0 //#define frequency1 current_props._frequency1 #define sweep_points setting._sweep_points diff --git a/plot.c b/plot.c index 31140d5..7aba0ad 100644 --- a/plot.c +++ b/plot.c @@ -1625,7 +1625,8 @@ static void cell_draw_marker_info(int x0, int y0) int search_gate = 10; if (markers[0].index < search_gate) search_gate = markers[0].index * 2 / 3; - while (f * h_i < frequencies[sweep_points-1]) { + freq_t stop = getFrequency(sweep_points-1); + while (f * h_i < stop) { if (search_maximum(1, f*h_i, search_gate) ) { // use marker 1 for searching harmonics h_count++; f = (f * 3 + markers[1].frequency / h_i) >> 2; diff --git a/sa_core.c b/sa_core.c index 2c2e496..dbd6ee7 100644 --- a/sa_core.c +++ b/sa_core.c @@ -54,7 +54,6 @@ bool debug_avoid_second = false; int current_index = -1; setting_t setting; -freq_t frequencies[POINTS_COUNT]; uint16_t actual_rbw_x10 = 0; freq_t frequency_step_x10 = 0; @@ -908,7 +907,7 @@ void limits_update(void) { if (setting.limits[i].enabled) { active = true; - while (j < sweep_points && (frequencies[j] < setting.limits[i].frequency || setting.limits[i].frequency == 0)) + while (j < sweep_points && (getFrequency(j) < setting.limits[i].frequency || setting.limits[i].frequency == 0)) stored_t[j++] = setting.limits[i].level; } } @@ -2158,10 +2157,10 @@ done: int binary_search_frequency(freq_t f) // Search which index in the frequency tabled matches with frequency f using actual_rbw { int L = 0; - int frequency_seatch_gate = (frequencies[1] - frequencies[0]) >> 1; - if (f < frequencies[0]) + int frequency_seatch_gate = (getFrequency(1) - getFrequency(0)) >> 1; + if (f < getFrequency(0)) return -1; - if (f > frequencies[sweep_points-1]) + if (f > getFrequency(sweep_points-1)) return -1; // int R = (sizeof frequencies)/sizeof(int) - 1; int R = sweep_points - 1; @@ -2169,9 +2168,10 @@ int binary_search_frequency(freq_t f) // Search which index in the frequenc freq_t fplus = f + frequency_seatch_gate; // actual_rbw_x10 * frequency_seatch_gate; while (L <= R) { int m = (L + R) / 2; - if (frequencies[m] < fmin) + freq_t f = getFrequency(m); + if (f < fmin) L = m + 1; - else if (frequencies[m] > fplus) + else if (f > fplus) R = m - 1; else return m; // index is m @@ -2181,14 +2181,14 @@ int binary_search_frequency(freq_t f) // Search which index in the frequenc int index_of_frequency(freq_t f) // Search which index in the frequency tabled matches with frequency f using actual_rbw { - freq_t f_step = frequencies[1] - frequencies[0]; + freq_t f_step = getFrequency(1) - getFrequency(0); if (f_step == 0) return 0; - if (f < frequencies[0]) + if (f < getFrequency(0)) return -1; - if (f > frequencies[sweep_points-1]) + if (f > getFrequency(sweep_points-1)) return -1; - int i = ((f - frequencies[0] ) + (f_step >> 1)) / f_step; + int i = ((f - getFrequency(0) ) + (f_step >> 1)) / f_step; return i; #if 0 // int R = (sizeof frequencies)/sizeof(int) - 1; @@ -2198,9 +2198,10 @@ int index_of_frequency(freq_t f) // Search which index in the frequency tab freq_t fplus = f + frequency_seatch_gate; // actual_rbw_x10 * frequency_seatch_gate; while (L <= R) { int m = (L + R) / 2; - if (frequencies[m] < fmin) + freq_t f = getFrequency(m); + if (f < fmin) L = m + 1; - else if (frequencies[m] > fplus) + else if (f > fplus) R = m - 1; else return m; // index is m @@ -2217,11 +2218,11 @@ void interpolate_maximum(int m) ref_marker_levels = stored_t; else ref_marker_levels = actual_t; - const int idx = markers[m].index; - markers[m].frequency = frequencies[idx]; + const int idx = markers[m].index; + markers[m].frequency = getFrequency(idx); if (idx > 0 && idx < sweep_points-1) { - const int32_t delta_Hz = (int64_t)frequencies[idx + 0] - frequencies[idx + 1]; + const int32_t delta_Hz = (int64_t)getFrequency(idx + 0) - getFrequency(idx + 1); #ifdef TINYSA4 #define INTER_TYPE double #else @@ -3469,12 +3470,13 @@ again: // Spur redu } else f_low = f_high = real_old_freq[SI4463_RX] + real_offset; float f_error_low, f_error_high; + float freq = getFrequency(i); if (setting.frequency_step == 0) { - f_error_low = ((float)frequencies[i] - (float)f_low); - f_error_high = ((float)f_high-(float)frequencies[i]); + f_error_low = (freq - f_low); + f_error_high = (f_high - freq); } else { - f_error_low = ((float)f_low-(float)frequencies[i])/setting.frequency_step; - f_error_high = ((float)f_high-(float)frequencies[i])/setting.frequency_step; + f_error_low = (f_low - freq)/setting.frequency_step; + f_error_high = (f_high- freq)/setting.frequency_step; } char spur = ' '; int delta=0; @@ -3797,8 +3799,9 @@ static bool sweep(bool break_on_operation) debug_avoid_second = false; debug_avoid_label: debug_avoid_second = debug_avoid_second; + freq_t current_freq = getFrequency(i); // --------------------- measure ------------------------- - pureRSSI_t rssi = perform(break_on_operation, i, frequencies[i], setting.tracking); // Measure RSSI for one of the frequencies + pureRSSI_t rssi = perform(break_on_operation, i, current_freq, setting.tracking); // Measure RSSI for one of the frequencies #ifdef TINYSA4 if (rssi == IGNORE_RSSI) RSSI = -174.0; @@ -3835,12 +3838,12 @@ static bool sweep(bool break_on_operation) #define AGC_RSSI_THRESHOLD (-55+get_attenuation()) float local_rssi = RSSI +setting.external_gain; if (local_rssi > AGC_RSSI_THRESHOLD && local_rssi > agc_prev_rssi) { - agc_peak_freq = frequencies[i]; + agc_peak_freq = current_freq; agc_peak_rssi = agc_prev_rssi = local_rssi; } if (local_rssi < AGC_RSSI_THRESHOLD) agc_prev_rssi = -150; - freq_t delta_freq = frequencies[i] - agc_peak_freq; + freq_t delta_freq = current_freq - agc_peak_freq; if (agc_peak_freq != 0 && delta_freq < 2000000) { int max_gain = (-25 - agc_peak_rssi ) / 4; auto_set_AGC_LNA(false, 16 + delta_freq * max_gain / 2000000 ); // enable LNA and stepwise gain @@ -3961,7 +3964,7 @@ static bool sweep(bool break_on_operation) float d_offset = 0.0; int d_start = 0; if (setting.average == AV_DECONV && setting.frequency_step != 0) { - d_width = (sweep_points * (actual_rbw_x10 * 250) / (frequencies[sweep_points-1] - frequencies[0])); + d_width = (sweep_points * (actual_rbw_x10 * 250) / get_sweep_frequency(ST_SPAN)); d_start = sweep_points/2 - d_width/2; d_offset = stored_t[d_start]; for (int i=0; i 10) { // ----------AM measurement int l = markers[1].index; @@ -4518,8 +4520,8 @@ static volatile int dummy; markers[1].index = l; markers[2].index = r; } - freq_t lf = frequencies[l]; - freq_t rf = frequencies[r]; + freq_t lf = getFrequency(l); + freq_t rf = getFrequency(r); markers[1].frequency = lf; markers[2].frequency = rf; #endif @@ -4529,35 +4531,28 @@ static volatile int dummy; if (r < l) { l = markers[1].index; r = markers[0].index; - markers[0].index = l; - markers[1].index = r; } - freq_t lf = frequencies[l]; - freq_t rf = frequencies[r]; - markers[0].frequency = lf; - markers[1].frequency = rf; - + set_marker_index(0, l); + set_marker_index(1, r); + freq_t lf = markers[0].frequency; + freq_t rf = markers[1].frequency; markers[2].enabled = search_maximum(2, lf - (rf - lf), 12); markers[3].enabled = search_maximum(3, rf + (rf - lf), 12); } else if (setting.measurement == M_PHASE_NOISE && markers[0].index > 10) { // ------------Phase noise measurement - markers[1].index = markers[0].index + (setting.mode == M_LOW ? WIDTH/4 : -WIDTH/4); // Position phase noise marker at requested offset - markers[1].frequency = frequencies[markers[1].index]; + // Position phase noise marker at requested offset + set_marker_index(1, markers[0].index + (setting.mode == M_LOW ? WIDTH/4 : -WIDTH/4)); } else if ((setting.measurement == M_PASS_BAND || setting.measurement == M_FM) && markers[0].index > 10) { // ----------------Pass band measurement int t = 0; float v = actual_t[markers[0].index] - (in_selftest ? 6.0 : 3.0); while (t < markers[0].index && actual_t[t+1] < v) // Find left -3dB point t++; - if (t< markers[0].index) { - markers[1].index = t; - markers[1].frequency = frequencies[t]; - } + if (t< markers[0].index) + set_marker_index(1, t); t = setting._sweep_points-1;; while (t > markers[0].index && actual_t[t-1] < v) // find right -3dB point t--; - if (t > markers[0].index) { - markers[2].index = t; - markers[2].frequency = frequencies[t]; - } + if (t > markers[0].index) + set_marker_index(2, t); } else if (setting.measurement == M_AM) { // ----------------AM measurement if (S_IS_AUTO(setting.agc )) { #ifdef __SI4432__ @@ -4600,7 +4595,7 @@ static volatile int dummy; peakLevel = actual_t[peakIndex]; cur_max = 1; } - peakFreq = frequencies[peakIndex]; + peakFreq = getFrequency(peakIndex); min_level = temp_min_level; } // } while (MODE_OUTPUT(setting.mode) && setting.modulation != MO_NONE); // Never exit sweep loop while in output mode with modulation diff --git a/ui.c b/ui.c index 1e4d556..fc327dc 100644 --- a/ui.c +++ b/ui.c @@ -633,7 +633,7 @@ get_marker_frequency(int marker) return 0; if (!markers[marker].enabled) return 0; - return frequencies[markers[marker].index]; + return getFrequency(markers[marker].index); } static UI_FUNCTION_CALLBACK(menu_marker_op_cb) @@ -734,7 +734,7 @@ static UI_FUNCTION_CALLBACK(menu_marker_search_cb) if (data > 1) // Maximum related interpolate_maximum(active_marker); else - markers[active_marker].frequency = frequencies[i]; + markers[active_marker].frequency = getFrequency(i); } redraw_marker(active_marker); // if (data == 4) @@ -1632,7 +1632,7 @@ lever_move_marker(int status) if (idx > sweep_points-1) idx = sweep_points-1 ; } markers[active_marker].index = idx; - markers[active_marker].frequency = frequencies[idx]; + markers[active_marker].frequency = getFrequency(idx); redraw_marker(active_marker); markers[active_marker].mtype &= ~M_TRACKING; // Disable tracking when dragging marker step++; @@ -1976,7 +1976,7 @@ drag_marker(int t, int m) index = search_nearest_index(touch_x, touch_y, t); if (index >= 0) { markers[m].index = index; - markers[m].frequency = frequencies[index]; + markers[m].frequency = getFrequency(index); redraw_marker(m); } } while (touch_check()!= EVT_TOUCH_RELEASED); @@ -2137,7 +2137,7 @@ void save_to_sd(int mask) if (res == FR_OK) { for (int i = 0; i < sweep_points; i++) { char *buf = (char *)spi_buffer; - if (mask & 1) buf += plot_printf(buf, 100, "%U, ", frequencies[i]); + if (mask & 1) buf += plot_printf(buf, 100, "%U, ", getFrequency(i)); if (mask & 2) buf += plot_printf(buf, 100, "%f ", value(measured[TRACE_ACTUAL][i])); if (mask & 4) buf += plot_printf(buf, 100, "%f ", value(measured[TRACE_STORED][i])); if (mask & 8) buf += plot_printf(buf, 100, "%f", value(measured[TRACE_TEMP][i])); diff --git a/ui_sa.c b/ui_sa.c index 09c8f31..7bff292 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -561,7 +561,7 @@ static UI_FUNCTION_ADV_CALLBACK(menu_restart_acb){ if(b){ if (current_index >= 0 && setting.sweep) { float current_level = setting.level + ((float)current_index)* setting.level_sweep / (float)sweep_points; - plot_printf(b->text, sizeof b->text, "STOP %5.3QHz %+.1fdBm", frequencies[current_index], current_level); + plot_printf(b->text, sizeof b->text, "STOP %5.3QHz %+.1fdBm", getFrequency(current_index), current_level); } else plot_printf(b->text, sizeof b->text, "START SWEEP"); @@ -1578,7 +1578,7 @@ static UI_FUNCTION_ADV_CALLBACK(menu_marker_select_acb) } markers[data-1].enabled = true; // interpolate_maximum(data-1); // possibly not a maximum - markers[data-1].frequency = frequencies[markers[data-1].index]; + set_marker_index(data-1, markers[data-1].index); active_marker_select(data-1); menu_push_submenu(menu_marker_modify); redraw_marker(active_marker); @@ -1664,7 +1664,7 @@ static UI_FUNCTION_ADV_CALLBACK(menu_marker_ref_select_acb) } markers[data-1].enabled = true; // interpolate_maximum(data-1); // possibly not a maximum - markers[data-1].frequency = frequencies[markers[data-1].index]; + set_marker_index(data-1, markers[data-1].index); markers[active_marker].ref = data-1; redraw_marker(active_marker); menu_move_back(false);