From 7aa0b62012ce9f8c89b917a6f8d40ddda13a8ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sun, 1 Nov 2020 20:43:59 +0100 Subject: [PATCH] Implement saftey memory dump feature and increase heap space --- stm-firmware/Makefile | 2 + .../reflow-controller/safety/safety-memory.h | 10 +++ stm-firmware/include/stm-periph/backup-ram.h | 2 + stm-firmware/safety/safety-memory.c | 27 +++++++ stm-firmware/shell.c | 75 ++++++++++++++++++- stm-firmware/stm-periph/backup-ram.c | 5 ++ stm-firmware/stm32f407vet6_flash.ld | 2 +- 7 files changed, 121 insertions(+), 2 deletions(-) diff --git a/stm-firmware/Makefile b/stm-firmware/Makefile index e126dd8..bcd0508 100644 --- a/stm-firmware/Makefile +++ b/stm-firmware/Makefile @@ -54,6 +54,8 @@ INCLUDEPATH += -Iconfig-parser/include CFILES += base64-lib/src/base64-lib.c INCLUDEPATH += -Ibase64-lib/include +DEFINES += -DBASE64_LOOKUP_TABLE_SECTION="\".ccm.bss\"" + DEBUG_DEFINES = -DDEBUGBUILD RELEASE_DEFINES = diff --git a/stm-firmware/include/reflow-controller/safety/safety-memory.h b/stm-firmware/include/reflow-controller/safety/safety-memory.h index e28f3fc..81a6965 100644 --- a/stm-firmware/include/reflow-controller/safety/safety-memory.h +++ b/stm-firmware/include/reflow-controller/safety/safety-memory.h @@ -22,6 +22,7 @@ #define __SAFETY_MEMORY_H__ #include +#include /** @addtogroup safety-memory * @{ @@ -245,6 +246,15 @@ int safety_memory_get_config_override_count(uint32_t *count); */ int safety_memory_get_config_override(uint32_t idx, struct config_override *config_override); +/** + * @brief Get a base64 dump of the whole safety memory. + * @param[out] buffer Buffer to write the base 64 dump into. + * @param buffsize Size of buffer. Must be large enough to hold the data plus a '\0' terminator + * @param[out] used_size Number of written bytes including the '\0' terminator. May be NULL. + * @return 0 if successful + */ +int safety_memory_dump_base64(char *buffer, size_t buffsize, size_t *used_size); + #endif /* __SAFETY_MEMORY_H__ */ /** @} */ diff --git a/stm-firmware/include/stm-periph/backup-ram.h b/stm-firmware/include/stm-periph/backup-ram.h index 2d33cd6..e77ec32 100644 --- a/stm-firmware/include/stm-periph/backup-ram.h +++ b/stm-firmware/include/stm-periph/backup-ram.h @@ -55,3 +55,5 @@ int backup_ram_get_data(uint32_t addr, uint32_t *data, uint32_t count); int backup_ram_write_data(uint32_t addr, const uint32_t *data, uint32_t count); uint32_t backup_ram_get_size_in_words(void); + +volatile void *backup_ram_get_base_ptr(void); diff --git a/stm-firmware/safety/safety-memory.c b/stm-firmware/safety/safety-memory.c index c0ab3a6..23d3ecb 100644 --- a/stm-firmware/safety/safety-memory.c +++ b/stm-firmware/safety/safety-memory.c @@ -22,6 +22,7 @@ #include #include #include +#include static int word_to_error_memory_entry(uint32_t entry_data, struct error_memory_entry *out) { @@ -481,3 +482,29 @@ int safety_memory_insert_config_override(struct config_override *config_override int safety_memory_get_config_override_count(uint32_t *count); int safety_memory_get_config_override(uint32_t idx, struct config_override *config_override); + +int safety_memory_dump_base64(char *buffer, size_t buffsize, size_t *used_size) +{ + uint32_t safety_mem_size; + size_t output_size; + const char *backup_mem_ptr; + int res; + + if (!buffer) + return -1000; + + safety_mem_size = backup_ram_get_size_in_words() * 4U; + output_size = base64_calculate_encoded_size(safety_mem_size); + + if (output_size + 1 > buffsize) + return -1001; + + backup_mem_ptr = (const char *)backup_ram_get_base_ptr(); + res = base64_encode(backup_mem_ptr, buffer, safety_mem_size, buffsize, &output_size); + if (res) + return -1; + buffer[output_size] = '\0'; + if (used_size) + *used_size = output_size + 1u; + return 0; +} diff --git a/stm-firmware/shell.c b/stm-firmware/shell.c index 24ca4ff..5d1a9fe 100644 --- a/stm-firmware/shell.c +++ b/stm-firmware/shell.c @@ -39,6 +39,7 @@ #include #include #include +#include #ifndef GIT_VER #define GIT_VER "VERSION NOT SET" @@ -546,6 +547,70 @@ static shellmatta_retCode_t shell_cmd_panic(const shellmatta_handle_t handle, co return SHELLMATTA_OK; } +static char *get_safety_mem_dump(size_t *used_bytes) +{ + char *buffer; + int res; + + buffer = (char *)malloc(5470); + + res = safety_memory_dump_base64(buffer, 5470UL, used_bytes); + if (res) { + if (buffer) + free(buffer); + buffer = NULL; + } + + return buffer; +} + +static shellmatta_retCode_t shell_cmd_dump_safety_mem(const shellmatta_handle_t handle, const char *arguments, + uint32_t length) +{ + (void)arguments; + (void)length; + static char *buffer; + static const char *ptr; + size_t used_bytes; + static size_t full_lines = 0; + static size_t current_line; + size_t remainder; + static const char *hline = "----------------------------------------------------------------"; + + if (full_lines == 0) { + shellmatta_printf(handle, "Safety memory content\r\n%s\r\n", hline); + + buffer = get_safety_mem_dump(&used_bytes); + if (!buffer) { + shellmatta_printf(handle, "Error dumping memory!\r\n"); + return SHELLMATTA_OK; + } + + full_lines = (used_bytes - 1) / 64; + remainder = (used_bytes - 1) % 64; + if (remainder) + full_lines++; + + ptr = buffer; + current_line = 0; + return SHELLMATTA_BUSY; + } else { + if (current_line < full_lines) { + shellmatta_printf(handle, "%.64s\r\n", ptr); + ptr += 64; + current_line++; + } else { + shellmatta_printf(handle, "%s\r\n", hline); + full_lines = 0; + if (buffer) + free(buffer); + buffer = NULL; + return SHELLMATTA_OK; + } + } + return SHELLMATTA_BUSY; +} + //typedef struct shellmatta_cmd //{ // char *cmd; /**< command name */ @@ -555,7 +620,7 @@ static shellmatta_retCode_t shell_cmd_panic(const shellmatta_handle_t handle, co // shellmatta_cmdFct_t cmdFct; /**< pointer to the cmd callack function */ // struct shellmatta_cmd *next; /**< pointer to next command or NULL */ //} shellmatta_cmd_t; -static shellmatta_cmd_t cmd[17] = { +static shellmatta_cmd_t cmd[18] = { { .cmd = "version", .cmdAlias = "ver", @@ -690,6 +755,14 @@ static shellmatta_cmd_t cmd[17] = { .helpText = "Panic Mode!", .usageText = "", .cmdFct = shell_cmd_panic, + .next = &cmd[17], + }, + { + .cmd = "safety-mem-dump", + .cmdAlias = NULL, + .helpText = "", + .usageText = "", + .cmdFct = shell_cmd_dump_safety_mem, .next = NULL, }, }; diff --git a/stm-firmware/stm-periph/backup-ram.c b/stm-firmware/stm-periph/backup-ram.c index 720f004..176bde7 100644 --- a/stm-firmware/stm-periph/backup-ram.c +++ b/stm-firmware/stm-periph/backup-ram.c @@ -107,3 +107,8 @@ uint32_t backup_ram_get_size_in_words(void) { return (uint32_t)BACKUP_RAM_SIZE_WORDS; } + +volatile void *backup_ram_get_base_ptr(void) +{ + return backup_ram; +} diff --git a/stm-firmware/stm32f407vet6_flash.ld b/stm-firmware/stm32f407vet6_flash.ld index 10e0d90..7a7d8b5 100644 --- a/stm-firmware/stm32f407vet6_flash.ld +++ b/stm-firmware/stm32f407vet6_flash.ld @@ -24,7 +24,7 @@ /* USER PARAMETERS */ __ld_stack_size = 0x3000; -__ld_heap_size = 0x2100; +__ld_heap_size = 0x4200; __stack_corruption_area_size = 128; /* END OF USER PARAMETERS */