tasks.c File Reference
#include <stdlib.h>
#include <string.h>
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "StackMacros.h"
#include "tasks_mod.c.inc"
Include dependency graph for tasks.c:

Data Structures

struct  tskTaskControlBlock
 

Macros

#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
 
#define taskYIELD_IF_USING_PREEMPTION()
 
#define taskNOT_WAITING_NOTIFICATION   ( ( uint8_t ) 0 )
 
#define taskWAITING_NOTIFICATION   ( ( uint8_t ) 1 )
 
#define taskNOTIFICATION_RECEIVED   ( ( uint8_t ) 2 )
 
#define tskSTACK_FILL_BYTE   ( 0xa5U )
 
#define tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE   ( ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) || ( portUSING_MPU_WRAPPERS == 1 ) )
 
#define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB   ( ( uint8_t ) 0 )
 
#define tskSTATICALLY_ALLOCATED_STACK_ONLY   ( ( uint8_t ) 1 )
 
#define tskSTATICALLY_ALLOCATED_STACK_AND_TCB   ( ( uint8_t ) 2 )
 
#define tskBLOCKED_CHAR   ( 'B' )
 
#define tskREADY_CHAR   ( 'R' )
 
#define tskDELETED_CHAR   ( 'D' )
 
#define tskSUSPENDED_CHAR   ( 'S' )
 
#define taskRECORD_READY_PRIORITY(uxPriority)
 
#define taskSELECT_HIGHEST_PRIORITY_TASK()
 
#define taskRESET_READY_PRIORITY(uxPriority)
 
#define portRESET_READY_PRIORITY(uxPriority, uxTopReadyPriority)
 
#define taskSWITCH_DELAYED_LISTS()
 
#define prvAddTaskToReadyList(pxTCB)
 
#define prvGetTCBFromHandle(pxHandle)   ( ( ( pxHandle ) == NULL ) ? ( TCB_t * ) pxCurrentTCB : ( TCB_t * ) ( pxHandle ) )
 
#define taskEVENT_LIST_ITEM_VALUE_IN_USE   0x80000000UL
 

Typedefs

typedef struct tskTaskControlBlock tskTCB
 
typedef tskTCB TCB_t
 

Functions

void vTaskPrioritySet (TaskHandle_t xTask, UBaseType_t uxNewPriority)
 
void vTaskStartScheduler (void)
 
void vTaskEndScheduler (void)
 
void vTaskSuspendAll (void)
 
BaseType_t xTaskResumeAll (void)
 
TickType_t xTaskGetTickCount (void)
 
TickType_t xTaskGetTickCountFromISR (void)
 
UBaseType_t uxTaskGetNumberOfTasks (void)
 
char * pcTaskGetName (TaskHandle_t xTaskToQuery)
 
UBaseType_t uxTaskGetSystemState (TaskStatus_t *const pxTaskStatusArray, const UBaseType_t uxArraySize, uint32_t *const pulTotalRunTime)
 
BaseType_t xTaskIncrementTick (void)
 
void vTaskSwitchContext (void)
 
void vTaskPlaceOnEventList (List_t *const pxEventList, const TickType_t xTicksToWait)
 
void vTaskPlaceOnUnorderedEventList (List_t *pxEventList, const TickType_t xItemValue, const TickType_t xTicksToWait)
 
BaseType_t xTaskRemoveFromEventList (const List_t *const pxEventList)
 
BaseType_t xTaskRemoveFromUnorderedEventList (ListItem_t *pxEventListItem, const TickType_t xItemValue)
 
void vTaskSetTimeOutState (TimeOut_t *const pxTimeOut)
 
BaseType_t xTaskCheckForTimeOut (TimeOut_t *const pxTimeOut, TickType_t *const pxTicksToWait)
 
void vTaskMissedYield (void)
 
void vTaskGetInfo (TaskHandle_t xTask, TaskStatus_t *pxTaskStatus, BaseType_t xGetFreeStackSpace, eTaskState eState)
 
TickType_t uxTaskResetEventItemValue (void)
 

Variables

PRIVILEGED_DATA TCB_t *volatile pxCurrentTCB = NULL
 

Macro Definition Documentation

