From 71a44a828405d186e1ca1790372c7e41da92466e Mon Sep 17 00:00:00 2001 From: kbracher Date: Fri, 6 Jun 2025 18:29:21 +0200 Subject: [PATCH] track PC, PSR across WWDG resets --- AMS_Master_Code/Core/Inc/main.h | 6 +++++- AMS_Master_Code/Core/Src/main.c | 13 +++++++++++++ AMS_Master_Code/Core/Src/stm32h7xx_it.c | 9 ++++++--- AMS_Master_Code/STM32H7A3XX_FLASH.ld | 7 +++++++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/AMS_Master_Code/Core/Inc/main.h b/AMS_Master_Code/Core/Inc/main.h index dc8eb58..626c76e 100644 --- a/AMS_Master_Code/Core/Inc/main.h +++ b/AMS_Master_Code/Core/Inc/main.h @@ -50,6 +50,10 @@ extern int pos_air_closed; extern int precharge_closed; extern int pre_and_air_open; +extern volatile uint32_t wwdg_flag; +extern volatile uint32_t wwdg_pc; // PC at the time of WWDG interrupt +extern volatile uint32_t wwdg_psr; // PSR at the time of WWDG interrupt + /* USER CODE END EC */ /* Exported macro ------------------------------------------------------------*/ @@ -123,7 +127,7 @@ void Error_Handler(void); #define WAKE2_GPIO_Port GPIOB /* USER CODE BEGIN Private defines */ - +#define WWDG_TRIGGERED_FLAG 0xDEADCAFE // Flag to indicate WWDG was triggered /* USER CODE END Private defines */ #ifdef __cplusplus diff --git a/AMS_Master_Code/Core/Src/main.c b/AMS_Master_Code/Core/Src/main.c index bc0c967..028baad 100644 --- a/AMS_Master_Code/Core/Src/main.c +++ b/AMS_Master_Code/Core/Src/main.c @@ -82,6 +82,15 @@ int pos_air_closed; int precharge_closed; int pre_and_air_open; // used to be:int precharge_opened = 0; now we read if PC is open from the pin -> high if open +/* Variables for WWDG interrupt handling */ +[[gnu::section(".noinit")]] // keep these variables in .noinit section to preserve their values across resets +volatile uint32_t wwdg_flag; // Flag to indicate WWDG was triggered +[[gnu::section(".noinit")]] +volatile uint32_t wwdg_pc; // PC at the time of WWDG interrupt +[[gnu::section(".noinit")]] +volatile uint32_t wwdg_psr; // PSR at the time of WWDG interrupt + + /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -220,7 +229,11 @@ int main(void) debug_log(LOG_LEVEL_INFO, "Starting BMS..."); if (wdg_reset) { debug_log(LOG_LEVEL_WARNING, "Last reset was caused by watchdog!"); + if (wwdg_flag == WWDG_TRIGGERED_FLAG) { + debug_log(LOG_LEVEL_WARNING, "WWDG was triggered! PC: 0x%08lX, PSR: 0x%08lX", wwdg_pc, wwdg_psr); + } } + wwdg_flag = 0; // clear the flag auto ret = battery_init(&hspi1); while (ret != HAL_OK) { //TODO: properly handle reapeated initialization failure diff --git a/AMS_Master_Code/Core/Src/stm32h7xx_it.c b/AMS_Master_Code/Core/Src/stm32h7xx_it.c index c5b1f78..c94661d 100644 --- a/AMS_Master_Code/Core/Src/stm32h7xx_it.c +++ b/AMS_Master_Code/Core/Src/stm32h7xx_it.c @@ -22,6 +22,7 @@ #include "stm32h7xx_it.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ +#include "cmsis_gcc.h" #include "log.h" /* USER CODE END Includes */ @@ -208,9 +209,11 @@ void WWDG_IRQHandler(void) /* USER CODE BEGIN WWDG_IRQn 0 */ // Grab PC from the stack to log it uint32_t *stack_pointer = (uint32_t *)__get_MSP(); - uint32_t prev_pc = stack_pointer[6]; // PC is the 7th word in the stack frame - uint32_t prev_psr = stack_pointer[7]; // PSR is the 8th word in the stack frame - log_fatal("Watchdog triggered! PC: 0x%08lX, PSR: 0x%08lX", prev_pc, prev_psr); + wwdg_pc = stack_pointer[6]; // PC is the 7th word in the stack frame + wwdg_psr = stack_pointer[7]; // PSR is the 8th word in the stack frame + wwdg_flag = WWDG_TRIGGERED_FLAG; // Set the flag to indicate WWDG was triggered + log_fatal("Watchdog triggered! PC: 0x%08lX, PSR: 0x%08lX", wwdg_pc, wwdg_psr); + __DSB(); // Ensure ram is written to before entering the handler /* USER CODE END WWDG_IRQn 0 */ HAL_WWDG_IRQHandler(&hwwdg1); /* USER CODE BEGIN WWDG_IRQn 1 */ diff --git a/AMS_Master_Code/STM32H7A3XX_FLASH.ld b/AMS_Master_Code/STM32H7A3XX_FLASH.ld index 2b39280..77cfacd 100644 --- a/AMS_Master_Code/STM32H7A3XX_FLASH.ld +++ b/AMS_Master_Code/STM32H7A3XX_FLASH.ld @@ -183,6 +183,13 @@ SECTIONS __bss_end__ = _ebss; } >DTCMRAM + /* Keep some data across soft resets */ + . = ALIGN(4); + .no_init : + { + *(.no_init) + } >DTCMRAM + /* User_heap_stack section, used to check that there is enough RAM left */ ._user_heap_stack : {