From 11b16dd340e8b4846b0e378080863ead5877b7b8 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 15 Dec 2021 02:00:26 -0300 Subject: [PATCH 1/4] Adds support to change LoopTask Stack size --- cores/esp32/Arduino.h | 5 +++++ cores/esp32/main.cpp | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index 71ffe75197e..1e528c99319 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -179,6 +179,11 @@ uint16_t makeWord(uint8_t h, uint8_t l); #define word(...) makeWord(__VA_ARGS__) +#define ESP_LOOP_TASK_STACK_SIZE(sz) \ + size_t getArduinoLoopTaskStackSize(void) { \ + return sz; \ + } + unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); diff --git a/cores/esp32/main.cpp b/cores/esp32/main.cpp index 903e80f1748..f4e79844818 100644 --- a/cores/esp32/main.cpp +++ b/cores/esp32/main.cpp @@ -33,6 +33,10 @@ void yieldIfNecessary(void){ bool loopTaskWDTEnabled; +__attribute__((weak)) size_t getArduinoLoopTaskStackSize(void) { + return ARDUINO_LOOP_STACK_SIZE; +} + void loopTask(void *pvParameters) { setup(); @@ -64,7 +68,7 @@ extern "C" void app_main() #endif loopTaskWDTEnabled = false; initArduino(); - xTaskCreateUniversal(loopTask, "loopTask", ARDUINO_LOOP_STACK_SIZE, NULL, 1, &loopTaskHandle, ARDUINO_RUNNING_CORE); + xTaskCreateUniversal(loopTask, "loopTask", getArduinoLoopTaskStackSize(), NULL, 1, &loopTaskHandle, ARDUINO_RUNNING_CORE); } #endif From 7ce004ebfb888fc83e101de273aee8af624c29d8 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 15 Dec 2021 15:53:54 -0300 Subject: [PATCH 2/4] Adds support to change LoopTask Stack size --- cores/esp32/Arduino.h | 6 +-- .../ArduinoStackSize/ArduinoStackSize.ino | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 libraries/ESP32/examples/ArduinoStackSize/ArduinoStackSize.ino diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index 1e528c99319..1dad1a96305 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -179,10 +179,8 @@ uint16_t makeWord(uint8_t h, uint8_t l); #define word(...) makeWord(__VA_ARGS__) -#define ESP_LOOP_TASK_STACK_SIZE(sz) \ - size_t getArduinoLoopTaskStackSize(void) { \ - return sz; \ - } +size_t getArduinoLoopTaskStackSize(void); +#define SET_LOOPTASK_STACK_SIZE(sz) size_t getArduinoLoopTaskStackSize() { return sz;} unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); diff --git a/libraries/ESP32/examples/ArduinoStackSize/ArduinoStackSize.ino b/libraries/ESP32/examples/ArduinoStackSize/ArduinoStackSize.ino new file mode 100644 index 00000000000..7051fa6b249 --- /dev/null +++ b/libraries/ESP32/examples/ArduinoStackSize/ArduinoStackSize.ino @@ -0,0 +1,37 @@ +/* + ESP32 Arduino creates a task to run setup() and then to execute loop() continuously + This task can be found at https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/main.cpp + + By default "loopTask" will be created with a stack size of 8KB. + This should be plenty for most general sketches. + + There is a way to change the stack size of this task by using + SET_LOOPTASK_STACK_SIZE(size); + It will bypass the default stack size of 8KB and allow the user to define a new size. + + It is recommend this value to be higher than 8KB, for instance 16KB. + This increasing may be necessary for the sketches that use deep recursion for instance. + + In this example, you can verify it by changing or just commenting out SET_LOOPTASK_STACK_SIZE(); +*/ + +#define ARDUINO_TASK_STACK_SIZE (16*1024) // 16KB + +// This sets Arduino Stack Size - comment this line to use default 8K stack size +SET_LOOPTASK_STACK_SIZE(ARDUINO_TASK_STACK_SIZE); + +void setup() { + Serial.begin(115200); + + Serial.printf("Arduino Stack was set to %d bytes", getArduinoLoopTaskStackSize()); + + // Print unused stack for the task that is running setup() + Serial.printf("\nSetup() - Free Stack Space: %d", uxTaskGetStackHighWaterMark(NULL)); +} + +void loop() { + delay(1000); + + // Print unused stack for the task that is running loop() - the same as for setup() + Serial.printf("\nLoop() - Free Stack Space: %d", uxTaskGetStackHighWaterMark(NULL)); +} From 301f93d9ae2c6e22a7c35993559b580403e07b77 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 15 Dec 2021 15:57:16 -0300 Subject: [PATCH 3/4] Adds support to change LoopTask Stack size --- cores/esp32/Arduino.h | 2 +- .../ESP32/examples/ArduinoStackSize/ArduinoStackSize.ino | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index 1dad1a96305..8014d3c8342 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -180,7 +180,7 @@ uint16_t makeWord(uint8_t h, uint8_t l); #define word(...) makeWord(__VA_ARGS__) size_t getArduinoLoopTaskStackSize(void); -#define SET_LOOPTASK_STACK_SIZE(sz) size_t getArduinoLoopTaskStackSize() { return sz;} +#define SET_LOOP_TASK_STACK_SIZE(sz) size_t getArduinoLoopTaskStackSize() { return sz;} unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); diff --git a/libraries/ESP32/examples/ArduinoStackSize/ArduinoStackSize.ino b/libraries/ESP32/examples/ArduinoStackSize/ArduinoStackSize.ino index 7051fa6b249..df5614da436 100644 --- a/libraries/ESP32/examples/ArduinoStackSize/ArduinoStackSize.ino +++ b/libraries/ESP32/examples/ArduinoStackSize/ArduinoStackSize.ino @@ -6,19 +6,19 @@ This should be plenty for most general sketches. There is a way to change the stack size of this task by using - SET_LOOPTASK_STACK_SIZE(size); + SET_LOOP_TASK_STACK_SIZE(size); It will bypass the default stack size of 8KB and allow the user to define a new size. It is recommend this value to be higher than 8KB, for instance 16KB. This increasing may be necessary for the sketches that use deep recursion for instance. - In this example, you can verify it by changing or just commenting out SET_LOOPTASK_STACK_SIZE(); + In this example, you can verify it by changing or just commenting out SET_LOOP_TASK_STACK_SIZE(); */ #define ARDUINO_TASK_STACK_SIZE (16*1024) // 16KB // This sets Arduino Stack Size - comment this line to use default 8K stack size -SET_LOOPTASK_STACK_SIZE(ARDUINO_TASK_STACK_SIZE); +SET_LOOP_TASK_STACK_SIZE(ARDUINO_TASK_STACK_SIZE); void setup() { Serial.begin(115200); From b6409000f20986de3b3b81a10694a9b5e9bdbcce Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Wed, 15 Dec 2021 16:07:36 -0300 Subject: [PATCH 4/4] Adds support to change LoopTask Stack size --- libraries/ESP32/examples/ArduinoStackSize/ArduinoStackSize.ino | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libraries/ESP32/examples/ArduinoStackSize/ArduinoStackSize.ino b/libraries/ESP32/examples/ArduinoStackSize/ArduinoStackSize.ino index df5614da436..73c42023591 100644 --- a/libraries/ESP32/examples/ArduinoStackSize/ArduinoStackSize.ino +++ b/libraries/ESP32/examples/ArduinoStackSize/ArduinoStackSize.ino @@ -15,10 +15,9 @@ In this example, you can verify it by changing or just commenting out SET_LOOP_TASK_STACK_SIZE(); */ -#define ARDUINO_TASK_STACK_SIZE (16*1024) // 16KB // This sets Arduino Stack Size - comment this line to use default 8K stack size -SET_LOOP_TASK_STACK_SIZE(ARDUINO_TASK_STACK_SIZE); +SET_LOOP_TASK_STACK_SIZE(16*1024); // 16KB void setup() { Serial.begin(115200);