fix: improve boot fault visibility and defer watchdog start

This commit is contained in:
2026-04-20 08:12:06 +08:00
parent 77da670f5c
commit c8f27e21f1
7 changed files with 54 additions and 9 deletions
+2 -1
View File
@@ -44,6 +44,7 @@
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
/* Section where include file can be added */ /* Section where include file can be added */
#include "debug_log.h"
/* USER CODE END Includes */ /* USER CODE END Includes */
/* Ensure definitions are only used by the compiler, and not by the assembler. */ /* Ensure definitions are only used by the compiler, and not by the assembler. */
@@ -137,7 +138,7 @@ See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
/* Normal assert() semantics without relying on the provision of an assert.h /* Normal assert() semantics without relying on the provision of an assert.h
header file. */ header file. */
/* USER CODE BEGIN 1 */ /* USER CODE BEGIN 1 */
#define configASSERT( x ) if ((x) == 0) {taskDISABLE_INTERRUPTS(); for( ;; );} #define configASSERT( x ) do { if ((x) == 0) { debug_log_fault_context("config-assert", __FILE__, __LINE__); taskDISABLE_INTERRUPTS(); for( ;; ) { } } } while (0)
/* USER CODE END 1 */ /* USER CODE END 1 */
/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS /* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
+1
View File
@@ -17,6 +17,7 @@ void debug_log_printf(const char *fmt, ...);
void debug_log_boot(const char *tag); void debug_log_boot(const char *tag);
void debug_log_fault(const char *tag); void debug_log_fault(const char *tag);
void debug_log_runtime_snapshot(void); void debug_log_runtime_snapshot(void);
void debug_log_fault_context(const char *tag, const char *file, int line);
#ifdef __cplusplus #ifdef __cplusplus
} }
+1 -1
View File
@@ -38,7 +38,7 @@ extern IWDG_HandleTypeDef hiwdg;
/* USER CODE END Private defines */ /* USER CODE END Private defines */
void MX_IWDG_Init(void); HAL_StatusTypeDef MX_IWDG_Init(void);
/* USER CODE BEGIN Prototypes */ /* USER CODE BEGIN Prototypes */
+13
View File
@@ -75,6 +75,19 @@ void debug_log_fault(const char *tag)
debug_log_printf("[FAULT] %s\r\n", (tag != NULL) ? tag : "(null)"); debug_log_printf("[FAULT] %s\r\n", (tag != NULL) ? tag : "(null)");
} }
void debug_log_fault_context(const char *tag, const char *file, int line)
{
debug_log_printf("[FAULT] %s file=%s line=%d free=%lu min=%lu seq=%lu drop=%lu last_drop=%lu\r\n",
(tag != NULL) ? tag : "(null)",
(file != NULL) ? file : "(null)",
line,
(unsigned long)xPortGetFreeHeapSize(),
(unsigned long)xPortGetMinimumEverFreeHeapSize(),
(unsigned long)g_rtt_log_seq,
(unsigned long)g_rtt_log_drop_count,
(unsigned long)g_rtt_log_last_drop_seq);
}
void debug_log_runtime_snapshot(void) void debug_log_runtime_snapshot(void)
{ {
UBaseType_t default_hwm; UBaseType_t default_hwm;
+11 -1
View File
@@ -89,13 +89,23 @@ static void StartDefaultTask(void *argument)
{ {
TickType_t last_snapshot = xTaskGetTickCount(); TickType_t last_snapshot = xTaskGetTickCount();
BaseType_t alive_logged = pdFALSE; BaseType_t alive_logged = pdFALSE;
BaseType_t iwdg_ready = pdFALSE;
(void)argument; (void)argument;
debug_log_boot("default-task"); debug_log_boot("default-task");
if (MX_IWDG_Init() == HAL_OK) {
debug_log_write("[BOOT] iwdg-started\r\n");
iwdg_ready = pdTRUE;
} else {
debug_log_write("[BOOT] iwdg-init-fail\r\n");
}
for (;;) { for (;;) {
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
HAL_IWDG_Refresh(&hiwdg); if (iwdg_ready == pdTRUE) {
HAL_IWDG_Refresh(&hiwdg);
}
if (alive_logged == pdFALSE) { if (alive_logged == pdFALSE) {
debug_log_write("[RTOS] alive\r\n"); debug_log_write("[RTOS] alive\r\n");
alive_logged = pdTRUE; alive_logged = pdTRUE;
+3 -3
View File
@@ -27,7 +27,7 @@
IWDG_HandleTypeDef hiwdg; IWDG_HandleTypeDef hiwdg;
/* IWDG init function */ /* IWDG init function */
void MX_IWDG_Init(void) HAL_StatusTypeDef MX_IWDG_Init(void)
{ {
/* USER CODE BEGIN IWDG_Init 0 */ /* USER CODE BEGIN IWDG_Init 0 */
@@ -42,12 +42,12 @@ void MX_IWDG_Init(void)
hiwdg.Init.Reload = 4095; hiwdg.Init.Reload = 4095;
if (HAL_IWDG_Init(&hiwdg) != HAL_OK) if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
{ {
Error_Handler(); return HAL_ERROR;
} }
/* USER CODE BEGIN IWDG_Init 2 */ /* USER CODE BEGIN IWDG_Init 2 */
/* USER CODE END IWDG_Init 2 */ /* USER CODE END IWDG_Init 2 */
return HAL_OK;
} }
/* USER CODE BEGIN 1 */ /* USER CODE BEGIN 1 */
+23 -3
View File
@@ -142,7 +142,6 @@ int main(void)
/* Initialize all configured peripherals */ /* Initialize all configured peripherals */
MX_GPIO_Init(); MX_GPIO_Init();
MX_DMA_Init(); MX_DMA_Init();
MX_IWDG_Init();
MX_USART1_UART_Init(); MX_USART1_UART_Init();
MX_USART2_UART_Init(); MX_USART2_UART_Init();
MX_USART3_UART_Init(); MX_USART3_UART_Init();
@@ -231,7 +230,28 @@ void SystemClock_Config(void)
*/ */
void Debug_TrapWithRttHint(const char *tag) void Debug_TrapWithRttHint(const char *tag)
{ {
debug_log_fault(tag); debug_log_fault_context(tag, __FILE__, __LINE__);
}
void vApplicationMallocFailedHook(void)
{
debug_log_fault_context("malloc-failed", __FILE__, __LINE__);
__disable_irq();
for (;;)
{
}
}
void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName)
{
debug_log_printf("[FAULT] stack-overflow task=%s\r\n",
(pcTaskName != NULL) ? pcTaskName : "(null)");
debug_log_fault_context("stack-overflow", __FILE__, __LINE__);
(void)xTask;
__disable_irq();
for (;;)
{
}
} }
/* USER CODE END 4 */ /* USER CODE END 4 */
@@ -244,7 +264,7 @@ void Error_Handler(void)
{ {
/* USER CODE BEGIN Error_Handler_Debug */ /* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */ /* User can add his own implementation to report the HAL error return state */
debug_log_fault("error-handler"); debug_log_fault_context("error-handler", __FILE__, __LINE__);
__disable_irq(); __disable_irq();
while (1) while (1)
{ {