From 17734f257ded04cdb577402f5a5741f650090465 Mon Sep 17 00:00:00 2001 From: DiSlord Date: Thu, 27 Feb 2020 20:53:45 +0300 Subject: [PATCH] Disable unused ChibiOS options: CH_CFG_USE_WAITEXIT (used only is Shell run as thread) CH_CFG_USE_EVENTS (NanoVNA not use events) CH_CFG_USE_EVENTS_TIMEOUT (NanoVNA not use events) Implement stack use check in "threads" command, now free stack space show in table as "stk free" in hex Check stack usage by sweep, and main threads (seems all ok, but add 64 bytes to sweep) Replace some const values to defined --- chconf.h | 6 +- main.c | 190 ++++++++++++++++++++++++++++++------------------------- plot.c | 29 +++++---- ui.c | 17 +++-- 4 files changed, 129 insertions(+), 113 deletions(-) diff --git a/chconf.h b/chconf.h index 34e6afe..09a6139 100644 --- a/chconf.h +++ b/chconf.h @@ -156,7 +156,7 @@ * * @note The default is @p TRUE. */ -#define CH_CFG_USE_WAITEXIT TRUE +#define CH_CFG_USE_WAITEXIT FALSE /** * @brief Semaphores APIs. @@ -221,7 +221,7 @@ * * @note The default is @p TRUE. */ -#define CH_CFG_USE_EVENTS TRUE +#define CH_CFG_USE_EVENTS FALSE /** * @brief Events Flags APIs with timeout. @@ -231,7 +231,7 @@ * @note The default is @p TRUE. * @note Requires @p CH_CFG_USE_EVENTS. */ -#define CH_CFG_USE_EVENTS_TIMEOUT TRUE +#define CH_CFG_USE_EVENTS_TIMEOUT FALSE /** * @brief Synchronous Messages APIs. diff --git a/main.c b/main.c index 8c2c5fe..0ebbd40 100644 --- a/main.c +++ b/main.c @@ -80,55 +80,58 @@ int8_t cal_auto_interpolate = TRUE; uint16_t redraw_request = 0; // contains REDRAW_XXX flags int16_t vbat = 0; -static THD_WORKING_AREA(waThread1, 512); +// +// Profile stack usage (enable threads command by def ENABLE_THREADS_COMMAND) show: +// Stack maximum usage = 480 bytes, free stack = 32+64 bytes +// +static THD_WORKING_AREA(waThread1, 512+64); static THD_FUNCTION(Thread1, arg) { - (void)arg; - chRegSetThreadName("sweep"); + (void)arg; + chRegSetThreadName("sweep"); - while (1) { - bool completed = false; - if (sweep_enabled || sweep_once) { - chMtxLock(&mutex); - // Sweep require 8367 system tick - completed = sweep(true); - sweep_once = FALSE; - chMtxUnlock(&mutex); - } else { - __WFI(); + while (1) { + bool completed = false; + if (sweep_enabled || sweep_once) { + chMtxLock(&mutex); + // Sweep require 8367 system tick + completed = sweep(true); + sweep_once = FALSE; + chMtxUnlock(&mutex); + } else { + __WFI(); + } + + chMtxLock(&mutex); + // Ui and render require 800 system tick + ui_process(); + + if (sweep_enabled) { + if (vbat != -1) { + adc_stop(ADC1); + vbat = adc_vbat_read(ADC1); + touch_start_watchdog(); + draw_battery_status(); } - chMtxLock(&mutex); - // Ui and render require 800 system tick - ui_process(); + // calculate trace coordinates and plot only if scan completed + if (completed) { + plot_into_index(measured); + redraw_request |= REDRAW_CELLS; - if (sweep_enabled) { - if (vbat != -1) { - adc_stop(ADC1); - vbat = adc_vbat_read(ADC1); - touch_start_watchdog(); - draw_battery_status(); - } - - /* calculate trace coordinates and plot only if scan completed */ - if (completed) { - plot_into_index(measured); - redraw_request |= REDRAW_CELLS; - - if (marker_tracking) { - int i = marker_search(); - if (i != -1 && active_marker != -1) { - markers[active_marker].index = i; - redraw_request |= REDRAW_MARKER; - } + if (marker_tracking) { + int i = marker_search(); + if (i != -1 && active_marker != -1) { + markers[active_marker].index = i; + redraw_request |= REDRAW_MARKER; } } } - - /* plot trace and other indications as raster */ - draw_all(completed); // flush markmap only if scan completed to prevent remaining traces - chMtxUnlock(&mutex); } + // plot trace and other indications as raster + draw_all(completed); // flush markmap only if scan completed to prevent remaining traces + chMtxUnlock(&mutex); + } } void @@ -207,6 +210,7 @@ transform_domain(void) break; } + for (int ch = 0; ch < 2; ch++) { memcpy(tmp, measured[ch], sizeof(measured[0])); for (int i = 0; i < POINTS_COUNT; i++) { @@ -1939,12 +1943,22 @@ VNA_SHELL_FUNCTION(cmd_threads) { thread_t *tp; (void)argc; (void)argv; - chprintf(chp, "stklimit stack addr refs prio state name"VNA_SHELL_NEWLINE_STR); + shell_printf("stklimit| stack|stk free| addr|refs|prio| state| name"VNA_SHELL_NEWLINE_STR); 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; - shell_printf("%08x %08x %08x %4u %4u %9s %12s"VNA_SHELL_NEWLINE_STR, - stklimit, (uint32_t)tp->ctx.sp, (uint32_t)tp, +#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 + + + shell_printf("%08x|%08x|%08x|%08x|%4u|%4u|%9s|%12s"VNA_SHELL_NEWLINE_STR, + 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); tp = chRegNextThread(tp); @@ -2142,68 +2156,65 @@ static DACConfig dac1cfg1 = { datamode: DAC_DHRM_12BIT_RIGHT }; + +// Main thread stack size defined in makefile USE_PROCESS_STACKSIZE = 0x200 +// Profile stack usage (enable threads command by def ENABLE_THREADS_COMMAND) show: +// Stack maximum usage = 472 bytes (need test more and run all commands), free stack = 40 bytes +// int main(void) { - halInit(); - chSysInit(); + halInit(); + chSysInit(); - chMtxObjectInit(&mutex); + chMtxObjectInit(&mutex); - //palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(1) | PAL_STM32_OTYPE_OPENDRAIN); - //palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(1) | PAL_STM32_OTYPE_OPENDRAIN); - i2cStart(&I2CD1, &i2ccfg); - si5351_init(); + //palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(1) | PAL_STM32_OTYPE_OPENDRAIN); + //palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(1) | PAL_STM32_OTYPE_OPENDRAIN); + i2cStart(&I2CD1, &i2ccfg); + si5351_init(); - // MCO on PA8 - //palSetPadMode(GPIOA, 8, PAL_MODE_ALTERNATE(0)); - /* - * Initializes a serial-over-USB CDC driver. - */ - sduObjectInit(&SDU1); - sduStart(&SDU1, &serusbcfg); + // MCO on PA8 + //palSetPadMode(GPIOA, 8, PAL_MODE_ALTERNATE(0)); +/* + * Initializes a serial-over-USB CDC driver. + */ + sduObjectInit(&SDU1); + sduStart(&SDU1, &serusbcfg); +/* + * Activates the USB driver and then the USB bus pull-up on D+. + * Note, a delay is inserted in order to not have to disconnect the cable + * after a reset. + */ + usbDisconnectBus(serusbcfg.usbp); + chThdSleepMilliseconds(100); + usbStart(serusbcfg.usbp, &usbcfg); + usbConnectBus(serusbcfg.usbp); - /* - * Activates the USB driver and then the USB bus pull-up on D+. - * Note, a delay is inserted in order to not have to disconnect the cable - * after a reset. - */ - usbDisconnectBus(serusbcfg.usbp); - chThdSleepMilliseconds(100); - usbStart(serusbcfg.usbp, &usbcfg); - usbConnectBus(serusbcfg.usbp); - - /* - * SPI LCD Initialize - */ +/* + * SPI LCD Initialize + */ ili9341_init(); - /* - * Initialize graph plotting - */ - plot_init(); - - /* restore config */ +/* restore config */ config_recall(); dac1cfg1.init = config.dac_value; - /* - * Starting DAC1 driver, setting up the output pin as analog as suggested - * by the Reference Manual. - */ +/* + * Starting DAC1 driver, setting up the output pin as analog as suggested + * by the Reference Manual. + */ dacStart(&DACD2, &dac1cfg1); - /* initial frequencies */ +/* initial frequencies */ update_frequencies(); - /* restore frequencies and calibration properties from flash memory */ +/* restore frequencies and calibration properties from flash memory */ if (config.default_loadcal >= 0) caldata_recall(config.default_loadcal); - redraw_frame(); - - /* - * I2S Initialize - */ +/* + * I2S Initialize + */ tlv320aic3204_init(); i2sInit(); i2sObjectInit(&I2SD2); @@ -2211,12 +2222,17 @@ int main(void) i2sStartExchange(&I2SD2); ui_init(); - + //Initialize graph plotting + plot_init(); + redraw_frame(); chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO-1, Thread1, NULL); while (1) { if (SDU1.config->usbp->state == USB_ACTIVE) { #ifdef VNA_SHELL_THREAD +#if CH_CFG_USE_WAITEXIT == FALSE +#error "VNA_SHELL_THREAD use chThdWait, need enable CH_CFG_USE_WAITEXIT in chconf.h" +#endif thread_t *shelltp = chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO + 1, myshellThread, NULL); diff --git a/plot.c b/plot.c index c909e09..b696397 100644 --- a/plot.c +++ b/plot.c @@ -1462,7 +1462,7 @@ cell_drawstring(char *str, int x, int y) static void cell_draw_marker_info(int x0, int y0) { - char buf[32]; + char buf[24]; int t; if (active_marker < 0) return; @@ -1474,8 +1474,8 @@ cell_draw_marker_info(int x0, int y0) for (mk = 0; mk < MARKERS_MAX; mk++) { if (!markers[mk].enabled) continue; - int xpos = 1 + (j%2)*146 + CELLOFFSETX - x0; - int ypos = 1 + (j/2)*8 - y0; + int xpos = 1 + (j%2)*(WIDTH/2) + CELLOFFSETX - x0; + int ypos = 1 + (j/2)*(FONT_GET_HEIGHT+1) - y0; setForegroundColor(config.trace_color[t]); if (mk == active_marker) @@ -1507,13 +1507,13 @@ cell_draw_marker_info(int x0, int y0) // draw marker delta if (!uistat.marker_delta && previous_marker >= 0 && active_marker != previous_marker && markers[previous_marker].enabled) { int idx0 = markers[previous_marker].index; - int xpos = 180 + CELLOFFSETX - x0; - int ypos = 1 + (j/2)*8 - y0; + int xpos = (WIDTH/2+30) + CELLOFFSETX - x0; + int ypos = 1 + (j/2)*(FONT_GET_HEIGHT+1) - y0; - plot_printf(buf, sizeof buf, S_DELTA"%d-%d", active_marker+1, previous_marker+1); + plot_printf(buf, sizeof buf, S_DELTA"%d-%d:", active_marker+1, previous_marker+1); setForegroundColor(DEFAULT_FG_COLOR); cell_drawstring(buf, xpos, ypos); - xpos += 24; + xpos += 27; if ((domain_mode & DOMAIN_MODE) == DOMAIN_FREQ) { uint32_t freq = frequencies[idx]; uint32_t freq1 = frequencies[idx0]; @@ -1528,8 +1528,8 @@ cell_draw_marker_info(int x0, int y0) for (t = 0; t < TRACES_MAX; t++) { if (!trace[t].enabled) continue; - int xpos = 1 + (j%2)*146 + CELLOFFSETX - x0; - int ypos = 1 + (j/2)*8 - y0; + int xpos = 1 + (j%2)*(WIDTH/2) + CELLOFFSETX - x0; + int ypos = 1 + (j/2)*(FONT_GET_HEIGHT+1) - y0; setForegroundColor(config.trace_color[t]); if (t == uistat.current_trace) @@ -1550,8 +1550,8 @@ cell_draw_marker_info(int x0, int y0) } // draw marker frequency - int xpos = 185 + CELLOFFSETX - x0; - int ypos = 1 + (j/2)*8 - y0; + int xpos = (WIDTH/2+40) + CELLOFFSETX - x0; + int ypos = 1 + (j/2)*(FONT_GET_HEIGHT+1) - y0; setForegroundColor(DEFAULT_FG_COLOR); if (uistat.lever_mode == LM_MARKER) @@ -1572,7 +1572,7 @@ cell_draw_marker_info(int x0, int y0) if (electrical_delay != 0) { // draw electrical delay int xpos = 21 + CELLOFFSETX - x0; - int ypos = 1 + ((j+1)/2)*8 - y0; + int ypos = 1 + ((j+1)/2)*(FONT_GET_HEIGHT+1) - y0; float light_speed_ps = 299792458e-12; //(m/ps) plot_printf(buf, sizeof buf, "Edelay %Fs %Fm", electrical_delay * 1e-12, @@ -1663,7 +1663,7 @@ draw_battery_status(void) { if (vbat<=0) return; - uint8_t string_buf[25]; + uint8_t string_buf[16]; // Set battery color setForegroundColor(vbat < BATTERY_WARNING_LEVEL ? DEFAULT_LOW_BAT_COLOR : DEFAULT_NORMAL_BAT_COLOR); setBackgroundColor(DEFAULT_BG_COLOR); @@ -1697,7 +1697,8 @@ request_to_redraw_grid(void) void redraw_frame(void) { - ili9341_fill(0, 0, 320, 240, DEFAULT_BG_COLOR); + setBackgroundColor(DEFAULT_BG_COLOR); + clearScreen(); draw_frequencies(); draw_cal_status(); } diff --git a/ui.c b/ui.c index d5c2d83..a1765e2 100644 --- a/ui.c +++ b/ui.c @@ -1271,7 +1271,7 @@ draw_numeric_area_frame(void) ili9341_fill(0, 240-NUM_INPUT_HEIGHT, 320, NUM_INPUT_HEIGHT, DEFAULT_MENU_COLOR); setForegroundColor(DEFAULT_MENU_TEXT_COLOR); setBackgroundColor(DEFAULT_MENU_COLOR); - ili9341_drawstring(keypad_mode_label[keypad_mode], 10, 240-FONT_GET_HEIGHT-(NUM_INPUT_HEIGHT-FONT_GET_HEIGHT)/2); + ili9341_drawstring(keypad_mode_label[keypad_mode], 10, 240-(FONT_GET_HEIGHT+NUM_INPUT_HEIGHT)/2); //ili9341_drawfont(KP_KEYPAD, 300, 216); } @@ -1297,8 +1297,8 @@ draw_numeric_input(const char *buf) if (ui_mode == UI_NUMERIC && uistat.digit == 8-i) { fg = DEFAULT_SPEC_INPUT_COLOR; focused = TRUE; - if (uistat.digit_mode) - bg = DEFAULT_MENU_COLOR; +// if (uistat.digit_mode) +// bg = DEFAULT_MENU_COLOR; } setForegroundColor(fg); setBackgroundColor(bg); @@ -1307,12 +1307,12 @@ draw_numeric_input(const char *buf) else if (focused) // c not number, but focused ili9341_drawfont(0, x, 240-NUM_INPUT_HEIGHT+4); else // erase - ili9341_fill(x, 240-NUM_INPUT_HEIGHT+4, 20, 24, bg); + ili9341_fill(x, 240-NUM_INPUT_HEIGHT+4, NUM_FONT_GET_HEIGHT, NUM_FONT_GET_WIDTH+2+8, bg); x += xsim&0x8000 ? NUM_FONT_GET_WIDTH+2+8 : NUM_FONT_GET_WIDTH+2; } // erase last - ili9341_fill(x, 240-NUM_INPUT_HEIGHT+4, NUM_FONT_GET_WIDTH+2+8, 24, DEFAULT_MENU_COLOR); + ili9341_fill(x, 240-NUM_INPUT_HEIGHT+4, NUM_FONT_GET_WIDTH+2+8, NUM_FONT_GET_WIDTH+2+8, DEFAULT_MENU_COLOR); } static int @@ -1607,7 +1607,7 @@ ui_mode_numeric(int _keypad_mode) keypad_mode = _keypad_mode; ui_mode = UI_NUMERIC; area_width = AREA_WIDTH_NORMAL; - area_height = 240-32;//AREA_HEIGHT_NORMAL - 32; + area_height = 240-NUM_INPUT_HEIGHT;//AREA_HEIGHT_NORMAL - 32; draw_numeric_area_frame(); fetch_numeric_target(); @@ -1629,7 +1629,7 @@ ui_mode_keypad(int _keypad_mode) keypads_last_index = i; ui_mode = UI_KEYPAD; - area_width = AREA_WIDTH_NORMAL - 60; + area_width = AREA_WIDTH_NORMAL - MENU_BUTTON_WIDTH; area_height = HEIGHT - 32; draw_menu(); draw_keypad(); @@ -1740,7 +1740,7 @@ ui_process_normal(void) if (status & EVT_BUTTON_SINGLE_CLICK) { ui_mode_menu(); } else { - switch (uistat.lever_mode) { + switch (uistat.lever_mode) { case LM_MARKER: lever_move_marker(status); break; case LM_SEARCH: lever_search_marker(status); break; case LM_CENTER: @@ -2051,7 +2051,6 @@ ui_process_lever(void) } } - static void drag_marker(int t, int m) {