Jeremy

LanguageENZH

freeRTOS API Reference

Update: 2026-07-01

1.Task

Task Creation


// Create a new task and add it to the list of tasks that are ready to run.
BaseType_t xTaskCreate( TaskFunction_t pvTaskCode,
                         const char * const pcName,
                         const configSTACK_DEPTH_TYPE uxStackDepth,
                         void *pvParameters,
                         UBaseType_t uxPriority,
                         TaskHandle_t *pxCreatedTask
                       );

// Create a new task and add it to the list of tasks that are ready to run.
TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
                                 const char * const pcName,
                                 const uint32_t ulStackDepth,
                                 void * const pvParameters,
                                 UBaseType_t uxPriority,
                                 StackType_t * const puxStackBuffer,
                                 StaticTask_t * const pxTaskBuffer );

/**
 * Create a new Memory Protection Unit (MPU) restricted task and add it to the list of tasks that are ready to run.
 */
BaseType_t xTaskCreateRestrictedStatic( TaskParameters_t *pxTaskDefinition,
                                        TaskHandle_t *pxCreatedTask );


// Remove a task from the RTOS kernels management. The task being deleted will be removed from all ready, blocked, suspended and event lists.
void vTaskDelete( TaskHandle_t xTask );

Task Control


// Delay a task for a given number of ticks. The actual time that the task remains blocked depends on the tick rate.
void vTaskDelay( const TickType_t xTicksToDelay );

// Delay a task until a specified time.
void vTaskDelayUntil( TickType_t *pxPreviousWakeTime,
                      const TickType_t xTimeIncrement );

UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask );

void vTaskPrioritySet( TaskHandle_t xTask,
                       UBaseType_t uxNewPriority );

// Suspend any task. When suspended a task will never get any microcontroller processing time, no matter what its priority.
void vTaskSuspend( TaskHandle_t xTaskToSuspend );

// Resumes a suspended task.
void vTaskResume( TaskHandle_t xTaskToResume );

// A function to resume a suspended task that can be called from within an ISR.
BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume );

// Forces a task to leave the Blocked state, and enter the Ready state, even if the event the task was in the Blocked state to wait for has not occurred, and any specified timeout has not expired.
BaseType_t xTaskAbortDelay( TaskHandle_t xTask );

// Obtain the priority of any task. This function is safe to use from within an interrupt service routine (ISR).
UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask );

// Obtain the base priority of any task.
UBaseType_t uxTaskBasePriorityGet( const TaskHandle_t xTask );

// Obtain the base priority of any task.
UBaseType_t uxTaskBasePriorityGetFromISR( const TaskHandle_t xTask );

Task Utilities


UBaseType_t uxTaskGetSystemState(
                       TaskStatus_t * const pxTaskStatusArray,
                       const UBaseType_t uxArraySize,
                       unsigned long * const pulTotalRunTime );

void vTaskGetInfo( TaskHandle_t xTask,
                   TaskStatus_t *pxTaskStatus,
                   BaseType_t xGetFreeStackSpace,
                   eTaskState eState );

TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask );
TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask );


UBaseType_t uxTaskGetStackHighWaterMark
 ( TaskHandle_t xTask );

configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2
 ( TaskHandle_t xTask );


BaseType_t xTaskCallApplicationTaskHook(
                                         TaskHandle_t xTask,
                                         void *pvParameter );

void vTaskSetApplicationTaskTag(
                                 TaskHandle_t xTask,
                                 TaskHookFunction_t pxTagValue );

void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
                                        BaseType_t xIndex,
                                        void *pvValue )

void *pvTaskGetThreadLocalStoragePointer(
                                 TaskHandle_t xTaskToQuery,
                                 BaseType_t xIndex );

void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut );

BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
                                 TickType_t * const pxTicksToWait );

eSleepModeStatus eTaskConfirmSleepModeStatus( void );

2. RTOS kernel control


// The taskENTER_CRITICAL() and taskEXIT_CRITICAL() macros provide a basic critical section implementation that works by simply disabling interrupts, either globally, or up to a specific interrupt priority level. See the vTaskSuspendAll() RTOS API function for information on creating a critical section without disabling interrupts.
void taskENTER_CRITICAL( void );
void taskEXIT_CRITICAL( void );

// The taskENTER_CRITICAL_FROM_ISR() and taskEXIT_CRITICAL_FROM_ISR() macros provide a basic critical section implementation that works by simply disabling interrupts, either globally, or up to a specific interrupt priority level.
UBaseType_t taskENTER_CRITICAL_FROM_ISR( void );
void taskEXIT_CRITICAL_FROM_ISR( UBaseType_t uxSavedInterruptStatus );

// Starts the RTOS scheduler. After calling the RTOS kernel has control over which tasks are executed and when.
void vTaskStartScheduler( void );

// Stops the RTOS kernel tick.
void vTaskEndScheduler( void );

// Suspends the scheduler.
void vTaskSuspendAll( void );

// Resumes the scheduler after it was suspended using a call to vTaskSuspendAll().
BaseType_t xTaskResumeAll( void );

void vTaskStepTick( TickType_t xTicksToJump );

BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp );

Direct to task notifications


// ulTaskNotifyTake() and ulTaskNotifyTakeIndexed() are equivalent macros - the only difference being ulTaskNotifyTakeIndexed() can operate on any task notification within the array and ulTaskNotifyTake() always operates on the task notification at array index 0.
BaseType_t xTaskNotifyGive( TaskHandle_t xTaskToNotify );

BaseType_t xTaskNotifyGiveIndexed( TaskHandle_t xTaskToNotify,
                                  UBaseType_t uxIndexToNotify );

// Versions of xTaskNotifyGive() and xTaskNotifyGiveIndexed() that can be used from an interrupt service routine (ISR).
void vTaskNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
                             BaseType_t *pxHigherPriorityTaskWoken );

void vTaskNotifyGiveIndexedFromISR( TaskHandle_t xTaskHandle,
                                    UBaseType_t uxIndexToNotify,
                                    BaseType_t *pxHigherPriorityTaskWoken );

uint32_t ulTaskNotifyTake( BaseType_t xClearCountOnExit,
                           TickType_t xTicksToWait );

uint32_t ulTaskNotifyTakeIndexed( UBaseType_t uxIndexToWaitOn,
                                  BaseType_t xClearCountOnExit,
                                  TickType_t xTicksToWait );

BaseType_t xTaskNotify( TaskHandle_t xTaskToNotify,
                        uint32_t ulValue,
                        eNotifyAction eAction );

BaseType_t xTaskNotifyIndexed( TaskHandle_t xTaskToNotify,
                              UBaseType_t uxIndexToNotify,
                              uint32_t ulValue,
                              eNotifyAction eAction );

// xTaskNotifyAndQueryIndexed() performs the same operation as xTaskNotifyIndexed() with the addition that it also returns the target task's prior notification value
BaseType_t xTaskNotifyAndQuery( TaskHandle_t xTaskToNotify,
                                uint32_t ulValue,
                                eNotifyAction eAction,
                                uint32_t *pulPreviousNotifyValue );

BaseType_t xTaskNotifyAndQueryIndexed( TaskHandle_t xTaskToNotify,
                                      UBaseType_t uxIndexToNotify,
                                      uint32_t ulValue,
                                      eNotifyAction eAction,
                                      uint32_t *pulPreviousNotifyValue );

BaseType_t xTaskNotifyAndQueryFromISR(
                    TaskHandle_t xTaskToNotify,
                    uint32_t ulValue,
                    eNotifyAction eAction,
                    uint32_t *pulPreviousNotifyValue,
                    BaseType_t *pxHigherPriorityTaskWoken );

BaseType_t xTaskNotifyAndQueryIndexedFromISR(
                    TaskHandle_t xTaskToNotify,
                    UBaseType_t uxIndexToNotify
                    uint32_t ulValue,
                    eNotifyAction eAction,
                    uint32_t *pulPreviousNotifyValue,
                    BaseType_t *pxHigherPriorityTaskWoken );


 BaseType_t xTaskNotifyFromISR( TaskHandle_t xTaskToNotify,
                                uint32_t ulValue,
                                eNotifyAction eAction,
                                BaseType_t *pxHigherPriorityTaskWoken );

 BaseType_t xTaskNotifyIndexedFromISR( TaskHandle_t xTaskToNotify,
                                       UBaseType_t uxIndexToNotify,
                                       uint32_t ulValue,
                                       eNotifyAction eAction,
                                       BaseType_t *pxHigherPriorityTaskWoken );


// xTaskNotifyWait() waits, with an optional timeout, for the calling task to receive a notification. If the receiving RTOS task was already Blocked waiting for a notification when the notification it is waiting for arrives the receiving RTOS task will be removed from the Blocked state and the notification cleared.
BaseType_t xTaskNotifyWait( uint32_t ulBitsToClearOnEntry,
                            uint32_t ulBitsToClearOnExit,
                            uint32_t *pulNotificationValue,
                            TickType_t xTicksToWait );

BaseType_t xTaskNotifyWaitIndexed( UBaseType_t uxIndexToWaitOn,
                                  uint32_t ulBitsToClearOnEntry,
                                  uint32_t ulBitsToClearOnExit,
                                  uint32_t *pulNotificationValue,
                                  TickType_t xTicksToWait );

// If a notification is sent to an index within the array of notifications then the notification at that index is said to be 'pending' until the task reads its notification value or explicitly clears the notification state to 'not pending' by calling xTaskNotifyStateClear().

BaseType_t xTaskNotifyStateClear( TaskHandle_t xTask );

BaseType_t xTaskNotifyStateClearIndexed( TaskHandle_t xTask,
                                         UBaseType_t uxIndexToClear );


uint32_t ulTaskNotifyValueClear( TaskHandle_t xTask,
                                 uint32_t ulBitsToClear );

uint32_t ulTaskNotifyValueClearIndexed( TaskHandle_t xTask,
                                        UBaseType_t uxIndexToClear,
                                        uint32_t ulBitsToClear );

3.Queues

Queue Management


