Page 1 of 1

ESP32 2Tasks on 2 Cores - Board reset

Posted: Wed Mar 27, 2019 8:21 am
by Rajkiran
/* Below is the code for the 2 tasks which will run on different core viz Core 1 and 2.
The Tasks would be triggered in the "Key press interrupt". I see that Tasks start but board
get reboot immediately.
Can anyone guide me here *
ESP32_2Tasks_2Cores_Error.PNG
ESP32_2Tasks_2Cores_Error.PNG (60 KiB) Viewed 10894 times
ESP32_2Tasks_2Cores_Error.PNG
ESP32_2Tasks_2Cores_Error.PNG (60 KiB) Viewed 10894 times
/


#define DELAY1 delay(800)
#define DELAY2 delay(500)

TaskHandle_t Task1_Handler;
TaskHandle_t Task2_Handler;

// LED pins
const int led1 = 13;
const int led2 = 23;

struct Button
{
const uint8_t PIN;
uint32_t numberKeyPresses;
bool pressed;
};

Button button1 = {2, 0, false};

/* Resume both task on Key press */
void IRAM_ATTR isr()
{
BaseType_t taskYieldRequired = 0;

button1.pressed = true;

taskYieldRequired = xTaskResumeFromISR(Task1_Handler);
taskYieldRequired = xTaskResumeFromISR(Task2_Handler);
}


void setup()
{
Serial.begin(115200);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);

/* intr init */
pinMode(button1.PIN, INPUT_PULLUP);
attachInterrupt(button1.PIN, isr, FALLING);

//create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(Task1code, "Task1", 8000, NULL, 1, &Task1_Handler, 0);
vTaskSuspend(Task1_Handler);

//create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(Task2code, "Task2", 8000, NULL, 1, &Task2_Handler, 1);
vTaskSuspend(Task2_Handler);

//esp_task_wdt_init(10,1);
}


//Task1code: blinks an LED every 800 ms
void Task1code( void * pvParameters )
{
uint8_t i;

Serial.print("********** Task1 core running ");
Serial.println(xPortGetCoreID());

for(i=0;i<2;i++)
{
digitalWrite(led1, HIGH);
DELAY1;
digitalWrite(led1, LOW);
DELAY1;
}
}


//Task2code: blinks an LED every 500 ms
void Task2code( void * pvParameters )
{
uint8_t i;

Serial.print("################ Task2 CORE ");
Serial.println(xPortGetCoreID());

for(i=0;i<2;i++)
{
digitalWrite(led2, HIGH);
DELAY2;
digitalWrite(led2, LOW);
DELAY2;
}
}

void loop()
{
if(button1.pressed)
{
Serial.print("in Intpt hit ");
button1.pressed = false;
}
}

Re: ESP32 2Tasks on 2 Cores - Board reset

Posted: Thu Mar 28, 2019 2:49 am
by ESP_Sprite
Hard to say. Can you use e.g. addr2line to decode that backtrace?

Re: ESP32 2Tasks on 2 Cores - Board reset

Posted: Thu Mar 28, 2019 8:15 am
by boarchuz
It looks like you're checking if 'taskYieldRequired' but not actually yielding.

Code: Select all

if(taskYieldRequired == pdTRUE)
{
  taskYIELD();
}

Re: ESP32 2Tasks on 2 Cores - Board reset

Posted: Thu Mar 28, 2019 9:49 pm
by idahowalker
Your issue could be one of two tasks Task2 or loop(). loop() runs on core 1 and, using freeRTOS loop should look like this :

Code: Select all

void loop() 
Also, the default priority of loop() is 1,assign your taskings to something higher than 1; like 3. And, please, use code tags.

Also, your tasks exit. Once they have run once they are then cleaned up by loop() and no longer in effect. You'll need to recreate them or not have them exit. Look into events.

Re: ESP32 2Tasks on 2 Cores - Board reset

Posted: Thu Mar 28, 2019 9:59 pm
by idahowalker
An example of a round robin:

Code: Select all

#include "freertos/event_groups.h"

