From 41eac5bce709e5ecfaa88d2f910c590b3e9f6035 Mon Sep 17 00:00:00 2001 From: DiSlord Date: Sun, 16 May 2021 10:58:47 +0300 Subject: [PATCH] Use timer macros --- adc.c | 2 +- adc_F303.c | 2 +- nanovna.h | 2 ++ sa_core.c | 8 ++++---- si4432.c | 4 ++-- si4468.c | 2 +- 6 files changed, 11 insertions(+), 9 deletions(-) diff --git a/adc.c b/adc.c index a55b4a8..d61068f 100644 --- a/adc.c +++ b/adc.c @@ -80,7 +80,7 @@ int16_t adc_vbat_read(void) // Vbat measure averange count = 2^VBAT_AVERAGE #define VBAT_AVERAGE 4 // Measure vbat every 5 second -#define VBAT_MEASURE_INTERVAL 50000 +#define VBAT_MEASURE_INTERVAL S2ST(5) static int16_t vbat_raw = 0; static systime_t vbat_time = -VBAT_MEASURE_INTERVAL-1; diff --git a/adc_F303.c b/adc_F303.c index 8cbfdc7..9691b80 100644 --- a/adc_F303.c +++ b/adc_F303.c @@ -96,7 +96,7 @@ int16_t adc_vbat_read(void) // Vbat measure averange count = 2^VBAT_AVERAGE #define VBAT_AVERAGE 4 // Measure vbat every 5 second -#define VBAT_MEASURE_INTERVAL 50000 +#define VBAT_MEASURE_INTERVAL S2ST(5) static int16_t vbat_raw = 0; static systime_t vbat_time = -VBAT_MEASURE_INTERVAL-1; diff --git a/nanovna.h b/nanovna.h index f04bfc0..75a0832 100644 --- a/nanovna.h +++ b/nanovna.h @@ -1444,6 +1444,8 @@ enum { T_AUTO, T_NORMAL, T_SINGLE, T_DONE, T_UP, T_DOWN, T_MODE, T_PRE, T_POST, T_MID }; +//!!! Warning can show not correct results on CH_CFG_ST_FREQUENCY not round by 1000 or > 1000000UL +#define sa_ST2US(n) ((n)*(1000000UL/(CH_CFG_ST_FREQUENCY))) extern const uint8_t SI4432_RBW_count; extern void SI4432_Listen(int s); diff --git a/sa_core.c b/sa_core.c index ef05dd2..92b174a 100644 --- a/sa_core.c +++ b/sa_core.c @@ -3427,7 +3427,7 @@ again: // Spur redu // i = 1; // Everything set so skip LO setting #define MODULATION_CYCLES_TEST 10000 if (in_selftest && modulation_count_iter++ >= 10000) { - start_of_sweep_timestamp = (chVTGetSystemTimeX() - start_of_sweep_timestamp)*MODULATION_STEPS*100/MODULATION_CYCLES_TEST; // uS per cycle + start_of_sweep_timestamp = sa_ST2US(chVTGetSystemTimeX() - start_of_sweep_timestamp)*MODULATION_STEPS/MODULATION_CYCLES_TEST; // uS per cycle return 0; } goto modulation_again; // Keep repeating sweep loop till user aborts by input @@ -3761,7 +3761,7 @@ static bool sweep(bool break_on_operation) } } } - systime_t local_sweep_time = (chVTGetSystemTimeX() - start_of_sweep_timestamp)*100 ; + systime_t local_sweep_time = sa_ST2US(chVTGetSystemTimeX() - start_of_sweep_timestamp); if (setting.actual_sweep_time_us > ONE_SECOND_TIME) local_sweep_time = setting.actual_sweep_time_us; if (show_bar && (( local_sweep_time > ONE_SECOND_TIME && (i & 0x07) == 0) || ( local_sweep_time > ONE_SECOND_TIME*10)) ) @@ -4002,7 +4002,7 @@ static bool sweep(bool break_on_operation) // ---------------------- process measured actual sweep time ----------------- // For CW mode value calculated in SI4432_Fill if (setting.measure_sweep_time_us == 0) - setting.measure_sweep_time_us = (chVTGetSystemTimeX() - start_of_sweep_timestamp) * 100; + setting.measure_sweep_time_us = sa_ST2US(chVTGetSystemTimeX() - start_of_sweep_timestamp); // Update actual time on change on status panel uint32_t delta = abs((int)(setting.actual_sweep_time_us - setting.measure_sweep_time_us)); @@ -4104,7 +4104,7 @@ static bool sweep(bool break_on_operation) } else if (actual_max_level > target_level && setting.attenuate_x2 < 60) { delta = actual_max_level - target_level; } - if ((chVTGetSystemTimeX() - sweep_elapsed > 10000 && ( delta < -5 || delta > +5)) || delta > 10 ) { + if ((chVTGetSystemTimeX() - sweep_elapsed > MS2ST(1000) && ( delta < -5 || delta > +5)) || delta > 10 ) { setting.attenuate_x2 += delta + delta; if (setting.attenuate_x2 < 0) setting.attenuate_x2= 0; diff --git a/si4432.c b/si4432.c index 6fd8c27..88b07d0 100644 --- a/si4432.c +++ b/si4432.c @@ -593,7 +593,7 @@ void SI4432_trigger_fill(int s, uint8_t trigger_lvl, int up_direction, int trigg case ST_ARMING: if (i == sweep_points-1) { waiting = ST_WAITING; - setting.measure_sweep_time_us = (chVTGetSystemTimeX() - measure)*100; + setting.measure_sweep_time_us = sa_ST2US(chVTGetSystemTimeX() - measure); } break; case ST_WAITING: @@ -674,7 +674,7 @@ void SI4432_Fill(int s, int start) shiftInBuf(sel, SI4432_REG_RSSI, &age[start], sweep_points - start, t); #endif // __enable_irq(); - setting.measure_sweep_time_us = (chVTGetSystemTimeX() - measure)*100; + setting.measure_sweep_time_us = sa_ST2US(chVTGetSystemTimeX() - measure); buf_index = start; // Is used to skip 1st entry during level triggering buf_end = sweep_points - 1; buf_read = true; diff --git a/si4468.c b/si4468.c index dd3ff32..7f589f7 100644 --- a/si4468.c +++ b/si4468.c @@ -1276,7 +1276,7 @@ void SI446x_Fill(int s, int start) } while(1); __enable_irq(); - setting.measure_sweep_time_us = (chVTGetSystemTimeX() - measure)*100; + setting.measure_sweep_time_us = sa_ST2US(chVTGetSystemTimeX() - measure); buf_index = (start<=0 ? 0 : start); // Is used to skip 1st entry during level triggering buf_read = true; }