From 65edf01ca261c6b9dcedb19b4d132494ad2c4905 Mon Sep 17 00:00:00 2001 From: kyz Date: Thu, 25 Jun 2026 14:50:16 +0800 Subject: [PATCH] heap scan, stack overflow check --- Aging_app/freertos/inc/FreeRTOSConfig.h | 6 ++-- Aging_app/freertos/src/main.c | 20 ++++++++++++ Aging_app/freertos/src/task_lists.c | 43 +++++++++++++++++++++++-- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/Aging_app/freertos/inc/FreeRTOSConfig.h b/Aging_app/freertos/inc/FreeRTOSConfig.h index 2eac637..5183ad2 100644 --- a/Aging_app/freertos/inc/FreeRTOSConfig.h +++ b/Aging_app/freertos/inc/FreeRTOSConfig.h @@ -76,15 +76,15 @@ PRIORITY THAN THIS! (higher priorities are lower numeric values. */ /* Hook function related definitions. */ #define configUSE_IDLE_HOOK 0 #define configUSE_TICK_HOOK 0 -#define configUSE_MALLOC_FAILED_HOOK 0 -#define configCHECK_FOR_STACK_OVERFLOW 0 +#define configUSE_MALLOC_FAILED_HOOK 1 +#define configCHECK_FOR_STACK_OVERFLOW 2 #define configUSE_DAEMON_TASK_STARTUP_HOOK 0 /* Run time and task stats gathering related definitions. */ #define configGENERATE_RUN_TIME_STATS 0 #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() #define portGET_RUN_TIME_COUNTER_VALUE() xTaskGetTickCount() -#define configUSE_TRACE_FACILITY 0 +#define configUSE_TRACE_FACILITY 1 #define configUSE_STATS_FORMATTING_FUNCTIONS 1 /* Co-routine related definitions. */ diff --git a/Aging_app/freertos/src/main.c b/Aging_app/freertos/src/main.c index 4ab3148..2aba253 100644 --- a/Aging_app/freertos/src/main.c +++ b/Aging_app/freertos/src/main.c @@ -187,3 +187,23 @@ void TempCallBackTask(void) { Temp_Now = Adc_Collect_Temp(); } + +//----------------------------------- +// freertos/src/heap_stats.c + +#include "FreeRTOS.h" +#include "task.h" +#include + +/* Õ»Òç³ö¹³×Óº¯Êý */ +void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName ) +{ + /* ¿ÉÒÔÌí¼ÓÆäËû´¦ÀíÂß¼­£¬È縴λϵͳ */ + for( ;; ); +} + +/* mallocʧ°Ü¹³×Óº¯Êý */ +void vApplicationMallocFailedHook( void ) +{ + for( ;; ); +} diff --git a/Aging_app/freertos/src/task_lists.c b/Aging_app/freertos/src/task_lists.c index 02d8052..eb57aab 100644 --- a/Aging_app/freertos/src/task_lists.c +++ b/Aging_app/freertos/src/task_lists.c @@ -86,6 +86,10 @@ static void queueHeartBeatTask(void *parames) for( ;; ) { + { + void mem_scan( void ); + mem_scan(); + } xSemaphoreTake(xSemaphoreTx, portMAX_DELAY); //strcpy((char *)uart0TxBuff, "$CMSGH,<"__DEVICES_ID__">,$#"); sprintf((char *)uart0TxBuff, "$CMSGH,<%s>,$#", g_devinfo.serial); @@ -919,9 +923,42 @@ void Tasks_Create(void) xTaskCreate(queueCommandTask, "Command", configMINIMAL_STACK_SIZE+128, NULL, 1, &Command_Handler); xTaskCreate(queueGatherTask1, "Gather1", configMINIMAL_STACK_SIZE+128, NULL, 1, &Gather_Handler1); xTaskCreate(queueGatherTask2, "Gather2", configMINIMAL_STACK_SIZE+128, NULL, 1, &Gather_Handler2); - xTaskCreate(queueSendTask, "Senddata", configMINIMAL_STACK_SIZE, NULL, 1, &Send_Handler); - xTaskCreate(queueHeartBeatTask, "Heartbeat", configMINIMAL_STACK_SIZE, NULL, 1, &HeartBeat_Handler); - xTaskCreate(queueUserDaemonTask, "UserDaemon", configMINIMAL_STACK_SIZE, NULL, 1, &UserDaemon_Handler); + xTaskCreate(queueSendTask, "Senddata", configMINIMAL_STACK_SIZE+32, NULL, 1, &Send_Handler); + xTaskCreate(queueHeartBeatTask, "Heartbeat", configMINIMAL_STACK_SIZE+32, NULL, 1, &HeartBeat_Handler); + xTaskCreate(queueUserDaemonTask, "UserDaemon", configMINIMAL_STACK_SIZE+32, NULL, 1, &UserDaemon_Handler); xTaskCreate(parameterTask, "Parameter", configMINIMAL_STACK_SIZE+128, NULL, 1, &Parameter_Handler); } +// freertos/src/task_mem_stats.c + +#include "FreeRTOS.h" +#include "task.h" +//#include "heap_4.h" /* Èç¹ûʹÓÃÆäËûheap·½°¸£¬°üº¬¶ÔÓ¦µÄÍ·Îļþ */ +#include + +volatile size_t xFreeHeapSize; +volatile size_t xMinimumEverFreeHeapSize; +UBaseType_t uxHighWaterMark[10]; +/* ÄÚ´æÍ³¼ÆÈÎÎñ */ +void mem_scan( void ) +{ + /* »ñÈ¡¶ÑÄÚ´æÐÅÏ¢ */ + xFreeHeapSize = xPortGetFreeHeapSize(); + xMinimumEverFreeHeapSize = xPortGetMinimumEverFreeHeapSize(); + + uxHighWaterMark[0] = uxTaskGetStackHighWaterMark(Command_Handler); + uxHighWaterMark[1] = uxTaskGetStackHighWaterMark(Gather_Handler1); + uxHighWaterMark[2] = uxTaskGetStackHighWaterMark(Gather_Handler2); + uxHighWaterMark[3] = uxTaskGetStackHighWaterMark(Send_Handler); + uxHighWaterMark[4] = uxTaskGetStackHighWaterMark(HeartBeat_Handler); + uxHighWaterMark[5] = uxTaskGetStackHighWaterMark(UserDaemon_Handler); + + /* ´òÓ¡ÄÚ´æÐÅÏ¢ */ + //printf("========================================\r\n"); + //printf("ÄÚ´æÍ³¼ÆÐÅÏ¢ (ÿ5Ãë)\r\n"); + //printf("----------------------------------------\r\n"); + //printf("µ±Ç°¿ÕÏжÑÄÚ´æ: %u bytes\r\n", (unsigned int)xFreeHeapSize); + //printf("ÀúÊ·×îС¿ÕÏжÑ: %u bytes\r\n", (unsigned int)xMinimumEverFreeHeapSize); + //printf("ÒÑʹÓöÑÄÚ´æ: %u bytes\r\n", + // (unsigned int)(configTOTAL_HEAP_SIZE - xFreeHeapSize)); +} -- 2.34.1