Page 1 of 1

ESP32 Dual Core Stack Size

Posted: Mon Oct 31, 2022 4:48 pm
by mihmansaye
  1.  
  2. How should I choose the stack size? And how many bytes is the maximum stack size? Is what is called a stack actually RAM?
  3.  
  4. Serial.println(String(ESP.getHeapSize() / 1024) + " Kb"); the total stack size I learned with this code?
  5.  
  1.  
  2. TaskHandle_t Task1;
  3. TaskHandle_t Task2;
  4.  
  5. // LED pins
  6. const int led1 = 2;
  7. const int led2 = 4;
  8.  
  9. void setup() {
  10.   Serial.begin(115200);
  11.   pinMode(led1, OUTPUT);
  12.   pinMode(led2, OUTPUT);
  13.  
  14.   //create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
  15.   xTaskCreatePinnedToCore(
  16.                     Task1code,   /* Task function. */
  17.                     "Task1",     /* name of task. */
  18.                     10000,       /* Stack size of task */
  19.                     NULL,        /* parameter of the task */
  20.                     1,           /* priority of the task */
  21.                     &Task1,      /* Task handle to keep track of created task */
  22.                     0);          /* pin task to core 0 */                  
  23.   delay(500);
  24.  
  25.   //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1
  26.   xTaskCreatePinnedToCore(
  27.                     Task2code,   /* Task function. */
  28.                     "Task2",     /* name of task. */
  29.                     10000,       /* Stack size of task */
  30.                     NULL,        /* parameter of the task */
  31.                     1,           /* priority of the task */
  32.                     &Task2,      /* Task handle to keep track of created task */
  33.                     1);          /* pin task to core 1 */
  34.     delay(500);
  35.  
  36.  
  37. Serial.println(uxTaskGetStackHighWaterMark(Task1));
  38. Serial.println(uxTaskGetStackHighWaterMark(Task2));
  39.  
  40. }
  41.  
  42. //Task1code: blinks an LED every 1000 ms
  43. void Task1code( void * pvParameters ){
  44.   Serial.print("Task1 running on core ");
  45.   Serial.println(xPortGetCoreID());
  46.  
  47.   for(;;){
  48.     digitalWrite(led1, HIGH);
  49.     delay(1000);
  50.     digitalWrite(led1, LOW);
  51.     delay(1000);
  52.   }
  53. }
  54.  
  55. //Task2code: blinks an LED every 700 ms
  56. void Task2code( void * pvParameters ){
  57.   Serial.print("Task2 running on core ");
  58.   Serial.println(xPortGetCoreID());
  59.  
  60.   for(;;){
  61.     digitalWrite(led2, HIGH);
  62.     delay(700);
  63.     digitalWrite(led2, LOW);
  64.     delay(700);
  65.   }
  66. }
  67.  
  68. void loop() {
  69.  
  70. }
  71.  

Re: ESP32 Dual Core Stack Size

Posted: Wed Nov 02, 2022 3:40 pm
by mikemoy
To find out how much stack your using for a Task, look into uxTaskGetStackHighWaterMark()
This will tell you if you need to increase it or not.