Monday, February 14, 2022

ESP 32 FreeRTOS Task Create in Dual Core

ESP 32 FreeRTOS, LED Turn On and Turn Off:

In This Artical i will explain you, how to  use FreeRTOS in ESP32 Board-

Projects 1- Create three tasks with different priority , Task 1 and Task 2 running in core 0 and task 3 running in core 1 (here we are accessing dual core at the same projects:


BaseType_t xTaskCreate(TaskFunction_t pvTaskCode
const char *constpcName
const uint32_t usStackDepth
void *constpvParameters
UBaseType_t uxPriority
TaskHandle_t *constpvCreatedTask
const BaseType_t xCoreID)

Create a new task with a specified affinity.

This function is similar to xTaskCreate, but allows setting task affinity in SMP system.

Return

pdPASS if the task was successfully created and added to a ready list, otherwise an error code defined in the file projdefs.h

Parameters
  • pvTaskCode: Pointer to the task entry function. Tasks must be implemented to never return (i.e. continuous loop), or should be terminated using vTaskDelete function.

  • pcName: A descriptive name for the task. This is mainly used to facilitate debugging. Max length defined by configMAX_TASK_NAME_LEN - default is 16.

  • usStackDepth: The size of the task stack specified as the number of bytes. Note that this differs from vanilla FreeRTOS.

  • pvParameters: Pointer that will be used as the parameter for the task being created.

  • uxPriority: The priority at which the task should run. Systems that include MPU support can optionally create tasks in a privileged (system) mode by setting bit portPRIVILEGE_BIT of the priority parameter. For example, to create a privileged task at priority 2 the uxPriority parameter should be set to ( 2 | portPRIVILEGE_BIT ).

  • pvCreatedTask: Used to pass back a handle by which the created task can be referenced.

  • xCoreID: If the value is tskNO_AFFINITY, the created task is not pinned to any CPU, and the scheduler can run it on any core available. Values 0 or 1 indicate the index number of the CPU which the task should be pinned to. Specifying values larger than (portNUM_PROCESSORS - 1) will cause the function to fail.







example:
code begin here:


TaskHandle_t Task1; TaskHandle_t Task2; TaskHandle_t Task3; const int led1 = 2;// input 1 const int led2 = 3;// input 2 const int led3 = 4;// input 3 void setup() { Serial.begin(115200); pinMode(led1, INPUT); pinMode(led2, INPUT); pinMode(led3, INPUT); xTaskCreate(led_1,"Task1",1000,NULL,1,&Task1,0); // task running in core 0 delay(300); xTaskCreate(led_2,"Task2",1000,NULL,1,&Task2,0); // task running in core 0 delay(300); xTaskCreate(led_3,"Task3",1000,NULL,1,&Task3,1); // task running in core 1 delay(300); } void led_1( void * parameter ){ Serial.print("Task1 is running on core "); Serial.println(xPortGetCoreID()); for(;;){ digitalWrite(led1, HIGH); delay(300); digitalWrite(led1, LOW); delay(300); } } void led_2( void * parameter ){ Serial.print("Task2 is running on core "); Serial.println(xPortGetCoreID()); for(;;){ digitalWrite(led2, HIGH); delay(300); digitalWrite(led2, LOW); delay(300); } } void led_3( void * parameter ){ Serial.print("Task3 is running on core "); Serial.println(xPortGetCoreID()); for(;;){ digitalWrite(led3, HIGH); delay(300); digitalWrite(led3, LOW); delay(300); } } void loop() { }




ESP 32 FreeRTOS Task Create in Dual Core

ESP 32 FreeRTOS, LED Turn On and Turn Off: In This Artical i will explain you, how to  use FreeRTOS in ESP32 Board- Projects 1- Create three...