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:
  1. TaskHandle_t Task10Handle;
  2. TaskHandle_t Task11Handle;
  3.  
  4. void setup()
  5. {
  6.   Serial.begin(115200);
  7.   analogReadResolution(12);
  8.  
  9.   xTaskCreatePinnedToCore(Task10, "Task-10", 2048, NULL, 1, &Task10Handle, 1);
  10.   xTaskCreatePinnedToCore(Task11, "Task-11", 2048, NULL, 2, &Task11Handle, 1);
  11. }
  12.  
  13. void loop() {}
  14.  
  15. void Task10(void *pvParameters)
  16. {
  17.   while (true)
  18.   {
  19.     int analogValue34 = analogRead(34);
  20.     Serial.print("ADC1_6: "); Serial.println(analogValue34);
  21.     vTaskDelay(pdMS_TO_TICKS(1000)); //
  22.   }
  23. }
  24.  
  25. void Task11(void *pvParameter)
  26. {
  27.   while (true)
  28.   {
  29.     int analogValue35 = analogRead(35);
  30.     Serial.print("ADC1_7: "); Serial.println(analogValue35);
  31.     vTaskDelay(pdMS_TO_TICKS(1000)); //
  32.   }
  33. }

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

Posted: Mon Aug 28, 2023 12:49 am
by ESP_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 2073 times
Figure-1

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

Sketch:
  1. TaskHandle_t Task10Handle;
  2. TaskHandle_t Task11Handle;
  3.  
  4. #define LED 2     //BLU
  5. #define LED10 21  //RED
  6. #define LED11 22  //GRN
  7.  
  8. void setup()
  9. {
  10.   Serial.begin(115200);
  11.   pinMode(LED, OUTPUT);
  12.   pinMode(LED10, OUTPUT);
  13.   pinMode(LED11, OUTPUT);
  14.  
  15.   xTaskCreatePinnedToCore(Task10, "Task-10", 2048, NULL, 2, &Task10Handle, 1);//RED LED10
  16.   xTaskCreatePinnedToCore(Task11, "Task-11", 2048, NULL, 3, &Task11Handle, 1);//GRN LED11
  17. }
  18.  
  19. void loop()//BLU
  20. {
  21.   digitalWrite(LED, HIGH);
  22.   vTaskDelay(1500);
  23.   digitalWrite(LED, LOW);
  24.   vTaskDelay(1500);
  25. }
  26.  
  27. void Task10(void *pvParameters)//RED
  28. {
  29.   while (true)
  30.   {
  31.     digitalWrite(LED10, HIGH);
  32.     vTaskDelay(500);
  33.     digitalWrite(LED10, LOW);
  34.     vTaskDelay(500);
  35.   }
  36. }
  37.  
  38. void Task11(void *pvParameters)//GRN
  39. {
  40.   while (true)
  41.   {
  42.     digitalWrite(LED11, HIGH);
  43.     vTaskDelay(250);
  44.     digitalWrite(LED11, LOW);
  45.     vTaskDelay(250);
  46.   }
  47. }