Skip to content

Commit 611e0ff

Browse files
committed
Enable malloc error and stack overflow error detection in FreeRTOS. Count them and display them in the SystemInfo app.
1 parent 1911e2d commit 611e0ff

File tree

5 files changed

+42
-13
lines changed

5 files changed

+42
-13
lines changed

src/FreeRTOSConfig.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979
/* Hook function related definitions. */
8080
#define configUSE_IDLE_HOOK 0
8181
#define configUSE_TICK_HOOK 0
82-
#define configCHECK_FOR_STACK_OVERFLOW 0
83-
#define configUSE_MALLOC_FAILED_HOOK 0
82+
#define configCHECK_FOR_STACK_OVERFLOW 1
83+
#define configUSE_MALLOC_FAILED_HOOK 1
8484

8585
/* Run time and task stats gathering related definitions. */
8686
#define configGENERATE_RUN_TIME_STATS 0

src/displayapp/screens/SystemInfo.cpp

+13-10
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ std::unique_ptr<Screen> SystemInfo::CreateScreen2() {
177177
return std::make_unique<Screens::Label>(1, 5, label);
178178
}
179179

180+
extern int mallocFailedCount;
181+
extern int stackOverflowCount;
180182
std::unique_ptr<Screen> SystemInfo::CreateScreen3() {
181183
lv_mem_monitor_t mon;
182184
lv_mem_monitor(&mon);
@@ -188,22 +190,23 @@ std::unique_ptr<Screen> SystemInfo::CreateScreen3() {
188190
"#808080 BLE MAC#\n"
189191
" %02x:%02x:%02x:%02x:%02x:%02x"
190192
"\n"
191-
"#808080 LVGL Memory#\n"
192-
" #808080 used# %d (%d%%)\n"
193-
" #808080 max used# %lu\n"
194-
" #808080 frag# %d%%\n"
195-
" #808080 free# %d",
193+
"\n"
194+
"#808080 Memory heap#\n"
195+
" #808080 Free# %d\n"
196+
" #808080 Min free# %d\n"
197+
" #808080 Alloc err# %d\n"
198+
" #808080 Ovrfl err# %d\n",
196199
bleAddr[5],
197200
bleAddr[4],
198201
bleAddr[3],
199202
bleAddr[2],
200203
bleAddr[1],
201204
bleAddr[0],
202-
static_cast<int>(mon.total_size - mon.free_size),
203-
mon.used_pct,
204-
mon.max_used,
205-
mon.frag_pct,
206-
static_cast<int>(mon.free_biggest_size));
205+
xPortGetFreeHeapSize(),
206+
xPortGetMinimumEverFreeHeapSize(),
207+
mallocFailedCount,
208+
stackOverflowCount
209+
);
207210
lv_obj_align(label, lv_scr_act(), LV_ALIGN_CENTER, 0, 0);
208211
return std::make_unique<Screens::Label>(2, 5, label);
209212
}

src/main.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Pinetime::Drivers::Cst816S touchPanel {twiMaster, touchPanelTwiAddress};
8383
#include "displayapp/DisplayAppRecovery.h"
8484
#else
8585
#include "displayapp/DisplayApp.h"
86+
#include "main.h"
8687
#endif
8788
Pinetime::Drivers::Bma421 motionSensor {twiMaster, motionSensorTwiAddress};
8889
Pinetime::Drivers::Hrs3300 heartRateSensor {twiMaster, heartRateSensorTwiAddress};
@@ -144,7 +145,17 @@ Pinetime::System::SystemTask systemTask(spi,
144145
fs,
145146
touchHandler,
146147
buttonHandler);
148+
int mallocFailedCount = 0;
149+
int stackOverflowCount = 0;
150+
extern "C" {
151+
void vApplicationMallocFailedHook() {
152+
mallocFailedCount++;
153+
}
147154

155+
void vApplicationStackOverflowHook(TaskHandle_t /*xTask*/, char */*pcTaskName*/) {
156+
stackOverflowCount++;
157+
}
158+
}
148159
/* Variable Declarations for variables in noinit SRAM
149160
Increment NoInit_MagicValue upon adding variables to this area
150161
*/

src/main.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
#include <nrfx_gpiote.h>
66

77
void nrfx_gpiote_evt_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action);
8-
void DebounceTimerCallback(TimerHandle_t xTimer);
8+
void DebounceTimerCallback(TimerHandle_t xTimer);
9+
10+
extern int mallocFailedCount;
11+
extern int stackOverflowCount;

src/recoveryLoader.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ void DisplayProgressBar(uint8_t percent, uint16_t color) {
139139
}
140140
}
141141

142+
int mallocFailedCount = 0;
143+
int stackOverflowCount = 0;
144+
extern "C" {
145+
void vApplicationMallocFailedHook() {
146+
mallocFailedCount++;
147+
}
148+
149+
void vApplicationStackOverflowHook(TaskHandle_t /*xTask*/, char */*pcTaskName*/) {
150+
stackOverflowCount++;
151+
}
152+
}
153+
142154
int main(void) {
143155
TaskHandle_t taskHandle;
144156
RefreshWatchdog();

0 commit comments

Comments
 (0)