How does FreeRTOS allocate more service time (CPU cycles) to higher priority level task?

GolamMostafa
Posts: 19
Joined: Sun Jul 23, 2023 12:30 am

How does FreeRTOS allocate more service time (CPU cycles) to higher priority level task?

Postby GolamMostafa » Sun Aug 27, 2023 2:55 pm

I have been experimenting with the following setup (Fig-1) and the sketch to understand the mechanism of FreeRTOS in allocating more time to Task11 (with higher priority level 2) than Tasks10 with priority level 1. Would appreciate you explain this mechanism.
Sketch:

Code: Untitled.cpp Select all


TaskHandle_t Task10Handle;
TaskHandle_t Task11Handle;

void setup()
{
Serial.begin(115200);
analogReadResolution(12);

xTaskCreatePinnedToCore(Task10, "Task-10", 2048, NULL, 1, &Task10Handle, 1);
xTaskCreatePinnedToCore(Task11, "Task-11", 2048, NULL, 2, &Task11Handle, 1);
}

void loop() {}

void Task10(void *pvParameters)
{
while (true)
{
int analogValue34 = analogRead(34);
Serial.print("ADC1_6: "); Serial.println(analogValue34);
vTaskDelay(pdMS_TO_TICKS(1000)); //
}
}

void Task11(void *pvParameter)
{
while (true)
{
int analogValue35 = analogRead(35);
Serial.print("ADC1_7: "); Serial.println(analogValue35);
vTaskDelay(pdMS_TO_TICKS(1000)); //
}
}
Attachments
esp32InterTaskNotification.png
esp32InterTaskNotification.png (20.52 KiB) Viewed 2805 times

Sprite
Espressif staff
Espressif staff
Posts: 10599
Joined: Thu Nov 26, 2015 4:08 am

Re: How does FreeRTOS allocate more service time (CPU cycles) to higher priority level task?

Postby Sprite » Mon Aug 28, 2023 12:49 am

It won't. Given different priorities, FreeRTOS always allocates 100% of the time to the highest-priority task that is not blocking: it will always run that task until the task runs into something that de-schedules it (like the vTaskDelay in your example). See also here.

GolamMostafa
Posts: 19
Joined: Sun Jul 23, 2023 12:30 am

Re: How does FreeRTOS allocate more service time (CPU cycles) to higher priority level task?

Postby GolamMostafa » Mon Aug 28, 2023 3:33 pm

Thanks for the reply.

GolamMostafa
Posts: 19
Joined: Sun Jul 23, 2023 12:30 am

Re: How does FreeRTOS allocate more service time (CPU cycles) to higher priority level task?

Postby GolamMostafa » Tue Sep 05, 2023 8:01 am

I have prepared and tested the following three tasks sketch that blinks three LEDs concurrently at different blink rates. Are there anything exaggerate particulalry on the definition of Tick Time and Time Slice (Fig-2)?

Schematic:
esp32-3led.png
esp32-3led.png (11.07 KiB) Viewed 2496 times
Figure-1

Task State Diagram:
taskStateDiag.png
taskStateDiag.png (36.71 KiB) Viewed 2507 times
Figure-2:

Sketch:

Code: Untitled.cpp Select all

TaskHandle_t Task10Handle;
TaskHandle_t Task11Handle;

#define LED 2 //BLU
#define LED10 21 //RED
#define LED11 22 //GRN

void setup()
{
Serial.begin(115200);
pinMode(LED, OUTPUT);
pinMode(LED10, OUTPUT);
pinMode(LED11, OUTPUT);

xTaskCreatePinnedToCore(Task10, "Task-10", 2048, NULL, 2, &Task10Handle, 1);//RED LED10
xTaskCreatePinnedToCore(Task11, "Task-11", 2048, NULL, 3, &Task11Handle, 1);//GRN LED11
}

void loop()//BLU
{
digitalWrite(LED, HIGH);
vTaskDelay(1500);
digitalWrite(LED, LOW);
vTaskDelay(1500);
}

void Task10(void *pvParameters)//RED
{
while (true)
{
digitalWrite(LED10, HIGH);
vTaskDelay(500);
digitalWrite(LED10, LOW);
vTaskDelay(500);
}
}

void Task11(void *pvParameters)//GRN
{
while (true)
{
digitalWrite(LED11, HIGH);
vTaskDelay(250);
digitalWrite(LED11, LOW);
vTaskDelay(250);
}
}

Who is online

Users browsing this forum: ChatGPT-User, PetalBot, Qwantbot and 3 guests