From 62d83ee44ffcf747d22def7168df9599db21e708 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Mon, 12 Oct 2020 15:11:59 +0200 Subject: [PATCH] Added sample mean calculation --- main.c | 6 ++-- nanovna.h | 3 +- sa_core.c | 18 ++++++++---- si4432.c | 86 +++++++++++++++++++++++++++++++++++++++---------------- ui.c | 31 ++++++++++++++++++++ ui_sa.c | 4 +-- 6 files changed, 113 insertions(+), 35 deletions(-) diff --git a/main.c b/main.c index a9af0cd..fffc3d0 100644 --- a/main.c +++ b/main.c @@ -866,7 +866,8 @@ config_t config = { .menu_active_color = DEFAULT_MENU_ACTIVE_COLOR, .trace_color = { DEFAULT_TRACE_1_COLOR, DEFAULT_TRACE_2_COLOR, DEFAULT_TRACE_3_COLOR}, // .touch_cal = { 693, 605, 124, 171 }, // 2.4 inch LCD panel - .touch_cal = { 347, 495, 160, 205 }, // 2.8 inch LCD panel +// .touch_cal = { 347, 495, 160, 205 }, // 2.8 inch LCD panel + .touch_cal = { 261, 605, 115, 146 }, // 4 inch panel ._mode = _MODE_USB, ._serial_speed = USART_SPEED_SETTING(SERIAL_DEFAULT_BITRATE), #ifdef __VNA__ @@ -874,6 +875,7 @@ config_t config = { #endif .vbat_offset = 500, .frequency_IF2 = 2048600000, + .setting_frequency_10mhz = 1000015.0, .low_level_offset = 100, // Uncalibrated .high_level_offset = 100, // Uncalibrated .correction_frequency = { 10000, 100000, 200000, 500000, 50000000, 140000000, 200000000, 300000000, 330000000, 350000000 }, @@ -2278,7 +2280,7 @@ VNA_SHELL_FUNCTION(cmd_color) #if CH_CFG_USE_REGISTRY == FALSE #error "Threads Requite enabled CH_CFG_USE_REGISTRY in chconf.h" #endif -static const char *states[] = {CH_STATE_NAMES}; +const char *states[] = {CH_STATE_NAMES}; VNA_SHELL_FUNCTION(cmd_threads) { thread_t *tp; diff --git a/nanovna.h b/nanovna.h index 5da66ff..d517857 100644 --- a/nanovna.h +++ b/nanovna.h @@ -237,7 +237,7 @@ void set_decay(int); void set_noise(int); void toggle_tracking_output(void); extern int32_t frequencyExtra; -void set_10mhz(uint32_t); +void set_10mhz(float); void set_modulation(int); void set_modulation_frequency(int); int search_maximum(int m, int center, int span); @@ -473,6 +473,7 @@ typedef struct config { uint16_t gridlines; uint16_t hambands; uint32_t frequency_IF2; + float setting_frequency_10mhz; int8_t cor_am; int8_t cor_wfm; int8_t cor_nfm; diff --git a/sa_core.c b/sa_core.c index e0ae891..0803c52 100644 --- a/sa_core.c +++ b/sa_core.c @@ -87,8 +87,8 @@ void update_min_max_freq(void) minFreq = 00000000; maxFreq = 2000000000; #else - minFreq = 24*setting_frequency_10mhz; - maxFreq = 96*setting_frequency_10mhz; + minFreq = 24*config.setting_frequency_10mhz; + maxFreq = 96*config.setting_frequency_10mhz; #endif break; case M_GENHIGH: @@ -182,8 +182,8 @@ void reset_settings(int m) minFreq = 00000000; maxFreq = 2000000000; #else - minFreq = 13*setting_frequency_10mhz; - maxFreq = 120*setting_frequency_10mhz; + minFreq = 13*config.setting_frequency_10mhz; + maxFreq = 120*config.setting_frequency_10mhz; #endif set_sweep_frequency(ST_START, minFreq); set_sweep_frequency(ST_STOP, maxFreq); @@ -1448,9 +1448,11 @@ search_maximum(int m, int center, int span) //static int spur_old_stepdelay = 0; static const unsigned int spur_IF = 433600000; // The IF frequency for which the spur table is value -static const unsigned int spur_alternate_IF = 433900000; // if the frequency is found in the spur table use this IF frequency +static const unsigned int spur_alternate_IF = 434100000; // if the frequency is found in the spur table use this IF frequency static const int spur_table[] = // Frequencies to avoid { + 117716000, + 746083000, #if 0 // 580000, // 433.8 MHz table // 880000, //? @@ -1871,6 +1873,9 @@ modulation_again: #ifdef __ADF4351__ // START_PROFILE; if (setting.mode == M_LOW) { + if (i > 0 && setting.frequency_step < 1000) { + set_freq (SI4463_RX, setting.frequency_IF - setting.frequency_step*i); // sweep RX, local_IF = 0 in high mode + } else { uint32_t extra_IF = local_IF; if (config.frequency_IF2 != 0) { extra_IF = config.frequency_IF2; @@ -1883,7 +1888,8 @@ modulation_again: set_freq (ADF4351_LO, extra_IF-lf); // set LO SI4432 to below IF frequency } else set_freq (ADF4351_LO, extra_IF+lf); // otherwise to above IF - } else if (setting.mode == M_HIGH) { + } + } else if (setting.mode == M_HIGH) { set_freq (SI4463_RX, local_IF+lf); // sweep RX, local_IF = 0 in high mode } // STOP_PROFILE; diff --git a/si4432.c b/si4432.c index 5cbbb0b..309de6b 100644 --- a/si4432.c +++ b/si4432.c @@ -209,11 +209,12 @@ static void shiftOutBuf(uint8_t *buf, uint16_t size) { } #endif -int setting_frequency_10mhz = 10000000; +//float setting_frequency_10mhz = 1000000.0; -void set_10mhz(uint32_t f) +void set_10mhz(float f) { - setting_frequency_10mhz = f; + config.setting_frequency_10mhz = f; + config_save(); } int SI4432_step_delay = 1500; @@ -904,6 +905,7 @@ int debug = 0; int ADF4351_LE[2] = { 10, 11}; int ADF4351_Mux = 7; +int ADF4351_frequency_changed = false; //#define DEBUG(X) // Serial.print( X ) //#define DEBUGLN(X) Serial.println( X ) @@ -1012,6 +1014,7 @@ void ADF4351_set_frequency(int channel, uint32_t freq, int drive) // freq / 10H ADF4351_prep_frequency(channel,freq + offs, drive); //START_PROFILE; ADF4351_Set(channel); + ADF4351_frequency_changed = true; //STOP_PROFILE; } @@ -1082,7 +1085,7 @@ void ADF4351_prep_frequency(int channel, unsigned long freq, int drive) // freq (void)drive; // START_PROFILE; // if (channel == 0) - RFout=freq/1000000.0; // To MHz + RFout=freq/config.setting_frequency_10mhz; // To MHz // else // RFout=freq/1000002.764; // To MHz @@ -1234,7 +1237,8 @@ void ADF4351_prep_frequency(int channel, unsigned long freq, int drive) // freq #define Npresc 0 // No High performance mode -#define MIN_DELAY 1 +int SI4463_frequency_changed = false; +#define MIN_DELAY 2 #include @@ -1497,29 +1501,63 @@ int16_t Si446x_RSSI(void) // if (s != SI446X_STATE_RX) // SI4463_start_rx(0); - uint8_t data[3] = { + volatile uint8_t data[3] = { SI446X_CMD_GET_MODEM_STATUS, 0xFF }; // volatile si446x_state_t s = getState(); //START_PROFILE; - if (SI4432_step_delay) + if (SI4432_step_delay && ADF4351_frequency_changed) { my_microsecond_delay(SI4432_step_delay); -again: - data[0] = SI446X_CMD_GET_MODEM_STATUS; - data[1] = 0xFF; - -SI4463_do_api(data, 2, data, 3); - if (data[2] == 255) - goto again; - if (data[2] == 0) - goto again; - volatile int16_t rssi = data[2] - 120 * 2; - if (rssi < -238) - while(1) - rssi = rssi; -//STOP_PROFILE; - return DEVICE_TO_PURE_RSSI(rssi); + ADF4351_frequency_changed = false; + } + + int i = 3; //setting.repeat; + int RSSI_RAW[3]; + do{ + + + + again: + data[0] = SI446X_CMD_GET_MODEM_STATUS; + data[1] = 0xFF; + + SI4463_do_api(data, 2, data, 3); + if (data[2] == 255) + goto again; + if (data[2] == 0){ + data[2] += 1; + goto again; + } + if (data[2] > 150) + data[2] = data[2]+1; + RSSI_RAW[--i] = data[2] - 120 * 2; + // if (rssi < -238) + // while(1) + // rssi = rssi; + //STOP_PROFILE; + + if (i == 0) break; + my_microsecond_delay(100); + }while(1); + int t; + if (RSSI_RAW[0] > RSSI_RAW[1]) { + t = RSSI_RAW[1]; + RSSI_RAW[1] = RSSI_RAW[0]; + RSSI_RAW[0] = t; + } + if (RSSI_RAW[1] > RSSI_RAW[2]) { + t = RSSI_RAW[2]; + RSSI_RAW[2] = RSSI_RAW[1]; + RSSI_RAW[1] = t; + } + if (RSSI_RAW[0] > RSSI_RAW[1]) { + t = RSSI_RAW[1]; + RSSI_RAW[1] = RSSI_RAW[0]; + RSSI_RAW[0] = t; + } + + return DEVICE_TO_PURE_RSSI(RSSI_RAW[1]); } @@ -1776,7 +1814,7 @@ void SI4463_set_freq(uint32_t freq, uint32_t step_size) { int Odiv; int D; - uint32_t offs = ((freq / 1000)* 195) / 1000; + uint32_t offs = ((freq / 1000)* 0) / 1000; float RFout=(freq+offs)/1000000.0; // To MHz if (RFout >= 820) { // till 1140MHz @@ -1847,7 +1885,7 @@ void SI4463_set_freq(uint32_t freq, uint32_t step_size) }; SI4463_do_api(data2, sizeof(data2), NULL, 0); SI4463_start_rx(0); - my_microsecond_delay(1000); + my_microsecond_delay(3000); } diff --git a/ui.c b/ui.c index fae0040..95c57ae 100644 --- a/ui.c +++ b/ui.c @@ -406,6 +406,7 @@ touch_position(int *x, int *y) *y = (last_touch_y - config.touch_cal[1]) * 16 / config.touch_cal[3]; } + void show_version(void) { @@ -421,6 +422,36 @@ show_version(void) do {shift>>=1; y+=5;} while (shift&1); ili9341_drawstring(info_about[i++], x, y+=5); } + + static char buf[96]; +extern const char *states[]; + #define ENABLE_THREADS_COMMAND + +#ifdef ENABLE_THREADS_COMMAND + thread_t *tp; + tp = chRegFirstThread(); + do { + uint32_t max_stack_use = 0U; +#if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || (CH_CFG_USE_DYNAMIC == TRUE) + uint32_t stklimit = (uint32_t)tp->wabase; +#if CH_DBG_FILL_THREADS == TRUE + uint8_t *p = (uint8_t *)tp->wabase; while(p[max_stack_use]==CH_DBG_STACK_FILL_VALUE) max_stack_use++; +#endif +#else + uint32_t stklimit = 0U; +#endif + plot_printf(buf, sizeof(buf), "%08x|%08x|%08x|%08x|%4u|%4u|%9s|%12s", + stklimit, (uint32_t)tp->ctx.sp, max_stack_use, (uint32_t)tp, + (uint32_t)tp->refs - 1, (uint32_t)tp->prio, states[tp->state], + tp->name == NULL ? "" : tp->name); + ili9341_drawstring(buf, x, y+=FONT_STR_HEIGHT); + tp = chRegNextThread(tp); + } while (tp != NULL); +#endif + + + + while (true) { if (touch_check() == EVT_TOUCH_PRESSED) break; diff --git a/ui_sa.c b/ui_sa.c index 705e4a5..09b6417 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -1638,7 +1638,7 @@ static const menuitem_t menu_sweep_speed[] = static const menuitem_t menu_settings3[] = { - { MT_KEYPAD, KM_10MHZ, "CORRECT\nFREQUENCY", "Enter actual l0MHz frequency"}, + { MT_KEYPAD, KM_10MHZ, "CORRECT\nFREQUENCY", "Enter actual lMHz frequency"}, { MT_KEYPAD, KM_GRIDLINES, "MINIMUM\nGRIDLINES", "Enter minimum horizontal grid divisions"}, // { MT_KEYPAD, KM_COR_AM, "COR\nAM", "Enter AM modulation correction"}, { MT_KEYPAD, KM_COR_WFM, "COR\nWFM", "Enter WFM modulation correction"}, @@ -2010,7 +2010,7 @@ static void fetch_numeric_target(void) plot_printf(uistat.text, sizeof uistat.text, "%3d", ((int32_t)uistat.value)); break; case KM_10MHZ: - uistat.value = setting_frequency_10mhz; + uistat.value = config.setting_frequency_10mhz; plot_printf(uistat.text, sizeof uistat.text, "%3.6fMHz", uistat.value / 1000000.0); break; case KM_OFFSET: