How the schedular behaves when all the task are of same priority?

I am new to FreeRTOS and trying to understand how the schedular works when all the created task have same priority.

will there be preemption? I mean one task can preempt the other one in the below code?

Just for better understanding of the problem i have included the source code as well

#include <Arduino_FreeRTOS.h>

#define YELLOW 6
#define GREEN  7
#define BLUE   8

#define SET(digitalPin) digitalWrite(digitalPin, HIGH)
#define RESET(digitalPin) digitalWrite(digitalPin, LOW)
#define TOGGLE(digitalPin) digitalWrite(digitalPin, digitalRead(digitalPin)^1)

typedef int taskProfiler;
taskProfiler yellowLedProfiler = 0;
taskProfiler greenLedProfiler = 0;
taskProfiler blueLedProfiler = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  xTaskCreate(yellowLedControllerTask, "YellowLED",128,NULL,1,NULL);
  xTaskCreate(greenLedControllerTask, "greenLED",128,NULL,1,NULL);
  xTaskCreate(blueLedControllerTask, "blueLED",128,NULL,1,NULL);
}
void yellowLedControllerTask(void *pvParameters){
  pinMode(YELLOW, OUTPUT);
  while(1){
    SET(YELLOW);
    yellowLedProfiler++;
    Serial.print("YellowLedProfiler: ");Serial.println(yellowLedProfiler);
    RESET(YELLOW);
    delay(30);     
  }
}
void greenLedControllerTask(void *pvParameters){
  pinMode(GREEN, OUTPUT);
  while(1){
    SET(GREEN);
    greenLedProfiler++;
    Serial.print("greenLedProfiler: ");Serial.println(greenLedProfiler);
    RESET(GREEN);
    delay(30);
  }
}
void blueLedControllerTask(void *pvParameters){
  pinMode(BLUE, OUTPUT);
  while(1){
    SET(BLUE);
    blueLedProfiler++;
    Serial.print("blueLedProfiler: ");Serial.println(blueLedProfiler);
    RESET(BLUE);
    delay(30);
  }
}
void loop() {
}

The use of preemption is controlled by the configUSE_PREEMTION flag in FreeRTOSConfig.h

Note, the function loop becomes part of a task, so should include at least a delay.

I don’t know if the “delay” function does a “spin wait” or calls vTaskDelay, so I would avoid using it (unless you can confirm it calls FreeRTOS) and do actual blocking with vTaskDelay.

1 Like

In addition to avoid trouble better define configASSERT and enable stack checking during development/debug builds.
The (minimal) task stacks are probably too small because you’re using stack hungry print functions in your task code.

1 Like

Thanks for the response, it really helped me.

as far as i know, delay is implemented using timer0 function in arduino library and it does not depend on vTaskDelay() function of FreeRTOS.

Thanks.

Noted. I will try doing so.

And delays that work by just waiting (by busy-loop testing) for the time to pass do not provide for the multi-tasking that is desired with FreeRTOS.

1 Like