cancel
Showing results for 
Search instead for 
Did you mean: 

FreeRTOS Idle Task Priority is not '1', is '0'

selracelosuarg
Associate II

Hello,
I am configuring a project from scratch for a STM32 FreeRTOS based.

From FreeRTOS documentation:

 

 

Low priority numbers denote low priority tasks.
The idle task has priority zero (tskIDLE_PRIORITY).

 

 

 

 

It is possible for application tasks to share the idle task priority (tskIDLE_PRIORITY).
See the configIDLE_SHOULD_YIELD configuration parameter for information on how this behaviour can be configured.

 

 

 

 

#define tskIDLE_PRIORITY			( ( unsigned portBASE_TYPE ) 0U )

 

 

This matches with what I can see from my project debugging:

selracelosuarg_0-1740160077699.png

But, I don't understant why in STM32 code, in 'cmsis_os2.h', IDLE thread priority is '1' instead of '0'.

 

 

/// Priority values.
typedef enum {
  osPriorityNone          =  0,         ///< No priority (not initialized).
  osPriorityIdle          =  1,         ///< Reserved for Idle thread.
  osPriorityLow           =  8,         ///< Priority: low

 

 

And also, the 'osThreadNew' does not let me create a task with priority '0', in 'cmsis_os2.c'

 

 

[...]
prio  = (UBaseType_t)osPriorityNormal;      
[...]
if (attr->priority != osPriorityNone) {
    prio = (UBaseType_t)attr->priority;
}
if ((prio < osPriorityIdle) || (prio > osPriorityISR) || ((attr->attr_bits & osThreadJoinable) == osThreadJoinable)) {
    return (NULL);
}
[...]

 

 

Is this a mistake? Or is the intended behaviour?

From my point of view:

  • There is a mistake in 'cmsis_os2.h', as IDLE task priority is not '1', but '0'.
  • 'osThreadNew' should let the developer to create a new task with priority '0'. In some architectures on other Microcontrollers I've been working with, although it may not be the 'best practice', we have decided to not use 'os_delays' in any task and they are just calling 'sched_yields' instead. Then all the system is build on top of priority '0' to make sure that 'IDLE' task can also enter in the scheduler, as it may run for sure as

 

 

The idle task is responsible for freeing memory allocated by the RTOS to tasks that have since been deleted.
It is therefore important in applications that make use of the vTaskDelete() function to ensure the idle task is not starved of processing time.
The idle task has no other active functions so can legitimately be starved of microcontroller time under all other conditions.

 

 

 

Looking forward for your answer,

Thanks,

Carles

 

 

 

 

2 REPLIES 2
MHoll.2
Senior

Cmsis_os2.c/h is wrapper to make FreeRTOS CMSIS compatible, so the priorities in cmsis_os2.h are different from the priorities used by FreeRTOS.

In FreeRTOS the Idle Thread is created automatically, you don't have to start or define it by yourself.

Martin

I understand it, thanks.

I already know that I don't need to create the Idle task by myself. My point is: if I want to work with FreeRTOS through CMSIS layer, I can not set another task with the same priority as the FreeRTOS Idle task, and that's a shame.

Thanks for your answer.