I have an application where the high water mark is 16 after many minutes of using the app. But if I reduce the allocated stack size by just 1 byte, the app hangs within seconds of starting the app. Why?
I would think the “16” means that there are 16 bytes of stack space that have never been used since the task started, therefore I should be able to reduce the stack size by 15 and still have 1 byte unused. But any attempt to get the high water mark below 16 causes the app to hang within seconds or at most a few minutes.
I’m playing with this app based on an Arduino Unu:
https://learn.adafruit.com/arduin-o-phone-arduino-powered-diy-cellphone/parts-and-prep . I’ve added FreeRTOS and some code to display the current time. There are three tasks:
The idle task, which does nothing except call uxTaskGetStackHighWaterMark(NULL) and save the value in a global, volatile variable.
The “GPS” task (priority 0), which reads the time from a GPS module and displays it on the LCD display. It also calls uxTaskGetStackHighWaterMark(NULL) and saves the value in a global, volatile variable.
The “Phone” task (priority 1), which monitors the touch sense screen, handles touches, and updates the display as the user enters a phone number. It also keeps track of its high water mark, and displays all the high water marks whenever the user touches the screen.
The GPS task communicates with the GPS module using SoftwareSerial, which can’t tolerate interruptions from the Phone task, so I’ve enclosed all communications with the GPS module with vTaskSuspend(PhoneTaskHandle) and vTaskResume(PhoneTaskHandle).
Both the GPS and the Phone task access the display, so I have DisplaySemaphore = xSemaphoreCreateMutex(); and I enclose all communication with the display with xSemaphoreTake(DisplaySemaphore, portMAX_DELAY) and xSemaphoreGive(DisplaySemaphore).
I’ve changed these configurations from the Amazon FreeRTOSConfig.h V10.0.0:
configUSE_TIMERS 0 // The Arduino port uses the watchdog timer for scheduling.
I currently have the GPS task stack size set at 236 and the Phone task at 251. With these settings, the high water marks for the processes, after many minutes of running and using the app, are Idle: 31, GPS: 32, Phone: 32.
If I decrease any of those stack sizes by 17 (which should leave 14 or 15 bytes of headroom), the app hangs within seconds (usually) or a few minutes of use. Why is that?
Edit: When adjusting stack space, sometimes the app will hang and the Arduino LED will blink, indicating insufficient stack or heap space. But usually when the app hangs, the LED does NOT blink.