diff --git a/Core/Inc/FreeRTOSConfig.h b/Core/Inc/FreeRTOSConfig.h index 73fc8b8..824ce61 100644 --- a/Core/Inc/FreeRTOSConfig.h +++ b/Core/Inc/FreeRTOSConfig.h @@ -44,6 +44,7 @@ /* USER CODE BEGIN Includes */ /* Section where include file can be added */ +#include "debug_log.h" /* USER CODE END Includes */ /* 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 header file. */ /* 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 */ /* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS diff --git a/Core/Inc/debug_log.h b/Core/Inc/debug_log.h index 531affd..b660374 100644 --- a/Core/Inc/debug_log.h +++ b/Core/Inc/debug_log.h @@ -17,6 +17,7 @@ void debug_log_printf(const char *fmt, ...); void debug_log_boot(const char *tag); void debug_log_fault(const char *tag); void debug_log_runtime_snapshot(void); +void debug_log_fault_context(const char *tag, const char *file, int line); #ifdef __cplusplus } diff --git a/Core/Inc/iwdg.h b/Core/Inc/iwdg.h index 474e114..dec6d5e 100644 --- a/Core/Inc/iwdg.h +++ b/Core/Inc/iwdg.h @@ -38,7 +38,7 @@ extern IWDG_HandleTypeDef hiwdg; /* USER CODE END Private defines */ -void MX_IWDG_Init(void); +HAL_StatusTypeDef MX_IWDG_Init(void); /* USER CODE BEGIN Prototypes */ diff --git a/Core/Src/debug_log.c b/Core/Src/debug_log.c index 38a4103..dbcc94e 100644 --- a/Core/Src/debug_log.c +++ b/Core/Src/debug_log.c @@ -75,6 +75,19 @@ void debug_log_fault(const char *tag) 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) { UBaseType_t default_hwm; diff --git a/Core/Src/freertos.c b/Core/Src/freertos.c index b5d0aa4..c4fa22a 100644 --- a/Core/Src/freertos.c +++ b/Core/Src/freertos.c @@ -89,13 +89,23 @@ static void StartDefaultTask(void *argument) { TickType_t last_snapshot = xTaskGetTickCount(); BaseType_t alive_logged = pdFALSE; + BaseType_t iwdg_ready = pdFALSE; (void)argument; 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 (;;) { HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); - HAL_IWDG_Refresh(&hiwdg); + if (iwdg_ready == pdTRUE) { + HAL_IWDG_Refresh(&hiwdg); + } if (alive_logged == pdFALSE) { debug_log_write("[RTOS] alive\r\n"); alive_logged = pdTRUE; diff --git a/Core/Src/iwdg.c b/Core/Src/iwdg.c index 6d08c97..af1a482 100644 --- a/Core/Src/iwdg.c +++ b/Core/Src/iwdg.c @@ -27,7 +27,7 @@ IWDG_HandleTypeDef hiwdg; /* IWDG init function */ -void MX_IWDG_Init(void) +HAL_StatusTypeDef MX_IWDG_Init(void) { /* USER CODE BEGIN IWDG_Init 0 */ @@ -42,12 +42,12 @@ void MX_IWDG_Init(void) hiwdg.Init.Reload = 4095; if (HAL_IWDG_Init(&hiwdg) != HAL_OK) { - Error_Handler(); + return HAL_ERROR; } /* USER CODE BEGIN IWDG_Init 2 */ /* USER CODE END IWDG_Init 2 */ - + return HAL_OK; } /* USER CODE BEGIN 1 */ diff --git a/Core/Src/main.c b/Core/Src/main.c index 09d7da3..30cb793 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -142,7 +142,6 @@ int main(void) /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); - MX_IWDG_Init(); MX_USART1_UART_Init(); MX_USART2_UART_Init(); MX_USART3_UART_Init(); @@ -231,7 +230,28 @@ void SystemClock_Config(void) */ 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 */ @@ -244,7 +264,7 @@ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* 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(); while (1) {