#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
#define portRESET_READY_PRIORITY (   uxPriority,
  uxTopReadyPriority 
)
#define prvAddTaskToReadyList (   pxTCB)
Value:
taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
vListInsertEnd( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
#define tracePOST_MOVED_TASK_TO_READY_STATE(pxTCB)
Definition: FreeRTOS.h:418
void vListInsertEnd(List_t *const pxList, ListItem_t *const pxNewListItem) PRIVILEGED_FUNCTION
Definition: list.c:116
#define traceMOVED_TASK_TO_READY_STATE(pxTCB)
Definition: FreeRTOS.h:414
#define taskRECORD_READY_PRIORITY(uxPriority)
Definition: tasks.c:164
#define prvGetTCBFromHandle (   pxHandle)    ( ( ( pxHandle ) == NULL ) ? ( TCB_t * ) pxCurrentTCB : ( TCB_t * ) ( pxHandle ) )
#define taskEVENT_LIST_ITEM_VALUE_IN_USE   0x80000000UL
#define taskNOT_WAITING_NOTIFICATION   ( ( uint8_t ) 0 )
#define taskNOTIFICATION_RECEIVED   ( ( uint8_t ) 2 )
#define taskRECORD_READY_PRIORITY (   uxPriority)
Value:
{ \
if( ( uxPriority ) > uxTopReadyPriority ) \
{ \
uxTopReadyPriority = ( uxPriority ); \
} \
} /* taskRECORD_READY_PRIORITY */
#define taskRESET_READY_PRIORITY (   uxPriority)
#define taskSELECT_HIGHEST_PRIORITY_TASK ( )
Value:
{ \
UBaseType_t uxTopPriority = uxTopReadyPriority; \
\
/* Find the highest priority queue that contains ready tasks. */ \
while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
{ \
configASSERT( uxTopPriority ); \
--uxTopPriority; \
} \
\
/* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
the same priority get an equal share of the processor time. */ \
listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
uxTopReadyPriority = uxTopPriority; \
} /* taskSELECT_HIGHEST_PRIORITY_TASK */
#define listGET_OWNER_OF_NEXT_ENTRY(pxTCB, pxList)
Definition: list.h:318
PRIVILEGED_DATA TCB_t *volatile pxCurrentTCB
Definition: tasks.c:372
#define listLIST_IS_EMPTY(pxList)
Definition: list.h:291
unsigned long UBaseType_t
Definition: portmacro.h:99
#define configASSERT(x)
Definition: FreeRTOS.h:276
#define taskSWITCH_DELAYED_LISTS ( )
Value:
{ \
List_t *pxTemp; \
\
/* The delayed tasks list should be empty when the lists are switched. */ \
configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
\
pxTemp = pxDelayedTaskList; \
pxDelayedTaskList = pxOverflowDelayedTaskList; \
pxOverflowDelayedTaskList = pxTemp; \
xNumOfOverflows++; \
prvResetNextTaskUnblockTime(); \
}
#define listLIST_IS_EMPTY(pxList)
Definition: list.h:291
#define configASSERT(x)
Definition: FreeRTOS.h:276
struct xLIST List_t
#define taskWAITING_NOTIFICATION   ( ( uint8_t ) 1 )
#define taskYIELD_IF_USING_PREEMPTION ( )
#define tskBLOCKED_CHAR   ( 'B' )
#define tskDELETED_CHAR   ( 'D' )
#define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB   ( ( uint8_t ) 0 )
#define tskREADY_CHAR   ( 'R' )
#define tskSTACK_FILL_BYTE   ( 0xa5U )
#define tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE   ( ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) || ( portUSING_MPU_WRAPPERS == 1 ) )
#define tskSTATICALLY_ALLOCATED_STACK_AND_TCB   ( ( uint8_t ) 2 )
#define tskSTATICALLY_ALLOCATED_STACK_ONLY   ( ( uint8_t ) 1 )
#define tskSUSPENDED_CHAR   ( 'S' )

Typedef Documentation

typedef tskTCB TCB_t
typedef struct tskTaskControlBlock tskTCB

Function Documentation

char* pcTaskGetName ( TaskHandle_t  xTaskToQuery)
UBaseType_t uxTaskGetNumberOfTasks ( void  )
UBaseType_t uxTaskGetSystemState ( TaskStatus_t *const  pxTaskStatusArray,
const UBaseType_t  uxArraySize,
uint32_t *const  pulTotalRunTime 
)

configUSE_TRACE_FACILITY must be defined as 1 in FreeRTOSConfig.h for uxTaskGetSystemState() to be available.

uxTaskGetSystemState() populates an TaskStatus_t structure for each task in the system. TaskStatus_t structures contain, among other things, members for the task handle, task name, task priority, task state, and total amount of run time consumed by the task. See the TaskStatus_t structure definition in this file for the full member list.

NOTE: This function is intended for debugging use only as its use results in the scheduler remaining suspended for an extended period.

Parameters
pxTaskStatusArrayA pointer to an array of TaskStatus_t structures. The array must contain at least one TaskStatus_t structure for each task that is under the control of the RTOS. The number of tasks under the control of the RTOS can be determined using the uxTaskGetNumberOfTasks() API function.
uxArraySizeThe size of the array pointed to by the pxTaskStatusArray parameter. The size is specified as the number of indexes in the array, or the number of TaskStatus_t structures contained in the array, not by the number of bytes in the array.
pulTotalRunTimeIf configGENERATE_RUN_TIME_STATS is set to 1 in FreeRTOSConfig.h then *pulTotalRunTime is set by uxTaskGetSystemState() to the total run time (as defined by the run time stats clock, see http://www.freertos.org/rtos-run-time-stats.html) since the target booted. pulTotalRunTime can be set to NULL to omit the total run time information.
Returns
The number of TaskStatus_t structures that were populated by uxTaskGetSystemState(). This should equal the number returned by the uxTaskGetNumberOfTasks() API function, but will be zero if the value passed in the uxArraySize parameter was too small.

Example usage:

 // This example demonstrates how a human readable table of run time stats
 // information is generated from raw data provided by uxTaskGetSystemState().
 // The human readable table is written to pcWriteBuffer
 void vTaskGetRunTimeStats( char *pcWriteBuffer )
 {
 TaskStatus_t *pxTaskStatusArray;
 volatile UBaseType_t uxArraySize, x;
 uint32_t ulTotalRunTime, ulStatsAsPercentage;
    // Make sure the write buffer does not contain a string.
    *pcWriteBuffer = 0x00;
    // Take a snapshot of the number of tasks in case it changes while this
    // function is executing.
    uxArraySize = uxTaskGetNumberOfTasks();
    // Allocate a TaskStatus_t structure for each task.  An array could be
    // allocated statically at compile time.
    pxTaskStatusArray = pvPortMalloc( uxArraySize * sizeof( TaskStatus_t ) );
    if( pxTaskStatusArray != NULL )
    {
        // Generate raw status information about each task.
        uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalRunTime );
        // For percentage calculations.
        ulTotalRunTime /= 100UL;
        // Avoid divide by zero errors.
        if( ulTotalRunTime > 0 )
        {
            // For each populated position in the pxTaskStatusArray array,
            // format the raw data as human readable ASCII data
            for( x = 0; x < uxArraySize; x++ )
            {
                // What percentage of the total run time has the task used?
                // This will always be rounded down to the nearest integer.
                // ulTotalRunTimeDiv100 has already been divided by 100.
                ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalRunTime;
                if( ulStatsAsPercentage > 0UL )
                {
                    sprintf( pcWriteBuffer, "%s\t\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].pcTaskName, pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
                }
                else
                {
                    // If the percentage is zero here then the task has
                    // consumed less than 1% of the total run time.
                    sprintf( pcWriteBuffer, "%s\t\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].pcTaskName, pxTaskStatusArray[ x ].ulRunTimeCounter );
                }
                pcWriteBuffer += strlen( ( char * ) pcWriteBuffer );
            }
        }
        // The array is no longer needed, free the memory it consumes.
        vPortFree( pxTaskStatusArray );
    }
 }
 
TickType_t uxTaskResetEventItemValue ( void  )
void vTaskEndScheduler ( void  )
void vTaskGetInfo ( TaskHandle_t  xTask,
TaskStatus_t pxTaskStatus,
BaseType_t  xGetFreeStackSpace,
eTaskState  eState 
)
void vTaskMissedYield ( void  )
void vTaskPlaceOnEventList ( List_t *const  pxEventList,
const TickType_t  xTicksToWait 
)
void vTaskPlaceOnUnorderedEventList ( List_t pxEventList,
const TickType_t  xItemValue,
const TickType_t  xTicksToWait 
)
void vTaskPrioritySet ( TaskHandle_t  xTask,
UBaseType_t  uxNewPriority 
)
void vTaskSetTimeOutState ( TimeOut_t *const  pxTimeOut)
void vTaskStartScheduler ( void  )
void vTaskSuspendAll ( void  )
void vTaskSwitchContext ( void  )
BaseType_t xTaskCheckForTimeOut ( TimeOut_t *const  pxTimeOut,
TickType_t *const  pxTicksToWait 
)
TickType_t xTaskGetTickCount ( void  )
TickType_t xTaskGetTickCountFromISR ( void  )
BaseType_t xTaskIncrementTick ( void  )
BaseType_t xTaskRemoveFromEventList ( const List_t *const  pxEventList)
BaseType_t xTaskRemoveFromUnorderedEventList ( ListItem_t pxEventListItem,
const TickType_t  xItemValue 
)
BaseType_t xTaskResumeAll ( void  )

Variable Documentation

PRIVILEGED_DATA TCB_t* volatile pxCurrentTCB = NULL