From 611e0ff7684818949f7f18546b540ff55de9b8ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Milants?= Date: Sun, 26 Mar 2023 14:53:34 +0200 Subject: [PATCH] Enable malloc error and stack overflow error detection in FreeRTOS. Count them and display them in the SystemInfo app. --- src/FreeRTOSConfig.h | 4 ++-- src/displayapp/screens/SystemInfo.cpp | 23 +++++++++++++---------- src/main.cpp | 11 +++++++++++ src/main.h | 5 ++++- src/recoveryLoader.cpp | 12 ++++++++++++ 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/FreeRTOSConfig.h b/src/FreeRTOSConfig.h index ab0cf1ba..cf18f418 100644 --- a/src/FreeRTOSConfig.h +++ b/src/FreeRTOSConfig.h @@ -79,8 +79,8 @@ /* Hook function related definitions. */ #define configUSE_IDLE_HOOK 0 #define configUSE_TICK_HOOK 0 -#define configCHECK_FOR_STACK_OVERFLOW 0 -#define configUSE_MALLOC_FAILED_HOOK 0 +#define configCHECK_FOR_STACK_OVERFLOW 1 +#define configUSE_MALLOC_FAILED_HOOK 1 /* Run time and task stats gathering related definitions. */ #define configGENERATE_RUN_TIME_STATS 0 diff --git a/src/displayapp/screens/SystemInfo.cpp b/src/displayapp/screens/SystemInfo.cpp index d6fa7365..dd15221b 100644 --- a/src/displayapp/screens/SystemInfo.cpp +++ b/src/displayapp/screens/SystemInfo.cpp @@ -177,6 +177,8 @@ std::unique_ptr SystemInfo::CreateScreen2() { return std::make_unique(1, 5, label); } +extern int mallocFailedCount; +extern int stackOverflowCount; std::unique_ptr SystemInfo::CreateScreen3() { lv_mem_monitor_t mon; lv_mem_monitor(&mon); @@ -188,22 +190,23 @@ std::unique_ptr SystemInfo::CreateScreen3() { "#808080 BLE MAC#\n" " %02x:%02x:%02x:%02x:%02x:%02x" "\n" - "#808080 LVGL Memory#\n" - " #808080 used# %d (%d%%)\n" - " #808080 max used# %lu\n" - " #808080 frag# %d%%\n" - " #808080 free# %d", + "\n" + "#808080 Memory heap#\n" + " #808080 Free# %d\n" + " #808080 Min free# %d\n" + " #808080 Alloc err# %d\n" + " #808080 Ovrfl err# %d\n", bleAddr[5], bleAddr[4], bleAddr[3], bleAddr[2], bleAddr[1], bleAddr[0], - static_cast(mon.total_size - mon.free_size), - mon.used_pct, - mon.max_used, - mon.frag_pct, - static_cast(mon.free_biggest_size)); + xPortGetFreeHeapSize(), + xPortGetMinimumEverFreeHeapSize(), + mallocFailedCount, + stackOverflowCount + ); lv_obj_align(label, lv_scr_act(), LV_ALIGN_CENTER, 0, 0); return std::make_unique(2, 5, label); } diff --git a/src/main.cpp b/src/main.cpp index 951365a8..889d3934 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -83,6 +83,7 @@ Pinetime::Drivers::Cst816S touchPanel {twiMaster, touchPanelTwiAddress}; #include "displayapp/DisplayAppRecovery.h" #else #include "displayapp/DisplayApp.h" + #include "main.h" #endif Pinetime::Drivers::Bma421 motionSensor {twiMaster, motionSensorTwiAddress}; Pinetime::Drivers::Hrs3300 heartRateSensor {twiMaster, heartRateSensorTwiAddress}; @@ -144,7 +145,17 @@ Pinetime::System::SystemTask systemTask(spi, fs, touchHandler, buttonHandler); +int mallocFailedCount = 0; +int stackOverflowCount = 0; +extern "C" { +void vApplicationMallocFailedHook() { + mallocFailedCount++; +} +void vApplicationStackOverflowHook(TaskHandle_t /*xTask*/, char */*pcTaskName*/) { + stackOverflowCount++; +} +} /* Variable Declarations for variables in noinit SRAM Increment NoInit_MagicValue upon adding variables to this area */ diff --git a/src/main.h b/src/main.h index 2ff32b8f..4475dc8b 100644 --- a/src/main.h +++ b/src/main.h @@ -5,4 +5,7 @@ #include void nrfx_gpiote_evt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action); -void DebounceTimerCallback(TimerHandle_t xTimer); \ No newline at end of file +void DebounceTimerCallback(TimerHandle_t xTimer); + +extern int mallocFailedCount; +extern int stackOverflowCount; \ No newline at end of file diff --git a/src/recoveryLoader.cpp b/src/recoveryLoader.cpp index d6f8d49b..0a4aa622 100644 --- a/src/recoveryLoader.cpp +++ b/src/recoveryLoader.cpp @@ -139,6 +139,18 @@ void DisplayProgressBar(uint8_t percent, uint16_t color) { } } +int mallocFailedCount = 0; +int stackOverflowCount = 0; +extern "C" { +void vApplicationMallocFailedHook() { + mallocFailedCount++; +} + +void vApplicationStackOverflowHook(TaskHandle_t /*xTask*/, char */*pcTaskName*/) { + stackOverflowCount++; +} +} + int main(void) { TaskHandle_t taskHandle; RefreshWatchdog();