2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /* Standard includes. */
33 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
34 * all the API functions to use the MPU wrappers. That should only be done when
35 * task.h is included from an application file. */
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
38 /* FreeRTOS includes. */
42 #include "stack_macros.h"
44 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
45 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
46 * for the header files above, but not in this file, in order to generate the
47 * correct privileged Vs unprivileged linkage and placement. */
48 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
50 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
51 * functions but without including stdio.h here. */
52 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
54 /* At the bottom of this file are two optional functions that can be used
55 * to generate human readable text from the raw data generated by the
56 * uxTaskGetSystemState() function. Note the formatting functions are provided
57 * for convenience only, and are NOT considered part of the kernel. */
59 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
61 #if ( configUSE_PREEMPTION == 0 )
63 /* If the cooperative scheduler is being used then a yield should not be
64 * performed just because a higher priority task has been woken. */
65 #define taskYIELD_IF_USING_PREEMPTION()
67 #define taskYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
70 /* Values that can be assigned to the ucNotifyState member of the TCB. */
71 #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
72 #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
73 #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
76 * The value used to fill the stack of a task when the task is created. This
77 * is used purely for checking the high water mark for tasks.
79 #define tskSTACK_FILL_BYTE ( 0xa5U )
81 /* Bits used to record how a task's stack and TCB were allocated. */
82 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
83 #define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
84 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
86 /* If any of the following are set then task stacks are filled with a known
87 * value so the high water mark can be determined. If none of the following are
88 * set then don't fill the stack so there is no unnecessary dependency on memset. */
89 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
90 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
92 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
96 * Macros used by vListTask to indicate which state a task is in.
98 #define tskRUNNING_CHAR ( 'X' )
99 #define tskBLOCKED_CHAR ( 'B' )
100 #define tskREADY_CHAR ( 'R' )
101 #define tskDELETED_CHAR ( 'D' )
102 #define tskSUSPENDED_CHAR ( 'S' )
105 * Some kernel aware debuggers require the data the debugger needs access to to
106 * be global, rather than file scope.
108 #ifdef portREMOVE_STATIC_QUALIFIER
112 /* The name allocated to the Idle task. This can be overridden by defining
113 * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
114 #ifndef configIDLE_TASK_NAME
115 #define configIDLE_TASK_NAME "IDLE"
118 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
120 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
121 * performed in a generic way that is not optimised to any particular
122 * microcontroller architecture. */
124 /* uxTopReadyPriority holds the priority of the highest priority ready
126 #define taskRECORD_READY_PRIORITY( uxPriority ) \
128 if( ( uxPriority ) > uxTopReadyPriority ) \
130 uxTopReadyPriority = ( uxPriority ); \
132 } /* taskRECORD_READY_PRIORITY */
134 /*-----------------------------------------------------------*/
136 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
138 UBaseType_t uxTopPriority = uxTopReadyPriority; \
140 /* Find the highest priority queue that contains ready tasks. */ \
141 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
143 configASSERT( uxTopPriority ); \
147 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
148 * the same priority get an equal share of the processor time. */ \
149 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
150 uxTopReadyPriority = uxTopPriority; \
151 } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */
153 /*-----------------------------------------------------------*/
155 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
156 * they are only required when a port optimised method of task selection is
158 #define taskRESET_READY_PRIORITY( uxPriority )
159 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
161 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
163 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
164 * performed in a way that is tailored to the particular microcontroller
165 * architecture being used. */
167 /* A port optimised version is provided. Call the port defined macros. */
168 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( ( uxPriority ), uxTopReadyPriority )
170 /*-----------------------------------------------------------*/
172 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
174 UBaseType_t uxTopPriority; \
176 /* Find the highest priority list that contains ready tasks. */ \
177 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
178 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
179 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
182 /*-----------------------------------------------------------*/
184 /* A port optimised version is provided, call it only if the TCB being reset
185 * is being referenced from a ready list. If it is referenced from a delayed
186 * or suspended list then it won't be in a ready list. */
187 #define taskRESET_READY_PRIORITY( uxPriority ) \
189 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
191 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
195 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
197 /*-----------------------------------------------------------*/
199 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
200 * count overflows. */
201 #define taskSWITCH_DELAYED_LISTS() \
205 /* The delayed tasks list should be empty when the lists are switched. */ \
206 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
208 pxTemp = pxDelayedTaskList; \
209 pxDelayedTaskList = pxOverflowDelayedTaskList; \
210 pxOverflowDelayedTaskList = pxTemp; \
212 prvResetNextTaskUnblockTime(); \
215 /*-----------------------------------------------------------*/
218 * Place the task represented by pxTCB into the appropriate ready list for
219 * the task. It is inserted at the end of the list.
221 #define prvAddTaskToReadyList( pxTCB ) \
223 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
224 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
225 listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
226 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB ); \
228 /*-----------------------------------------------------------*/
231 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
232 * where NULL is used to indicate that the handle of the currently executing
233 * task should be used in place of the parameter. This macro simply checks to
234 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
236 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
238 /* The item value of the event list item is normally used to hold the priority
239 * of the task to which it belongs (coded to allow it to be held in reverse
240 * priority order). However, it is occasionally borrowed for other purposes. It
241 * is important its value is not updated due to a task priority change while it is
242 * being used for another purpose. The following bit definition is used to inform
243 * the scheduler that the value should not be changed - in which case it is the
244 * responsibility of whichever module is using the value to ensure it gets set back
245 * to its original value when it is released. */
246 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
247 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U
248 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
249 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
250 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
251 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000000000000000ULL
255 * Task control block. A task control block (TCB) is allocated for each task,
256 * and stores task state information, including a pointer to the task's context
257 * (the task's run time environment, including register values)
259 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
261 volatile StackType_t * pxTopOfStack; /**< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
263 #if ( portUSING_MPU_WRAPPERS == 1 )
264 xMPU_SETTINGS xMPUSettings; /**< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
267 ListItem_t xStateListItem; /**< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
268 ListItem_t xEventListItem; /**< Used to reference a task from an event list. */
269 UBaseType_t uxPriority; /**< The priority of the task. 0 is the lowest priority. */
270 StackType_t * pxStack; /**< Points to the start of the stack. */
271 char pcTaskName[ configMAX_TASK_NAME_LEN ]; /**< Descriptive name given to the task when created. Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
273 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
274 StackType_t * pxEndOfStack; /**< Points to the highest valid address for the stack. */
277 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
278 UBaseType_t uxCriticalNesting; /**< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
281 #if ( configUSE_TRACE_FACILITY == 1 )
282 UBaseType_t uxTCBNumber; /**< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
283 UBaseType_t uxTaskNumber; /**< Stores a number specifically for use by third party trace code. */
286 #if ( configUSE_MUTEXES == 1 )
287 UBaseType_t uxBasePriority; /**< The priority last assigned to the task - used by the priority inheritance mechanism. */
288 UBaseType_t uxMutexesHeld;
291 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
292 TaskHookFunction_t pxTaskTag;
295 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
296 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
299 #if ( configGENERATE_RUN_TIME_STATS == 1 )
300 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /**< Stores the amount of time the task has spent in the Running state. */
303 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
304 configTLS_BLOCK_TYPE xTLSBlock; /**< Memory block used as Thread Local Storage (TLS) Block for the task. */
307 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
308 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
309 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
312 /* See the comments in FreeRTOS.h with the definition of
313 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
314 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
315 uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
318 #if ( INCLUDE_xTaskAbortDelay == 1 )
319 uint8_t ucDelayAborted;
322 #if ( configUSE_POSIX_ERRNO == 1 )
327 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
328 * below to enable the use of older kernel aware debuggers. */
329 typedef tskTCB TCB_t;
331 /*lint -save -e956 A manual analysis and inspection has been used to determine
332 * which static variables must be declared volatile. */
333 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
335 /* Lists for ready and blocked tasks. --------------------
336 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
337 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
338 * the static qualifier. */
339 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
340 PRIVILEGED_DATA static List_t xDelayedTaskList1; /**< Delayed tasks. */
341 PRIVILEGED_DATA static List_t xDelayedTaskList2; /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
342 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /**< Points to the delayed task list currently being used. */
343 PRIVILEGED_DATA static List_t * volatile pxOverflowDelayedTaskList; /**< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
344 PRIVILEGED_DATA static List_t xPendingReadyList; /**< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready list when the scheduler is resumed. */
346 #if ( INCLUDE_vTaskDelete == 1 )
348 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
349 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
353 #if ( INCLUDE_vTaskSuspend == 1 )
355 PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
359 /* Global POSIX errno. Its value is changed upon context switching to match
360 * the errno of the currently running task. */
361 #if ( configUSE_POSIX_ERRNO == 1 )
362 int FreeRTOS_errno = 0;
365 /* Other file private variables. --------------------------------*/
366 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
367 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
368 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
369 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
370 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
371 PRIVILEGED_DATA static volatile BaseType_t xYieldPending = pdFALSE;
372 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
373 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
374 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
375 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandle = NULL; /**< Holds the handle of the idle task. The idle task is created automatically when the scheduler is started. */
377 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
378 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
379 * to determine the number of priority lists to read back from the remote target. */
380 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
382 /* Context switches are held pending while the scheduler is suspended. Also,
383 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
384 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
385 * If an interrupt needs to unblock a task while the scheduler is suspended then it
386 * moves the task's event list item into the xPendingReadyList, ready for the
387 * kernel to move the task from the pending ready list into the real ready list
388 * when the scheduler is unsuspended. The pending ready list itself can only be
389 * accessed from a critical section. */
390 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
392 #if ( configGENERATE_RUN_TIME_STATS == 1 )
394 /* Do not move these variables to function scope as doing so prevents the
395 * code working with debuggers that need to remove the static qualifier. */
396 PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime = 0UL; /**< Holds the value of a timer/counter the last time a task was switched in. */
397 PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime = 0UL; /**< Holds the total amount of execution time as defined by the run time counter clock. */
403 /*-----------------------------------------------------------*/
405 /* File private functions. --------------------------------*/
408 * Utility task that simply returns pdTRUE if the task referenced by xTask is
409 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
410 * is in any other state.
412 #if ( INCLUDE_vTaskSuspend == 1 )
414 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
416 #endif /* INCLUDE_vTaskSuspend */
419 * Utility to ready all the lists used by the scheduler. This is called
420 * automatically upon the creation of the first task.
422 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
425 * The idle task, which as all tasks is implemented as a never ending loop.
426 * The idle task is automatically created and added to the ready lists upon
427 * creation of the first user task.
429 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
430 * language extensions. The equivalent prototype for this function is:
432 * void prvIdleTask( void *pvParameters );
435 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) portNORETURN PRIVILEGED_FUNCTION;
438 * Utility to free all memory allocated by the scheduler to hold a TCB,
439 * including the stack pointed to by the TCB.
441 * This does not free memory allocated by the task itself (i.e. memory
442 * allocated by calls to pvPortMalloc from within the tasks application code).
444 #if ( INCLUDE_vTaskDelete == 1 )
446 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
451 * Used only by the idle task. This checks to see if anything has been placed
452 * in the list of tasks waiting to be deleted. If so the task is cleaned up
453 * and its TCB deleted.
455 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
458 * The currently executing task is entering the Blocked state. Add the task to
459 * either the current or the overflow delayed task list.
461 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
462 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
465 * Fills an TaskStatus_t structure with information on each task that is
466 * referenced from the pxList list (which may be a ready list, a delayed list,
467 * a suspended list, etc.).
469 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
470 * NORMAL APPLICATION CODE.
472 #if ( configUSE_TRACE_FACILITY == 1 )
474 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
476 eTaskState eState ) PRIVILEGED_FUNCTION;
481 * Searches pxList for a task with name pcNameToQuery - returning a handle to
482 * the task if it is found, or NULL if the task is not found.
484 #if ( INCLUDE_xTaskGetHandle == 1 )
486 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
487 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
492 * When a task is created, the stack of the task is filled with a known value.
493 * This function determines the 'high water mark' of the task stack by
494 * determining how much of the stack remains at the original preset value.
496 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
498 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
503 * Return the amount of time, in ticks, that will pass before the kernel will
504 * next move a task from the Blocked state to the Running state.
506 * This conditional compilation should use inequality to 0, not equality to 1.
507 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
508 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
509 * set to a value other than 1.
511 #if ( configUSE_TICKLESS_IDLE != 0 )
513 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
518 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
519 * will exit the Blocked state.
521 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
523 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
526 * Helper function used to pad task names with spaces when printing out
527 * human readable tables of task information.
529 static char * prvWriteNameToBuffer( char * pcBuffer,
530 const char * pcTaskName ) PRIVILEGED_FUNCTION;
535 * Called after a Task_t structure has been allocated either statically or
536 * dynamically to fill in the structure's members.
538 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
539 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
540 const uint32_t ulStackDepth,
541 void * const pvParameters,
542 UBaseType_t uxPriority,
543 TaskHandle_t * const pxCreatedTask,
545 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
548 * Called after a new task has been created and initialised to place the task
549 * under the control of the scheduler.
551 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
554 * freertos_tasks_c_additions_init() should only be called if the user definable
555 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
556 * called by the function.
558 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
560 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
564 /*-----------------------------------------------------------*/
566 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
568 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
569 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
570 const uint32_t ulStackDepth,
571 void * const pvParameters,
572 UBaseType_t uxPriority,
573 StackType_t * const puxStackBuffer,
574 StaticTask_t * const pxTaskBuffer )
577 TaskHandle_t xReturn;
579 configASSERT( puxStackBuffer != NULL );
580 configASSERT( pxTaskBuffer != NULL );
582 #if ( configASSERT_DEFINED == 1 )
584 /* Sanity check that the size of the structure used to declare a
585 * variable of type StaticTask_t equals the size of the real task
587 volatile size_t xSize = sizeof( StaticTask_t );
588 configASSERT( xSize == sizeof( TCB_t ) );
589 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
591 #endif /* configASSERT_DEFINED */
593 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
595 /* The memory used for the task's TCB and stack are passed into this
596 * function - use them. */
597 pxNewTCB = ( TCB_t * ) pxTaskBuffer; /*lint !e740 !e9087 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
598 memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
599 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
601 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
603 /* Tasks can be created statically or dynamically, so note this
604 * task was created statically in case the task is later deleted. */
605 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
607 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
609 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
610 prvAddNewTaskToReadyList( pxNewTCB );
620 #endif /* SUPPORT_STATIC_ALLOCATION */
621 /*-----------------------------------------------------------*/
623 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
625 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
626 TaskHandle_t * pxCreatedTask )
629 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
631 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
632 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
634 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
636 /* Allocate space for the TCB. Where the memory comes from depends
637 * on the implementation of the port malloc function and whether or
638 * not static allocation is being used. */
639 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
640 memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
642 /* Store the stack location in the TCB. */
643 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
645 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
647 /* Tasks can be created statically or dynamically, so note this
648 * task was created statically in case the task is later deleted. */
649 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
651 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
653 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
654 pxTaskDefinition->pcName,
655 ( uint32_t ) pxTaskDefinition->usStackDepth,
656 pxTaskDefinition->pvParameters,
657 pxTaskDefinition->uxPriority,
658 pxCreatedTask, pxNewTCB,
659 pxTaskDefinition->xRegions );
661 prvAddNewTaskToReadyList( pxNewTCB );
668 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
669 /*-----------------------------------------------------------*/
671 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
673 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
674 TaskHandle_t * pxCreatedTask )
677 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
679 configASSERT( pxTaskDefinition->puxStackBuffer );
681 if( pxTaskDefinition->puxStackBuffer != NULL )
683 /* Allocate space for the TCB. Where the memory comes from depends
684 * on the implementation of the port malloc function and whether or
685 * not static allocation is being used. */
686 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
688 if( pxNewTCB != NULL )
690 memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
692 /* Store the stack location in the TCB. */
693 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
695 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
697 /* Tasks can be created statically or dynamically, so note
698 * this task had a statically allocated stack in case it is
699 * later deleted. The TCB was allocated dynamically. */
700 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
702 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
704 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
705 pxTaskDefinition->pcName,
706 ( uint32_t ) pxTaskDefinition->usStackDepth,
707 pxTaskDefinition->pvParameters,
708 pxTaskDefinition->uxPriority,
709 pxCreatedTask, pxNewTCB,
710 pxTaskDefinition->xRegions );
712 prvAddNewTaskToReadyList( pxNewTCB );
720 #endif /* portUSING_MPU_WRAPPERS */
721 /*-----------------------------------------------------------*/
723 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
725 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
726 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
727 const configSTACK_DEPTH_TYPE usStackDepth,
728 void * const pvParameters,
729 UBaseType_t uxPriority,
730 TaskHandle_t * const pxCreatedTask )
735 /* If the stack grows down then allocate the stack then the TCB so the stack
736 * does not grow into the TCB. Likewise if the stack grows up then allocate
737 * the TCB then the stack. */
738 #if ( portSTACK_GROWTH > 0 )
740 /* Allocate space for the TCB. Where the memory comes from depends on
741 * the implementation of the port malloc function and whether or not static
742 * allocation is being used. */
743 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
745 if( pxNewTCB != NULL )
747 memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
749 /* Allocate space for the stack used by the task being created.
750 * The base of the stack memory stored in the TCB so the task can
751 * be deleted later if required. */
752 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
754 if( pxNewTCB->pxStack == NULL )
756 /* Could not allocate the stack. Delete the allocated TCB. */
757 vPortFree( pxNewTCB );
762 #else /* portSTACK_GROWTH */
764 StackType_t * pxStack;
766 /* Allocate space for the stack used by the task being created. */
767 pxStack = pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation is the stack. */
769 if( pxStack != NULL )
771 /* Allocate space for the TCB. */
772 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) ); /*lint !e9087 !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack, and the first member of TCB_t is always a pointer to the task's stack. */
774 if( pxNewTCB != NULL )
776 memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
778 /* Store the stack location in the TCB. */
779 pxNewTCB->pxStack = pxStack;
783 /* The stack cannot be used as the TCB was not created. Free
785 vPortFreeStack( pxStack );
793 #endif /* portSTACK_GROWTH */
795 if( pxNewTCB != NULL )
797 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
799 /* Tasks can be created statically or dynamically, so note this
800 * task was created dynamically in case it is later deleted. */
801 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
803 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
805 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
806 prvAddNewTaskToReadyList( pxNewTCB );
811 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
817 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
818 /*-----------------------------------------------------------*/
820 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
821 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
822 const uint32_t ulStackDepth,
823 void * const pvParameters,
824 UBaseType_t uxPriority,
825 TaskHandle_t * const pxCreatedTask,
827 const MemoryRegion_t * const xRegions )
829 StackType_t * pxTopOfStack;
832 #if ( portUSING_MPU_WRAPPERS == 1 )
833 /* Should the task be created in privileged mode? */
834 BaseType_t xRunPrivileged;
836 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
838 xRunPrivileged = pdTRUE;
842 xRunPrivileged = pdFALSE;
844 uxPriority &= ~portPRIVILEGE_BIT;
845 #endif /* portUSING_MPU_WRAPPERS == 1 */
847 /* Avoid dependency on memset() if it is not required. */
848 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
850 /* Fill the stack with a known value to assist debugging. */
851 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
853 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
855 /* Calculate the top of stack address. This depends on whether the stack
856 * grows from high memory to low (as per the 80x86) or vice versa.
857 * portSTACK_GROWTH is used to make the result positive or negative as required
859 #if ( portSTACK_GROWTH < 0 )
861 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
862 pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /*lint !e923 !e9033 !e9078 MISRA exception. Avoiding casts between pointers and integers is not practical. Size differences accounted for using portPOINTER_SIZE_TYPE type. Checked by assert(). */
864 /* Check the alignment of the calculated top of stack is correct. */
865 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
867 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
869 /* Also record the stack's high address, which may assist
871 pxNewTCB->pxEndOfStack = pxTopOfStack;
873 #endif /* configRECORD_STACK_HIGH_ADDRESS */
875 #else /* portSTACK_GROWTH */
877 pxTopOfStack = pxNewTCB->pxStack;
879 /* Check the alignment of the stack buffer is correct. */
880 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
882 /* The other extreme of the stack space is required if stack checking is
884 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
886 #endif /* portSTACK_GROWTH */
888 /* Store the task name in the TCB. */
891 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
893 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
895 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
896 * configMAX_TASK_NAME_LEN characters just in case the memory after the
897 * string is not accessible (extremely unlikely). */
898 if( pcName[ x ] == ( char ) 0x00 )
904 mtCOVERAGE_TEST_MARKER();
908 /* Ensure the name string is terminated in the case that the string length
909 * was greater or equal to configMAX_TASK_NAME_LEN. */
910 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
914 mtCOVERAGE_TEST_MARKER();
917 /* This is used as an array index so must ensure it's not too large. */
918 configASSERT( uxPriority < configMAX_PRIORITIES );
920 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
922 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
926 mtCOVERAGE_TEST_MARKER();
929 pxNewTCB->uxPriority = uxPriority;
930 #if ( configUSE_MUTEXES == 1 )
932 pxNewTCB->uxBasePriority = uxPriority;
934 #endif /* configUSE_MUTEXES */
936 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
937 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
939 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
940 * back to the containing TCB from a generic item in a list. */
941 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
943 /* Event lists are always in priority order. */
944 listSET_LIST_ITEM_VALUE( &( pxNewTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
945 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
947 #if ( portUSING_MPU_WRAPPERS == 1 )
949 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
953 /* Avoid compiler warning about unreferenced parameter. */
958 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
960 /* Allocate and initialize memory for the task's TLS Block. */
961 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
965 /* Initialize the TCB stack to look as if the task was already running,
966 * but had been interrupted by the scheduler. The return address is set
967 * to the start of the task function. Once the stack has been initialised
968 * the top of stack variable is updated. */
969 #if ( portUSING_MPU_WRAPPERS == 1 )
971 /* If the port has capability to detect stack overflow,
972 * pass the stack end address to the stack initialization
973 * function as well. */
974 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
976 #if ( portSTACK_GROWTH < 0 )
978 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
980 #else /* portSTACK_GROWTH */
982 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
984 #endif /* portSTACK_GROWTH */
986 #else /* portHAS_STACK_OVERFLOW_CHECKING */
988 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
990 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
992 #else /* portUSING_MPU_WRAPPERS */
994 /* If the port has capability to detect stack overflow,
995 * pass the stack end address to the stack initialization
996 * function as well. */
997 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
999 #if ( portSTACK_GROWTH < 0 )
1001 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1003 #else /* portSTACK_GROWTH */
1005 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1007 #endif /* portSTACK_GROWTH */
1009 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1011 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1013 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1015 #endif /* portUSING_MPU_WRAPPERS */
1017 if( pxCreatedTask != NULL )
1019 /* Pass the handle out in an anonymous way. The handle can be used to
1020 * change the created task's priority, delete the created task, etc.*/
1021 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1025 mtCOVERAGE_TEST_MARKER();
1028 /*-----------------------------------------------------------*/
1030 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1032 /* Ensure interrupts don't access the task lists while the lists are being
1034 taskENTER_CRITICAL();
1036 uxCurrentNumberOfTasks++;
1038 if( pxCurrentTCB == NULL )
1040 /* There are no other tasks, or all the other tasks are in
1041 * the suspended state - make this the current task. */
1042 pxCurrentTCB = pxNewTCB;
1044 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1046 /* This is the first task to be created so do the preliminary
1047 * initialisation required. We will not recover if this call
1048 * fails, but we will report the failure. */
1049 prvInitialiseTaskLists();
1053 mtCOVERAGE_TEST_MARKER();
1058 /* If the scheduler is not already running, make this task the
1059 * current task if it is the highest priority task to be created
1061 if( xSchedulerRunning == pdFALSE )
1063 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
1065 pxCurrentTCB = pxNewTCB;
1069 mtCOVERAGE_TEST_MARKER();
1074 mtCOVERAGE_TEST_MARKER();
1080 #if ( configUSE_TRACE_FACILITY == 1 )
1082 /* Add a counter into the TCB for tracing only. */
1083 pxNewTCB->uxTCBNumber = uxTaskNumber;
1085 #endif /* configUSE_TRACE_FACILITY */
1086 traceTASK_CREATE( pxNewTCB );
1088 prvAddTaskToReadyList( pxNewTCB );
1090 portSETUP_TCB( pxNewTCB );
1092 taskEXIT_CRITICAL();
1094 if( xSchedulerRunning != pdFALSE )
1096 /* If the created task is of a higher priority than the current task
1097 * then it should run now. */
1098 if( pxCurrentTCB->uxPriority < pxNewTCB->uxPriority )
1100 taskYIELD_IF_USING_PREEMPTION();
1104 mtCOVERAGE_TEST_MARKER();
1109 mtCOVERAGE_TEST_MARKER();
1112 /*-----------------------------------------------------------*/
1114 #if ( INCLUDE_vTaskDelete == 1 )
1116 void vTaskDelete( TaskHandle_t xTaskToDelete )
1120 taskENTER_CRITICAL();
1122 /* If null is passed in here then it is the calling task that is
1124 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1126 /* Remove task from the ready/delayed list. */
1127 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1129 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1133 mtCOVERAGE_TEST_MARKER();
1136 /* Is the task waiting on an event also? */
1137 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1139 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1143 mtCOVERAGE_TEST_MARKER();
1146 /* Increment the uxTaskNumber also so kernel aware debuggers can
1147 * detect that the task lists need re-generating. This is done before
1148 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1152 if( pxTCB == pxCurrentTCB )
1154 /* A task is deleting itself. This cannot complete within the
1155 * task itself, as a context switch to another task is required.
1156 * Place the task in the termination list. The idle task will
1157 * check the termination list and free up any memory allocated by
1158 * the scheduler for the TCB and stack of the deleted task. */
1159 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1161 /* Increment the ucTasksDeleted variable so the idle task knows
1162 * there is a task that has been deleted and that it should therefore
1163 * check the xTasksWaitingTermination list. */
1164 ++uxDeletedTasksWaitingCleanUp;
1166 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1167 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1168 traceTASK_DELETE( pxTCB );
1170 /* The pre-delete hook is primarily for the Windows simulator,
1171 * in which Windows specific clean up operations are performed,
1172 * after which it is not possible to yield away from this task -
1173 * hence xYieldPending is used to latch that a context switch is
1175 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPending );
1179 --uxCurrentNumberOfTasks;
1180 traceTASK_DELETE( pxTCB );
1182 /* Reset the next expected unblock time in case it referred to
1183 * the task that has just been deleted. */
1184 prvResetNextTaskUnblockTime();
1187 taskEXIT_CRITICAL();
1189 /* If the task is not deleting itself, call prvDeleteTCB from outside of
1190 * critical section. If a task deletes itself, prvDeleteTCB is called
1191 * from prvCheckTasksWaitingTermination which is called from Idle task. */
1192 if( pxTCB != pxCurrentTCB )
1194 prvDeleteTCB( pxTCB );
1197 /* Force a reschedule if it is the currently running task that has just
1199 if( xSchedulerRunning != pdFALSE )
1201 if( pxTCB == pxCurrentTCB )
1203 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
1204 portYIELD_WITHIN_API();
1208 mtCOVERAGE_TEST_MARKER();
1213 #endif /* INCLUDE_vTaskDelete */
1214 /*-----------------------------------------------------------*/
1216 #if ( INCLUDE_xTaskDelayUntil == 1 )
1218 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
1219 const TickType_t xTimeIncrement )
1221 TickType_t xTimeToWake;
1222 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
1224 configASSERT( pxPreviousWakeTime );
1225 configASSERT( ( xTimeIncrement > 0U ) );
1226 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
1230 /* Minor optimisation. The tick count cannot change in this
1232 const TickType_t xConstTickCount = xTickCount;
1234 /* Generate the tick time at which the task wants to wake. */
1235 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
1237 if( xConstTickCount < *pxPreviousWakeTime )
1239 /* The tick count has overflowed since this function was
1240 * lasted called. In this case the only time we should ever
1241 * actually delay is if the wake time has also overflowed,
1242 * and the wake time is greater than the tick time. When this
1243 * is the case it is as if neither time had overflowed. */
1244 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1246 xShouldDelay = pdTRUE;
1250 mtCOVERAGE_TEST_MARKER();
1255 /* The tick time has not overflowed. In this case we will
1256 * delay if either the wake time has overflowed, and/or the
1257 * tick time is less than the wake time. */
1258 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1260 xShouldDelay = pdTRUE;
1264 mtCOVERAGE_TEST_MARKER();
1268 /* Update the wake time ready for the next call. */
1269 *pxPreviousWakeTime = xTimeToWake;
1271 if( xShouldDelay != pdFALSE )
1273 traceTASK_DELAY_UNTIL( xTimeToWake );
1275 /* prvAddCurrentTaskToDelayedList() needs the block time, not
1276 * the time to wake, so subtract the current tick count. */
1277 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
1281 mtCOVERAGE_TEST_MARKER();
1284 xAlreadyYielded = xTaskResumeAll();
1286 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1287 * have put ourselves to sleep. */
1288 if( xAlreadyYielded == pdFALSE )
1290 portYIELD_WITHIN_API();
1294 mtCOVERAGE_TEST_MARKER();
1297 return xShouldDelay;
1300 #endif /* INCLUDE_xTaskDelayUntil */
1301 /*-----------------------------------------------------------*/
1303 #if ( INCLUDE_vTaskDelay == 1 )
1305 void vTaskDelay( const TickType_t xTicksToDelay )
1307 BaseType_t xAlreadyYielded = pdFALSE;
1309 /* A delay time of zero just forces a reschedule. */
1310 if( xTicksToDelay > ( TickType_t ) 0U )
1312 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
1317 /* A task that is removed from the event list while the
1318 * scheduler is suspended will not get placed in the ready
1319 * list or removed from the blocked list until the scheduler
1322 * This task cannot be in an event list as it is the currently
1323 * executing task. */
1324 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
1326 xAlreadyYielded = xTaskResumeAll();
1330 mtCOVERAGE_TEST_MARKER();
1333 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1334 * have put ourselves to sleep. */
1335 if( xAlreadyYielded == pdFALSE )
1337 portYIELD_WITHIN_API();
1341 mtCOVERAGE_TEST_MARKER();
1345 #endif /* INCLUDE_vTaskDelay */
1346 /*-----------------------------------------------------------*/
1348 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
1350 eTaskState eTaskGetState( TaskHandle_t xTask )
1353 List_t const * pxStateList;
1354 List_t const * pxDelayedList;
1355 List_t const * pxOverflowedDelayedList;
1356 const TCB_t * const pxTCB = xTask;
1358 configASSERT( pxTCB );
1360 if( pxTCB == pxCurrentTCB )
1362 /* The task calling this function is querying its own state. */
1367 taskENTER_CRITICAL();
1369 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
1370 pxDelayedList = pxDelayedTaskList;
1371 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
1373 taskEXIT_CRITICAL();
1375 if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
1377 /* The task being queried is referenced from one of the Blocked
1382 #if ( INCLUDE_vTaskSuspend == 1 )
1383 else if( pxStateList == &xSuspendedTaskList )
1385 /* The task being queried is referenced from the suspended
1386 * list. Is it genuinely suspended or is it blocked
1388 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
1390 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1394 /* The task does not appear on the event list item of
1395 * and of the RTOS objects, but could still be in the
1396 * blocked state if it is waiting on its notification
1397 * rather than waiting on an object. If not, is
1399 eReturn = eSuspended;
1401 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1403 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1410 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1412 eReturn = eSuspended;
1414 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1421 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
1423 #if ( INCLUDE_vTaskDelete == 1 )
1424 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
1426 /* The task being queried is referenced from the deleted
1427 * tasks list, or it is not referenced from any lists at
1433 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
1435 /* If the task is not in any other state, it must be in the
1436 * Ready (including pending ready) state. */
1442 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
1444 #endif /* INCLUDE_eTaskGetState */
1445 /*-----------------------------------------------------------*/
1447 #if ( INCLUDE_uxTaskPriorityGet == 1 )
1449 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
1451 TCB_t const * pxTCB;
1452 UBaseType_t uxReturn;
1454 taskENTER_CRITICAL();
1456 /* If null is passed in here then it is the priority of the task
1457 * that called uxTaskPriorityGet() that is being queried. */
1458 pxTCB = prvGetTCBFromHandle( xTask );
1459 uxReturn = pxTCB->uxPriority;
1461 taskEXIT_CRITICAL();
1466 #endif /* INCLUDE_uxTaskPriorityGet */
1467 /*-----------------------------------------------------------*/
1469 #if ( INCLUDE_uxTaskPriorityGet == 1 )
1471 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
1473 TCB_t const * pxTCB;
1474 UBaseType_t uxReturn;
1475 portBASE_TYPE xSavedInterruptState;
1477 /* RTOS ports that support interrupt nesting have the concept of a
1478 * maximum system call (or maximum API call) interrupt priority.
1479 * Interrupts that are above the maximum system call priority are keep
1480 * permanently enabled, even when the RTOS kernel is in a critical section,
1481 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
1482 * is defined in FreeRTOSConfig.h then
1483 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1484 * failure if a FreeRTOS API function is called from an interrupt that has
1485 * been assigned a priority above the configured maximum system call
1486 * priority. Only FreeRTOS functions that end in FromISR can be called
1487 * from interrupts that have been assigned a priority at or (logically)
1488 * below the maximum system call interrupt priority. FreeRTOS maintains a
1489 * separate interrupt safe API to ensure interrupt entry is as fast and as
1490 * simple as possible. More information (albeit Cortex-M specific) is
1491 * provided on the following link:
1492 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1493 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1495 xSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
1497 /* If null is passed in here then it is the priority of the calling
1498 * task that is being queried. */
1499 pxTCB = prvGetTCBFromHandle( xTask );
1500 uxReturn = pxTCB->uxPriority;
1502 portCLEAR_INTERRUPT_MASK_FROM_ISR( xSavedInterruptState );
1507 #endif /* INCLUDE_uxTaskPriorityGet */
1508 /*-----------------------------------------------------------*/
1510 #if ( INCLUDE_vTaskPrioritySet == 1 )
1512 void vTaskPrioritySet( TaskHandle_t xTask,
1513 UBaseType_t uxNewPriority )
1516 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
1517 BaseType_t xYieldRequired = pdFALSE;
1519 configASSERT( uxNewPriority < configMAX_PRIORITIES );
1521 /* Ensure the new priority is valid. */
1522 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1524 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1528 mtCOVERAGE_TEST_MARKER();
1531 taskENTER_CRITICAL();
1533 /* If null is passed in here then it is the priority of the calling
1534 * task that is being changed. */
1535 pxTCB = prvGetTCBFromHandle( xTask );
1537 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
1539 #if ( configUSE_MUTEXES == 1 )
1541 uxCurrentBasePriority = pxTCB->uxBasePriority;
1545 uxCurrentBasePriority = pxTCB->uxPriority;
1549 if( uxCurrentBasePriority != uxNewPriority )
1551 /* The priority change may have readied a task of higher
1552 * priority than the calling task. */
1553 if( uxNewPriority > uxCurrentBasePriority )
1555 if( pxTCB != pxCurrentTCB )
1557 /* The priority of a task other than the currently
1558 * running task is being raised. Is the priority being
1559 * raised above that of the running task? */
1560 if( uxNewPriority > pxCurrentTCB->uxPriority )
1562 xYieldRequired = pdTRUE;
1566 mtCOVERAGE_TEST_MARKER();
1571 /* The priority of the running task is being raised,
1572 * but the running task must already be the highest
1573 * priority task able to run so no yield is required. */
1576 else if( pxTCB == pxCurrentTCB )
1578 /* Setting the priority of the running task down means
1579 * there may now be another task of higher priority that
1580 * is ready to execute. */
1581 xYieldRequired = pdTRUE;
1585 /* Setting the priority of any other task down does not
1586 * require a yield as the running task must be above the
1587 * new priority of the task being modified. */
1590 /* Remember the ready list the task might be referenced from
1591 * before its uxPriority member is changed so the
1592 * taskRESET_READY_PRIORITY() macro can function correctly. */
1593 uxPriorityUsedOnEntry = pxTCB->uxPriority;
1595 #if ( configUSE_MUTEXES == 1 )
1597 /* Only change the priority being used if the task is not
1598 * currently using an inherited priority. */
1599 if( pxTCB->uxBasePriority == pxTCB->uxPriority )
1601 pxTCB->uxPriority = uxNewPriority;
1605 mtCOVERAGE_TEST_MARKER();
1608 /* The base priority gets set whatever. */
1609 pxTCB->uxBasePriority = uxNewPriority;
1611 #else /* if ( configUSE_MUTEXES == 1 ) */
1613 pxTCB->uxPriority = uxNewPriority;
1615 #endif /* if ( configUSE_MUTEXES == 1 ) */
1617 /* Only reset the event list item value if the value is not
1618 * being used for anything else. */
1619 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
1621 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1625 mtCOVERAGE_TEST_MARKER();
1628 /* If the task is in the blocked or suspended list we need do
1629 * nothing more than change its priority variable. However, if
1630 * the task is in a ready list it needs to be removed and placed
1631 * in the list appropriate to its new priority. */
1632 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
1634 /* The task is currently in its ready list - remove before
1635 * adding it to its new ready list. As we are in a critical
1636 * section we can do this even if the scheduler is suspended. */
1637 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1639 /* It is known that the task is in its ready list so
1640 * there is no need to check again and the port level
1641 * reset macro can be called directly. */
1642 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
1646 mtCOVERAGE_TEST_MARKER();
1649 prvAddTaskToReadyList( pxTCB );
1653 mtCOVERAGE_TEST_MARKER();
1656 if( xYieldRequired != pdFALSE )
1658 taskYIELD_IF_USING_PREEMPTION();
1662 mtCOVERAGE_TEST_MARKER();
1665 /* Remove compiler warning about unused variables when the port
1666 * optimised task selection is not being used. */
1667 ( void ) uxPriorityUsedOnEntry;
1670 taskEXIT_CRITICAL();
1673 #endif /* INCLUDE_vTaskPrioritySet */
1674 /*-----------------------------------------------------------*/
1676 #if ( INCLUDE_vTaskSuspend == 1 )
1678 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
1682 taskENTER_CRITICAL();
1684 /* If null is passed in here then it is the running task that is
1685 * being suspended. */
1686 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
1688 traceTASK_SUSPEND( pxTCB );
1690 /* Remove task from the ready/delayed list and place in the
1691 * suspended list. */
1692 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1694 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1698 mtCOVERAGE_TEST_MARKER();
1701 /* Is the task waiting on an event also? */
1702 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1704 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1708 mtCOVERAGE_TEST_MARKER();
1711 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
1713 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1717 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1719 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1721 /* The task was blocked to wait for a notification, but is
1722 * now suspended, so no notification was received. */
1723 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
1727 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1729 taskEXIT_CRITICAL();
1731 if( xSchedulerRunning != pdFALSE )
1733 /* Reset the next expected unblock time in case it referred to the
1734 * task that is now in the Suspended state. */
1735 taskENTER_CRITICAL();
1737 prvResetNextTaskUnblockTime();
1739 taskEXIT_CRITICAL();
1743 mtCOVERAGE_TEST_MARKER();
1746 if( pxTCB == pxCurrentTCB )
1748 if( xSchedulerRunning != pdFALSE )
1750 /* The current task has just been suspended. */
1751 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
1752 portYIELD_WITHIN_API();
1756 /* The scheduler is not running, but the task that was pointed
1757 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
1758 * must be adjusted to point to a different task. */
1759 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
1761 /* No other tasks are ready, so set pxCurrentTCB back to
1762 * NULL so when the next task is created pxCurrentTCB will
1763 * be set to point to it no matter what its relative priority
1765 pxCurrentTCB = NULL;
1769 vTaskSwitchContext();
1775 mtCOVERAGE_TEST_MARKER();
1779 #endif /* INCLUDE_vTaskSuspend */
1780 /*-----------------------------------------------------------*/
1782 #if ( INCLUDE_vTaskSuspend == 1 )
1784 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
1786 BaseType_t xReturn = pdFALSE;
1787 const TCB_t * const pxTCB = xTask;
1789 /* Accesses xPendingReadyList so must be called from a critical
1792 /* It does not make sense to check if the calling task is suspended. */
1793 configASSERT( xTask );
1795 /* Is the task being resumed actually in the suspended list? */
1796 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
1798 /* Has the task already been resumed from within an ISR? */
1799 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
1801 /* Is it in the suspended list because it is in the Suspended
1802 * state, or because is is blocked with no timeout? */
1803 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
1809 mtCOVERAGE_TEST_MARKER();
1814 mtCOVERAGE_TEST_MARKER();
1819 mtCOVERAGE_TEST_MARKER();
1823 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
1825 #endif /* INCLUDE_vTaskSuspend */
1826 /*-----------------------------------------------------------*/
1828 #if ( INCLUDE_vTaskSuspend == 1 )
1830 void vTaskResume( TaskHandle_t xTaskToResume )
1832 TCB_t * const pxTCB = xTaskToResume;
1834 /* It does not make sense to resume the calling task. */
1835 configASSERT( xTaskToResume );
1837 /* The parameter cannot be NULL as it is impossible to resume the
1838 * currently executing task. */
1839 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
1841 taskENTER_CRITICAL();
1843 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
1845 traceTASK_RESUME( pxTCB );
1847 /* The ready list can be accessed even if the scheduler is
1848 * suspended because this is inside a critical section. */
1849 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
1850 prvAddTaskToReadyList( pxTCB );
1852 /* A higher priority task may have just been resumed. */
1853 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
1855 /* This yield may not cause the task just resumed to run,
1856 * but will leave the lists in the correct state for the
1858 taskYIELD_IF_USING_PREEMPTION();
1862 mtCOVERAGE_TEST_MARKER();
1867 mtCOVERAGE_TEST_MARKER();
1870 taskEXIT_CRITICAL();
1874 mtCOVERAGE_TEST_MARKER();
1878 #endif /* INCLUDE_vTaskSuspend */
1880 /*-----------------------------------------------------------*/
1882 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
1884 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
1886 BaseType_t xYieldRequired = pdFALSE;
1887 TCB_t * const pxTCB = xTaskToResume;
1888 portBASE_TYPE xSavedInterruptStatus;
1890 configASSERT( xTaskToResume );
1892 /* RTOS ports that support interrupt nesting have the concept of a
1893 * maximum system call (or maximum API call) interrupt priority.
1894 * Interrupts that are above the maximum system call priority are keep
1895 * permanently enabled, even when the RTOS kernel is in a critical section,
1896 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
1897 * is defined in FreeRTOSConfig.h then
1898 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1899 * failure if a FreeRTOS API function is called from an interrupt that has
1900 * been assigned a priority above the configured maximum system call
1901 * priority. Only FreeRTOS functions that end in FromISR can be called
1902 * from interrupts that have been assigned a priority at or (logically)
1903 * below the maximum system call interrupt priority. FreeRTOS maintains a
1904 * separate interrupt safe API to ensure interrupt entry is as fast and as
1905 * simple as possible. More information (albeit Cortex-M specific) is
1906 * provided on the following link:
1907 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1908 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1910 xSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1912 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
1914 traceTASK_RESUME_FROM_ISR( pxTCB );
1916 /* Check the ready lists can be accessed. */
1917 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
1919 /* Ready lists can be accessed so move the task from the
1920 * suspended list to the ready list directly. */
1921 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
1923 xYieldRequired = pdTRUE;
1925 /* Mark that a yield is pending in case the user is not
1926 * using the return value to initiate a context switch
1927 * from the ISR using portYIELD_FROM_ISR. */
1928 xYieldPending = pdTRUE;
1932 mtCOVERAGE_TEST_MARKER();
1935 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
1936 prvAddTaskToReadyList( pxTCB );
1940 /* The delayed or ready lists cannot be accessed so the task
1941 * is held in the pending ready list until the scheduler is
1943 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
1948 mtCOVERAGE_TEST_MARKER();
1951 portCLEAR_INTERRUPT_MASK_FROM_ISR( xSavedInterruptStatus );
1953 return xYieldRequired;
1956 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
1957 /*-----------------------------------------------------------*/
1959 void vTaskStartScheduler( void )
1963 /* Add the idle task at the lowest priority. */
1964 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1966 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
1967 StackType_t * pxIdleTaskStackBuffer = NULL;
1968 uint32_t ulIdleTaskStackSize;
1970 /* The Idle task is created using user provided RAM - obtain the
1971 * address of the RAM then create the idle task. */
1972 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
1973 xIdleTaskHandle = xTaskCreateStatic( prvIdleTask,
1974 configIDLE_TASK_NAME,
1975 ulIdleTaskStackSize,
1976 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
1977 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
1978 pxIdleTaskStackBuffer,
1979 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
1981 if( xIdleTaskHandle != NULL )
1990 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1992 /* The Idle task is being created using dynamically allocated RAM. */
1993 xReturn = xTaskCreate( prvIdleTask,
1994 configIDLE_TASK_NAME,
1995 configMINIMAL_STACK_SIZE,
1997 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
1998 &xIdleTaskHandle ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2000 #endif /* configSUPPORT_STATIC_ALLOCATION */
2002 #if ( configUSE_TIMERS == 1 )
2004 if( xReturn == pdPASS )
2006 xReturn = xTimerCreateTimerTask();
2010 mtCOVERAGE_TEST_MARKER();
2013 #endif /* configUSE_TIMERS */
2015 if( xReturn == pdPASS )
2017 /* freertos_tasks_c_additions_init() should only be called if the user
2018 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
2019 * the only macro called by the function. */
2020 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
2022 freertos_tasks_c_additions_init();
2026 /* Interrupts are turned off here, to ensure a tick does not occur
2027 * before or during the call to xPortStartScheduler(). The stacks of
2028 * the created tasks contain a status word with interrupts switched on
2029 * so interrupts will automatically get re-enabled when the first task
2031 portDISABLE_INTERRUPTS();
2033 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
2035 /* Switch C-Runtime's TLS Block to point to the TLS
2036 * block specific to the task that will run first. */
2037 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
2041 xNextTaskUnblockTime = portMAX_DELAY;
2042 xSchedulerRunning = pdTRUE;
2043 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
2045 /* If configGENERATE_RUN_TIME_STATS is defined then the following
2046 * macro must be defined to configure the timer/counter used to generate
2047 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
2048 * is set to 0 and the following line fails to build then ensure you do not
2049 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
2050 * FreeRTOSConfig.h file. */
2051 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
2053 traceTASK_SWITCHED_IN();
2055 /* Setting up the timer tick is hardware specific and thus in the
2056 * portable interface. */
2057 xPortStartScheduler();
2059 /* In most cases, xPortStartScheduler() will not return. If it
2060 * returns pdTRUE then there was not enough heap memory available
2061 * to create either the Idle or the Timer task. If it returned
2062 * pdFALSE, then the application called xTaskEndScheduler().
2063 * Most ports don't implement xTaskEndScheduler() as there is
2064 * nothing to return to. */
2068 /* This line will only be reached if the kernel could not be started,
2069 * because there was not enough FreeRTOS heap to create the idle task
2070 * or the timer task. */
2071 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
2074 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
2075 * meaning xIdleTaskHandle is not used anywhere else. */
2076 ( void ) xIdleTaskHandle;
2078 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
2079 * from getting optimized out as it is no longer used by the kernel. */
2080 ( void ) uxTopUsedPriority;
2082 /*-----------------------------------------------------------*/
2084 void vTaskEndScheduler( void )
2086 /* Stop the scheduler interrupts and call the portable scheduler end
2087 * routine so the original ISRs can be restored if necessary. The port
2088 * layer must ensure interrupts enable bit is left in the correct state. */
2089 portDISABLE_INTERRUPTS();
2090 xSchedulerRunning = pdFALSE;
2091 vPortEndScheduler();
2093 /*----------------------------------------------------------*/
2095 void vTaskSuspendAll( void )
2097 /* A critical section is not required as the variable is of type
2098 * BaseType_t. Please read Richard Barry's reply in the following link to a
2099 * post in the FreeRTOS support forum before reporting this as a bug! -
2100 * https://goo.gl/wu4acr */
2102 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
2103 * do not otherwise exhibit real time behaviour. */
2104 portSOFTWARE_BARRIER();
2106 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
2107 * is used to allow calls to vTaskSuspendAll() to nest. */
2108 ++uxSchedulerSuspended;
2110 /* Enforces ordering for ports and optimised compilers that may otherwise place
2111 * the above increment elsewhere. */
2112 portMEMORY_BARRIER();
2114 /*----------------------------------------------------------*/
2116 #if ( configUSE_TICKLESS_IDLE != 0 )
2118 static TickType_t prvGetExpectedIdleTime( void )
2121 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
2123 /* uxHigherPriorityReadyTasks takes care of the case where
2124 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
2125 * task that are in the Ready state, even though the idle task is
2127 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
2129 if( uxTopReadyPriority > tskIDLE_PRIORITY )
2131 uxHigherPriorityReadyTasks = pdTRUE;
2136 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
2138 /* When port optimised task selection is used the uxTopReadyPriority
2139 * variable is used as a bit map. If bits other than the least
2140 * significant bit are set then there are tasks that have a priority
2141 * above the idle priority that are in the Ready state. This takes
2142 * care of the case where the co-operative scheduler is in use. */
2143 if( uxTopReadyPriority > uxLeastSignificantBit )
2145 uxHigherPriorityReadyTasks = pdTRUE;
2148 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
2150 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
2154 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
2156 /* There are other idle priority tasks in the ready state. If
2157 * time slicing is used then the very next tick interrupt must be
2161 else if( uxHigherPriorityReadyTasks != pdFALSE )
2163 /* There are tasks in the Ready state that have a priority above the
2164 * idle priority. This path can only be reached if
2165 * configUSE_PREEMPTION is 0. */
2170 xReturn = xNextTaskUnblockTime - xTickCount;
2176 #endif /* configUSE_TICKLESS_IDLE */
2177 /*----------------------------------------------------------*/
2179 BaseType_t xTaskResumeAll( void )
2181 TCB_t * pxTCB = NULL;
2182 BaseType_t xAlreadyYielded = pdFALSE;
2184 /* If uxSchedulerSuspended is zero then this function does not match a
2185 * previous call to vTaskSuspendAll(). */
2186 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
2188 /* It is possible that an ISR caused a task to be removed from an event
2189 * list while the scheduler was suspended. If this was the case then the
2190 * removed task will have been added to the xPendingReadyList. Once the
2191 * scheduler has been resumed it is safe to move all the pending ready
2192 * tasks from this list into their appropriate ready list. */
2193 taskENTER_CRITICAL();
2195 --uxSchedulerSuspended;
2197 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
2199 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
2201 /* Move any readied tasks from the pending list into the
2202 * appropriate ready list. */
2203 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
2205 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
2206 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
2207 portMEMORY_BARRIER();
2208 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
2209 prvAddTaskToReadyList( pxTCB );
2211 /* If the moved task has a priority higher than the current
2212 * task then a yield must be performed. */
2213 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
2215 xYieldPending = pdTRUE;
2219 mtCOVERAGE_TEST_MARKER();
2225 /* A task was unblocked while the scheduler was suspended,
2226 * which may have prevented the next unblock time from being
2227 * re-calculated, in which case re-calculate it now. Mainly
2228 * important for low power tickless implementations, where
2229 * this can prevent an unnecessary exit from low power
2231 prvResetNextTaskUnblockTime();
2234 /* If any ticks occurred while the scheduler was suspended then
2235 * they should be processed now. This ensures the tick count does
2236 * not slip, and that any delayed tasks are resumed at the correct
2239 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
2241 if( xPendedCounts > ( TickType_t ) 0U )
2245 if( xTaskIncrementTick() != pdFALSE )
2247 xYieldPending = pdTRUE;
2251 mtCOVERAGE_TEST_MARKER();
2255 } while( xPendedCounts > ( TickType_t ) 0U );
2261 mtCOVERAGE_TEST_MARKER();
2265 if( xYieldPending != pdFALSE )
2267 #if ( configUSE_PREEMPTION != 0 )
2269 xAlreadyYielded = pdTRUE;
2272 taskYIELD_IF_USING_PREEMPTION();
2276 mtCOVERAGE_TEST_MARKER();
2282 mtCOVERAGE_TEST_MARKER();
2285 taskEXIT_CRITICAL();
2287 return xAlreadyYielded;
2289 /*-----------------------------------------------------------*/
2291 TickType_t xTaskGetTickCount( void )
2295 /* Critical section required if running on a 16 bit processor. */
2296 portTICK_TYPE_ENTER_CRITICAL();
2298 xTicks = xTickCount;
2300 portTICK_TYPE_EXIT_CRITICAL();
2304 /*-----------------------------------------------------------*/
2306 TickType_t xTaskGetTickCountFromISR( void )
2309 portBASE_TYPE xSavedInterruptStatus;
2311 /* RTOS ports that support interrupt nesting have the concept of a maximum
2312 * system call (or maximum API call) interrupt priority. Interrupts that are
2313 * above the maximum system call priority are kept permanently enabled, even
2314 * when the RTOS kernel is in a critical section, but cannot make any calls to
2315 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
2316 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2317 * failure if a FreeRTOS API function is called from an interrupt that has been
2318 * assigned a priority above the configured maximum system call priority.
2319 * Only FreeRTOS functions that end in FromISR can be called from interrupts
2320 * that have been assigned a priority at or (logically) below the maximum
2321 * system call interrupt priority. FreeRTOS maintains a separate interrupt
2322 * safe API to ensure interrupt entry is as fast and as simple as possible.
2323 * More information (albeit Cortex-M specific) is provided on the following
2324 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2325 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2327 xSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
2329 xReturn = xTickCount;
2331 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( xSavedInterruptStatus );
2335 /*-----------------------------------------------------------*/
2337 UBaseType_t uxTaskGetNumberOfTasks( void )
2339 /* A critical section is not required because the variables are of type
2341 return uxCurrentNumberOfTasks;
2343 /*-----------------------------------------------------------*/
2345 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2349 /* If null is passed in here then the name of the calling task is being
2351 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
2352 configASSERT( pxTCB );
2353 return &( pxTCB->pcTaskName[ 0 ] );
2355 /*-----------------------------------------------------------*/
2357 #if ( INCLUDE_xTaskGetHandle == 1 )
2359 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
2360 const char pcNameToQuery[] )
2364 TCB_t * pxReturn = NULL;
2367 BaseType_t xBreakLoop;
2369 /* This function is called with the scheduler suspended. */
2371 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
2373 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
2377 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
2379 /* Check each character in the name looking for a match or
2381 xBreakLoop = pdFALSE;
2383 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
2385 cNextChar = pxNextTCB->pcTaskName[ x ];
2387 if( cNextChar != pcNameToQuery[ x ] )
2389 /* Characters didn't match. */
2390 xBreakLoop = pdTRUE;
2392 else if( cNextChar == ( char ) 0x00 )
2394 /* Both strings terminated, a match must have been
2396 pxReturn = pxNextTCB;
2397 xBreakLoop = pdTRUE;
2401 mtCOVERAGE_TEST_MARKER();
2404 if( xBreakLoop != pdFALSE )
2410 if( pxReturn != NULL )
2412 /* The handle has been found. */
2415 } while( pxNextTCB != pxFirstTCB );
2419 mtCOVERAGE_TEST_MARKER();
2425 #endif /* INCLUDE_xTaskGetHandle */
2426 /*-----------------------------------------------------------*/
2428 #if ( INCLUDE_xTaskGetHandle == 1 )
2430 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2432 UBaseType_t uxQueue = configMAX_PRIORITIES;
2435 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
2436 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
2440 /* Search the ready lists. */
2444 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
2448 /* Found the handle. */
2451 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2453 /* Search the delayed lists. */
2456 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
2461 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
2464 #if ( INCLUDE_vTaskSuspend == 1 )
2468 /* Search the suspended list. */
2469 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
2474 #if ( INCLUDE_vTaskDelete == 1 )
2478 /* Search the deleted list. */
2479 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
2484 ( void ) xTaskResumeAll();
2489 #endif /* INCLUDE_xTaskGetHandle */
2490 /*-----------------------------------------------------------*/
2492 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
2494 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
2495 StackType_t ** ppuxStackBuffer,
2496 StaticTask_t ** ppxTaskBuffer )
2501 configASSERT( ppuxStackBuffer != NULL );
2502 configASSERT( ppxTaskBuffer != NULL );
2504 pxTCB = prvGetTCBFromHandle( xTask );
2506 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
2508 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
2510 *ppuxStackBuffer = pxTCB->pxStack;
2511 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
2514 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
2516 *ppuxStackBuffer = pxTCB->pxStack;
2517 *ppxTaskBuffer = NULL;
2525 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
2527 *ppuxStackBuffer = pxTCB->pxStack;
2528 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
2531 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
2536 #endif /* configSUPPORT_STATIC_ALLOCATION */
2537 /*-----------------------------------------------------------*/
2539 #if ( configUSE_TRACE_FACILITY == 1 )
2541 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
2542 const UBaseType_t uxArraySize,
2543 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
2545 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
2549 /* Is there a space in the array for each task in the system? */
2550 if( uxArraySize >= uxCurrentNumberOfTasks )
2552 /* Fill in an TaskStatus_t structure with information on each
2553 * task in the Ready state. */
2557 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
2558 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2560 /* Fill in an TaskStatus_t structure with information on each
2561 * task in the Blocked state. */
2562 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
2563 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
2565 #if ( INCLUDE_vTaskDelete == 1 )
2567 /* Fill in an TaskStatus_t structure with information on
2568 * each task that has been deleted but not yet cleaned up. */
2569 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
2573 #if ( INCLUDE_vTaskSuspend == 1 )
2575 /* Fill in an TaskStatus_t structure with information on
2576 * each task in the Suspended state. */
2577 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
2581 #if ( configGENERATE_RUN_TIME_STATS == 1 )
2583 if( pulTotalRunTime != NULL )
2585 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
2586 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
2588 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
2592 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
2594 if( pulTotalRunTime != NULL )
2596 *pulTotalRunTime = 0;
2599 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
2603 mtCOVERAGE_TEST_MARKER();
2606 ( void ) xTaskResumeAll();
2611 #endif /* configUSE_TRACE_FACILITY */
2612 /*----------------------------------------------------------*/
2614 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
2616 TaskHandle_t xTaskGetIdleTaskHandle( void )
2618 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
2619 * started, then xIdleTaskHandle will be NULL. */
2620 configASSERT( ( xIdleTaskHandle != NULL ) );
2621 return xIdleTaskHandle;
2624 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
2625 /*----------------------------------------------------------*/
2627 /* This conditional compilation should use inequality to 0, not equality to 1.
2628 * This is to ensure vTaskStepTick() is available when user defined low power mode
2629 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
2631 #if ( configUSE_TICKLESS_IDLE != 0 )
2633 void vTaskStepTick( TickType_t xTicksToJump )
2635 /* Correct the tick count value after a period during which the tick
2636 * was suppressed. Note this does *not* call the tick hook function for
2637 * each stepped tick. */
2638 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
2640 if( ( xTickCount + xTicksToJump ) == xNextTaskUnblockTime )
2642 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
2643 * xTaskIncrementTick() when the scheduler resumes. This ensures
2644 * that any delayed tasks are resumed at the correct time. */
2645 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
2646 configASSERT( xTicksToJump != ( TickType_t ) 0 );
2648 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
2649 taskENTER_CRITICAL();
2653 taskEXIT_CRITICAL();
2658 mtCOVERAGE_TEST_MARKER();
2661 xTickCount += xTicksToJump;
2662 traceINCREASE_TICK_COUNT( xTicksToJump );
2665 #endif /* configUSE_TICKLESS_IDLE */
2666 /*----------------------------------------------------------*/
2668 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
2670 BaseType_t xYieldOccurred;
2672 /* Must not be called with the scheduler suspended as the implementation
2673 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
2674 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
2676 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
2677 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
2680 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
2681 taskENTER_CRITICAL();
2683 xPendedTicks += xTicksToCatchUp;
2685 taskEXIT_CRITICAL();
2686 xYieldOccurred = xTaskResumeAll();
2688 return xYieldOccurred;
2690 /*----------------------------------------------------------*/
2692 #if ( INCLUDE_xTaskAbortDelay == 1 )
2694 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
2696 TCB_t * pxTCB = xTask;
2699 configASSERT( pxTCB );
2703 /* A task can only be prematurely removed from the Blocked state if
2704 * it is actually in the Blocked state. */
2705 if( eTaskGetState( xTask ) == eBlocked )
2709 /* Remove the reference to the task from the blocked list. An
2710 * interrupt won't touch the xStateListItem because the
2711 * scheduler is suspended. */
2712 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2714 /* Is the task waiting on an event also? If so remove it from
2715 * the event list too. Interrupts can touch the event list item,
2716 * even though the scheduler is suspended, so a critical section
2718 taskENTER_CRITICAL();
2720 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2722 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2724 /* This lets the task know it was forcibly removed from the
2725 * blocked state so it should not re-evaluate its block time and
2726 * then block again. */
2727 pxTCB->ucDelayAborted = pdTRUE;
2731 mtCOVERAGE_TEST_MARKER();
2734 taskEXIT_CRITICAL();
2736 /* Place the unblocked task into the appropriate ready list. */
2737 prvAddTaskToReadyList( pxTCB );
2739 /* A task being unblocked cannot cause an immediate context
2740 * switch if preemption is turned off. */
2741 #if ( configUSE_PREEMPTION == 1 )
2743 /* Preemption is on, but a context switch should only be
2744 * performed if the unblocked task has a priority that is
2745 * higher than the currently executing task. */
2746 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
2748 /* Pend the yield to be performed when the scheduler
2749 * is unsuspended. */
2750 xYieldPending = pdTRUE;
2754 mtCOVERAGE_TEST_MARKER();
2757 #endif /* configUSE_PREEMPTION */
2764 ( void ) xTaskResumeAll();
2769 #endif /* INCLUDE_xTaskAbortDelay */
2770 /*----------------------------------------------------------*/
2772 BaseType_t xTaskIncrementTick( void )
2775 TickType_t xItemValue;
2776 BaseType_t xSwitchRequired = pdFALSE;
2778 /* Called by the portable layer each time a tick interrupt occurs.
2779 * Increments the tick then checks to see if the new tick value will cause any
2780 * tasks to be unblocked. */
2781 traceTASK_INCREMENT_TICK( xTickCount );
2783 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
2785 /* Minor optimisation. The tick count cannot change in this
2787 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
2789 /* Increment the RTOS tick, switching the delayed and overflowed
2790 * delayed lists if it wraps to 0. */
2791 xTickCount = xConstTickCount;
2793 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
2795 taskSWITCH_DELAYED_LISTS();
2799 mtCOVERAGE_TEST_MARKER();
2802 /* See if this tick has made a timeout expire. Tasks are stored in
2803 * the queue in the order of their wake time - meaning once one task
2804 * has been found whose block time has not expired there is no need to
2805 * look any further down the list. */
2806 if( xConstTickCount >= xNextTaskUnblockTime )
2810 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
2812 /* The delayed list is empty. Set xNextTaskUnblockTime
2813 * to the maximum possible value so it is extremely
2815 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
2816 * next time through. */
2817 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2822 /* The delayed list is not empty, get the value of the
2823 * item at the head of the delayed list. This is the time
2824 * at which the task at the head of the delayed list must
2825 * be removed from the Blocked state. */
2826 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
2827 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
2829 if( xConstTickCount < xItemValue )
2831 /* It is not time to unblock this item yet, but the
2832 * item value is the time at which the task at the head
2833 * of the blocked list must be removed from the Blocked
2834 * state - so record the item value in
2835 * xNextTaskUnblockTime. */
2836 xNextTaskUnblockTime = xItemValue;
2837 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
2841 mtCOVERAGE_TEST_MARKER();
2844 /* It is time to remove the item from the Blocked state. */
2845 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
2847 /* Is the task waiting on an event also? If so remove
2848 * it from the event list. */
2849 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2851 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
2855 mtCOVERAGE_TEST_MARKER();
2858 /* Place the unblocked task into the appropriate ready
2860 prvAddTaskToReadyList( pxTCB );
2862 /* A task being unblocked cannot cause an immediate
2863 * context switch if preemption is turned off. */
2864 #if ( configUSE_PREEMPTION == 1 )
2866 /* Preemption is on, but a context switch should
2867 * only be performed if the unblocked task's
2868 * priority is higher than the currently executing
2870 * The case of equal priority tasks sharing
2871 * processing time (which happens when both
2872 * preemption and time slicing are on) is
2874 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
2876 xSwitchRequired = pdTRUE;
2880 mtCOVERAGE_TEST_MARKER();
2883 #endif /* configUSE_PREEMPTION */
2888 /* Tasks of equal priority to the currently running task will share
2889 * processing time (time slice) if preemption is on, and the application
2890 * writer has not explicitly turned time slicing off. */
2891 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
2893 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 )
2895 xSwitchRequired = pdTRUE;
2899 mtCOVERAGE_TEST_MARKER();
2902 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
2904 #if ( configUSE_TICK_HOOK == 1 )
2906 /* Guard against the tick hook being called when the pended tick
2907 * count is being unwound (when the scheduler is being unlocked). */
2908 if( xPendedTicks == ( TickType_t ) 0 )
2910 vApplicationTickHook();
2914 mtCOVERAGE_TEST_MARKER();
2917 #endif /* configUSE_TICK_HOOK */
2919 #if ( configUSE_PREEMPTION == 1 )
2921 if( xYieldPending != pdFALSE )
2923 xSwitchRequired = pdTRUE;
2927 mtCOVERAGE_TEST_MARKER();
2930 #endif /* configUSE_PREEMPTION */
2936 /* The tick hook gets called at regular intervals, even if the
2937 * scheduler is locked. */
2938 #if ( configUSE_TICK_HOOK == 1 )
2940 vApplicationTickHook();
2945 return xSwitchRequired;
2947 /*-----------------------------------------------------------*/
2949 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
2951 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
2952 TaskHookFunction_t pxHookFunction )
2956 /* If xTask is NULL then it is the task hook of the calling task that is
2960 xTCB = ( TCB_t * ) pxCurrentTCB;
2967 /* Save the hook function in the TCB. A critical section is required as
2968 * the value can be accessed from an interrupt. */
2969 taskENTER_CRITICAL();
2971 xTCB->pxTaskTag = pxHookFunction;
2973 taskEXIT_CRITICAL();
2976 #endif /* configUSE_APPLICATION_TASK_TAG */
2977 /*-----------------------------------------------------------*/
2979 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
2981 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
2984 TaskHookFunction_t xReturn;
2986 /* If xTask is NULL then set the calling task's hook. */
2987 pxTCB = prvGetTCBFromHandle( xTask );
2989 /* Save the hook function in the TCB. A critical section is required as
2990 * the value can be accessed from an interrupt. */
2991 taskENTER_CRITICAL();
2993 xReturn = pxTCB->pxTaskTag;
2995 taskEXIT_CRITICAL();
3000 #endif /* configUSE_APPLICATION_TASK_TAG */
3001 /*-----------------------------------------------------------*/
3003 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3005 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
3008 TaskHookFunction_t xReturn;
3009 portBASE_TYPE xSavedInterruptStatus;
3011 /* If xTask is NULL then set the calling task's hook. */
3012 pxTCB = prvGetTCBFromHandle( xTask );
3014 /* Save the hook function in the TCB. A critical section is required as
3015 * the value can be accessed from an interrupt. */
3016 xSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
3018 xReturn = pxTCB->pxTaskTag;
3020 portCLEAR_INTERRUPT_MASK_FROM_ISR( xSavedInterruptStatus );
3025 #endif /* configUSE_APPLICATION_TASK_TAG */
3026 /*-----------------------------------------------------------*/
3028 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3030 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
3031 void * pvParameter )
3036 /* If xTask is NULL then we are calling our own task hook. */
3039 xTCB = pxCurrentTCB;
3046 if( xTCB->pxTaskTag != NULL )
3048 xReturn = xTCB->pxTaskTag( pvParameter );
3058 #endif /* configUSE_APPLICATION_TASK_TAG */
3059 /*-----------------------------------------------------------*/
3061 void vTaskSwitchContext( void )
3063 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
3065 /* The scheduler is currently suspended - do not allow a context
3067 xYieldPending = pdTRUE;
3071 xYieldPending = pdFALSE;
3072 traceTASK_SWITCHED_OUT();
3074 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3076 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3077 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
3079 ulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
3082 /* Add the amount of time the task has been running to the
3083 * accumulated time so far. The time the task started running was
3084 * stored in ulTaskSwitchedInTime. Note that there is no overflow
3085 * protection here so count values are only valid until the timer
3086 * overflows. The guard against negative values is to protect
3087 * against suspect run time stat counter implementations - which
3088 * are provided by the application, not the kernel. */
3089 if( ulTotalRunTime > ulTaskSwitchedInTime )
3091 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime );
3095 mtCOVERAGE_TEST_MARKER();
3098 ulTaskSwitchedInTime = ulTotalRunTime;
3100 #endif /* configGENERATE_RUN_TIME_STATS */
3102 /* Check for stack overflow, if configured. */
3103 taskCHECK_FOR_STACK_OVERFLOW();
3105 /* Before the currently running task is switched out, save its errno. */
3106 #if ( configUSE_POSIX_ERRNO == 1 )
3108 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
3112 /* Select a new task to run using either the generic C or port
3113 * optimised asm code. */
3114 taskSELECT_HIGHEST_PRIORITY_TASK(); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3115 traceTASK_SWITCHED_IN();
3117 /* After the new task is switched in, update the global errno. */
3118 #if ( configUSE_POSIX_ERRNO == 1 )
3120 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
3124 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3126 /* Switch C-Runtime's TLS Block to point to the TLS
3127 * Block specific to this task. */
3128 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3133 /*-----------------------------------------------------------*/
3135 void vTaskPlaceOnEventList( List_t * const pxEventList,
3136 const TickType_t xTicksToWait )
3138 configASSERT( pxEventList );
3140 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
3141 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
3143 /* Place the event list item of the TCB in the appropriate event list.
3144 * This is placed in the list in priority order so the highest priority task
3145 * is the first to be woken by the event.
3147 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
3148 * Normally, the xItemValue of a TCB's ListItem_t members is:
3149 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
3150 * Therefore, the event list is sorted in descending priority order.
3152 * The queue that contains the event list is locked, preventing
3153 * simultaneous access from interrupts. */
3154 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3156 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3158 /*-----------------------------------------------------------*/
3160 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
3161 const TickType_t xItemValue,
3162 const TickType_t xTicksToWait )
3164 configASSERT( pxEventList );
3166 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3167 * the event groups implementation. */
3168 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
3170 /* Store the item value in the event list item. It is safe to access the
3171 * event list item here as interrupts won't access the event list item of a
3172 * task that is not in the Blocked state. */
3173 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
3175 /* Place the event list item of the TCB at the end of the appropriate event
3176 * list. It is safe to access the event list here because it is part of an
3177 * event group implementation - and interrupts don't access event groups
3178 * directly (instead they access them indirectly by pending function calls to
3179 * the task level). */
3180 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3182 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3184 /*-----------------------------------------------------------*/
3186 #if ( configUSE_TIMERS == 1 )
3188 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
3189 TickType_t xTicksToWait,
3190 const BaseType_t xWaitIndefinitely )
3192 configASSERT( pxEventList );
3194 /* This function should not be called by application code hence the
3195 * 'Restricted' in its name. It is not part of the public API. It is
3196 * designed for use by kernel code, and has special calling requirements -
3197 * it should be called with the scheduler suspended. */
3200 /* Place the event list item of the TCB in the appropriate event list.
3201 * In this case it is assume that this is the only task that is going to
3202 * be waiting on this event list, so the faster vListInsertEnd() function
3203 * can be used in place of vListInsert. */
3204 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3206 /* If the task should block indefinitely then set the block time to a
3207 * value that will be recognised as an indefinite delay inside the
3208 * prvAddCurrentTaskToDelayedList() function. */
3209 if( xWaitIndefinitely != pdFALSE )
3211 xTicksToWait = portMAX_DELAY;
3214 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
3215 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
3218 #endif /* configUSE_TIMERS */
3219 /*-----------------------------------------------------------*/
3221 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
3223 TCB_t * pxUnblockedTCB;
3226 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
3227 * called from a critical section within an ISR. */
3229 /* The event list is sorted in priority order, so the first in the list can
3230 * be removed as it is known to be the highest priority. Remove the TCB from
3231 * the delayed list, and add it to the ready list.
3233 * If an event is for a queue that is locked then this function will never
3234 * get called - the lock count on the queue will get modified instead. This
3235 * means exclusive access to the event list is guaranteed here.
3237 * This function assumes that a check has already been made to ensure that
3238 * pxEventList is not empty. */
3239 pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3240 configASSERT( pxUnblockedTCB );
3241 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
3243 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3245 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
3246 prvAddTaskToReadyList( pxUnblockedTCB );
3248 #if ( configUSE_TICKLESS_IDLE != 0 )
3250 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3251 * might be set to the blocked task's time out time. If the task is
3252 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
3253 * normally left unchanged, because it is automatically reset to a new
3254 * value when the tick count equals xNextTaskUnblockTime. However if
3255 * tickless idling is used it might be more important to enter sleep mode
3256 * at the earliest possible time - so reset xNextTaskUnblockTime here to
3257 * ensure it is updated at the earliest possible time. */
3258 prvResetNextTaskUnblockTime();
3264 /* The delayed and ready lists cannot be accessed, so hold this task
3265 * pending until the scheduler is resumed. */
3266 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
3269 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
3271 /* Return true if the task removed from the event list has a higher
3272 * priority than the calling task. This allows the calling task to know if
3273 * it should force a context switch now. */
3276 /* Mark that a yield is pending in case the user is not using the
3277 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
3278 xYieldPending = pdTRUE;
3287 /*-----------------------------------------------------------*/
3289 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
3290 const TickType_t xItemValue )
3292 TCB_t * pxUnblockedTCB;
3294 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3295 * the event flags implementation. */
3296 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
3298 /* Store the new item value in the event list. */
3299 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
3301 /* Remove the event list form the event flag. Interrupts do not access
3303 pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3304 configASSERT( pxUnblockedTCB );
3305 listREMOVE_ITEM( pxEventListItem );
3307 #if ( configUSE_TICKLESS_IDLE != 0 )
3309 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3310 * might be set to the blocked task's time out time. If the task is
3311 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
3312 * normally left unchanged, because it is automatically reset to a new
3313 * value when the tick count equals xNextTaskUnblockTime. However if
3314 * tickless idling is used it might be more important to enter sleep mode
3315 * at the earliest possible time - so reset xNextTaskUnblockTime here to
3316 * ensure it is updated at the earliest possible time. */
3317 prvResetNextTaskUnblockTime();
3321 /* Remove the task from the delayed list and add it to the ready list. The
3322 * scheduler is suspended so interrupts will not be accessing the ready
3324 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
3325 prvAddTaskToReadyList( pxUnblockedTCB );
3327 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
3329 /* The unblocked task has a priority above that of the calling task, so
3330 * a context switch is required. This function is called with the
3331 * scheduler suspended so xYieldPending is set so the context switch
3332 * occurs immediately that the scheduler is resumed (unsuspended). */
3333 xYieldPending = pdTRUE;
3336 /*-----------------------------------------------------------*/
3338 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
3340 configASSERT( pxTimeOut );
3341 taskENTER_CRITICAL();
3343 pxTimeOut->xOverflowCount = xNumOfOverflows;
3344 pxTimeOut->xTimeOnEntering = xTickCount;
3346 taskEXIT_CRITICAL();
3348 /*-----------------------------------------------------------*/
3350 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
3352 /* For internal use only as it does not use a critical section. */
3353 pxTimeOut->xOverflowCount = xNumOfOverflows;
3354 pxTimeOut->xTimeOnEntering = xTickCount;
3356 /*-----------------------------------------------------------*/
3358 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
3359 TickType_t * const pxTicksToWait )
3363 configASSERT( pxTimeOut );
3364 configASSERT( pxTicksToWait );
3366 taskENTER_CRITICAL();
3368 /* Minor optimisation. The tick count cannot change in this block. */
3369 const TickType_t xConstTickCount = xTickCount;
3370 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
3372 #if ( INCLUDE_xTaskAbortDelay == 1 )
3373 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
3375 /* The delay was aborted, which is not the same as a time out,
3376 * but has the same result. */
3377 pxCurrentTCB->ucDelayAborted = pdFALSE;
3383 #if ( INCLUDE_vTaskSuspend == 1 )
3384 if( *pxTicksToWait == portMAX_DELAY )
3386 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
3387 * specified is the maximum block time then the task should block
3388 * indefinitely, and therefore never time out. */
3394 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
3396 /* The tick count is greater than the time at which
3397 * vTaskSetTimeout() was called, but has also overflowed since
3398 * vTaskSetTimeOut() was called. It must have wrapped all the way
3399 * around and gone past again. This passed since vTaskSetTimeout()
3402 *pxTicksToWait = ( TickType_t ) 0;
3404 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
3406 /* Not a genuine timeout. Adjust parameters for time remaining. */
3407 *pxTicksToWait -= xElapsedTime;
3408 vTaskInternalSetTimeOutState( pxTimeOut );
3413 *pxTicksToWait = ( TickType_t ) 0;
3417 taskEXIT_CRITICAL();
3421 /*-----------------------------------------------------------*/
3423 void vTaskMissedYield( void )
3425 xYieldPending = pdTRUE;
3427 /*-----------------------------------------------------------*/
3429 #if ( configUSE_TRACE_FACILITY == 1 )
3431 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
3433 UBaseType_t uxReturn;
3434 TCB_t const * pxTCB;
3439 uxReturn = pxTCB->uxTaskNumber;
3449 #endif /* configUSE_TRACE_FACILITY */
3450 /*-----------------------------------------------------------*/
3452 #if ( configUSE_TRACE_FACILITY == 1 )
3454 void vTaskSetTaskNumber( TaskHandle_t xTask,
3455 const UBaseType_t uxHandle )
3462 pxTCB->uxTaskNumber = uxHandle;
3466 #endif /* configUSE_TRACE_FACILITY */
3469 * -----------------------------------------------------------
3471 * ----------------------------------------------------------
3473 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
3474 * language extensions. The equivalent prototype for this function is:
3476 * void prvIdleTask( void *pvParameters );
3480 portTASK_FUNCTION( prvIdleTask, pvParameters )
3482 /* Stop warnings. */
3483 ( void ) pvParameters;
3485 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
3486 * SCHEDULER IS STARTED. **/
3488 /* In case a task that has a secure context deletes itself, in which case
3489 * the idle task is responsible for deleting the task's secure context, if
3491 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
3495 /* See if any tasks have deleted themselves - if so then the idle task
3496 * is responsible for freeing the deleted task's TCB and stack. */
3497 prvCheckTasksWaitingTermination();
3499 #if ( configUSE_PREEMPTION == 0 )
3501 /* If we are not using preemption we keep forcing a task switch to
3502 * see if any other task has become available. If we are using
3503 * preemption we don't need to do this as any task becoming available
3504 * will automatically get the processor anyway. */
3507 #endif /* configUSE_PREEMPTION */
3509 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
3511 /* When using preemption tasks of equal priority will be
3512 * timesliced. If a task that is sharing the idle priority is ready
3513 * to run then the idle task should yield before the end of the
3516 * A critical region is not required here as we are just reading from
3517 * the list, and an occasional incorrect value will not matter. If
3518 * the ready list at the idle priority contains more than one task
3519 * then a task other than the idle task is ready to execute. */
3520 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) 1 )
3526 mtCOVERAGE_TEST_MARKER();
3529 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
3531 #if ( configUSE_IDLE_HOOK == 1 )
3533 /* Call the user defined function from within the idle task. */
3534 vApplicationIdleHook();
3536 #endif /* configUSE_IDLE_HOOK */
3538 /* This conditional compilation should use inequality to 0, not equality
3539 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
3540 * user defined low power mode implementations require
3541 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
3542 #if ( configUSE_TICKLESS_IDLE != 0 )
3544 TickType_t xExpectedIdleTime;
3546 /* It is not desirable to suspend then resume the scheduler on
3547 * each iteration of the idle task. Therefore, a preliminary
3548 * test of the expected idle time is performed without the
3549 * scheduler suspended. The result here is not necessarily
3551 xExpectedIdleTime = prvGetExpectedIdleTime();
3553 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
3557 /* Now the scheduler is suspended, the expected idle
3558 * time can be sampled again, and this time its value can
3560 configASSERT( xNextTaskUnblockTime >= xTickCount );
3561 xExpectedIdleTime = prvGetExpectedIdleTime();
3563 /* Define the following macro to set xExpectedIdleTime to 0
3564 * if the application does not want
3565 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
3566 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
3568 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
3570 traceLOW_POWER_IDLE_BEGIN();
3571 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
3572 traceLOW_POWER_IDLE_END();
3576 mtCOVERAGE_TEST_MARKER();
3579 ( void ) xTaskResumeAll();
3583 mtCOVERAGE_TEST_MARKER();
3586 #endif /* configUSE_TICKLESS_IDLE */
3589 /*-----------------------------------------------------------*/
3591 #if ( configUSE_TICKLESS_IDLE != 0 )
3593 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
3595 #if ( INCLUDE_vTaskSuspend == 1 )
3596 /* The idle task exists in addition to the application tasks. */
3597 const UBaseType_t uxNonApplicationTasks = 1;
3598 #endif /* INCLUDE_vTaskSuspend */
3600 eSleepModeStatus eReturn = eStandardSleep;
3602 /* This function must be called from a critical section. */
3604 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
3606 /* A task was made ready while the scheduler was suspended. */
3607 eReturn = eAbortSleep;
3609 else if( xYieldPending != pdFALSE )
3611 /* A yield was pended while the scheduler was suspended. */
3612 eReturn = eAbortSleep;
3614 else if( xPendedTicks != 0 )
3616 /* A tick interrupt has already occurred but was held pending
3617 * because the scheduler is suspended. */
3618 eReturn = eAbortSleep;
3621 #if ( INCLUDE_vTaskSuspend == 1 )
3622 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
3624 /* If all the tasks are in the suspended list (which might mean they
3625 * have an infinite block time rather than actually being suspended)
3626 * then it is safe to turn all clocks off and just wait for external
3628 eReturn = eNoTasksWaitingTimeout;
3630 #endif /* INCLUDE_vTaskSuspend */
3633 mtCOVERAGE_TEST_MARKER();
3639 #endif /* configUSE_TICKLESS_IDLE */
3640 /*-----------------------------------------------------------*/
3642 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
3644 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
3650 if( ( xIndex >= 0 ) &&
3651 ( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
3653 pxTCB = prvGetTCBFromHandle( xTaskToSet );
3654 configASSERT( pxTCB != NULL );
3655 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
3659 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
3660 /*-----------------------------------------------------------*/
3662 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
3664 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
3667 void * pvReturn = NULL;
3670 if( ( xIndex >= 0 ) &&
3671 ( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
3673 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3674 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
3684 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
3685 /*-----------------------------------------------------------*/
3687 #if ( portUSING_MPU_WRAPPERS == 1 )
3689 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
3690 const MemoryRegion_t * const xRegions )
3694 /* If null is passed in here then we are modifying the MPU settings of
3695 * the calling task. */
3696 pxTCB = prvGetTCBFromHandle( xTaskToModify );
3698 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
3701 #endif /* portUSING_MPU_WRAPPERS */
3702 /*-----------------------------------------------------------*/
3704 static void prvInitialiseTaskLists( void )
3706 UBaseType_t uxPriority;
3708 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
3710 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
3713 vListInitialise( &xDelayedTaskList1 );
3714 vListInitialise( &xDelayedTaskList2 );
3715 vListInitialise( &xPendingReadyList );
3717 #if ( INCLUDE_vTaskDelete == 1 )
3719 vListInitialise( &xTasksWaitingTermination );
3721 #endif /* INCLUDE_vTaskDelete */
3723 #if ( INCLUDE_vTaskSuspend == 1 )
3725 vListInitialise( &xSuspendedTaskList );
3727 #endif /* INCLUDE_vTaskSuspend */
3729 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
3731 pxDelayedTaskList = &xDelayedTaskList1;
3732 pxOverflowDelayedTaskList = &xDelayedTaskList2;
3734 /*-----------------------------------------------------------*/
3736 static void prvCheckTasksWaitingTermination( void )
3738 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
3740 #if ( INCLUDE_vTaskDelete == 1 )
3744 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
3745 * being called too often in the idle task. */
3746 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
3748 taskENTER_CRITICAL();
3750 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3751 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3752 --uxCurrentNumberOfTasks;
3753 --uxDeletedTasksWaitingCleanUp;
3755 taskEXIT_CRITICAL();
3757 prvDeleteTCB( pxTCB );
3760 #endif /* INCLUDE_vTaskDelete */
3762 /*-----------------------------------------------------------*/
3764 #if ( configUSE_TRACE_FACILITY == 1 )
3766 void vTaskGetInfo( TaskHandle_t xTask,
3767 TaskStatus_t * pxTaskStatus,
3768 BaseType_t xGetFreeStackSpace,
3773 /* xTask is NULL then get the state of the calling task. */
3774 pxTCB = prvGetTCBFromHandle( xTask );
3776 pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
3777 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
3778 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
3779 pxTaskStatus->pxStackBase = pxTCB->pxStack;
3780 #if ( ( portSTACK_GROWTH > 0 ) && ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
3781 pxTaskStatus->pxTopOfStack = pxTCB->pxTopOfStack;
3782 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
3784 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
3786 #if ( configUSE_MUTEXES == 1 )
3788 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
3792 pxTaskStatus->uxBasePriority = 0;
3796 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3798 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
3802 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
3806 /* Obtaining the task state is a little fiddly, so is only done if the
3807 * value of eState passed into this function is eInvalid - otherwise the
3808 * state is just set to whatever is passed in. */
3809 if( eState != eInvalid )
3811 if( pxTCB == pxCurrentTCB )
3813 pxTaskStatus->eCurrentState = eRunning;
3817 pxTaskStatus->eCurrentState = eState;
3819 #if ( INCLUDE_vTaskSuspend == 1 )
3821 /* If the task is in the suspended list then there is a
3822 * chance it is actually just blocked indefinitely - so really
3823 * it should be reported as being in the Blocked state. */
3824 if( eState == eSuspended )
3828 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3830 pxTaskStatus->eCurrentState = eBlocked;
3833 ( void ) xTaskResumeAll();
3836 #endif /* INCLUDE_vTaskSuspend */
3841 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
3844 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
3845 * parameter is provided to allow it to be skipped. */
3846 if( xGetFreeStackSpace != pdFALSE )
3848 #if ( portSTACK_GROWTH > 0 )
3850 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
3854 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
3860 pxTaskStatus->usStackHighWaterMark = 0;
3864 #endif /* configUSE_TRACE_FACILITY */
3865 /*-----------------------------------------------------------*/
3867 #if ( configUSE_TRACE_FACILITY == 1 )
3869 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
3873 configLIST_VOLATILE TCB_t * pxNextTCB;
3874 configLIST_VOLATILE TCB_t * pxFirstTCB;
3875 UBaseType_t uxTask = 0;
3877 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3879 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3881 /* Populate an TaskStatus_t structure within the
3882 * pxTaskStatusArray array for each task that is referenced from
3883 * pxList. See the definition of TaskStatus_t in task.h for the
3884 * meaning of each TaskStatus_t structure member. */
3887 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3888 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
3890 } while( pxNextTCB != pxFirstTCB );
3894 mtCOVERAGE_TEST_MARKER();
3900 #endif /* configUSE_TRACE_FACILITY */
3901 /*-----------------------------------------------------------*/
3903 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
3905 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
3907 uint32_t ulCount = 0U;
3909 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
3911 pucStackByte -= portSTACK_GROWTH;
3915 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
3917 return ( configSTACK_DEPTH_TYPE ) ulCount;
3920 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
3921 /*-----------------------------------------------------------*/
3923 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
3925 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
3926 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
3927 * user to determine the return type. It gets around the problem of the value
3928 * overflowing on 8-bit types without breaking backward compatibility for
3929 * applications that expect an 8-bit return type. */
3930 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
3933 uint8_t * pucEndOfStack;
3934 configSTACK_DEPTH_TYPE uxReturn;
3936 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
3937 * the same except for their return type. Using configSTACK_DEPTH_TYPE
3938 * allows the user to determine the return type. It gets around the
3939 * problem of the value overflowing on 8-bit types without breaking
3940 * backward compatibility for applications that expect an 8-bit return
3943 pxTCB = prvGetTCBFromHandle( xTask );
3945 #if portSTACK_GROWTH < 0
3947 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
3951 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
3955 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
3960 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
3961 /*-----------------------------------------------------------*/
3963 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
3965 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
3968 uint8_t * pucEndOfStack;
3969 UBaseType_t uxReturn;
3971 pxTCB = prvGetTCBFromHandle( xTask );
3973 #if portSTACK_GROWTH < 0
3975 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
3979 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
3983 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
3988 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
3989 /*-----------------------------------------------------------*/
3991 #if ( INCLUDE_vTaskDelete == 1 )
3993 static void prvDeleteTCB( TCB_t * pxTCB )
3995 /* This call is required specifically for the TriCore port. It must be
3996 * above the vPortFree() calls. The call is also used by ports/demos that
3997 * want to allocate and clean RAM statically. */
3998 portCLEAN_UP_TCB( pxTCB );
4000 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
4002 /* Free up the memory allocated for the task's TLS Block. */
4003 configDEINIT_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
4007 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
4009 /* The task can only have been allocated dynamically - free both
4010 * the stack and TCB. */
4011 vPortFreeStack( pxTCB->pxStack );
4014 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
4016 /* The task could have been allocated statically or dynamically, so
4017 * check what was statically allocated before trying to free the
4019 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
4021 /* Both the stack and TCB were allocated dynamically, so both
4023 vPortFreeStack( pxTCB->pxStack );
4026 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4028 /* Only the stack was statically allocated, so the TCB is the
4029 * only memory that must be freed. */
4034 /* Neither the stack nor the TCB were allocated dynamically, so
4035 * nothing needs to be freed. */
4036 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
4037 mtCOVERAGE_TEST_MARKER();
4040 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
4043 #endif /* INCLUDE_vTaskDelete */
4044 /*-----------------------------------------------------------*/
4046 static void prvResetNextTaskUnblockTime( void )
4048 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4050 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
4051 * the maximum possible value so it is extremely unlikely that the
4052 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
4053 * there is an item in the delayed list. */
4054 xNextTaskUnblockTime = portMAX_DELAY;
4058 /* The new current delayed list is not empty, get the value of
4059 * the item at the head of the delayed list. This is the time at
4060 * which the task at the head of the delayed list should be removed
4061 * from the Blocked state. */
4062 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
4065 /*-----------------------------------------------------------*/
4067 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
4069 TaskHandle_t xTaskGetCurrentTaskHandle( void )
4071 TaskHandle_t xReturn;
4073 /* A critical section is not required as this is not called from
4074 * an interrupt and the current TCB will always be the same for any
4075 * individual execution thread. */
4076 xReturn = pxCurrentTCB;
4081 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
4082 /*-----------------------------------------------------------*/
4084 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
4086 BaseType_t xTaskGetSchedulerState( void )
4090 if( xSchedulerRunning == pdFALSE )
4092 xReturn = taskSCHEDULER_NOT_STARTED;
4096 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4098 xReturn = taskSCHEDULER_RUNNING;
4102 xReturn = taskSCHEDULER_SUSPENDED;
4109 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
4110 /*-----------------------------------------------------------*/
4112 #if ( configUSE_MUTEXES == 1 )
4114 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
4116 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
4117 BaseType_t xReturn = pdFALSE;
4119 /* If the mutex was given back by an interrupt while the queue was
4120 * locked then the mutex holder might now be NULL. _RB_ Is this still
4121 * needed as interrupts can no longer use mutexes? */
4122 if( pxMutexHolder != NULL )
4124 /* If the holder of the mutex has a priority below the priority of
4125 * the task attempting to obtain the mutex then it will temporarily
4126 * inherit the priority of the task attempting to obtain the mutex. */
4127 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
4129 /* Adjust the mutex holder state to account for its new
4130 * priority. Only reset the event list item value if the value is
4131 * not being used for anything else. */
4132 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4134 listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4138 mtCOVERAGE_TEST_MARKER();
4141 /* If the task being modified is in the ready state it will need
4142 * to be moved into a new list. */
4143 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
4145 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4147 /* It is known that the task is in its ready list so
4148 * there is no need to check again and the port level
4149 * reset macro can be called directly. */
4150 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
4154 mtCOVERAGE_TEST_MARKER();
4157 /* Inherit the priority before being moved into the new list. */
4158 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4159 prvAddTaskToReadyList( pxMutexHolderTCB );
4163 /* Just inherit the priority. */
4164 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4167 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
4169 /* Inheritance occurred. */
4174 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
4176 /* The base priority of the mutex holder is lower than the
4177 * priority of the task attempting to take the mutex, but the
4178 * current priority of the mutex holder is not lower than the
4179 * priority of the task attempting to take the mutex.
4180 * Therefore the mutex holder must have already inherited a
4181 * priority, but inheritance would have occurred if that had
4182 * not been the case. */
4187 mtCOVERAGE_TEST_MARKER();
4193 mtCOVERAGE_TEST_MARKER();
4199 #endif /* configUSE_MUTEXES */
4200 /*-----------------------------------------------------------*/
4202 #if ( configUSE_MUTEXES == 1 )
4204 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
4206 TCB_t * const pxTCB = pxMutexHolder;
4207 BaseType_t xReturn = pdFALSE;
4209 if( pxMutexHolder != NULL )
4211 /* A task can only have an inherited priority if it holds the mutex.
4212 * If the mutex is held by a task then it cannot be given from an
4213 * interrupt, and if a mutex is given by the holding task then it must
4214 * be the running state task. */
4215 configASSERT( pxTCB == pxCurrentTCB );
4216 configASSERT( pxTCB->uxMutexesHeld );
4217 ( pxTCB->uxMutexesHeld )--;
4219 /* Has the holder of the mutex inherited the priority of another
4221 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
4223 /* Only disinherit if no other mutexes are held. */
4224 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
4226 /* A task can only have an inherited priority if it holds
4227 * the mutex. If the mutex is held by a task then it cannot be
4228 * given from an interrupt, and if a mutex is given by the
4229 * holding task then it must be the running state task. Remove
4230 * the holding task from the ready list. */
4231 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4233 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
4237 mtCOVERAGE_TEST_MARKER();
4240 /* Disinherit the priority before adding the task into the
4241 * new ready list. */
4242 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
4243 pxTCB->uxPriority = pxTCB->uxBasePriority;
4245 /* Reset the event list item value. It cannot be in use for
4246 * any other purpose if this task is running, and it must be
4247 * running to give back the mutex. */
4248 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4249 prvAddTaskToReadyList( pxTCB );
4251 /* Return true to indicate that a context switch is required.
4252 * This is only actually required in the corner case whereby
4253 * multiple mutexes were held and the mutexes were given back
4254 * in an order different to that in which they were taken.
4255 * If a context switch did not occur when the first mutex was
4256 * returned, even if a task was waiting on it, then a context
4257 * switch should occur when the last mutex is returned whether
4258 * a task is waiting on it or not. */
4263 mtCOVERAGE_TEST_MARKER();
4268 mtCOVERAGE_TEST_MARKER();
4273 mtCOVERAGE_TEST_MARKER();
4279 #endif /* configUSE_MUTEXES */
4280 /*-----------------------------------------------------------*/
4282 #if ( configUSE_MUTEXES == 1 )
4284 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
4285 UBaseType_t uxHighestPriorityWaitingTask )
4287 TCB_t * const pxTCB = pxMutexHolder;
4288 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
4289 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
4291 if( pxMutexHolder != NULL )
4293 /* If pxMutexHolder is not NULL then the holder must hold at least
4295 configASSERT( pxTCB->uxMutexesHeld );
4297 /* Determine the priority to which the priority of the task that
4298 * holds the mutex should be set. This will be the greater of the
4299 * holding task's base priority and the priority of the highest
4300 * priority task that is waiting to obtain the mutex. */
4301 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
4303 uxPriorityToUse = uxHighestPriorityWaitingTask;
4307 uxPriorityToUse = pxTCB->uxBasePriority;
4310 /* Does the priority need to change? */
4311 if( pxTCB->uxPriority != uxPriorityToUse )
4313 /* Only disinherit if no other mutexes are held. This is a
4314 * simplification in the priority inheritance implementation. If
4315 * the task that holds the mutex is also holding other mutexes then
4316 * the other mutexes may have caused the priority inheritance. */
4317 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
4319 /* If a task has timed out because it already holds the
4320 * mutex it was trying to obtain then it cannot of inherited
4321 * its own priority. */
4322 configASSERT( pxTCB != pxCurrentTCB );
4324 /* Disinherit the priority, remembering the previous
4325 * priority to facilitate determining the subject task's
4327 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
4328 uxPriorityUsedOnEntry = pxTCB->uxPriority;
4329 pxTCB->uxPriority = uxPriorityToUse;
4331 /* Only reset the event list item value if the value is not
4332 * being used for anything else. */
4333 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4335 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4339 mtCOVERAGE_TEST_MARKER();
4342 /* If the running task is not the task that holds the mutex
4343 * then the task that holds the mutex could be in either the
4344 * Ready, Blocked or Suspended states. Only remove the task
4345 * from its current state list if it is in the Ready state as
4346 * the task's priority is going to change and there is one
4347 * Ready list per priority. */
4348 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
4350 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4352 /* It is known that the task is in its ready list so
4353 * there is no need to check again and the port level
4354 * reset macro can be called directly. */
4355 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
4359 mtCOVERAGE_TEST_MARKER();
4362 prvAddTaskToReadyList( pxTCB );
4366 mtCOVERAGE_TEST_MARKER();
4371 mtCOVERAGE_TEST_MARKER();
4376 mtCOVERAGE_TEST_MARKER();
4381 mtCOVERAGE_TEST_MARKER();
4385 #endif /* configUSE_MUTEXES */
4386 /*-----------------------------------------------------------*/
4388 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
4390 void vTaskEnterCritical( void )
4392 portDISABLE_INTERRUPTS();
4394 if( xSchedulerRunning != pdFALSE )
4396 ( pxCurrentTCB->uxCriticalNesting )++;
4398 /* This is not the interrupt safe version of the enter critical
4399 * function so assert() if it is being called from an interrupt
4400 * context. Only API functions that end in "FromISR" can be used in an
4401 * interrupt. Only assert if the critical nesting count is 1 to
4402 * protect against recursive calls if the assert function also uses a
4403 * critical section. */
4404 if( pxCurrentTCB->uxCriticalNesting == 1 )
4406 portASSERT_IF_IN_ISR();
4411 mtCOVERAGE_TEST_MARKER();
4415 #endif /* portCRITICAL_NESTING_IN_TCB */
4416 /*-----------------------------------------------------------*/
4418 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
4420 void vTaskExitCritical( void )
4422 if( xSchedulerRunning != pdFALSE )
4424 if( pxCurrentTCB->uxCriticalNesting > 0U )
4426 ( pxCurrentTCB->uxCriticalNesting )--;
4428 if( pxCurrentTCB->uxCriticalNesting == 0U )
4430 portENABLE_INTERRUPTS();
4434 mtCOVERAGE_TEST_MARKER();
4439 mtCOVERAGE_TEST_MARKER();
4444 mtCOVERAGE_TEST_MARKER();
4448 #endif /* portCRITICAL_NESTING_IN_TCB */
4449 /*-----------------------------------------------------------*/
4451 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
4453 static char * prvWriteNameToBuffer( char * pcBuffer,
4454 const char * pcTaskName )
4458 /* Start by copying the entire string. */
4459 strcpy( pcBuffer, pcTaskName );
4461 /* Pad the end of the string with spaces to ensure columns line up when
4463 for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
4465 pcBuffer[ x ] = ' ';
4469 pcBuffer[ x ] = ( char ) 0x00;
4471 /* Return the new end of string. */
4472 return &( pcBuffer[ x ] );
4475 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
4476 /*-----------------------------------------------------------*/
4478 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
4480 void vTaskList( char * pcWriteBuffer )
4482 TaskStatus_t * pxTaskStatusArray;
4483 UBaseType_t uxArraySize, x;
4489 * This function is provided for convenience only, and is used by many
4490 * of the demo applications. Do not consider it to be part of the
4493 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
4494 * uxTaskGetSystemState() output into a human readable table that
4495 * displays task: names, states, priority, stack usage and task number.
4496 * Stack usage specified as the number of unused StackType_t words stack can hold
4497 * on top of stack - not the number of bytes.
4499 * vTaskList() has a dependency on the sprintf() C library function that
4500 * might bloat the code size, use a lot of stack, and provide different
4501 * results on different platforms. An alternative, tiny, third party,
4502 * and limited functionality implementation of sprintf() is provided in
4503 * many of the FreeRTOS/Demo sub-directories in a file called
4504 * printf-stdarg.c (note printf-stdarg.c does not provide a full
4505 * snprintf() implementation!).
4507 * It is recommended that production systems call uxTaskGetSystemState()
4508 * directly to get access to raw stats data, rather than indirectly
4509 * through a call to vTaskList().
4513 /* Make sure the write buffer does not contain a string. */
4514 *pcWriteBuffer = ( char ) 0x00;
4516 /* Take a snapshot of the number of tasks in case it changes while this
4517 * function is executing. */
4518 uxArraySize = uxCurrentNumberOfTasks;
4520 /* Allocate an array index for each task. NOTE! if
4521 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
4522 * equate to NULL. */
4523 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation allocates a struct that has the alignment requirements of a pointer. */
4525 if( pxTaskStatusArray != NULL )
4527 /* Generate the (binary) data. */
4528 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
4530 /* Create a human readable table from the binary data. */
4531 for( x = 0; x < uxArraySize; x++ )
4533 switch( pxTaskStatusArray[ x ].eCurrentState )
4536 cStatus = tskRUNNING_CHAR;
4540 cStatus = tskREADY_CHAR;
4544 cStatus = tskBLOCKED_CHAR;
4548 cStatus = tskSUSPENDED_CHAR;
4552 cStatus = tskDELETED_CHAR;
4555 case eInvalid: /* Fall through. */
4556 default: /* Should not get here, but it is included
4557 * to prevent static checking errors. */
4558 cStatus = ( char ) 0x00;
4562 /* Write the task name to the string, padding with spaces so it
4563 * can be printed in tabular form more easily. */
4564 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
4566 /* Write the rest of the string. */
4567 sprintf( pcWriteBuffer, "\t%c\t%u\t%u\t%u\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
4568 pcWriteBuffer += strlen( pcWriteBuffer ); /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
4571 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
4572 * is 0 then vPortFree() will be #defined to nothing. */
4573 vPortFree( pxTaskStatusArray );
4577 mtCOVERAGE_TEST_MARKER();
4581 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
4582 /*----------------------------------------------------------*/
4584 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
4586 void vTaskGetRunTimeStats( char * pcWriteBuffer )
4588 TaskStatus_t * pxTaskStatusArray;
4589 UBaseType_t uxArraySize, x;
4590 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
4595 * This function is provided for convenience only, and is used by many
4596 * of the demo applications. Do not consider it to be part of the
4599 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
4600 * of the uxTaskGetSystemState() output into a human readable table that
4601 * displays the amount of time each task has spent in the Running state
4602 * in both absolute and percentage terms.
4604 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
4605 * function that might bloat the code size, use a lot of stack, and
4606 * provide different results on different platforms. An alternative,
4607 * tiny, third party, and limited functionality implementation of
4608 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
4609 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
4610 * a full snprintf() implementation!).
4612 * It is recommended that production systems call uxTaskGetSystemState()
4613 * directly to get access to raw stats data, rather than indirectly
4614 * through a call to vTaskGetRunTimeStats().
4617 /* Make sure the write buffer does not contain a string. */
4618 *pcWriteBuffer = ( char ) 0x00;
4620 /* Take a snapshot of the number of tasks in case it changes while this
4621 * function is executing. */
4622 uxArraySize = uxCurrentNumberOfTasks;
4624 /* Allocate an array index for each task. NOTE! If
4625 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
4626 * equate to NULL. */
4627 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation allocates a struct that has the alignment requirements of a pointer. */
4629 if( pxTaskStatusArray != NULL )
4631 /* Generate the (binary) data. */
4632 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
4634 /* For percentage calculations. */
4635 ulTotalTime /= 100UL;
4637 /* Avoid divide by zero errors. */
4638 if( ulTotalTime > 0UL )
4640 /* Create a human readable table from the binary data. */
4641 for( x = 0; x < uxArraySize; x++ )
4643 /* What percentage of the total run time has the task used?
4644 * This will always be rounded down to the nearest integer.
4645 * ulTotalRunTime has already been divided by 100. */
4646 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
4648 /* Write the task name to the string, padding with
4649 * spaces so it can be printed in tabular form more
4651 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
4653 if( ulStatsAsPercentage > 0UL )
4655 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
4657 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
4661 /* sizeof( int ) == sizeof( long ) so a smaller
4662 * printf() library can be used. */
4663 sprintf( pcWriteBuffer, "\t%u\t\t%u%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
4669 /* If the percentage is zero here then the task has
4670 * consumed less than 1% of the total run time. */
4671 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
4673 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
4677 /* sizeof( int ) == sizeof( long ) so a smaller
4678 * printf() library can be used. */
4679 sprintf( pcWriteBuffer, "\t%u\t\t<1%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
4684 pcWriteBuffer += strlen( pcWriteBuffer ); /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
4689 mtCOVERAGE_TEST_MARKER();
4692 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
4693 * is 0 then vPortFree() will be #defined to nothing. */
4694 vPortFree( pxTaskStatusArray );
4698 mtCOVERAGE_TEST_MARKER();
4702 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
4703 /*-----------------------------------------------------------*/
4705 TickType_t uxTaskResetEventItemValue( void )
4707 TickType_t uxReturn;
4709 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
4711 /* Reset the event list item to its normal value - so it can be used with
4712 * queues and semaphores. */
4713 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4717 /*-----------------------------------------------------------*/
4719 #if ( configUSE_MUTEXES == 1 )
4721 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
4723 /* If xSemaphoreCreateMutex() is called before any tasks have been created
4724 * then pxCurrentTCB will be NULL. */
4725 if( pxCurrentTCB != NULL )
4727 ( pxCurrentTCB->uxMutexesHeld )++;
4730 return pxCurrentTCB;
4733 #endif /* configUSE_MUTEXES */
4734 /*-----------------------------------------------------------*/
4736 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
4738 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
4739 BaseType_t xClearCountOnExit,
4740 TickType_t xTicksToWait )
4744 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
4746 taskENTER_CRITICAL();
4748 /* Only block if the notification count is not already non-zero. */
4749 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
4751 /* Mark this task as waiting for a notification. */
4752 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
4754 if( xTicksToWait > ( TickType_t ) 0 )
4756 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4757 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
4759 /* All ports are written to allow a yield in a critical
4760 * section (some will yield immediately, others wait until the
4761 * critical section exits) - but it is not something that
4762 * application code should ever do. */
4763 portYIELD_WITHIN_API();
4767 mtCOVERAGE_TEST_MARKER();
4772 mtCOVERAGE_TEST_MARKER();
4775 taskEXIT_CRITICAL();
4777 taskENTER_CRITICAL();
4779 traceTASK_NOTIFY_TAKE( uxIndexToWait );
4780 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
4782 if( ulReturn != 0UL )
4784 if( xClearCountOnExit != pdFALSE )
4786 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
4790 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
4795 mtCOVERAGE_TEST_MARKER();
4798 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
4800 taskEXIT_CRITICAL();
4805 #endif /* configUSE_TASK_NOTIFICATIONS */
4806 /*-----------------------------------------------------------*/
4808 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
4810 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
4811 uint32_t ulBitsToClearOnEntry,
4812 uint32_t ulBitsToClearOnExit,
4813 uint32_t * pulNotificationValue,
4814 TickType_t xTicksToWait )
4818 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
4820 taskENTER_CRITICAL();
4822 /* Only block if a notification is not already pending. */
4823 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
4825 /* Clear bits in the task's notification value as bits may get
4826 * set by the notifying task or interrupt. This can be used to
4827 * clear the value to zero. */
4828 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
4830 /* Mark this task as waiting for a notification. */
4831 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
4833 if( xTicksToWait > ( TickType_t ) 0 )
4835 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4836 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
4838 /* All ports are written to allow a yield in a critical
4839 * section (some will yield immediately, others wait until the
4840 * critical section exits) - but it is not something that
4841 * application code should ever do. */
4842 portYIELD_WITHIN_API();
4846 mtCOVERAGE_TEST_MARKER();
4851 mtCOVERAGE_TEST_MARKER();
4854 taskEXIT_CRITICAL();
4856 taskENTER_CRITICAL();
4858 traceTASK_NOTIFY_WAIT( uxIndexToWait );
4860 if( pulNotificationValue != NULL )
4862 /* Output the current notification value, which may or may not
4864 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
4867 /* If ucNotifyValue is set then either the task never entered the
4868 * blocked state (because a notification was already pending) or the
4869 * task unblocked because of a notification. Otherwise the task
4870 * unblocked because of a timeout. */
4871 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
4873 /* A notification was not received. */
4878 /* A notification was already pending or a notification was
4879 * received while the task was waiting. */
4880 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
4884 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
4886 taskEXIT_CRITICAL();
4891 #endif /* configUSE_TASK_NOTIFICATIONS */
4892 /*-----------------------------------------------------------*/
4894 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
4896 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
4897 UBaseType_t uxIndexToNotify,
4899 eNotifyAction eAction,
4900 uint32_t * pulPreviousNotificationValue )
4903 BaseType_t xReturn = pdPASS;
4904 uint8_t ucOriginalNotifyState;
4906 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
4907 configASSERT( xTaskToNotify );
4908 pxTCB = xTaskToNotify;
4910 taskENTER_CRITICAL();
4912 if( pulPreviousNotificationValue != NULL )
4914 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
4917 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
4919 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
4924 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
4928 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
4931 case eSetValueWithOverwrite:
4932 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
4935 case eSetValueWithoutOverwrite:
4937 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
4939 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
4943 /* The value could not be written to the task. */
4951 /* The task is being notified without its notify value being
4957 /* Should not get here if all enums are handled.
4958 * Artificially force an assert by testing a value the
4959 * compiler can't assume is const. */
4960 configASSERT( xTickCount == ( TickType_t ) 0 );
4965 traceTASK_NOTIFY( uxIndexToNotify );
4967 /* If the task is in the blocked state specifically to wait for a
4968 * notification then unblock it now. */
4969 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
4971 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4972 prvAddTaskToReadyList( pxTCB );
4974 /* The task should not have been on an event list. */
4975 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
4977 #if ( configUSE_TICKLESS_IDLE != 0 )
4979 /* If a task is blocked waiting for a notification then
4980 * xNextTaskUnblockTime might be set to the blocked task's time
4981 * out time. If the task is unblocked for a reason other than
4982 * a timeout xNextTaskUnblockTime is normally left unchanged,
4983 * because it will automatically get reset to a new value when
4984 * the tick count equals xNextTaskUnblockTime. However if
4985 * tickless idling is used it might be more important to enter
4986 * sleep mode at the earliest possible time - so reset
4987 * xNextTaskUnblockTime here to ensure it is updated at the
4988 * earliest possible time. */
4989 prvResetNextTaskUnblockTime();
4993 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4995 /* The notified task has a priority above the currently
4996 * executing task so a yield is required. */
4997 taskYIELD_IF_USING_PREEMPTION();
5001 mtCOVERAGE_TEST_MARKER();
5006 mtCOVERAGE_TEST_MARKER();
5009 taskEXIT_CRITICAL();
5014 #endif /* configUSE_TASK_NOTIFICATIONS */
5015 /*-----------------------------------------------------------*/
5017 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5019 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
5020 UBaseType_t uxIndexToNotify,
5022 eNotifyAction eAction,
5023 uint32_t * pulPreviousNotificationValue,
5024 BaseType_t * pxHigherPriorityTaskWoken )
5027 uint8_t ucOriginalNotifyState;
5028 BaseType_t xReturn = pdPASS;
5029 portBASE_TYPE xSavedInterruptStatus;
5031 configASSERT( xTaskToNotify );
5032 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5034 /* RTOS ports that support interrupt nesting have the concept of a
5035 * maximum system call (or maximum API call) interrupt priority.
5036 * Interrupts that are above the maximum system call priority are keep
5037 * permanently enabled, even when the RTOS kernel is in a critical section,
5038 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
5039 * is defined in FreeRTOSConfig.h then
5040 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
5041 * failure if a FreeRTOS API function is called from an interrupt that has
5042 * been assigned a priority above the configured maximum system call
5043 * priority. Only FreeRTOS functions that end in FromISR can be called
5044 * from interrupts that have been assigned a priority at or (logically)
5045 * below the maximum system call interrupt priority. FreeRTOS maintains a
5046 * separate interrupt safe API to ensure interrupt entry is as fast and as
5047 * simple as possible. More information (albeit Cortex-M specific) is
5048 * provided on the following link:
5049 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5050 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
5052 pxTCB = xTaskToNotify;
5054 xSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5056 if( pulPreviousNotificationValue != NULL )
5058 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5061 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5062 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5067 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5071 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5074 case eSetValueWithOverwrite:
5075 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5078 case eSetValueWithoutOverwrite:
5080 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5082 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5086 /* The value could not be written to the task. */
5094 /* The task is being notified without its notify value being
5100 /* Should not get here if all enums are handled.
5101 * Artificially force an assert by testing a value the
5102 * compiler can't assume is const. */
5103 configASSERT( xTickCount == ( TickType_t ) 0 );
5107 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
5109 /* If the task is in the blocked state specifically to wait for a
5110 * notification then unblock it now. */
5111 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5113 /* The task should not have been on an event list. */
5114 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5116 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5118 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
5119 prvAddTaskToReadyList( pxTCB );
5123 /* The delayed and ready lists cannot be accessed, so hold
5124 * this task pending until the scheduler is resumed. */
5125 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
5128 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
5130 /* The notified task has a priority above the currently
5131 * executing task so a yield is required. */
5132 if( pxHigherPriorityTaskWoken != NULL )
5134 *pxHigherPriorityTaskWoken = pdTRUE;
5137 /* Mark that a yield is pending in case the user is not
5138 * using the "xHigherPriorityTaskWoken" parameter to an ISR
5139 * safe FreeRTOS function. */
5140 xYieldPending = pdTRUE;
5144 mtCOVERAGE_TEST_MARKER();
5148 portCLEAR_INTERRUPT_MASK_FROM_ISR( xSavedInterruptStatus );
5153 #endif /* configUSE_TASK_NOTIFICATIONS */
5154 /*-----------------------------------------------------------*/
5156 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5158 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
5159 UBaseType_t uxIndexToNotify,
5160 BaseType_t * pxHigherPriorityTaskWoken )
5163 uint8_t ucOriginalNotifyState;
5164 portBASE_TYPE xSavedInterruptStatus;
5166 configASSERT( xTaskToNotify );
5167 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5169 /* RTOS ports that support interrupt nesting have the concept of a
5170 * maximum system call (or maximum API call) interrupt priority.
5171 * Interrupts that are above the maximum system call priority are keep
5172 * permanently enabled, even when the RTOS kernel is in a critical section,
5173 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
5174 * is defined in FreeRTOSConfig.h then
5175 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
5176 * failure if a FreeRTOS API function is called from an interrupt that has
5177 * been assigned a priority above the configured maximum system call
5178 * priority. Only FreeRTOS functions that end in FromISR can be called
5179 * from interrupts that have been assigned a priority at or (logically)
5180 * below the maximum system call interrupt priority. FreeRTOS maintains a
5181 * separate interrupt safe API to ensure interrupt entry is as fast and as
5182 * simple as possible. More information (albeit Cortex-M specific) is
5183 * provided on the following link:
5184 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5185 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
5187 pxTCB = xTaskToNotify;
5189 xSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5191 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5192 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5194 /* 'Giving' is equivalent to incrementing a count in a counting
5196 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5198 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
5200 /* If the task is in the blocked state specifically to wait for a
5201 * notification then unblock it now. */
5202 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5204 /* The task should not have been on an event list. */
5205 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5207 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5209 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
5210 prvAddTaskToReadyList( pxTCB );
5214 /* The delayed and ready lists cannot be accessed, so hold
5215 * this task pending until the scheduler is resumed. */
5216 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
5219 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
5221 /* The notified task has a priority above the currently
5222 * executing task so a yield is required. */
5223 if( pxHigherPriorityTaskWoken != NULL )
5225 *pxHigherPriorityTaskWoken = pdTRUE;
5228 /* Mark that a yield is pending in case the user is not
5229 * using the "xHigherPriorityTaskWoken" parameter in an ISR
5230 * safe FreeRTOS function. */
5231 xYieldPending = pdTRUE;
5235 mtCOVERAGE_TEST_MARKER();
5239 portCLEAR_INTERRUPT_MASK_FROM_ISR( xSavedInterruptStatus );
5242 #endif /* configUSE_TASK_NOTIFICATIONS */
5243 /*-----------------------------------------------------------*/
5245 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5247 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
5248 UBaseType_t uxIndexToClear )
5253 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5255 /* If null is passed in here then it is the calling task that is having
5256 * its notification state cleared. */
5257 pxTCB = prvGetTCBFromHandle( xTask );
5259 taskENTER_CRITICAL();
5261 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
5263 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
5271 taskEXIT_CRITICAL();
5276 #endif /* configUSE_TASK_NOTIFICATIONS */
5277 /*-----------------------------------------------------------*/
5279 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5281 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
5282 UBaseType_t uxIndexToClear,
5283 uint32_t ulBitsToClear )
5288 /* If null is passed in here then it is the calling task that is having
5289 * its notification state cleared. */
5290 pxTCB = prvGetTCBFromHandle( xTask );
5292 taskENTER_CRITICAL();
5294 /* Return the notification as it was before the bits were cleared,
5295 * then clear the bit mask. */
5296 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
5297 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
5299 taskEXIT_CRITICAL();
5304 #endif /* configUSE_TASK_NOTIFICATIONS */
5305 /*-----------------------------------------------------------*/
5307 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5309 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
5311 return xTask->ulRunTimeCounter;
5315 /*-----------------------------------------------------------*/
5317 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5319 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
5321 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
5323 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
5325 /* For percentage calculations. */
5326 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
5328 /* Avoid divide by zero errors. */
5329 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
5331 ulReturn = xTask->ulRunTimeCounter / ulTotalTime;
5341 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
5342 /*-----------------------------------------------------------*/
5344 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5346 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
5348 return ulTaskGetRunTimeCounter( xIdleTaskHandle );
5352 /*-----------------------------------------------------------*/
5354 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5356 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
5358 return ulTaskGetRunTimePercent( xIdleTaskHandle );
5362 /*-----------------------------------------------------------*/
5364 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
5365 const BaseType_t xCanBlockIndefinitely )
5367 TickType_t xTimeToWake;
5368 const TickType_t xConstTickCount = xTickCount;
5370 #if ( INCLUDE_xTaskAbortDelay == 1 )
5372 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
5373 * reset to pdFALSE so it can be detected as having been set to pdTRUE
5374 * when the task leaves the Blocked state. */
5375 pxCurrentTCB->ucDelayAborted = pdFALSE;
5379 /* Remove the task from the ready list before adding it to the blocked list
5380 * as the same list item is used for both lists. */
5381 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5383 /* The current task must be in a ready list, so there is no need to
5384 * check, and the port reset macro can be called directly. */
5385 portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority ); /*lint !e931 pxCurrentTCB cannot change as it is the calling task. pxCurrentTCB->uxPriority and uxTopReadyPriority cannot change as called with scheduler suspended or in a critical section. */
5389 mtCOVERAGE_TEST_MARKER();
5392 #if ( INCLUDE_vTaskSuspend == 1 )
5394 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
5396 /* Add the task to the suspended task list instead of a delayed task
5397 * list to ensure it is not woken by a timing event. It will block
5399 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
5403 /* Calculate the time at which the task should be woken if the event
5404 * does not occur. This may overflow but this doesn't matter, the
5405 * kernel will manage it correctly. */
5406 xTimeToWake = xConstTickCount + xTicksToWait;
5408 /* The list item will be inserted in wake time order. */
5409 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
5411 if( xTimeToWake < xConstTickCount )
5413 /* Wake time has overflowed. Place this item in the overflow
5415 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5419 /* The wake time has not overflowed, so the current block list
5421 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5423 /* If the task entering the blocked state was placed at the
5424 * head of the list of blocked tasks then xNextTaskUnblockTime
5425 * needs to be updated too. */
5426 if( xTimeToWake < xNextTaskUnblockTime )
5428 xNextTaskUnblockTime = xTimeToWake;
5432 mtCOVERAGE_TEST_MARKER();
5437 #else /* INCLUDE_vTaskSuspend */
5439 /* Calculate the time at which the task should be woken if the event
5440 * does not occur. This may overflow but this doesn't matter, the kernel
5441 * will manage it correctly. */
5442 xTimeToWake = xConstTickCount + xTicksToWait;
5444 /* The list item will be inserted in wake time order. */
5445 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
5447 if( xTimeToWake < xConstTickCount )
5449 /* Wake time has overflowed. Place this item in the overflow list. */
5450 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5454 /* The wake time has not overflowed, so the current block list is used. */
5455 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5457 /* If the task entering the blocked state was placed at the head of the
5458 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
5460 if( xTimeToWake < xNextTaskUnblockTime )
5462 xNextTaskUnblockTime = xTimeToWake;
5466 mtCOVERAGE_TEST_MARKER();
5470 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
5471 ( void ) xCanBlockIndefinitely;
5473 #endif /* INCLUDE_vTaskSuspend */
5476 /* Code below here allows additional code to be inserted into this source file,
5477 * especially where access to file scope functions and data is needed (for example
5478 * when performing module tests). */
5480 #ifdef FREERTOS_MODULE_TEST
5481 #include "tasks_test_access_functions.h"
5485 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
5487 #include "freertos_tasks_c_additions.h"
5489 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
5490 static void freertos_tasks_c_additions_init( void )
5492 FREERTOS_TASKS_C_ADDITIONS_INIT();
5496 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */