The tasks will be run according to:
- interrupt / priority (pre-emptive scheduling)
- round robin scheduling
Consider the following code
struct LedTaskParams {
int pin;
int flashPeriod;
};
void Task_FlashLed(void * pvParameters)
{
configASSERT((LedTaskParams*)pvParameters != nullptr);
const LedTaskParams data = *((LedTaskParams*)pvParameters);
const int led_pin = data.pin;
const TickType_t xOuterFrequency = pdMS_TO_TICKS(data.flashPeriod);
const TickType_t xInnerFrequency = pdMS_TO_TICKS(10);
TickType_t lastRun = xTaskGetTickCount(); // 1
TickType_t flashMarker;
while(true)
{
/*
flashMarker = xTaskGetTickCount(); // 2
digitalToggle(led_pin);
vTaskDelayUntil(&flashMarker, xInnerFrequency );
*/ // 3
digitalToggle(led_pin);
vTaskDelay(xInnerFrequency);
digitalToggle(led_pin);
vTaskDelayUntil(&lastRun, xOuterFrequency);
}
}
LedTaskParams redParams(1, 200);
LedTaskParams greenParams(2, 300);
LedTaskParams blueParams(3, 100);
xTaskCreate(Task_FlashLed, "red-led", 1024, (void *)&redParams, TASK_PRIO_NORMAL, NULL);
xTaskCreate(Task_FlashLed, "green-led", 1024, (void *)&greenParams, TASK_PRIO_NORMAL, NULL);
xTaskCreate(Task_FlashLed, "blue-led", 1024, (void *)&blueParams, TASK_PRIO_HIGH, NULL);
The Blue LED is prioritised TASK_PRIO_HIGH
, whilst the other two tasks are prioritised TASK_PRIO_NORMAL
. This means that the Blue LED’s task will be scheduled for execution ahead of Green and Red tasks.
The RTOS scheduler will look for highest priority tasks in the ready state, and will therefore pick up the blue task. The blue task will toggle the led pin and then it will yield control by calling xTaskDelayUntil()
The scheduler will then look for the task of highest priority in the ready state. This time, it will be red or green, and since they are of equal priority, the actual task selected will be based on a round robin system i.e. each task will be given equal run time in turn, until a higher priority task is in the ready state.
Hopefully that helps -even though it is a very high level description
Edited after @richard-damon’s response:
- corrected mismatched variable names (from
lastRun
to lastFlash
)
- initialized
flashMarker
prior to toggling the LED upon entry into the loop
- replaced call to
vTaskDelayUntil
with call to vTaskDelay