How to enable dual-core ESP32 to enable dual-core function to perform multiple tasks at the same time

How to enable dual-core ESP32 to enable dual-core function to perform multiple tasks at the same time

Introduction

Insert image description here

The ESP32-WROOM-32 module has two built-in low-power Xtensa® 32-bit LX6 MCUs, and the two CPU cores (core 0 and core 1) can be controlled independently. Multiple pieces of code can be run simultaneously on both cores, allowing the ESP32 to multitask (pieces of code that perform certain things). Arduino IDE enables core 1 by default. ESP32 can run a real-time operating system RTOS and process several independently running tasks in parallel. To assign a specific part of the code to a specific core, a task needs to be created. When you create a task, you can choose which core it will run on, and its priority. Priority values ​​start at 0, where 0 is the lowest priority. The processor will run higher priority tasks first.

Insert image description here

Check which core of ESP32 is currently performing tasks

Open the Arduino IDE and upload the following code to the ESP32.
Call the xPortGetCoreID() function to obtain the core number.

void setup() {
    
    
  Serial.begin(115200);
  Serial.print("setup() running on core ");
  Serial.println(xPortGetCoreID());
}

void loop() {
    
    
  Serial.print("loop() running on core ");
  Serial.println(xPortGetCoreID());
}

Open the serial monitor to see that the current core 1 of the ESP32 is executing tasks
Insert image description here

Dual cores perform tasks simultaneously

Open Arduino IDE and upload the following code to ESP32

TaskHandle_t Task1;
TaskHandle_t Task2;

// LED pins
const int led1 = 2;
const int led2 = 4;
void setup() {
    
    
Serial.begin(115200); 
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);

  //create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
  xTaskCreatePinnedToCore(
                    Task1code,   /* Task function. */
                    "Task1",     /* name of task. */
                    10000,       /* Stack size of task */
                    NULL,        /* parameter of the task */
                    1,           /* priority of the task */
                    &Task1,      /* Task handle to keep track of created task */
                    0);          /* pin task to core 0 */                  
  delay(500); 

  //create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1
  xTaskCreatePinnedToCore(
                    Task2code,   /* Task function. */
                    "Task2",     /* name of task. */
                    10000,       /* Stack size of task */
                    NULL,        /* parameter of the task */
                    1,           /* priority of the task */
                    &Task2,      /* Task handle to keep track of created task */
                    1);          /* pin task to core 1 */
    delay(500); 
}

//Task1code: blinks an LED every 1000 ms
void Task1code( void * pvParameters ){
    
    
  Serial.print("Task1 running on core ");
  Serial.println(xPortGetCoreID());

  for(;;){
    
    
    digitalWrite(led1, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
    delay(1000);
  } 
}

//Task2code: blinks an LED every 700 ms
void Task2code( void * pvParameters ){
    
    
  Serial.print("Task2 running on core ");
  Serial.println(xPortGetCoreID());

  for(;;){
    
    
    digitalWrite(led2, HIGH);
    delay(700);
    digitalWrite(led2, LOW);
    delay(700);
  }
}

void loop() {
    
    
  
}

This code is executed on core 0 to make LED1 flash every 1000ms, and executed on core 1 to make LED2 flash every 700ms. Both tasks have a priority of 1, are executed simultaneously, and kernel information is printed on the serial monitor. If dual-core execution is not enabled and the default single-core sequence is used, LED1 and LED2 cannot flash at different intervals at the same time.
If you want to delete the task during task execution, you can call the function vTaskDelete(Task1).
Insert image description here

Summarize

ESP32 with dual-core function allows us to simplify the program and improve efficiency when designing applications with parallel functions. For example, use one core to take sensor readings and use another core to perform other tasks based on the readings.

Guess you like

Origin blog.csdn.net/qq_42250136/article/details/133929303