Skip to content

Commit 82ec74a

Browse files
authored
Adds support to change LoopTask Stack size (#6025)
## Summary Arduino ```setup()``` and ```loop()``` run under a Task with a fixed Stack size of 8KB. Users may want to change this size. This PR adds this possibility by just adding a line of code, as for example: ``` dart ESP_LOOP_TASK_STACK_SIZE(16384); void setup() { } void loop() { } ``` ## Impact None. It adds a new functionality to ESP32 Arduino. If ```ESP_LOOP_TASK_STACK_SIZE(newSize);``` is not declared/used, it will compile the sketch with the default stack size of 8KB. ## Related links fix #6010 #6010 (comment) Thanks @igrr for the suggestion!
1 parent 5940d89 commit 82ec74a

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

Diff for: cores/esp32/Arduino.h

+3
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ uint16_t makeWord(uint8_t h, uint8_t l);
179179

180180
#define word(...) makeWord(__VA_ARGS__)
181181

182+
size_t getArduinoLoopTaskStackSize(void);
183+
#define SET_LOOP_TASK_STACK_SIZE(sz) size_t getArduinoLoopTaskStackSize() { return sz;}
184+
182185
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
183186
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
184187

Diff for: cores/esp32/main.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ void yieldIfNecessary(void){
3333

3434
bool loopTaskWDTEnabled;
3535

36+
__attribute__((weak)) size_t getArduinoLoopTaskStackSize(void) {
37+
return ARDUINO_LOOP_STACK_SIZE;
38+
}
39+
3640
void loopTask(void *pvParameters)
3741
{
3842
setup();
@@ -64,7 +68,7 @@ extern "C" void app_main()
6468
#endif
6569
loopTaskWDTEnabled = false;
6670
initArduino();
67-
xTaskCreateUniversal(loopTask, "loopTask", ARDUINO_LOOP_STACK_SIZE, NULL, 1, &loopTaskHandle, ARDUINO_RUNNING_CORE);
71+
xTaskCreateUniversal(loopTask, "loopTask", getArduinoLoopTaskStackSize(), NULL, 1, &loopTaskHandle, ARDUINO_RUNNING_CORE);
6872
}
6973

7074
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
ESP32 Arduino creates a task to run setup() and then to execute loop() continuously
3+
This task can be found at https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/main.cpp
4+
5+
By default "loopTask" will be created with a stack size of 8KB.
6+
This should be plenty for most general sketches.
7+
8+
There is a way to change the stack size of this task by using
9+
SET_LOOP_TASK_STACK_SIZE(size);
10+
It will bypass the default stack size of 8KB and allow the user to define a new size.
11+
12+
It is recommend this value to be higher than 8KB, for instance 16KB.
13+
This increasing may be necessary for the sketches that use deep recursion for instance.
14+
15+
In this example, you can verify it by changing or just commenting out SET_LOOP_TASK_STACK_SIZE();
16+
*/
17+
18+
19+
// This sets Arduino Stack Size - comment this line to use default 8K stack size
20+
SET_LOOP_TASK_STACK_SIZE(16*1024); // 16KB
21+
22+
void setup() {
23+
Serial.begin(115200);
24+
25+
Serial.printf("Arduino Stack was set to %d bytes", getArduinoLoopTaskStackSize());
26+
27+
// Print unused stack for the task that is running setup()
28+
Serial.printf("\nSetup() - Free Stack Space: %d", uxTaskGetStackHighWaterMark(NULL));
29+
}
30+
31+
void loop() {
32+
delay(1000);
33+
34+
// Print unused stack for the task that is running loop() - the same as for setup()
35+
Serial.printf("\nLoop() - Free Stack Space: %d", uxTaskGetStackHighWaterMark(NULL));
36+
}

0 commit comments

Comments
 (0)