/* define event bits */
#define TASK_1_BIT        ( 1 << 0 ) //1
#define TASK_2_BIT        ( 1 << 1 ) //10
#define TASK_3_BIT        ( 1 << 2 ) //100
#define ALL_SYNC_BITS     (TASK_1_BIT | TASK_2_BIT | TASK_3_BIT) //111

/* create event group */
EventGroupHandle_t eg;

void setup() {

  Serial.begin(112500);
  eg = xEventGroupCreate();
  
  xTaskCreate(
      task1,           /* Task function. */
      "task1",        /* name of task. */
      10000,                    /* Stack size of task */
      NULL,                     /* parameter of the task */
      3,                        /* priority of the task */
      NULL);                    /* Task handle to keep track of created task */
  xTaskCreate(
      task2,           /* Task function. */
      "task2",        /* name of task. */
      10000,                    /* Stack size of task */
      NULL,                     /* parameter of the task */
      1,                        /* priority of the task */
      NULL);                    /* Task handle to keep track of created task */
  xTaskCreate(
      task3,           /* Task function. */
      "task3",        /* name of task. */
      10000,                    /* Stack size of task */
      NULL,                     /* parameter of the task */
      1,                        /* priority of the task */
      NULL);                    /* Task handle to keep track of created task */
}

void loop() {

}

void task1( void * parameter )
{
  for(;;){
    Serial.println("task1 done");
    /* task 2 finishes printing so set its event bit and wait until other tasks finish */
    EventBits_t uxBits = xEventGroupSync(eg, TASK_1_BIT, ALL_SYNC_BITS, portMAX_DELAY );
    /* if other tasks finished then all event bits would be set*/
    if( ( uxBits & ALL_SYNC_BITS ) == ALL_SYNC_BITS ){
        Serial.println("task 1 - all task done !!!");
    }
  }
  vTaskDelete( NULL );
}
/* this task is similar to sendTask1 */
void task2( void * parameter )
{

  for(;;){
    Serial.println("task2 done");
    /* task 2 finishes printing so set its event bit */
    EventBits_t uxBits = xEventGroupSync( eg, TASK_2_BIT, ALL_SYNC_BITS, portMAX_DELAY );
  }
  vTaskDelete( NULL );
}
void task3( void * parameter )
{
  for(;;){
    Serial.println("task3 done");
    /* task 3 finishes printing so set its event bit */
    EventBits_t uxBits = xEventGroupSync( eg, TASK_3_BIT, ALL_SYNC_BITS, portMAX_DELAY );
  }
  vTaskDelete( NULL );
}

Re: ESP32 2Tasks on 2 Cores - Board reset

Posted: Thu Mar 28, 2019 10:01 pm
by idahowalker
and here is a task using a delay:

Code: Select all

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/event_groups.h"

#define ESP32_WROVER_LED1 27 // LED D4
#define ESP32_WROVER_LED2 0 // LED D3
#define ESP32_LED 2
#define TaskStack10K 10000
#define TaskCore1 1
#define TaskCore0 0
#define Priority2 2
#define Priority3 3
#define Priority4 4
#define TestPin 4
#define OneK 1000




void setup() 
{
  Serial.begin(115200);
pinMode (ESP32_LED, OUTPUT);
pinMode ( TestPin, OUTPUT);
 xTaskCreatePinnedToCore( fBlinkBuiltIn, "fBlinkBuiltIn", TaskStack10K, NULL, Priority2, NULL, TaskCore1 ); //assigned to core 1
 
}
void loop() {}

void fBlinkBuiltIn( void* pvParameters )
{
  // toggle built in LED off/on
  for (;;)
  {
    vTaskDelay( pdMS_TO_TICKS( 10 ) );
    REG_WRITE( GPIO_OUT_W1TC_REG, BIT2 ); // GPIO2 LOW (clear)
    vTaskDelay( pdMS_TO_TICKS( OneK ) );
    REG_WRITE( GPIO_OUT_W1TS_REG, BIT2 ); //GPIO2 HIGH (set)
  }
  vTaskDelete( NULL );
}

Re: ESP32 2Tasks on 2 Cores - Board reset

Posted: Thu Mar 28, 2019 10:02 pm
by idahowalker
and you should find this useful" https://www.freertos.org/a00106.html