Skip to content

Commit 7ce004e

Browse files
committed
Adds support to change LoopTask Stack size
1 parent 11b16dd commit 7ce004e

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

cores/esp32/Arduino.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,8 @@ uint16_t makeWord(uint8_t h, uint8_t l);
179179

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

182-
#define ESP_LOOP_TASK_STACK_SIZE(sz) \
183-
size_t getArduinoLoopTaskStackSize(void) { \
184-
return sz; \
185-
}
182+
size_t getArduinoLoopTaskStackSize(void);
183+
#define SET_LOOPTASK_STACK_SIZE(sz) size_t getArduinoLoopTaskStackSize() { return sz;}
186184

187185
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
188186
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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_LOOPTASK_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_LOOPTASK_STACK_SIZE();
16+
*/
17+
18+
#define ARDUINO_TASK_STACK_SIZE (16*1024) // 16KB
19+
20+
// This sets Arduino Stack Size - comment this line to use default 8K stack size
21+
SET_LOOPTASK_STACK_SIZE(ARDUINO_TASK_STACK_SIZE);
22+
23+
void setup() {
24+
Serial.begin(115200);
25+
26+
Serial.printf("Arduino Stack was set to %d bytes", getArduinoLoopTaskStackSize());
27+
28+
// Print unused stack for the task that is running setup()
29+
Serial.printf("\nSetup() - Free Stack Space: %d", uxTaskGetStackHighWaterMark(NULL));
30+
}
31+
32+
void loop() {
33+
delay(1000);
34+
35+
// Print unused stack for the task that is running loop() - the same as for setup()
36+
Serial.printf("\nLoop() - Free Stack Space: %d", uxTaskGetStackHighWaterMark(NULL));
37+
}

0 commit comments

Comments
 (0)