From d3bdb987815fbcbdbc3fc5a9457faf2643c72116 Mon Sep 17 00:00:00 2001 From: Konrad Beckmann Date: Fri, 8 Jan 2021 16:04:20 +0100 Subject: [PATCH] Persist and show log after watchdog reset --- Core/Inc/main.h | 5 +++++ Core/Src/main.c | 33 ++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Core/Inc/main.h b/Core/Inc/main.h index dbc80359..fb777535 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -44,7 +44,10 @@ typedef enum { BSOD_MEMFAULT, BSOD_BUSFAULT, BSOD_USAGEFAULT, + BSOD_WATCHDOG, BSOD_OTHER, + + BSOD_COUNT, } BSOD_t; /* USER CODE END ET */ @@ -143,6 +146,8 @@ void wdog_refresh(void); #define BOOT_MAGIC_WATCHDOG 0xd066cafe #define BOOT_MAGIC_BSOD 0xbad00000 +#define BOOT_MAGIC_BSOD_MASK 0xffff0000 + /* USER CODE END Private defines */ #ifdef __cplusplus diff --git a/Core/Src/main.c b/Core/Src/main.c index 1e2605b2..49c06a2e 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -71,12 +71,12 @@ WWDG_HandleTypeDef hwwdg1; /* USER CODE BEGIN PV */ -char logbuf[1024 * 4] __attribute__((aligned(4))); -uint32_t log_idx; +char logbuf[1024 * 4] __attribute__((section (".persistent"))) __attribute__((aligned(4))); +uint32_t log_idx __attribute__((section (".persistent"))); +__attribute__((used)) __attribute__((section (".persistent"))) volatile uint32_t boot_magic; uint32_t boot_buttons; -__attribute__((used)) __attribute__((section (".persistent"))) volatile uint32_t boot_magic; /* USER CODE END PV */ @@ -110,6 +110,7 @@ const char *fault_list[] = { [BSOD_MEMFAULT] = "Memfault", [BSOD_BUSFAULT] = "Busfault", [BSOD_USAGEFAULT] = "Usagefault", + [BSOD_WATCHDOG] = "Watchdog", [BSOD_OTHER] = "Other", }; @@ -327,6 +328,7 @@ void wdog_refresh() int main(void) { /* USER CODE BEGIN 1 */ + uint8_t trigger_wdt_bsod = 0; for(int i = 0; i < 1000000; i++) { __NOP(); @@ -338,18 +340,31 @@ int main(void) memset(0x0, '\x41', (size_t)&__NULLPTR_LENGTH__); #pragma GCC diagnostic pop + // Don't reset the logbuf when rebooting from a watchdog reset + if (boot_magic != BOOT_MAGIC_WATCHDOG) { + log_idx = 0; + logbuf[0] = '\0'; + } + switch (boot_magic) { case BOOT_MAGIC_STANDBY: - printf("Boot from standby. boot_magic=0x%08lx\n", boot_magic); + printf("Boot from standby.\nboot_magic=0x%08lx\n", boot_magic); break; case BOOT_MAGIC_RESET: - printf("Boot from warm reset. boot_magic=0x%08lx\n", boot_magic); + printf("Boot from warm reset.\nboot_magic=0x%08lx\n", boot_magic); break; case BOOT_MAGIC_WATCHDOG: - printf("Boot from watchdog reset! boot_magic=0x%08lx\n", boot_magic); + printf("Boot from watchdog reset!\nboot_magic=0x%08lx\n", boot_magic); + trigger_wdt_bsod = 1; break; default: - printf("Boot from brownout? boot_magic=0x%08lx\n", boot_magic); + if ((boot_magic & BOOT_MAGIC_BSOD_MASK) == BOOT_MAGIC_BSOD) { + uint16_t fault_idx = boot_magic & 0xffff; + const char *fault = (fault_idx < BSOD_COUNT) ? fault_list[fault_idx] : "UNKOWN"; + printf("Boot from BSOD:\nboot_magic=0x%08lx %s\n", boot_magic, fault); + } else { + printf("Boot from brownout?\nboot_magic=0x%08lx\n", boot_magic); + } break; } @@ -407,6 +422,10 @@ int main(void) lcd_init(&hspi2, &hltdc); + if (trigger_wdt_bsod) { + BSOD(BSOD_WATCHDOG, 0, 0); + } + /* USER CODE END 2 */ /* Infinite loop */