FreeRTOS Task and Scheduler APIs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11
At a glance
Powered by AI
The document discusses the various APIs provided by FreeRTOS for tasks, scheduling, queues, semaphores, timers and event groups. It also covers concepts like tasks vs coroutines, notifications and stack overflow detection.

The main task and scheduler APIs discussed are taskCREATE(), vTaskDelay(), vTaskDelete(), vTaskPrioritySet(), xTaskGetSchedulerState() and configUSE_TIMERS.

The main queue APIs discussed are xQueueCreate(), xQueueSend(), xQueueReceive(), xQueuePeek(), xQueueOverwrite() and xQueueSelectFromSet().

Task and Scheduler APIs

******************************************************
portSWITCH_TO_USER_MODE()
vTaskAllocateMPURegions() MPU
xTaskCallApplicationHook()
xTaskCreate()
xTaskCreateRestricted()
vTaskDelay()
vTaskDelayUntil()
vTaskDelete()
taskDISABLE_INTERRUPTS()
taskENABLE_INTERRUPTS()
taskENTER_CRITICAL()
taskEXIT_CRITICAL()
xTaskGetApplicationTaskTag()
vTaskSetApplicationTaskTag()
xTaskGetCurrentTaskHandle()
xTaskGetIdleTaskHandle()

INCLUDE_xTaskGetIdleTaskHandle

uxTaskGetNumberOfTasks()
vTaskGetRunTimeStats()
xTaskGetSchedulerState()

INCLUDE_xTaskGetSchedulerState or

configUSE_TIMERS

uxTaskGetStackHighWaterMark()
eTaskGetState()
uxTaskGetSystemState()
pcTaskGetTaskName()
xTaskGetTickCount()
xTaskGetTickCountFromISR()

INCLUDE_eTaskGetState

vTaskList()

configUSE_TRACE_FACILITY and
configUSE_STATS_FORMATTING_FUNCTIONS

vTaskStartTrace
void vTaskStartTrace( char * pcBuffer, unsigned long ulBufferSize );

ulTaskEndTrace

xTaskNotify()
xTaskNotifyFromISR()
xTaskNotifyGive()
xTaskNotifyGiveFromISR()

ulTaskNotifyTake()
xTaskNotifyWait()
uxTaskPriorityGet()
vTaskPrioritySet()
vTaskResume()
xTaskResumeAll()
xTaskResumeFromISR()
vTaskSetApplicationTaskTag()
vTaskStartScheduler()
vTaskStepTick()
vTaskSuspend()
vTaskSuspendAll()
taskYIELD()

* Never Call from Critical Section

Queue APIs.
*************************************************************
****
vQueueAddToRegistry()

xQueueAddToSet()
xQueueCreate()
xQueueCreateSet()
vQueueDelete()
xQueueIsQueueEmptyFromISR()
xQueueIsQueueFullFromISR()
uxQueueMessagesWaiting()
uxQueueMessagesWaitingFromISR()
xQueueOverwrite()
xQueueOverwriteFromISR()
xQueuePeek()
xQueuePeekFromISR()
xQueueReceive()
xQueueReceiveFromISR()
xQueueRemoveFromSet()
xQueueReset()
xQueueSelectFromSet()
xQueueSelectFromSetFromISR()
xQueueSend(), xQueueSendToFront(), xQueueSendToBack()
xQueueSendFromISR(), xQueueSendToBackFromISR(),
xQueueSendToFrontFromISR()
uxQueueSpacesAvailable()
Semaphore APIs
*************************************************************
**
vSemaphoreCreateBinary()
xSemaphoreCreateBinary()
xSemaphoreCreateCounting()

xSemaphoreCreateMutex()
xSemaphoreCreateRecursiveMutex()
vSemaphoreDelete()
xSemaphoreGetMutexHolder()
xSemaphoreGive()
xSemaphoreGiveFromISR()
xSemaphoreGiveRecursive()
xSemaphoreTake()
xSemaphoreTakeFromISR()
xSemaphoreTakeRecursive()

Software Timer API


***********************************************************
xTimerChangePeriod()
xTimerChangePeriodFromISR()
xTimerCreate()
xTimerDelete()
xTimerGetTimerDaemonTaskHandle()
pvTimerGetTimerID()

pcTimerGetTimerName()
xTimerIsTimerActive()
xTimerPendFunctionCall()
xTimerPendFunctionCallFromISR()
xTimerReset()
xTimerResetFromISR()
xTimerStart()
xTimerStartFromISR()
xTimerStop()
xTimerStopFromISR()

Event Groups API


