From 1c1d1c4c972bb28a975d1f982dd1b4429a414821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Tue, 8 Sep 2020 19:23:14 +0200 Subject: [PATCH] Issue #18: Store correct flag number in error memory --- .../reflow-controller/safety/safety-config.h | 1 + stm-firmware/safety/safety-controller.c | 27 ++++++++++++++++++- 2 files changed, 27 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 846afcd..8ad78a0 100644 --- a/stm-firmware/include/reflow-controller/safety/safety-config.h +++ b/stm-firmware/include/reflow-controller/safety/safety-config.h @@ -23,6 +23,7 @@ enum safety_flag { + ERR_FLAG_NO_FLAG = 0, ERR_FLAG_MEAS_ADC_OFF = (1<<0), ERR_FLAG_MEAS_ADC_OVERFLOW = (1<<1), ERR_FLAG_MEAS_ADC_WATCHDOG = (1<<2), diff --git a/stm-firmware/safety/safety-controller.c b/stm-firmware/safety/safety-controller.c index 79cf6db..22dec1b 100644 --- a/stm-firmware/safety/safety-controller.c +++ b/stm-firmware/safety/safety-controller.c @@ -259,6 +259,31 @@ static void safety_controller_process_monitor_checks() safety_controller_process_active_timing_mons(); } +static uint8_t flag_enum_to_flag_no(enum safety_flag flag) +{ + uint32_t flag_mask; + uint8_t i; + + if (!is_power_of_two(flag)) + return 0xFF; + + flag_mask = (uint32_t)flag; + for (i = 0; i < 32; i++) { + if ((flag_mask >> i) & 0x1U) + break; + } + + return i; +} + +static enum safety_flag flag_no_to_flag_enum(uint8_t no) +{ + if (no >= COUNT_OF(flags)) + return ERR_FLAG_NO_FLAG; + + return (1U << no); +} + int safety_controller_report_error(enum safety_flag flag) { return safety_controller_report_error_with_key(flag, 0x0UL); @@ -281,7 +306,7 @@ int safety_controller_report_error_with_key(enum safety_flag flag, uint32_t key) if (flags[i].persistent && !old_state) { err_mem_entry.counter = 1; - err_mem_entry.flag_num = i; + err_mem_entry.flag_num = flag_enum_to_flag_no(flags[i].flag); err_mem_entry.type = SAFETY_MEMORY_ERR_ENTRY_FLAG; res = safety_memory_insert_error_entry(&err_mem_entry); if (res) {