diff --git a/nanovna.h b/nanovna.h index 0cfa323..a4f79b9 100644 --- a/nanovna.h +++ b/nanovna.h @@ -854,27 +854,6 @@ int plot_printf(char *str, int, const char *fmt, ...); //extern int setting_step_delay; //extern int actualStepDelay; //extern int setting_mode; -void update_rbw(void); -int get_actual_RBW(void); - -#define byte uint8_t -extern volatile int SI4432_Sel; // currently selected SI4432 -void SI4432_Write_Byte(byte ADR, byte DATA ); -byte SI4432_Read_Byte( byte ADR ); - -void SI4432_Init(void); -void SI4432_Drive(int); -int16_t SI4432_RSSI(uint32_t i, int s); -#ifdef __FAST_SWEEP__ -void SI4432_Fill(int s, int start); -#if 0 -int SI4432_is_fast_mode(void); -#endif -#endif -void SI4432_Set_Frequency ( uint32_t Freq ); -uint16_t SI4432_SET_RBW(uint16_t WISH); -void SI4432_SetReference(int freq); -extern uint16_t SI4432_force_RBW(int i); // Speed profile definition #define START_PROFILE systime_t time = chVTGetSystemTimeX(); @@ -886,6 +865,15 @@ extern uint16_t SI4432_force_RBW(int i); #define define_to_STR(x) STR1(x) // sa_core.c + +typedef uint8_t deviceRSSI_t; +typedef int16_t pureRSSI_t; + +// RSSI values conversion macrp +#define DEVICE_TO_PURE_RSSI(rssi) ((rssi)<<4) +#define float_TO_PURE_RSSI(rssi) ((rssi)*32.0) +#define PURE_TO_float(rssi) ((rssi)/32.0) + int get_waterfall(void); void toggle_tracking(void); void calibrate(void); @@ -896,6 +884,7 @@ void set_unit(int); void set_switches(int); void set_trigger_level(float); void set_trigger(int); +void update_rbw(void); //extern int setting_measurement; void self_test(int); //extern int setting_test; diff --git a/sa_cmd.c b/sa_cmd.c index a9bbe5c..3511919 100644 --- a/sa_cmd.c +++ b/sa_cmd.c @@ -16,11 +16,8 @@ */ -extern volatile int SI4432_Sel; // currently selected SI4432 -void SI4432_Write_Byte(byte ADR, byte DATA ); -byte SI4432_Read_Byte( byte ADR ); -int VFO = 0; -int points = 101; // For 's' and 'm' commands +static int VFO = 0; +static int points = 101; // For 's' and 'm' commands VNA_SHELL_FUNCTION(cmd_mode) { diff --git a/sa_core.c b/sa_core.c index c5475f8..06eccfa 100644 --- a/sa_core.c +++ b/sa_core.c @@ -1288,9 +1288,10 @@ static const int am_modulation[5] = { 4,0,1,5,7 }; // 5 step AM modulat static const int nfm_modulation[5] = { 0, 2, 1, -1, -2}; // 5 step narrow FM modulation static const int wfm_modulation[5] = { 0, 190, 118, -118, -190 }; // 5 step wide FM modulation -char age[POINTS_COUNT]; +deviceRSSI_t age[POINTS_COUNT]; static float old_a = -150; +static float correct_RSSI; systime_t start_of_sweep_timestamp; float perform(bool break_on_operation, int i, uint32_t f, int tracking) // Measure the RSSI for one frequency, used from sweep and other measurement routines. Must do all HW setup @@ -1299,8 +1300,6 @@ float perform(bool break_on_operation, int i, uint32_t f, int tracking) // M apply_settings(); // Initialize HW scandirty = true; // This is the first pass with new settings dirty = false; - if (setting.spur) // if in spur avoidance mode - setting.spur = 1; // resync spur in case of previous abort // Set for actual time pre calculated value (update after sweep) setting.actual_sweep_time_us = calc_min_sweep_time_us(); // Change actual sweep time as user input if it greater minimum @@ -1414,7 +1413,7 @@ float perform(bool break_on_operation, int i, uint32_t f, int tracking) // M } // -------------------------------- Acquisition loop for one requested frequency covering spur avoidance and vbwsteps ------------------------ - float RSSI = -150.0; + pureRSSI_t RSSI = float_TO_PURE_RSSI(-150.0); int t = 0; do { int offs = 0,sm; @@ -1432,13 +1431,18 @@ float perform(bool break_on_operation, int i, uint32_t f, int tracking) // M offs = (int)(offs * actual_rbw_x10/10.0); lf = (uint32_t)(f + offs); } - - + // Calculate the RSSI correction for later use + if (i == 0){ // only cases where the value can change on 0 point of sweep + correct_RSSI = getSI4432_RSSI_correction(); + if (setting.frequency_step != 0 ) + correct_RSSI+= get_level_offset() + + get_attenuation() + - get_signal_path_loss() + - setting.offset + + get_frequency_correction(f); + } // --------------- Set all the LO's ------------------------ -#ifdef __SPUR__ - float spur_RSSI = 0; -#endif if (/* MODE_INPUT(setting.mode) && */ i > 0 && FREQ_IS_CW()) // In input mode in zero span mode after first setting of the LO's goto skip_LO_setting; // No more LO changes required, save some time and jump over the code @@ -1556,16 +1560,7 @@ float perform(bool break_on_operation, int i, uint32_t f, int tracking) // M SI4432_Fill(MODE_SELECT(setting.mode), 0); } #endif - static float correct_RSSI; // This is re-used between calls - if (i == 0 || setting.frequency_step != 0 ){ // only cases where the value can change - correct_RSSI = get_level_offset() - + get_attenuation() - - get_signal_path_loss() - - setting.offset - + get_frequency_correction(f) - + getSI4432_RSSI_correction(); // calcuate the RSSI correction for later use - } - int16_t pureRSSI; + pureRSSI_t pureRSSI; // if ( i < 3) // shell_printf("%d %.3f %.3f %.1f\r\n", i, local_IF/1000000.0, lf/1000000.0, subRSSI); @@ -1582,20 +1577,20 @@ float perform(bool break_on_operation, int i, uint32_t f, int tracking) // M if (i == 0 && setting.frequency_step == 0 && setting.trigger != T_AUTO) { // if in zero span mode and wait for trigger to happen and NOT in trigger mode register uint16_t t_mode; - uint16_t trigger_lvl; + pureRSSI_t trigger_lvl; uint16_t data_level = T_LEVEL_UNDEF; // Calculate trigger level - trigger_lvl = (setting.trigger_level - correct_RSSI) * 32; + trigger_lvl = float_TO_PURE_RSSI(setting.trigger_level - correct_RSSI); if (setting.trigger_direction == T_UP) t_mode = T_UP_MASK; else t_mode = T_DOWN_MASK; - uint32_t count = 32; uint32_t additional_delay = 0;// reduce noise if (setting.sweep_time_us >= 100*ONE_MS_TIME) additional_delay = 20; + SI4432_Sel = MODE_SELECT(setting.mode); do{ // wait for trigger to happen - pureRSSI = SI4432_Read_Byte(SI4432_REG_RSSI)<<4; + pureRSSI = DEVICE_TO_PURE_RSSI((deviceRSSI_t)SI4432_Read_Byte(SI4432_REG_RSSI)); if (break_on_operation && operation_requested) // allow aborting a wait for trigger return 0; // abort @@ -1604,11 +1599,6 @@ float perform(bool break_on_operation, int i, uint32_t f, int tracking) // M data_level = ((data_level<<1) | (pureRSSI < trigger_lvl ? T_LEVEL_BELOW : T_LEVEL_ABOVE))&(T_LEVEL_CLEAN); if (data_level == t_mode) // wait trigger break; - // DIRTY HACK!!! FIX ME HERE - // not get data after dirty = true apply in code at first run!!!! - if (pureRSSI == 0 && --count == 0) - break; - if (additional_delay) my_microsecond_delay(additional_delay); }while(1); @@ -1624,27 +1614,26 @@ float perform(bool break_on_operation, int i, uint32_t f, int tracking) // M else pureRSSI = SI4432_RSSI(lf, MODE_SELECT(setting.mode)); // Get RSSI, either from pre-filled buffer - float subRSSI = pureRSSI / 32.0; - // add correction - subRSSI+=correct_RSSI; #ifdef __SPUR__ - if (setting.spur == 1) { // If first spur pass - spur_RSSI = subRSSI; // remember measure RSSI - setting.spur = -1; // and prepare for second pass - goto again; // Skip all other processing - } else if (setting.spur == -1) { // If second spur pass - subRSSI = ( subRSSI < spur_RSSI ? subRSSI : spur_RSSI); // Take minimum of two - setting.spur = 1; // and prepare for next call of perform. + static pureRSSI_t spur_RSSI = -1; + if (setting.spur == 1) { + if(spur_RSSI == -1) { // If first spur pass + spur_RSSI = pureRSSI; // remember measure RSSI + goto again; // Skip all other processing + } else { // If second spur pass + pureRSSI = ( pureRSSI < spur_RSSI ? pureRSSI : spur_RSSI); // Take minimum of two + spur_RSSI =-1; // and prepare for next call of perform. + } } #endif - if (RSSI < subRSSI) // Take max during subscanning - RSSI = subRSSI; + if (RSSI < pureRSSI) // Take max during subscanning + RSSI = pureRSSI; t++; // one subscan done if (break_on_operation && operation_requested) // break subscanning if requested break; // abort } while (t < vbwSteps); // till all sub steps done - return(RSSI); + return PURE_TO_float(RSSI) + correct_RSSI; // add correction } #define MAX_MAX 4 diff --git a/si4432.c b/si4432.c index 029bea8..dc71229 100644 --- a/si4432.c +++ b/si4432.c @@ -82,7 +82,7 @@ static uint8_t shiftIn(void) return value>>GPIO_SPI2_SDO; } -static inline void shiftInBuf(uint16_t sel, uint8_t addr, uint8_t *buf, uint16_t size, uint16_t delay) { +static inline void shiftInBuf(uint16_t sel, uint8_t addr, deviceRSSI_t *buf, uint16_t size, uint16_t delay) { uint8_t i = 0; do{ uint32_t value = addr; @@ -392,7 +392,7 @@ int SI4432_step_delay = 1500; //extern int setting.repeat; #ifdef __FAST_SWEEP__ -extern char age[POINTS_COUNT]; +extern deviceRSSI_t age[POINTS_COUNT]; static int buf_index = 0; static bool buf_read = false; @@ -432,7 +432,7 @@ void SI4432_Fill(int s, int start) my_microsecond_delay(t); } while(1); #else - shiftInBuf(sel, SI4432_REG_RSSI, (uint8_t *)&age[start], sweep_points - start, t); + shiftInBuf(sel, SI4432_REG_RSSI, &age[start], sweep_points - start, t); #endif // __enable_irq(); setting.measure_sweep_time_us = (chVTGetSystemTimeX() - measure)*100; @@ -448,10 +448,10 @@ float getSI4432_RSSI_correction(void){ return SI4432_RSSI_correction; }; -int16_t SI4432_RSSI(uint32_t i, int s) +pureRSSI_t SI4432_RSSI(uint32_t i, int s) { (void) i; - int32_t RSSI_RAW; + pureRSSI_t RSSI_RAW; (void) i; // SEE DATASHEET PAGE 61 #ifdef USE_SI4463 // Not used!!!!!!! @@ -464,7 +464,7 @@ int16_t SI4432_RSSI(uint32_t i, int s) if (buf_read) { if (buf_index == sweep_points-1) buf_read = false; - return (unsigned char)age[buf_index++]<<4; + return DEVICE_TO_PURE_RSSI(age[buf_index++]); } #endif SI4432_Sel = s; @@ -485,7 +485,7 @@ int16_t SI4432_RSSI(uint32_t i, int s) i = setting.repeat; RSSI_RAW = 0; do{ - RSSI_RAW += ((unsigned int)SI4432_Read_Byte(SI4432_REG_RSSI))<<4; + RSSI_RAW += DEVICE_TO_PURE_RSSI((deviceRSSI_t)SI4432_Read_Byte(SI4432_REG_RSSI)); if (--i == 0) break; my_microsecond_delay(100); }while(1); @@ -495,7 +495,8 @@ int16_t SI4432_RSSI(uint32_t i, int s) // if (MODE_INPUT(setting.mode) && RSSI_RAW == 0) // SI4432_Init(); #ifdef __SIMULATION__ - RSSI_RAW = Simulated_SI4432_RSSI(i,s)<<4; +#error "Fixme!!! add correct simulation in pureRSSI_t type" + RSSI_RAW = Simulated_SI4432_RSSI(i,s); #endif //STOP_PROFILE // Serial.println(dBm,2); diff --git a/si4432.h b/si4432.h index 81d4800..ff0367c 100644 --- a/si4432.h +++ b/si4432.h @@ -104,24 +104,37 @@ #define SI4432_FIFO 0x7F -typedef byte uint8_t; +typedef uint8_t byte; extern volatile int SI4432_Sel; // currently selected SI4432 void SI4432_Write_Byte(byte ADR, byte DATA ); byte SI4432_Read_Byte( byte ADR ); +void SI4432_Transmit(int d); +void SI4432_Receive(void); + void SI4432_Init(void); +void SI4432_Drive(int); float getSI4432_RSSI_correction(void); -int16_t SI4432_RSSI(uint32_t i, int s); +pureRSSI_t SI4432_RSSI(uint32_t i, int s); #ifdef __SIMULATION__ float Simulated_SI4432_RSSI(uint32_t i, int s); #endif void SI4432_Set_Frequency ( uint32_t Freq ); -void SI4432_Transmit(int d); -void SI4432_Receive(void); + +uint16_t SI4432_force_RBW(int i); uint16_t SI4432_SET_RBW(uint16_t WISH); +void SI4432_SetReference(int freq); + bool PE4302_Write_Byte(unsigned char DATA ); void PE4302_init(void); +#ifdef __FAST_SWEEP__ +void SI4432_Fill(int s, int start); +#if 0 +int SI4432_is_fast_mode(void); +#endif +#endif + #ifdef __ULTRA_SA__ extern int ADF4351_LE[]; extern int debug;