diff --git a/Makefile b/Makefile index 341524d..e16ea2e 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,8 @@ # Compiler options here. ifeq ($(USE_OPT),) - USE_OPT = -O2 -fno-inline-small-functions -ggdb -fomit-frame-pointer -falign-functions=16 --specs=nano.specs -fstack-usage -# USE_OPT = -fno-inline-small-functions -ggdb -fomit-frame-pointer -falign-functions=16 --specs=nano.specs -fstack-usage +# USE_OPT = -O2 -fno-inline-small-functions -ggdb -fomit-frame-pointer -falign-functions=16 --specs=nano.specs -fstack-usage + USE_OPT = -fno-inline-small-functions -ggdb -fomit-frame-pointer -falign-functions=16 --specs=nano.specs -fstack-usage endif # C specific options here (added to USE_OPT). diff --git a/nanovna.h b/nanovna.h index d5ff1e4..86ef5ce 100644 --- a/nanovna.h +++ b/nanovna.h @@ -637,7 +637,7 @@ int get_waterfall(void); void toggle_tracking(void); void calibrate(void); void reset_calibration(void); -void SetRefpos(int); +void SetReflevel(int); void SetScale(int); void SetRBW(int); void SetRX(int); diff --git a/sa_core.c b/sa_core.c index 2e5cc94..70852f4 100644 --- a/sa_core.c +++ b/sa_core.c @@ -1,21 +1,25 @@ -// --------------------------------------------------- #include "SI4432.h" // comment out for simulation -int setting_mode = M_LOW; +int setting_mode = -1; // To force initialzation + int dirty = true; int scandirty = true; int setting_attenuate = 0; +int setting_auto_attenuation; int setting_step_atten; int setting_rbw = 0; int setting_average = 0; int setting_show_stored = 0; int setting_subtract_stored = 0; -int setting_drive=13; // 0-7 , 7=+20dBm, 3dB steps +int setting_drive; // 0-7 , 7=+20dBm, 3dB steps int setting_agc = true; int setting_lna = false; +int setting_auto_reflevel; +int setting_reflevel; +int setting_scale; int setting_tracking = false; int setting_modulation = MO_NONE; int setting_step_delay = 0; @@ -41,12 +45,15 @@ int in_selftest = false; void reset_settings(int m) { setting_mode = m; + SetScale(10); + SetReflevel(-10); setting_attenuate = 0; setting_rbw = 0; setting_average = 0; setting_show_stored = 0; + setting_auto_attenuation = true; setting_subtract_stored = 0; - setting_drive=13; + setting_drive=12; setting_step_atten = 0; // Only used in low output mode setting_agc = true; setting_lna = false; @@ -54,8 +61,9 @@ void reset_settings(int m) setting_modulation = MO_NONE; setting_step_delay = 0; setting_vbw = 0; + setting_auto_reflevel = true; // Must be after SetReflevel setting_decay=20; - setting_noise=20; + setting_noise=5; trace[TRACE_STORED].enabled = false; trace[TRACE_TEMP].enabled = false; @@ -67,7 +75,7 @@ void reset_settings(int m) maxFreq = 520000000; set_sweep_frequency(ST_START, (int32_t) 0); set_sweep_frequency(ST_STOP, (int32_t) 350000000); - SetRefpos(-10); + setting_attenuate = 30; break; case M_GENLOW: setting_drive=8; @@ -81,7 +89,6 @@ void reset_settings(int m) maxFreq = 960000000; set_sweep_frequency(ST_START, (int32_t) minFreq); set_sweep_frequency(ST_STOP, (int32_t) maxFreq); - SetRefpos(-30); break; case M_GENHIGH: setting_drive=8; @@ -91,7 +98,6 @@ void reset_settings(int m) set_sweep_frequency(ST_SPAN, 0); break; } - SetScale(10); dirty = true; } @@ -116,7 +122,7 @@ void set_decay(int d) void set_noise(int d) { - if (d < 5 || d > 200) + if (d < 2 || d > 50) return; setting_noise = d; dirty = true; @@ -152,7 +158,7 @@ int GetMode(void) #define POWER_STEP 0 // Should be 5 dB but appearently it is lower -#define POWER_OFFSET 12 +#define POWER_OFFSET 20 #define SWITCH_ATTENUATION 29 int GetAttenuation(void) @@ -166,11 +172,21 @@ int GetAttenuation(void) return(setting_attenuate); } +void set_auto_attenuation(void) +{ + setting_auto_attenuation = true; + setting_attenuate = 30; +} + +void set_auto_reflevel(void) +{ + setting_auto_reflevel = true; +} void SetAttenuation(int a) { if (setting_mode == M_GENLOW) { - setting_drive = 0; + setting_drive = 8; // Start at lowest drive level; a = a + POWER_OFFSET; if (a > 0) { setting_drive++; @@ -361,20 +377,29 @@ int GetAGC(void) return(setting_agc); } -void SetRefpos(int level) +void SetReflevel(int level) { + setting_reflevel = (level / setting_scale) * setting_scale; set_trace_refpos(0, NGRIDY - level / get_trace_scale(0)); set_trace_refpos(1, NGRIDY - level / get_trace_scale(0)); set_trace_refpos(2, NGRIDY - level / get_trace_scale(0)); dirty = true; } +//int GetRefpos(void) { +// return (NGRIDY - get_trace_refpos(2)) * get_trace_scale(2); +//} + void SetScale(int s) { + setting_scale = s; set_trace_scale(0, s); set_trace_scale(1, s); set_trace_scale(2, s); } +//int GetScale(void) { +// return get_trace_refpos(2); +//} void SetMode(int m) { if (setting_mode == m) @@ -385,9 +410,9 @@ void SetMode(int m) void apply_settings(void) { if (setting_step_delay == 0){ - if (actual_rbw >142.0) actualStepDelay = 350; - else if (actual_rbw > 75.0) actualStepDelay = 450; - else if (actual_rbw > 56.0) actualStepDelay = 600; + if (actual_rbw >142.0) actualStepDelay = 450; + else if (actual_rbw > 75.0) actualStepDelay = 550; + else if (actual_rbw > 56.0) actualStepDelay = 650; else if (actual_rbw > 37.0) actualStepDelay = 800; else if (actual_rbw > 18.0) actualStepDelay = 1100; else if (actual_rbw > 9.0) actualStepDelay = 2000; @@ -494,11 +519,11 @@ case M_GENLOW: // Mixed output from 0 } else { SetSwitchTransmit(); } - SI4432_Transmit(setting_drive); // Not to overdrive the mixer + SI4432_Transmit(setting_drive); SI4432_Sel = 1; SetSwitchReceive(); - SI4432_Transmit(13); // Fix LO drive a 10dBm + SI4432_Transmit(12); // Fix LO drive a 10dBm break; case M_GENHIGH: // Direct output from 1 @@ -516,6 +541,10 @@ case M_GENHIGH: // Direct output from 1 break; } + SI4432_Sel = 1; + SI4432_Write_Byte(0x73, 0); // Back to nominal offset + SI4432_Write_Byte(0x74, 0); + } void update_rbw(void) @@ -864,9 +893,10 @@ static bool sweep(bool break_on_operation) temppeakLevel = actual_t[i]; } } +#endif if (temp_min_level > actual_t[i]) temp_min_level = actual_t[i]; -#endif + } // if (setting_spur == 1) { // setting_spur = -1; @@ -878,6 +908,32 @@ static bool sweep(bool break_on_operation) scandirty = false; draw_cal_status(); } + if (setting_mode == M_LOW && setting_auto_attenuation && max_index[0] > 0) { + if (actual_t[max_index[0]] - setting_attenuate < -30 && setting_attenuate >= 10) { + setting_attenuate -= setting_scale; + redraw_request |= REDRAW_CAL_STATUS; + dirty = true; // Must be above if(scandirty!!!!!) + } else if (actual_t[max_index[0]] - setting_attenuate > -20 && setting_attenuate <= 20) { + setting_attenuate += setting_scale; + redraw_request |= REDRAW_CAL_STATUS; + dirty = true; // Must be above if(scandirty!!!!!) + } + } + if (MODE_INPUT(setting_mode) && setting_auto_reflevel && max_index[0] > 0) { + if (actual_t[max_index[0]] > setting_reflevel - setting_scale/2) { + SetReflevel(setting_reflevel + setting_scale); + redraw_request |= REDRAW_CAL_STATUS; + dirty = true; // Must be above if(scandirty!!!!!) + } else if (temp_min_level < setting_reflevel - 9 * setting_scale && actual_t[max_index[0]] < setting_reflevel - setting_scale * 3 / 2) { + SetReflevel(setting_reflevel - setting_scale); + redraw_request |= REDRAW_CAL_STATUS; + dirty = true; // Must be above if(scandirty!!!!!) + } else if (temp_min_level > setting_reflevel - 9 * setting_scale + setting_scale * 3 / 2) { + SetReflevel(setting_reflevel + setting_scale); + redraw_request |= REDRAW_CAL_STATUS; + dirty = true; // Must be above if(scandirty!!!!!) + } + } #if 1 if (MODE_INPUT(setting_mode)) { int i = 0; @@ -926,15 +982,15 @@ static bool sweep(bool break_on_operation) #endif min_level = temp_min_level; #if 0 // Auto ref level setting - int scale = get_trace_scale(2); - int rp = (NGRIDY - get_trace_refpos(2)) * scale; + int scale = setting_scale; + int rp = GetRepos(); if (scale > 0 && peakLevel > rp && peakLevel - min_level < 8 * scale ) { - SetRefpos((((int)(peakLevel/scale)) + 1) * scale); + SetReflevel((((int)(peakLevel/scale)) + 1) * scale); } if (scale > 0 && min_level < rp - 9*scale && peakLevel - min_level < 8 * scale ) { int new_rp = (((int)((min_level + 9*scale)/scale)) - 1) * scale; if (new_rp < rp) - SetRefpos(new_rp); + SetReflevel(new_rp); } #endif @@ -969,7 +1025,7 @@ void draw_cal_status(void) ili9341_set_background(DEFAULT_BG_COLOR); - int yMax = (NGRIDY - get_trace_refpos(0)) * get_trace_scale(0); + int yMax = setting_reflevel; plot_printf(buf, BLEN, "%ddB", yMax); buf[5]=0; if (level_is_calibrated()) @@ -980,7 +1036,7 @@ void draw_cal_status(void) ili9341_drawstring(buf, x, y); y += YSTEP*2; - plot_printf(buf, BLEN, "%ddB/",(int)get_trace_scale(0)); + plot_printf(buf, BLEN, "%ddB/",(int)setting_scale); ili9341_drawstring(buf, x, y); if (setting_attenuate) { @@ -1076,7 +1132,7 @@ void draw_cal_status(void) y = HEIGHT-7 + OFFSETY; - plot_printf(buf, BLEN, "%ddB", (int)(yMax - get_trace_scale(0) * NGRIDY)); + plot_printf(buf, BLEN, "%ddB", (int)(yMax - setting_scale * NGRIDY)); buf[5]=0; if (level_is_calibrated()) color = DEFAULT_FG_COLOR; @@ -1382,7 +1438,7 @@ common_silent: goto common; } trace[TRACE_STORED].enabled = true; - SetRefpos(test_case[i].pass+10); + SetReflevel(test_case[i].pass+10); set_sweep_frequency(ST_CENTER, (int32_t)(test_case[i].center * 1000000)); set_sweep_frequency(ST_SPAN, (int32_t)(test_case[i].span * 1000000)); draw_cal_status(); diff --git a/si4432.c b/si4432.c index 021a3fc..bc3c0f9 100644 --- a/si4432.c +++ b/si4432.c @@ -282,12 +282,13 @@ void SI4432_Sub_Init(void) { SI4432_Reset(); - +#if 1 // Not sure if these add any value //set VCO and PLL Only for SI4432 V2 SI4432_Write_Byte(0x72, 0x1F); //write 0x1F to the Frequency Deviation register SI4432_Write_Byte(0x5A, 0x7F); //write 0x7F to the VCO Current Trimming register SI4432_Write_Byte(0x58, 0x80); //write 0xD7 to the ChargepumpCurrentTrimmingOverride register SI4432_Write_Byte(0x59, 0x40); //write 0x40 to the Divider Current Trimming register +#endif //set the AGC SI4432_Write_Byte(0x6A, 0x0B); //write 0x0B to the AGC Override 2 register diff --git a/ui_sa.c b/ui_sa.c index 37940f9..7734392 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -13,6 +13,9 @@ void set_refer_output(int); int get_refer_output(void); void SetAttenuation(int); int GetAttenuation(void); +void set_auto_attenuation(void); +void set_auto_reflevel(void); + void SetPowerLevel(int); void SetGenerate(int); void SetRBW(int); @@ -31,7 +34,7 @@ void SetSubtractStorage(void); void toggle_waterfall(void); void SetMode(int); int GetMode(void); -void SetRefpos(int); +void SetReflevel(int); void SetScale(int); void AllDirty(void); void MenuDirty(void); @@ -48,6 +51,10 @@ extern int setting_lna; extern int setting_agc; extern int setting_decay; extern int setting_noise; +extern int setting_auto_reflevel; +extern int setting_auto_attenuation; +extern int setting_reflevel; +extern int setting_scale; void SetModulation(int); extern int setting_modulation; void set_measurement(int); @@ -387,6 +394,24 @@ static void menu_measure_cb(int item, uint8_t data) // draw_cal_status(); } +static void menu_atten_cb(int item, uint8_t data) +{ + (void)item; + (void)data; + set_auto_attenuation(); + menu_move_back(); + ui_mode_normal(); +} + +static void menu_reflevel_cb(int item, uint8_t data) +{ + (void)item; + (void)data; + set_auto_reflevel(); + menu_move_back(); + ui_mode_normal(); +} + static void menu_storage_cb(int item, uint8_t data) { (void)item; @@ -528,10 +553,10 @@ const char *menu_drive_text[]={"-30dBm","-27dBm","-24dBm","-21dBm","-18dBm","-15 // ===[MENU DEFINITION]========================================================= static const menuitem_t menu_drive[] = { - { MT_CALLBACK, 7, " 20dBm", menu_drive_cb}, - { MT_CALLBACK, 6, " 16dBm", menu_drive_cb}, - { MT_CALLBACK, 5, " 12dBm", menu_drive_cb}, - { MT_CALLBACK, 4, " 8dBm", menu_drive_cb}, + { MT_CALLBACK, 15, " 20dBm", menu_drive_cb}, + { MT_CALLBACK, 14, " 16dBm", menu_drive_cb}, + { MT_CALLBACK, 13, " 12dBm", menu_drive_cb}, + { MT_CALLBACK, 12, " 8dBm", menu_drive_cb}, { MT_CANCEL, 255, S_LARROW" BACK", NULL }, { MT_NONE, 0, NULL, NULL } // sentinel }; @@ -609,16 +634,6 @@ static const menuitem_t menu_average[] = { { MT_CANCEL, 0, S_LARROW" BACK", NULL }, { MT_NONE, 0, NULL, NULL } // sentinel }; -#if 0 -static const menuitem_t menu_storage[] = { - { MT_CALLBACK, 0, "STORE", menu_storage_cb}, - { MT_CALLBACK, 1, "CLEAR", menu_storage_cb}, - { MT_CALLBACK, 2, "SUBTRACT", menu_storage_cb}, - { MT_CALLBACK, 3, "WATERFALL",menu_storage_cb}, - { MT_CANCEL, 0, S_LARROW" BACK", NULL }, - { MT_NONE, 0, NULL, NULL } // sentinel -}; -#endif static const menuitem_t menu_rbw[] = { { MT_CALLBACK, 0, " AUTO", menu_rbw_cb}, @@ -650,6 +665,7 @@ static const menuitem_t menu_reffer2[] = { { MT_FORM | MT_NONE, 0, NULL, NULL } // sentinel }; + static const menuitem_t menu_reffer[] = { { MT_FORM | MT_CALLBACK, 0, "OFF" , menu_reffer_cb}, { MT_FORM | MT_CALLBACK, 1, "30MHz", menu_reffer_cb}, @@ -661,58 +677,49 @@ static const menuitem_t menu_reffer[] = { { MT_FORM | MT_NONE, 0, NULL, NULL } // sentinel }; -static const menuitem_t menu_acquire[] = { - { MT_CALLBACK, 0, "AUTO", menu_autosettings_cb}, - { MT_KEYPAD, KM_ATTENUATION, "ATTEN", NULL}, - { MT_SUBMENU,0, "RBW", menu_rbw}, - { MT_SUBMENU,0, "CALC", menu_average}, +static const menuitem_t menu_atten[] = { + { MT_CALLBACK,0, "AUTO", menu_atten_cb}, + { MT_KEYPAD, KM_ATTENUATION, "MANUAL", NULL}, { MT_CANCEL, 0, S_LARROW" BACK", NULL }, { MT_FORM | MT_NONE, 0, NULL, NULL } // sentinel }; +static const menuitem_t menu_acquire[] = { + { MT_CALLBACK, 0, "RESET", menu_autosettings_cb}, + { MT_SUBMENU, 0, "ATTEN", menu_atten}, + { MT_SUBMENU,0, "RBW", menu_rbw}, + { MT_SUBMENU,0, "AVER", menu_average}, + { MT_CANCEL, 0, S_LARROW" BACK", NULL }, + { MT_FORM | MT_NONE, 0, NULL, NULL } // sentinel +}; + static const menuitem_t menu_acquirehigh[] = { - { MT_CALLBACK, 0, "AUTO", menu_autosettings_cb}, + { MT_CALLBACK, 0, "RESET", menu_autosettings_cb}, { MT_SUBMENU,0, "RBW", menu_rbw}, - { MT_SUBMENU,0, "CALC", menu_average}, + { MT_SUBMENU,0, "AVER", menu_average}, { MT_CANCEL, 0, S_LARROW" BACK", NULL }, { MT_NONE, 0, NULL, NULL } // sentinel }; - -static const menuitem_t menu_display[] = { - { MT_KEYPAD, KM_REFPOS, "\2REF\0LEVEL", NULL}, - { MT_SUBMENU,0, "\2SCALE/\0DIV", menu_dBper}, - { MT_CALLBACK, 0, "STORE", menu_storage_cb}, - { MT_CALLBACK, 1, "CLEAR", menu_storage_cb}, - { MT_CALLBACK, 2, "SUBTRACT", menu_storage_cb}, - { MT_CALLBACK, 3, "WATERFALL",menu_storage_cb}, - { MT_CANCEL, 0, S_LARROW" BACK", NULL }, +static const menuitem_t menu_reflevel[] = { + { MT_CALLBACK,0, "AUTO", menu_reflevel_cb}, + { MT_KEYPAD, KM_REFPOS, "MANUAL", NULL}, + { MT_CANCEL, 0, S_LARROW" BACK", NULL }, { MT_NONE, 0, NULL, NULL } // sentinel }; -#if 0 -static const menuitem_t menu_scale[] = { - { MT_KEYPAD, KM_REFPOS, "\2REF\0LEVEL", NULL}, - { MT_SUBMENU,0, "\2SCALE/\0DIV", menu_dBper}, - { MT_KEYPAD, KM_ATTENUATION, "ATTEN", NULL}, - { MT_SUBMENU,0, "AVERAGE", menu_average}, - { MT_KEYPAD, 0, "\2SPUR\0REDUCT.", menu_spur_cb}, - { MT_SUBMENU,0, "RBW", menu_rbw}, - { MT_CANCEL, 0, S_LARROW" BACK", NULL }, - { MT_NONE, 0, NULL, NULL } // sentinel -}; -static const menuitem_t menu_scalehigh[] = { - { MT_KEYPAD, KM_REFPOS, "\2REF\0LEVEL", NULL}, - { MT_SUBMENU,0, "\2SCALE/\0DIV", menu_dBper}, - { MT_SUBMENU,0, "AVERAGE", menu_average}, - { MT_SUBMENU,0, "RBW", menu_rbw}, - { MT_CANCEL, 0, S_LARROW" BACK", NULL }, +static const menuitem_t menu_display[] = { + { MT_SUBMENU, 0, "\2REF\0LEVEL", menu_reflevel}, + { MT_SUBMENU, 0, "\2SCALE/\0DIV",menu_dBper}, + { MT_CALLBACK,0, "STORE", menu_storage_cb}, + { MT_CALLBACK,1, "CLEAR", menu_storage_cb}, + { MT_CALLBACK,2, "SUBTRACT", menu_storage_cb}, + { MT_CALLBACK,3, "WATERFALL", menu_storage_cb}, + { MT_CANCEL, 0, S_LARROW" BACK", NULL }, { MT_NONE, 0, NULL, NULL } // sentinel }; -#endif - static const menuitem_t menu_stimulus[8] = { { MT_KEYPAD, KM_START, "START", NULL}, { MT_KEYPAD, KM_STOP, "STOP", NULL}, @@ -858,7 +865,7 @@ static const menuitem_t menu_mode[] = { #if 1 const menuitem_t menu_top[] = { { MT_SUBMENU, 0, "ACQUIRE", menu_acquire}, - { MT_SUBMENU, 0, "SCAN", menu_stimulus}, + { MT_SUBMENU, 0, "FREQ", menu_stimulus}, { MT_SUBMENU, 0, "DISPLAY", menu_display}, { MT_SUBMENU, 0, "MARKER", menu_marker}, { MT_SUBMENU, 0, "MEASURE", menu_measure}, @@ -871,7 +878,7 @@ const menuitem_t menu_top[] = { const menuitem_t menu_tophigh[] = { { MT_SUBMENU, 0, "ACQUIRE", menu_acquirehigh}, - { MT_SUBMENU, 0, "SCAN", menu_stimulus}, + { MT_SUBMENU, 0, "FREQ", menu_stimulus}, { MT_SUBMENU, 0, "DISPLAY", menu_display}, { MT_SUBMENU, 0, "MARKER", menu_marker}, { MT_SUBMENU, 0, "MEASURE", menu_measure}, @@ -1038,12 +1045,12 @@ static void fetch_numeric_target(void) plot_printf(uistat.text, sizeof uistat.text, "%3.3fMHz", uistat.value / 1000000.0); break; case KM_SCALE: - uistat.value = get_trace_scale(uistat.current_trace) * 1000; - plot_printf(uistat.text, sizeof uistat.text, "%ddB/", uistat.value / 1000); + uistat.value = setting_scale; + plot_printf(uistat.text, sizeof uistat.text, "%ddB/", uistat.value); break; case KM_REFPOS: - uistat.value = get_trace_refpos(uistat.current_trace) * 1000; - plot_printf(uistat.text, sizeof uistat.text, "%ddB", uistat.value / 1000); + uistat.value = setting_reflevel; + plot_printf(uistat.text, sizeof uistat.text, "%ddB", uistat.value); break; case KM_ATTENUATION: uistat.value = GetAttenuation(); @@ -1114,9 +1121,11 @@ set_numeric_value(void) set_trace_scale(2, uistat.value / 1000.0); break; case KM_REFPOS: - SetRefpos(uistat.value); + setting_auto_reflevel = false; + SetReflevel(uistat.value); break; case KM_ATTENUATION: + setting_auto_attenuation = false; SetAttenuation(uistat.value); break; case KM_ACTUALPOWER: