Task switching does not work without osDelay in loop

erikclaij wrote on Tuesday, June 13, 2017:


In this simple example, I have created a task where I start a Timer (for a blinking LED). After the timer is running a start the endless loop. The LED will only blink if I include the osDelay(1) function in it.

Am I missing something?


void ControlCenter(void const *argument)
    osTimerId activityLedID;

    osTimerDef(activityLed, ActivityLed);
    activityLedID = osTimerCreate(osTimer(activityLed), osTimerPeriodic, NULL);
    osTimerStart(activityLedID, 20UL);

      osDelay(1); // Why do I need this ????

hs2sf wrote on Tuesday, June 13, 2017:

Check your task prio vs. configTIMER_TASK_PRIORITY.
Seems that your task starves the timer task due to running at higher prio than the timer service. The delay puts your task in blocked state and allows the timer task to run for a while.
You should block your task(s) somehow (waiting for an event) to avoid hogging the CPU doing nothing.

richard_damon wrote on Wednesday, June 14, 2017:

Another posibility is that you have configured FreeRTOS to not use preemption, in which case the loop needs some call that allows FreeRTOS to preempt the task.

Also, there is no reason for a user task to just have a while(1) {} loop, a task should only use the CPU when it has something to do, and when it doesn’t, it should block till it has something (or exit if it is totally done).

erikclaij wrote on Thursday, June 15, 2017:

Thanks, it was indeed a prio problem.