// Creates a new queue and returns a handle by which the queue can be referenced.
QueueHandle_t xQueueCreate( UBaseType_t uxQueueLength,
                            UBaseType_t uxItemSize );

QueueHandle_t xQueueCreateStatic(
                            UBaseType_t uxQueueLength,
                            UBaseType_t uxItemSize,
                            uint8_t *pucQueueStorageBuffer,
                            StaticQueue_t *pxQueueBuffer );

// Post an item on a queue. The item is queued by copy, not by reference.
BaseType_t xQueueSend(
                      QueueHandle_t xQueue,
                      const void * pvItemToQueue,
                      TickType_t xTicksToWait
                    );

BaseType_t xQueueSendFromISR(
                              QueueHandle_t xQueue,
                              const void *pvItemToQueue,
                              BaseType_t *pxHigherPriorityTaskWoken
                            );

// Post an item to the back of a queue. The item is queued by copy, not by reference.
BaseType_t xQueueSendToBack(
                            QueueHandle_t xQueue,
                            const void * pvItemToQueue,
                            TickType_t xTicksToWait
                          );

BaseType_t xQueueSendToBackFromISR(
                                   QueueHandle_t xQueue,
                                   const void *pvItemToQueue,
                                   BaseType_t *pxHigherPriorityTaskWoken
                                  );

// Post an item to the front of a queue. The item is queued by copy, not by reference.
BaseType_t xQueueSendToFront( QueueHandle_t xQueue,
                              const void * pvItemToQueue,
                              TickType_t xTicksToWait
                            );

BaseType_t xQueueSendToFrontFromISR(
                                    QueueHandle_t xQueue,
                                    const void *pvItemToQueue,
                                    BaseType_t *pxHigherPriorityTaskWoken
                                   );
// Receive an item from a queue. The item is received by copy so a buffer of adequate size must be provided.
BaseType_t xQueueReceive(
                          QueueHandle_t xQueue,
                          void *pvBuffer,
                          TickType_t xTicksToWait
                        );

BaseType_t xQueueReceiveFromISR(
                                 QueueHandle_t xQueue,
                                 void *pvBuffer,
                                 BaseType_t *pxHigherPriorityTaskWoken
                               );

// A version of xQueueSendToBack() that will write to the queue even if the queue is full, overwriting data that is already held in the queue.
BaseType_t xQueueOverwrite(
                            QueueHandle_t xQueue,
                            const void * pvItemToQueue
                          );

BaseType_t xQueueOverwrite(
                           QueueHandle_t xQueue,
                           const void * pvItemToQueue,
                           BaseType_t *pxHigherPriorityTaskWoken
                          );

// This is a macro that calls the xQueueGenericReceive() function.
// Receive an item from a queue without removing the item from the queue.
BaseType_t xQueuePeek(
                      QueueHandle_t xQueue,
                      void *pvBuffer,
                      TickType_t xTicksToWait
                    );

BaseType_t xQueuePeekFromISR(
                              QueueHandle_t xQueue,
                              void *pvBuffer,
                             );

// Assigns a name to a queue and adds the queue to the registry.
void vQueueAddToRegistry(
                          QueueHandle_t xQueue,
                          char *pcQueueName,
                        );

// Removes a queue from the queue registry.
void vQueueUnregisterQueue( QueueHandle_t xQueue );

// Look up a queue name from the queue's handle.
const char *pcQueueGetName( QueueHandle_t xQueue );

// Retrieve pointers to a statically created queue's data structure buffer and storage area buffer. These are the same buffers that are supplied at the time of creation.
BaseType_t xQueueGetStaticBuffers( QueueHandle_t xQueue,
                                  uint8_t ** ppucQueueStorage,
                                  StaticQueue_t ** ppxStaticQueue );

Queue Set


// Queue sets provide a mechanism to allow an RTOS task to block (pend) on a read operation from multiple RTOS queues or semaphores simultaneously.
QueueSetHandle_t xQueueCreateSet(
                                 const UBaseType_t uxEventQueueLength
                                );

QueueSetHandle_t xQueueCreateSetStatic( const UBaseType_t uxEventQueueLength,
                                        uint8_t * pucQueueStorage,
                                        StaticQueue_t * pxStaticQueue );

// Adds an RTOS queue or semaphore to a queue set that was previously created by a call to xQueueCreateSet().
BaseType_t xQueueAddToSet(
                          QueueSetMemberHandle_t xQueueOrSemaphore,
                          QueueSetHandle_t xQueueSet
                         );

// Remove an RTOS queue or semaphore from a queue set.
BaseType_t xQueueRemoveFromSet(
                               QueueSetMemberHandle_t xQueueOrSemaphore,
                               QueueSetHandle_t xQueueSet
                              );

// xQueueSelectFromSet() effectively allows a task to block (pend) on a read operation on all the queues and semaphores in a queue set simultaneously.
QueueSetMemberHandle_t xQueueSelectFromSet(
                                           QueueSetHandle_t xQueueSet,
                                           const TickType_t xTicksToWait
                                          );

QueueSetMemberHandle_t xQueueSelectFromSetFromISR(
                                                  QueueSetHandle_t xQueueSet
                                                 );