From 7d6476651340ec77d45082f5b4e1494dd3e0bc53 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Sun, 5 Apr 2020 19:37:35 +0200 Subject: [PATCH] Optimize stack usage by shifting test and calibrate to lowest level --- main.c | 6 +++++- nanovna.h | 3 +++ plot.c | 9 +++++++++ sa_core.c | 51 +++++++++++++++++++++++++++++---------------------- ui.c | 17 +++++++++++++++++ ui_sa.c | 4 ++-- 6 files changed, 65 insertions(+), 25 deletions(-) diff --git a/main.c b/main.c index 51a7e6e..a0fd3ae 100644 --- a/main.c +++ b/main.c @@ -117,7 +117,7 @@ const char *info_about[]={ 0 // sentinel }; -static THD_WORKING_AREA(waThread1, 730); +static THD_WORKING_AREA(waThread1, 900); static THD_FUNCTION(Thread1, arg) { (void)arg; @@ -128,6 +128,10 @@ static THD_FUNCTION(Thread1, arg) if (sweep_mode&(SWEEP_ENABLE|SWEEP_ONCE)) { completed = sweep(true); sweep_mode&=~SWEEP_ONCE; + } else if (sweep_mode & SWEEP_SELFTEST) { + self_test(); // call from lowest level to save stack space + } else if (sweep_mode & SWEEP_CALIBRATE) { + calibrate(); // call from lowest level to save stack space } else { __WFI(); } diff --git a/nanovna.h b/nanovna.h index c8f8da2..469ad6b 100644 --- a/nanovna.h +++ b/nanovna.h @@ -139,6 +139,9 @@ enum { #define SWEEP_ENABLE 0x01 #define SWEEP_ONCE 0x02 +#define SWEEP_CALIBRATE 0x04 +#define SWEEP_SELFTEST 0x08 + extern int8_t sweep_mode; extern const char *info_about[]; diff --git a/plot.c b/plot.c index 1a8ef2b..045fcff 100644 --- a/plot.c +++ b/plot.c @@ -1838,6 +1838,15 @@ static void cell_draw_marker_info(int x0, int y0) cell_drawstring_7x13(buf, xpos, ypos); break; } + if (i >= 2 && in_selftest) { + plot_printf(buf, sizeof buf, "DO NOT SWITCH OFF!!"); + j = 2; + int xpos = 1 + (j%2)*(WIDTH/2) + CELLOFFSETX - x0; + int ypos = 1 + (j/2)*(16) - y0; + + cell_drawstring_7x13(buf, xpos, ypos); + break; + } if (!markers[i].enabled) continue; int idx = markers[i].index; diff --git a/sa_core.c b/sa_core.c index 7f87134..2f9bbe2 100644 --- a/sa_core.c +++ b/sa_core.c @@ -1074,7 +1074,7 @@ static const struct { {TC_FLAT, TP_10MHZEXTRA, 10, 4, -25, 20, -70}, // 8 BPF pass band flatness {TC_BELOW, TP_30MHZ, 430, 60, -75, 0, -85}, // 9 LPF cutoff {TC_END, 0, 0, 0, 0, 0, 0}, - {TC_MEASURE, TP_30MHZ, 30, 7, -25, 30, -85 }, // 11 Measure power level and noise + {TC_MEASURE, TP_30MHZ, 30, 7, -25, 30, -80 }, // 11 Measure power level and noise {TC_MEASURE, TP_30MHZ, 270, 4, -50, 30, -85 }, // 13 Measure powerlevel and noise {TC_MEASURE, TPH_30MHZ, 270, 4, -50, 30, -85 }, // 14 Calibrate power high mode {TC_END, 0, 0, 0, 0, 0, 0}, @@ -1129,7 +1129,7 @@ void cell_draw_test_info(int x0, int y0) do { i++; int xpos = 25 - x0; - int ypos = 40+i*INFO_SPACING - y0; + int ypos = 50+i*INFO_SPACING - y0; unsigned int color = RGBHEX(0xFFFFFF); if (i == -1) { plot_printf(self_test_status_buf, sizeof self_test_status_buf, "Self test status:"); @@ -1341,7 +1341,6 @@ common_silent: } extern void menu_autosettings_cb(int item); -extern void touch_wait_release(void); void self_test(void) { @@ -1359,25 +1358,25 @@ void self_test(void) test_prepare(i); test_acquire(i); // Acquire test test_status[i] = test_validate(i); // Validate test - chThdSleepMilliseconds(1000); if (test_status[i] != TS_PASS) { - touch_wait_release(); + wait_user(); } i++; } - touch_wait_release(); - // chThdSleepMilliseconds(2000); + ili9341_set_foreground(BRIGHT_COLOR_GREEN); + ili9341_drawstring_7x13("Self test complete", 30, 120); + ili9341_drawstring_7x13("Touch screen to continue", 30, 140); + wait_user(); + + sweep_mode = SWEEP_ENABLE; show_test_info = FALSE; trace[TRACE_STORED].enabled = false; set_trace_refpos(0, NGRIDY - (-10) / get_trace_scale(0)); set_trace_refpos(1, NGRIDY - (-10) / get_trace_scale(0)); set_trace_refpos(2, NGRIDY - (-10) / get_trace_scale(0)); set_refer_output(0); - SetMode(M_LOW); - SetAverage(0); - draw_cal_status(); + reset_settings(M_LOW); in_selftest = false; - menu_autosettings_cb(0); } void reset_calibration(void) @@ -1394,18 +1393,21 @@ void calibrate(void) float last_peak_level; in_selftest = true; SetPowerLevel(100); - menu_autosettings_cb(0); + reset_settings(M_LOW); int i = 10; // calibrate low mode power on 30 MHz; for (int j= 0; j < CALIBRATE_RBWS; j++ ) { SetRBW(power_rbw[j]); test_prepare(i); test_acquire(i); // Acquire test local_test_status = test_validate(i); // Validate test - chThdSleepMilliseconds(1000); +// chThdSleepMilliseconds(1000); if (local_test_status != TS_PASS) { - // touch_wait_release(); + ili9341_set_foreground(BRIGHT_COLOR_RED); + ili9341_drawstring_7x13("Calibration failed", 30, 120); + goto quit; } else SetPowerLevel(-25); + goto done; } i = 11; // Measure 270MHz in low mode SetRBW(100); @@ -1423,23 +1425,28 @@ void calibrate(void) test_prepare(i); test_acquire(i); // Acquire test local_test_status = test_validate(i); // Validate test - chThdSleepMilliseconds(1000); if (local_test_status != TS_PASS) { - touch_wait_release(); + ili9341_set_foreground(BRIGHT_COLOR_RED); + ili9341_drawstring_7x13("Calibration failed", 30, 120); + goto quit; } else SetPowerLevel(last_peak_level); } - touch_wait_release(); +done: + ili9341_set_foreground(BRIGHT_COLOR_GREEN); + ili9341_drawstring_7x13("Calibration complete", 30, 120); +quit: + ili9341_drawstring_7x13("Touch screen to continue", 30, 140); + wait_user(); + + in_selftest = false; + sweep_mode = SWEEP_ENABLE; trace[TRACE_STORED].enabled = false; set_trace_refpos(0, NGRIDY - (-10) / get_trace_scale(0)); set_trace_refpos(1, NGRIDY - (-10) / get_trace_scale(0)); set_trace_refpos(2, NGRIDY - (-10) / get_trace_scale(0)); set_refer_output(0); - SetMode(M_LOW); - SetAverage(0); - draw_cal_status(); - in_selftest = false; - menu_autosettings_cb(0); + reset_settings(M_LOW); } diff --git a/ui.c b/ui.c index a7822e3..01467da 100644 --- a/ui.c +++ b/ui.c @@ -2469,3 +2469,20 @@ ui_init() touch_start_watchdog(); } + +void wait_user(void) +{ + adc_stop(); + touch_wait_release(); +#if 0 + operation_requested = OP_NONE; + while (true) { + if (operation_requested & OP_TOUCH) + break; + if (operation_requested & OP_LEVER) + break; + } +#endif + touch_start_watchdog(); +} + diff --git a/ui_sa.c b/ui_sa.c index bf90342..6b06b7a 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -242,7 +242,7 @@ static void menu_calibrate_cb(int item, uint8_t data) (void)data; switch (item) { case 1: - calibrate(); + sweep_mode = SWEEP_CALIBRATE; menu_move_back(); ui_mode_normal(); break; @@ -272,7 +272,7 @@ static void menu_config_cb(int item, uint8_t data) case 2: menu_move_back(); ui_mode_normal(); - self_test(); + sweep_mode = SWEEP_SELFTEST; break; case 4: show_version();