diff --git a/stm-firmware/include/reflow-controller/safety/safety-memory.h b/stm-firmware/include/reflow-controller/safety/safety-memory.h index 7c62ac9..8ef7531 100644 --- a/stm-firmware/include/reflow-controller/safety/safety-memory.h +++ b/stm-firmware/include/reflow-controller/safety/safety-memory.h @@ -51,7 +51,8 @@ struct safety_memory_header { struct safety_memory_boot_status { uint32_t reboot_to_bootloader; - uint32_t code_updated; + uint32_t code_updated; + uint32_t reset_from_panic; }; enum safety_memory_state { @@ -67,7 +68,7 @@ enum safety_memory_error_entry_type { struct error_memory_entry { enum safety_memory_error_entry_type type; - uint16_t flag_num; + uint8_t flag_num; }; enum config_override_entry_type { @@ -79,11 +80,11 @@ struct config_override { enum config_override_entry_type type; union { struct { - uint16_t flag; + uint8_t flag; uint8_t weight; } weight_override; struct { - uint16_t flag; + uint8_t flag; uint8_t persistance; } persistance_override; } entry; @@ -95,6 +96,8 @@ int safety_memory_reinit(enum safety_memory_state *found_state); int safety_memory_get_boot_status(struct safety_memory_boot_status *status); +int safety_memory_set_boot_status(const struct safety_memory_boot_status *status); + int safety_memory_get_error_entry_count(uint32_t *count); int safety_memory_check(void); diff --git a/stm-firmware/safety/fault.c b/stm-firmware/safety/fault.c index 79a743d..86d8b8b 100644 --- a/stm-firmware/safety/fault.c +++ b/stm-firmware/safety/fault.c @@ -21,6 +21,8 @@ #include #include #include +#include +#include void HardFault_Handler(void) { @@ -36,6 +38,8 @@ void HardFault_Handler(void) void panic_mode(void) { + static struct safety_memory_boot_status IN_SECTION(.ccm.bss) boot_status; + /* Panic mode is esentially the same as a hardfault, * but it can be expected, that more functionality is still usable */ @@ -44,6 +48,11 @@ void panic_mode(void) oven_driver_apply_power_level(); /* TODO: implement panic mode */ + if (!safety_memory_get_boot_status(&boot_status)) { + boot_status.reset_from_panic = 0xFFFFFFFF; + (void)safety_memory_set_boot_status(&boot_status); + } + while (1); } diff --git a/stm-firmware/safety/safety-memory.c b/stm-firmware/safety/safety-memory.c index 7421219..0cb3239 100644 --- a/stm-firmware/safety/safety-memory.c +++ b/stm-firmware/safety/safety-memory.c @@ -195,7 +195,49 @@ int safety_memory_init(enum safety_memory_state *found_state) return safety_memory_reinit(found_state); } -int safety_memory_get_boot_status(struct safety_memory_boot_status *status); +int safety_memory_get_boot_status(struct safety_memory_boot_status *status) +{ + struct safety_memory_header header; + int res; + + if (!status) + return -1001; + + if (safety_memory_get_header(&header) != SAFETY_MEMORY_INIT_VALID_MEMORY) { + return -2000; + } + + if (safety_memory_check_crc()) + return -2001; + + res = backup_ram_get_data(header.boot_status_offset, (uint32_t *)status, wordsize_of(*status)); + if (res) + return -3000; + return 0; +} + +int safety_memory_set_boot_status(const struct safety_memory_boot_status *status) +{ + struct safety_memory_header header; + int res; + + if (!status) + return -1001; + + if (safety_memory_get_header(&header) != SAFETY_MEMORY_INIT_VALID_MEMORY) { + return -2000; + } + + if (safety_memory_check_crc()) + return -2001; + + res = backup_ram_write_data(header.boot_status_offset, (uint32_t *)status, wordsize_of(*status)); + res |= safety_memory_gen_crc(); + if (res) + return -3000; + + return 0; +} int safety_memory_get_error_entry_count(uint32_t *count);