Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
Persist and show log after watchdog reset
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeckmann committed Jan 8, 2021
1 parent 9359d8b commit d3bdb98
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
5 changes: 5 additions & 0 deletions Core/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ typedef enum {
BSOD_MEMFAULT,
BSOD_BUSFAULT,
BSOD_USAGEFAULT,
BSOD_WATCHDOG,
BSOD_OTHER,

BSOD_COUNT,
} BSOD_t;

/* USER CODE END ET */
Expand Down Expand Up @@ -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
Expand Down
33 changes: 26 additions & 7 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -110,6 +110,7 @@ const char *fault_list[] = {
[BSOD_MEMFAULT] = "Memfault",
[BSOD_BUSFAULT] = "Busfault",
[BSOD_USAGEFAULT] = "Usagefault",
[BSOD_WATCHDOG] = "Watchdog",
[BSOD_OTHER] = "Other",
};

Expand Down Expand Up @@ -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();
Expand All @@ -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;
}

Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit d3bdb98

Please sign in to comment.