Page 1 of 1

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

Posted: Sun Aug 27, 2023 2:55 pm
by GolamMostafa
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)); //
}
}

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

Posted: Mon Aug 28, 2023 12:49 am
by Sprite
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.

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

Posted: Mon Aug 28, 2023 3:33 pm
by GolamMostafa
Thanks for the reply.

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

Posted: Tue Sep 05, 2023 8:01 am
by GolamMostafa
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 2497 times
Figure-1

Task State Diagram:
taskStateDiag.png
taskStateDiag.png (36.71 KiB) Viewed 2508 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);
}
}