mirror of
https://github.com/0xFEEDC0DE64/arduino-esp32.git
synced 2025-06-25 18:01:33 +02:00
## 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 https://github.com/espressif/arduino-esp32/issues/6010#issuecomment-992701658 Thanks @igrr for the suggestion!
75 lines
1.6 KiB
C++
75 lines
1.6 KiB
C++
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "esp_task_wdt.h"
|
|
#include "Arduino.h"
|
|
#if (ARDUINO_USB_CDC_ON_BOOT|ARDUINO_USB_MSC_ON_BOOT|ARDUINO_USB_DFU_ON_BOOT)
|
|
#include "USB.h"
|
|
#if ARDUINO_USB_MSC_ON_BOOT
|
|
#include "FirmwareMSC.h"
|
|
#endif
|
|
#endif
|
|
|
|
#ifndef ARDUINO_LOOP_STACK_SIZE
|
|
#ifndef CONFIG_ARDUINO_LOOP_STACK_SIZE
|
|
#define ARDUINO_LOOP_STACK_SIZE 8192
|
|
#else
|
|
#define ARDUINO_LOOP_STACK_SIZE CONFIG_ARDUINO_LOOP_STACK_SIZE
|
|
#endif
|
|
#endif
|
|
|
|
TaskHandle_t loopTaskHandle = NULL;
|
|
|
|
#if CONFIG_AUTOSTART_ARDUINO
|
|
#if CONFIG_FREERTOS_UNICORE
|
|
void yieldIfNecessary(void){
|
|
static uint64_t lastYield = 0;
|
|
uint64_t now = millis();
|
|
if((now - lastYield) > 2000) {
|
|
lastYield = now;
|
|
vTaskDelay(5); //delay 1 RTOS tick
|
|
}
|
|
}
|
|
#endif
|
|
|
|
bool loopTaskWDTEnabled;
|
|
|
|
__attribute__((weak)) size_t getArduinoLoopTaskStackSize(void) {
|
|
return ARDUINO_LOOP_STACK_SIZE;
|
|
}
|
|
|
|
void loopTask(void *pvParameters)
|
|
{
|
|
setup();
|
|
for(;;) {
|
|
#if CONFIG_FREERTOS_UNICORE
|
|
yieldIfNecessary();
|
|
#endif
|
|
if(loopTaskWDTEnabled){
|
|
esp_task_wdt_reset();
|
|
}
|
|
loop();
|
|
if (serialEventRun) serialEventRun();
|
|
}
|
|
}
|
|
|
|
extern "C" void app_main()
|
|
{
|
|
#if ARDUINO_USB_CDC_ON_BOOT
|
|
Serial.begin();
|
|
#endif
|
|
#if ARDUINO_USB_MSC_ON_BOOT
|
|
MSC_Update.begin();
|
|
#endif
|
|
#if ARDUINO_USB_DFU_ON_BOOT
|
|
USB.enableDFU();
|
|
#endif
|
|
#if ARDUINO_USB_ON_BOOT
|
|
USB.begin();
|
|
#endif
|
|
loopTaskWDTEnabled = false;
|
|
initArduino();
|
|
xTaskCreateUniversal(loopTask, "loopTask", getArduinoLoopTaskStackSize(), NULL, 1, &loopTaskHandle, ARDUINO_RUNNING_CORE);
|
|
}
|
|
|
|
#endif
|