*************************************************************
xEventGroupCreate()
xEventGroupClearBits()
xEventGroupClearBitsFromISR()
vEventGroupDelete()
xEventGroupGetBits()
xEventGroupGetBitsFromISR()
xEventGroupSetBits()
xEventGroupSetBitsFromISR()
xEventGroupSync()
xEventGroupWaitBits()
Kernel Configuration.
FreeRTOSConfig.h
Constants that Start INCLUDE_

Constants that Start config


Special functions
BaseType_txTimerPendFunctionCallFromISR(
PendedFunction_txFunctionToPend,
void*pvParameter1,
uint32_tulParameter2,
BaseType_t*pxHigherPriorityTaskWoken);
void xFunctionToPend

( void *pvParameter1, uint32_t


ulParameter2 )

Naming Conventions
The RTOS kernel and demo application source code use the following conventions:

Variables
o

Variables of type uint32_t are prefixed ul, where the 'u' denotes 'unsigned' and the
'l' denotes 'long'.

Variables of type uint16_t are prefixed us, where the 'u' denotes 'unsigned' and
the 's' denotes 'short'.

Variables of type uint8_t are prefixed uc, where the 'u' denotes 'unsigned' and the
'c' denotes 'char'.

Variables of non stdint types are prefixed x. Examples include BaseType_t


and TickType_t, which are portable layer defined typedefs for the natural or
most efficient type for the architecture and the type used to hold the RTOS
tick count respectively.

Unsigned variables of non stdint types have an additional prefix u. For example
variables of type UBaseType_t (unsigned BaseType_t) are prefixed ux.

Variables of type size_t are also prefixed x.

Enumerated variables are prefixed e

Pointers have an additional prefixed p, for example a pointer to a uint16_t will


have prefix pus.

In line with MISRA guides, unqualified standard char types are only permitted to
hold ASCII characters and are prefixed c.

In line with MISRA guides, variables of type char * are only permitted to hold
pointers to ASCII strings and are prefixed pc.

Functions
o

File scope static (private) functions are prefixed with prv.

API functions are prefixed with their return type, as per the convention
defined for variables, with the addition of the prefix v for void.

API function names start with the name of the file in which they are defined. For
example vTaskDelete is defined in tasks.c, and has a void return type.

Macros
o

Macros are pre-fixed with the file in which they are defined. The pre-fix is
lower case. For example, configUSE_PREEMPTION is defined in
FreeRTOSConfig.h.

Other than the pre-fix, macros are written in all upper case, and use an
underscore to separate words.

Tasks vs Co-Routines

Co-routines
All the co-routines within an application share a single stack. This greatly reduces the amount of
RAM required compared to a similar application written using tasks.
Co-routines use prioritised cooperative scheduling with respect to other co-routines, but can be
included in an application that uses preemptive tasks.

vACo_RoutineFunction(

void

CoRoutineHandle_t xHandle,
UBaseType_t uxIndex )

crSTART( xHandle );

for( ;; )
{
-- Co-routine application code here. -}
}

crEND();

xCoRoutineCreate().
crDELAY()

vCoRoutineSchedule();

Tasks
Each task executes within its own context with no coincidental dependency on other
tasks within the system or the RTOS scheduler itself. Only one task within the
application can be executing at any point in time and the real time RTOS scheduler is
responsible for deciding which task this should be.
/* Create the task. */
if( xTaskCreate(
vATask,
/* Pointer to the function that implements
the task. */
"Demo task",
/* Text name given to the task. */
STACK_SIZE,
/* The size of the stack that should be created
for the task. This is defined in words, not
bytes. */
NULL,
/* The task does not use the
parameter. */
TASK_PRIORITY, /* The priority to assign to the newly created
task. */
&xHandle
/* The handle to the task being created will be
placed in xHandle. */
) == pdPASS )

Direct Notifications
E.g
vTaskNotifyGiveFromISR(xHandlingTask,&xHigherPriorityTaskWoken);

if(xHigherPriorityTaskWoken==pdTRUE)
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);

Global Variable Alternative


vTaskSetThreadLocalStoragePointer()
pvTaskGetThreadLocalStoragePointer()

Stack Overflow Detection


configCHECK_FOR_STACK_OVERFLOW to 1
The stack overflow hook function is called if the stack pointer contain a value that is outside of the
valid stack range.

configCHECK_FOR_STACK_OVERFLOW to 2
When a task is first created its stack is filled with a known value. When swapping a task out of the
Running state the RTOS kernel can check the last 16 bytes within the valid stack range to ensure
that these known values have not been overwritten by the task or interrupt activity.

TRACE
E.g
traceTASK_SWITCHED_OUT()
traceBLOCKING_ON_QUEUE_SEND()
traceQUEUE_CREATE_FAILED()
traceCREATE_MUTEX_FAILED ()
traceQUEUE_DELETE ()

You might also like