From a6bdc4dda80703b9bdb1285556d5b128095bfdc6 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Thu, 7 Dec 2023 16:20:54 +0100 Subject: [PATCH] Code cleaning --- main.c | 11 +++++---- nanovna.h | 4 +++- sa_core.c | 70 +++++++++++++++++++++++++++++++++++++++++++------------ ui.c | 10 ++++---- 4 files changed, 70 insertions(+), 25 deletions(-) diff --git a/main.c b/main.c index df24beb..432eca0 100644 --- a/main.c +++ b/main.c @@ -1143,13 +1143,16 @@ config_t config = { #endif }; + +const int to_calibrate[5] = {8,9,10,11,12}; + //properties_t current_props; //properties_t *active_props = ¤t_props; #ifdef TINYSA4 const freq_t v5_2_correction_frequency[CORRECTION_SIZE][CORRECTION_POINTS]= -{ +{ // * * * * /* low */ { 10000, 100000, 300000, 600000, 2000000, 3000000, 30000000, 60000000, 80000000, 120000000, 230000000, 360000000, 520000000, 560000000, 640000000, 750000000, 800000000, 810000000, 820000000, 830000000}, - /* low lna */ { 10000, 30000, 100000, 300000, 1000000, 5000000, 40000000, 70000000, 180000000, 210000000, 250000000, 270000000, 290000000, 480000000, 650000000, 740000000, 790000000, 810000000, 820000000, 830000000}, + /* low lna */ { 10000, 30000, 100000, 300000, 1000000, 5000000, 40000000, 70000000, 210000000, 240000000, 270000000, 300000000, 330000000, 480000000, 650000000, 740000000, 790000000, 810000000, 820000000, 830000000}, /* ultra */ { 30000000, 700000000, 980000000, 1910000000, 2550000000, 2800000000, 2810000000, 3020000000, 3120000000, 3430000000, 3750000000, 4230000000, 4480000000, 4810000000, 5430000000, 5640000000, 6390000000, 6660000000, 6980000000, 7250000000}, /* ultra lna */ { 30000000, 700000000, 1130000000, 1840000000, 2490000000, 2800000000, 2810000000, 3110000000, 3500000000, 3940000000, 4480000000, 4610000000, 4940000000, 5450000000, 6030000000, 6200000000, 6580000000, 6650000000, 7150000000, 7250000000}, /* direct */ { 140000000, 150000000, 160000000, 180000000, 280000000, 290000000, 300000000, 330000000, 340000000, 350000000, 410000000, 480000000, 560000000, 823000000, 830000000, 850000000, 890000000, 950000000, 1090000000, 1120000000}, @@ -2842,7 +2845,7 @@ static void dac_init(void){ #ifdef TINYSA4 void set_freq_boundaries(void) { - if (max2871) { + if (max2871) { // must all be harmonics of 30MHz MAX_LO_FREQ = 6300000000ULL + config.overclock; MAX_ABOVE_IF_FREQ = 4470000000ULL + config.overclock; // Range to use for below IF MIN_BELOW_IF_FREQ = 2310000000ULL + config.overclock; // Range to use for below IF @@ -2851,7 +2854,7 @@ void set_freq_boundaries(void) { MAX_ABOVE_IF_FREQ = 3360000000ULL + config.overclock; // Range to use for below IF MIN_BELOW_IF_FREQ = 2310000000ULL + config.overclock; // Range to use for below IF } - set_jump_freq( MAX_ABOVE_IF_FREQ, (config.harmonic_start?config.harmonic_start:ULTRA_MAX_FREQ)); + set_jump_freq( MAX_ABOVE_IF_FREQ, (config.harmonic_start?config.harmonic_start:ULTRA_MAX_FREQ), MIN_BELOW_IF_FREQ); } #endif int main(void) diff --git a/nanovna.h b/nanovna.h index f9d6112..7cef619 100644 --- a/nanovna.h +++ b/nanovna.h @@ -421,6 +421,8 @@ void set_auto_attenuation(void); void set_auto_reflevel(bool); int is_paused(void); float set_actual_power(float); +void set_actual_correction_value(int current_curve,int current_curve_index, float local_actual_level); +extern const int to_calibrate[5]; void SetGenerate(int); void set_RBW(uint32_t rbw_x10); #ifdef __VBW__ @@ -495,7 +497,7 @@ void toggle_high_out_adf4350(void); extern int high_out_adf4350; #endif int set_actual_freq(freq_t); -void set_jump_freq(freq_t a, freq_t b); +void set_jump_freq(freq_t a, freq_t b, freq_t c); int set_freq_corr(int); void set_IF2(int f); void set_R(int f); diff --git a/sa_core.c b/sa_core.c index 0a2064d..1fb77e4 100644 --- a/sa_core.c +++ b/sa_core.c @@ -726,7 +726,7 @@ void reset_settings(int m) set_sweep_frequency(ST_STOP, maxFreq); #ifdef TINYSA4 set_sweep_frequency(ST_STOP, DEFAULT_MAX_FREQ); // TODO <----------------- temp ---------------------- - setting.attenuate_x2 = 10; + setting.attenuate_x2 = 0; #else setting.attenuate_x2 = 60; #endif @@ -1604,6 +1604,13 @@ float set_actual_power(float target_level) // Set peak level to kno // dirty = true; // No HW update required, only status panel refresh } +void set_actual_correction_value(int current_curve,int current_curve_index, float actual_level){ + float new_offset = actual_level - marker_to_value(0) + config.correction_value[current_curve][current_curve_index]; // calculate offset based on difference between measured peak level and known peak level + if (new_offset > -30 && new_offset < 30) { + config.correction_value[current_curve][current_curve_index] = new_offset; + } +} + float get_level_offset(void) { if (setting.disable_correction) @@ -2294,7 +2301,7 @@ pureRSSI_t get_frequency_correction(freq_t f) // Frequency dependent RSSI c } if (setting.extra_lna) c += 1; -// if (LO_shifting) +// if (LO_shifting && signal_path != PATH_DIRECT) // cv += float_TO_PURE_RSSI(actual_rbw_x10>USE_SHIFT2_RBW ? config.shift2_level_offset : config.shift1_level_offset); } else if (setting.mode == M_GENLOW){ @@ -3486,9 +3493,11 @@ static void calculate_static_correction(void) // Calculate the #endif #ifdef __SI4463__ getSI4463_RSSI_correction() + #endif - - get_signal_path_loss() - + float_TO_PURE_RSSI( + - get_signal_path_loss(); + + pureRSSI_t temp = float_TO_PURE_RSSI( #ifndef TINYSA4 + get_level_offset() // Moved to frequency dependent part #endif @@ -3500,6 +3509,7 @@ static void calculate_static_correction(void) // Calculate the + (setting.mode == M_GENLOW ? (old_temp - 35.0) / 13.0 : (old_temp - 35.0) / 20.0) // About 7.7dB per 10 degrees C in output mode, 1 dB per 20 degrees in input mode #endif - setting.external_gain); + correct_RSSI += temp; } int hsical = -1; @@ -4745,7 +4755,7 @@ again: // Spur redu #endif #ifdef __SI4463__ pureRSSI = Si446x_RSSI(break_on_operation); - if (LO_shifting) + if (LO_shifting && signal_path != PATH_DIRECT) pureRSSI += float_TO_PURE_RSSI(actual_rbw_x10>USE_SHIFT2_RBW ? config.shift2_level_offset : (lf < LOW_SHIFT_FREQ ? config.shift1_level_offset: 0.0)); #endif @@ -4857,7 +4867,7 @@ again: // Spur redu pureRSSI = 0; else { pureRSSI = Si446x_RSSI(break_on_operation); - if (LO_shifting) + if (LO_shifting && signal_path != PATH_DIRECT) pureRSSI += float_TO_PURE_RSSI(actual_rbw_x10>USE_SHIFT2_RBW ? config.shift2_level_offset : (lf < LOW_SHIFT_FREQ ? config.shift1_level_offset: 0.0)); } if (setting.unit == U_RAW) @@ -7591,12 +7601,13 @@ const int power_rbw [5] = { 100, 300, 30, 10, 3 }; #ifdef TINYSA4 -#define JUMP_FREQS 6 -freq_t jump_freqs[JUMP_FREQS] = {LOW_SHIFT_FREQ, LOW_SHIFT_FREQ, DRIVE1_MAX_FREQ, DRIVE2_MAX_FREQ, 0, 0}; +#define JUMP_FREQS 7 +freq_t jump_freqs[JUMP_FREQS] = {LOW_SHIFT_FREQ, LOW_SHIFT_FREQ, DRIVE1_MAX_FREQ, DRIVE2_MAX_FREQ, 0, 0, 0}; -void set_jump_freq(freq_t a, freq_t b) { +void set_jump_freq(freq_t a, freq_t b, freq_t c) { jump_freqs[4] = a; jump_freqs[5] = b; + jump_freqs[6] = c; } void set_jump_config(int i, float v) { @@ -7618,6 +7629,9 @@ void set_jump_config(int i, float v) { break; case 5: config.harmonic_level_offset = v; + break; + case 6: + break; } } @@ -7635,11 +7649,13 @@ float get_jump_config(int i) { return config.shift3_level_offset; case 5: return config.harmonic_level_offset; + case 6: + return 0.0; } return 0; } -enum {CS_NORMAL, CS_LNA, CS_SWITCH, CS_ULTRA, CS_ULTRA_LNA, CS_DIRECT_REF, CS_DIRECT, CS_DIRECT_LNA, CS_HARMONIC, CS_HARMONIC_LNA, CS_MAX }; +enum {CS_NORMAL, CS_LNA, CS_SWITCH, CS_ULTRA, CS_ULTRA_LNA, CS_DIRECT_REF, CS_DIRECT, CS_DIRECT_LNA, CS_HARMONIC, CS_HARMONIC_LNA, /* CS_BPF_REF, CS_BPF, */ CS_CORRECTION_REF, CS_CORRECTION_LNA, CS_MAX }; #define ULTRA_HARMONIC_CAL_FREQ 5340000000 #else enum {CS_NORMAL, CS_SWITCH, CS_MAX }; @@ -7778,11 +7794,11 @@ void calibrate(void) draw_all(TRUE); set_jump_config(i, get_jump_config(i) + measure_jump(i)); calibration_busy(); - chThdSleepMilliseconds(500); + chThdSleepMilliseconds(100); test_acquire(TEST_JUMP); // Acquire test set_jump_config(i, get_jump_config(i) + measure_jump(i)); calibration_busy(); - chThdSleepMilliseconds(500); + chThdSleepMilliseconds(100); } setting.scale = 10; set_trace_scale(10); @@ -7793,8 +7809,9 @@ void calibrate(void) #endif reset_calibration(); in_calibration = true; + int current_correction_calibration = 0; for (calibration_stage = CS_NORMAL; calibration_stage < CS_MAX ; calibration_stage++) { - for (int k = 0; k<3; k++) { + for (int k = 0; k<3; k++) { // max 3 retries float offset = 0.0; for (int j= 0; j < CALIBRATE_RBWS; j++ ) { #if 1 @@ -7870,6 +7887,18 @@ void calibrate(void) test_path = 7; // harmonic lna path force_signal_path = true; break; + case CS_CORRECTION_REF: + if (!max2871) + goto done; + set_sweep_frequency(ST_CENTER, config.correction_frequency[1][to_calibrate[current_correction_calibration]]); + test_path = 0; + force_signal_path = true; + break; + case CS_CORRECTION_LNA: + set_sweep_frequency(ST_CENTER, config.correction_frequency[1][to_calibrate[current_correction_calibration]]); + test_path = 1; + force_signal_path = true; + break; #endif } set_average(0, AV_100); @@ -7940,7 +7969,17 @@ low_level: goto quit; } #ifdef TINYSA4 - if (calibration_stage == CS_DIRECT_REF) + if (calibration_stage == CS_CORRECTION_REF) { + direct_level = marker_to_value(0); + } else if (calibration_stage == CS_CORRECTION_LNA) { + if (direct_level > -90) // Check if on harmonic + set_actual_correction_value(1,to_calibrate[current_correction_calibration], direct_level); + if ((unsigned int) current_correction_calibration < sizeof(to_calibrate)/sizeof(int) - 1) { + current_correction_calibration++; + calibration_stage -= 2; // skip back to REF measurement + } + } + else if (calibration_stage == CS_DIRECT_REF) direct_level = marker_to_value(0); else if (calibration_stage == CS_DIRECT){ config.direct_level_offset += 1.0; @@ -7953,12 +7992,13 @@ low_level: #endif offset = set_actual_power(CAL_LEVEL); // Should be -23.5dBm (V0.2) OR 25 (V0.3) calibration_busy(); - chThdSleepMilliseconds(500); + chThdSleepMilliseconds(100); } if (offset > -0.2 && offset < 0.2) k = 3; } } +done: setting.below_IF = S_AUTO_OFF; in_calibration = false; #ifdef TINYSA4 diff --git a/ui.c b/ui.c index 6d9cd22..720b729 100644 --- a/ui.c +++ b/ui.c @@ -1755,11 +1755,11 @@ UI_FUNCTION_CALLBACK(menu_curve_confirm_cb) { (void)item; if (data) { - float new_offset = local_actual_level - peakLevel + config.correction_value[current_curve][current_curve_index]; // calculate offset based on difference between measured peak level and known peak level - if (new_offset > -30 && new_offset < 30) { - config.correction_value[current_curve][current_curve_index] = new_offset; - config_save(); - } + set_actual_correction_value(current_curve,current_curve_index, local_actual_level); +// float new_offset = local_actual_level - peakLevel + config.correction_value[current_curve][current_curve_index]; // calculate offset based on difference between measured peak level and known peak level +// if (new_offset > -30 && new_offset < 30) { +// config.correction_value[current_curve][current_curve_index] = new_offset; + config_save(); } force_signal_path = false; menu_move_back(false);