From 4f3016649d789292564d1b914c237779b2a6c280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Mon, 27 Jul 2020 21:29:15 +0200 Subject: [PATCH] First draft of safety controller --- .../reflow-controller/safety/safety-config.h | 1 + .../safety/safety-controller.h | 3 +++ .../reflow-controller/safety/watchdog.h | 7 ++++++ .../include/stm-periph/clock-enable-manager.h | 1 - stm-firmware/safety/safety-controller.c | 25 +++++++++++++------ stm-firmware/safety/watchdog.c | 12 +++++++++ 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/stm-firmware/include/reflow-controller/safety/safety-config.h b/stm-firmware/include/reflow-controller/safety/safety-config.h index 4e88316..073d3f1 100644 --- a/stm-firmware/include/reflow-controller/safety/safety-config.h +++ b/stm-firmware/include/reflow-controller/safety/safety-config.h @@ -34,6 +34,7 @@ enum safety_flag { ERR_FLAG_STACK = (1<<8), ERR_FLAG_SAFETY_ADC = (1<<9), ERR_FLAG_SYSTICK = (1<<10), + ERR_FLAG_WTCHDG_FIRED = (1<<11), }; enum timing_monitor { diff --git a/stm-firmware/include/reflow-controller/safety/safety-controller.h b/stm-firmware/include/reflow-controller/safety/safety-controller.h index 36be0f0..cfe29fb 100644 --- a/stm-firmware/include/reflow-controller/safety/safety-controller.h +++ b/stm-firmware/include/reflow-controller/safety/safety-controller.h @@ -29,6 +29,7 @@ #include #include +#include enum analog_monitor_status {ANALOG_MONITOR_OK = 0, ANALOG_MONITOR_ERROR, @@ -66,6 +67,8 @@ int safety_controller_get_flag(enum safety_flag flag, bool *status, bool try_ack int safety_controller_ack_flag(enum safety_flag flag); +int safety_controller_ack_flag_with_key(enum safety_flag flag, uint32_t key); + #endif /* __SAFETY_CONTROLLER_H__ */ /** @} */ diff --git a/stm-firmware/include/reflow-controller/safety/watchdog.h b/stm-firmware/include/reflow-controller/safety/watchdog.h index 9938aa2..f248547 100644 --- a/stm-firmware/include/reflow-controller/safety/watchdog.h +++ b/stm-firmware/include/reflow-controller/safety/watchdog.h @@ -23,6 +23,7 @@ #include #include +#include /** * @brief Setup the watchdog for the safety controller @@ -39,5 +40,11 @@ int watchdog_setup(uint8_t prescaler); */ int watchdog_ack(uint32_t magic); +/** + * @brief Check if reset was generated by the watchdog. + * @note This also clears the relevant flag, so the function will reutrn false when called a second time + * @return + */ +bool watchdog_check_reset_source(void); #endif /* __WATCHDOG_H__ */ diff --git a/stm-firmware/include/stm-periph/clock-enable-manager.h b/stm-firmware/include/stm-periph/clock-enable-manager.h index 6725639..60efb1f 100644 --- a/stm-firmware/include/stm-periph/clock-enable-manager.h +++ b/stm-firmware/include/stm-periph/clock-enable-manager.h @@ -64,5 +64,4 @@ int rcc_manager_enable_clock(volatile uint32_t *rcc_enable_register, uint8_t bit */ int rcc_manager_disable_clock(volatile uint32_t *rcc_enable_register, uint8_t bit_no); - #endif /* __CLOCK_ENABLE_MANAGER_H__ */ diff --git a/stm-firmware/safety/safety-controller.c b/stm-firmware/safety/safety-controller.c index 4881927..ccca4a3 100644 --- a/stm-firmware/safety/safety-controller.c +++ b/stm-firmware/safety/safety-controller.c @@ -30,9 +30,6 @@ #include #include #include - -#include -#include #include struct error_flag { @@ -40,6 +37,7 @@ struct error_flag { enum safety_flag flag; bool error_state; bool persistent; + uint32_t key; }; struct timing_mon { @@ -68,7 +66,7 @@ struct analog_mon { #define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x]))))) -#define ERR_FLAG_ENTRY(errflag, persistency) {.name=#errflag, .flag = (errflag), .error_state = false, .persistent = (persistency)} +#define ERR_FLAG_ENTRY(errflag, persistency) {.name=#errflag, .flag = (errflag), .error_state = false, .persistent = (persistency), .key = 0UL} #define TIM_MON_ENTRY(mon, min, max, flag) {.name=#mon, .monitor = (mon), .associated_flag=(flag), .min_delta = (min), .max_delta = (max), .last = 0ULL, .enabled= false} #define ANA_MON_ENTRY(mon, min_value, max_value, flag) {.name=#mon, .monitor = (mon), .associated_flag=(flag), .min = (min_value), .max = (max_value), .value = 0.0f, .valid = false} @@ -84,6 +82,7 @@ static struct error_flag flags[] = { ERR_FLAG_ENTRY(ERR_FLAG_STACK, true), ERR_FLAG_ENTRY(ERR_FLAG_SAFETY_ADC, true), ERR_FLAG_ENTRY(ERR_FLAG_SYSTICK, true), + ERR_FLAG_ENTRY(ERR_FLAG_WTCHDG_FIRED, true), }; static struct timing_mon timings[] = { @@ -301,6 +300,8 @@ int safety_controller_handle() safety_controller_check_stack(); safety_controller_handle_safety_adc(); + if (watchdog_check_reset_source()) + safety_controller_report_error(ERR_FLAG_WTCHDG_FIRED); safety_controller_process_checks(); @@ -371,14 +372,24 @@ int safety_controller_get_flag(enum safety_flag flag, bool *status, bool try_ack found_flag = find_error_flag(flag); if (found_flag) { *status = found_flag->error_state; - if (try_ack && !found_flag->persistent) - found_flag->error_state = false; + if (try_ack && !found_flag->persistent) { + /* Flag is generally non persistent + * If key is set, this function cannot remove the flag + */ + if (found_flag->key == 0UL) + found_flag->error_state = false; + } } return ret; } int safety_controller_ack_flag(enum safety_flag flag) +{ + return safety_controller_ack_flag_with_key(flag, 0UL); +} + +int safety_controller_ack_flag_with_key(enum safety_flag flag, uint32_t key) { int ret = -1; struct error_flag *found_flag; @@ -389,7 +400,7 @@ int safety_controller_ack_flag(enum safety_flag flag) found_flag = find_error_flag(flag); if (found_flag) { - if (!found_flag->persistent) { + if (!found_flag->persistent && found_flag->key == key) { found_flag->error_state = false; ret = 0; } else { diff --git a/stm-firmware/safety/watchdog.c b/stm-firmware/safety/watchdog.c index a4f1f8c..3bdb05a 100644 --- a/stm-firmware/safety/watchdog.c +++ b/stm-firmware/safety/watchdog.c @@ -105,4 +105,16 @@ int watchdog_ack(uint32_t magic) return ret; } +bool watchdog_check_reset_source(void) +{ + bool ret; + + ret = !!(RCC->CSR & RCC_CSR_WDGRSTF); + + if (ret) + RCC->CSR |= RCC_CSR_RMVF; + + return ret; +} + /** @} */