From d1d2d514bd7e61f5d3f9a3ae5076c78b2d1c79a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sun, 16 Aug 2020 12:34:41 +0200 Subject: [PATCH] Add timing monitor for main loop and add monitors to safety flag command --- .../reflow-controller/safety/safety-config.h | 2 + .../safety/safety-controller.h | 14 ++++++ stm-firmware/main.c | 3 +- stm-firmware/safety/safety-controller.c | 45 +++++++++++++++++++ stm-firmware/shell.c | 17 +++++++ 5 files changed, 80 insertions(+), 1 deletion(-) diff --git a/stm-firmware/include/reflow-controller/safety/safety-config.h b/stm-firmware/include/reflow-controller/safety/safety-config.h index 7ecabc7..4ae7f96 100644 --- a/stm-firmware/include/reflow-controller/safety/safety-config.h +++ b/stm-firmware/include/reflow-controller/safety/safety-config.h @@ -37,12 +37,14 @@ enum safety_flag { ERR_FLAG_WTCHDG_FIRED = (1<<11), ERR_FLAG_UNCAL = (1<<12), ERR_FLAG_DEBUG = (1<<13), + ERR_FLAG_TIMING_MAIN_LOOP = (1<<14), }; enum timing_monitor { ERR_TIMING_PID = (1<<0), ERR_TIMING_MEAS_ADC = (1<<1), ERR_TIMING_SAFETY_ADC = (1<<2), + ERR_TIMING_MAIN_LOOP = (1<<3), }; enum analog_value_monitor { diff --git a/stm-firmware/include/reflow-controller/safety/safety-controller.h b/stm-firmware/include/reflow-controller/safety/safety-controller.h index 55fa396..65e7e96 100644 --- a/stm-firmware/include/reflow-controller/safety/safety-controller.h +++ b/stm-firmware/include/reflow-controller/safety/safety-controller.h @@ -46,6 +46,14 @@ struct analog_monitor_info { uint64_t timestamp; }; +struct timing_monitor_info { + uint64_t last_run; + uint64_t min; + uint64_t max; + bool enabled; + uint64_t delta; +}; + /** * @brief Initialize the safety controller * @@ -94,6 +102,12 @@ int safety_controller_get_analog_mon_by_index(uint32_t index, struct analog_moni int safety_controller_get_analog_mon_name_by_index(uint32_t index, char *buffer, size_t buffsize); +int safety_controller_get_timing_mon_by_index(uint32_t index, struct timing_monitor_info *info); + +int safety_controller_get_timing_mon_name_by_index(uint32_t index, char *buffer, size_t buffsize); + +uint32_t safety_controller_get_timing_monitor_count(); + #endif /* __SAFETY_CONTROLLER_H__ */ /** @} */ diff --git a/stm-firmware/main.c b/stm-firmware/main.c index 75a1327..2597a1e 100644 --- a/stm-firmware/main.c +++ b/stm-firmware/main.c @@ -206,9 +206,9 @@ int main(void) shell_print_motd(shell_handle); while (1) { - sd_card_mounted = mount_sd_card_if_avail(sd_card_mounted); if (systick_ticks_have_passed(quarter_sec_timestamp, 250)) { + sd_card_mounted = mount_sd_card_if_avail(sd_card_mounted); quarter_sec_timestamp = systick_get_global_tick(); } @@ -220,6 +220,7 @@ int main(void) oven_driver_set_power(0); oven_driver_apply_power_level(); + safety_controller_report_timing(ERR_TIMING_MAIN_LOOP); if (menu_wait_request) __WFI(); else diff --git a/stm-firmware/safety/safety-controller.c b/stm-firmware/safety/safety-controller.c index e8790de..be99209 100644 --- a/stm-firmware/safety/safety-controller.c +++ b/stm-firmware/safety/safety-controller.c @@ -48,6 +48,7 @@ struct timing_mon { uint64_t min_delta; uint64_t max_delta; uint64_t last; + uint64_t calculated_delta; bool enabled; }; @@ -87,12 +88,14 @@ static volatile struct error_flag flags[] = { ERR_FLAG_ENTRY(ERR_FLAG_WTCHDG_FIRED, true), ERR_FLAG_ENTRY(ERR_FLAG_UNCAL, false), ERR_FLAG_ENTRY(ERR_FLAG_DEBUG, true), + ERR_FLAG_ENTRY(ERR_FLAG_TIMING_MAIN_LOOP, false), }; static volatile struct timing_mon timings[] = { TIM_MON_ENTRY(ERR_TIMING_PID, 2, 1000, ERR_FLAG_TIMING_PID), TIM_MON_ENTRY(ERR_TIMING_MEAS_ADC, 0, 50, ERR_FLAG_TIMING_MEAS_ADC), TIM_MON_ENTRY(ERR_TIMING_SAFETY_ADC, 10, SAFETY_CONTROLLER_ADC_DELAY_MS + 1000, ERR_FLAG_SAFETY_ADC), + TIM_MON_ENTRY(ERR_TIMING_MAIN_LOOP, 0, 1000, ERR_FLAG_TIMING_MAIN_LOOP), }; static volatile struct analog_mon analog_mons[] = { @@ -217,6 +220,7 @@ void safety_controller_report_timing(enum timing_monitor monitor) } } + tim->calculated_delta = timestamp - tim->last; tim->last = timestamp; tim->enabled = true; } @@ -463,6 +467,11 @@ uint32_t safety_controller_get_analog_monitor_count() return COUNT_OF(analog_mons); } +uint32_t safety_controller_get_timing_monitor_count() +{ + return COUNT_OF(timings); +} + int safety_controller_get_analog_mon_name_by_index(uint32_t index, char *buffer, size_t buffsize) { if (index >= COUNT_OF(analog_mons)) @@ -491,6 +500,20 @@ int safety_controller_get_flag_name_by_index(uint32_t index, char *buffer, size_ return 0; } +int safety_controller_get_timing_mon_name_by_index(uint32_t index, char *buffer, size_t buffsize) +{ + if (index >= COUNT_OF(timings)) + return -1; + + if (buffsize == 0 || !buffer) + return -1000; + + strncpy(buffer, timings[index].name, buffsize); + buffer[buffsize - 1] = 0; + + return 0; +} + int safety_controller_get_flag_by_index(uint32_t index, bool *status, enum safety_flag *flag_enum) { int ret = -1; @@ -543,4 +566,26 @@ int safety_controller_get_analog_mon_by_index(uint32_t index, struct analog_moni return 0; } +int safety_controller_get_timing_mon_by_index(uint32_t index, struct timing_monitor_info *info) +{ + volatile struct timing_mon *mon; + + if (!info) + return -1002; + + if (index >= COUNT_OF(timings)) { + return -1001; + } + + mon = &timings[index]; + + info->max = mon->max_delta; + info->min = mon->min_delta; + info->enabled = mon->enabled; + info->last_run = mon->last; + info->delta = mon->calculated_delta; + + return 0; +} + /** @} */ diff --git a/stm-firmware/shell.c b/stm-firmware/shell.c index 1a3226b..adcc5a4 100644 --- a/stm-firmware/shell.c +++ b/stm-firmware/shell.c @@ -355,6 +355,7 @@ static shellmatta_retCode_t shell_cmd_read_flags(const shellmatta_handle_t handl bool flag; int status; struct analog_monitor_info amon_info; + struct timing_monitor_info timing_info; shellmatta_printf(handle, "Error flags\r\n" "-----------\r\n"); @@ -405,6 +406,22 @@ static shellmatta_retCode_t shell_cmd_read_flags(const shellmatta_handle_t handl } } + shellmatta_printf(handle, "\r\nTiming Monitors\r\n" + "--------------\r\n"); + + + count = safety_controller_get_timing_monitor_count(); + for (i = 0; i < count; i++) { + (void)safety_controller_get_timing_mon_by_index(i, &timing_info); + (void)safety_controller_get_timing_mon_name_by_index(i, name, sizeof(name)); + + shellmatta_printf(handle, "\t%2lu) %-20s\t", i+1, name); + if (timing_info.enabled) + shellmatta_printf(handle, "%lu ms ago\r\n", (unsigned long int)timing_info.delta); + else + shellmatta_printf(handle, "[disabled]\r\n"); + } + return SHELLMATTA_OK; }