2 * FreeRTOS Kernel V10.4.3
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
27 /* Standard includes. */
31 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
32 * all the API functions to use the MPU wrappers. That should only be done when
33 * task.h is included from an application file. */
34 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
36 #define DEBUG_UNIT FREERTOS_TASKS
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() vTaskYieldWithinAPI()
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 away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
137 * they are only required when a port optimised method of task selection is
139 #define taskRESET_READY_PRIORITY( uxPriority )
140 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
142 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
144 #error configUSE_PORT_OPTIMISED_TASK_SELECTION not yet supported in SMP
146 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
147 * performed in a way that is tailored to the particular microcontroller
148 * architecture being used. */
150 /* A port optimised version is provided. Call the port defined macros. */
151 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( uxPriority, uxTopReadyPriority )
153 /*-----------------------------------------------------------*/
155 /* A port optimised version is provided, call it only if the TCB being reset
156 * is being referenced from a ready list. If it is referenced from a delayed
157 * or suspended list then it won't be in a ready list. */
158 #define taskRESET_READY_PRIORITY( uxPriority ) \
160 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
162 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
166 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
168 /*-----------------------------------------------------------*/
170 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
171 * count overflows. */
172 #define taskSWITCH_DELAYED_LISTS() \
176 /* The delayed tasks list should be empty when the lists are switched. */ \
177 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
179 pxTemp = pxDelayedTaskList; \
180 pxDelayedTaskList = pxOverflowDelayedTaskList; \
181 pxOverflowDelayedTaskList = pxTemp; \
183 prvResetNextTaskUnblockTime(); \
186 /*-----------------------------------------------------------*/
189 * Place the task represented by pxTCB into the appropriate ready list for
190 * the task. It is inserted at the end of the list.
192 #define prvAddTaskToReadyList( pxTCB ) \
193 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
194 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
195 vListInsertEnd( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
196 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB )
197 /*-----------------------------------------------------------*/
200 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
201 * where NULL is used to indicate that the handle of the currently executing
202 * task should be used in place of the parameter. This macro simply checks to
203 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
205 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
207 /* The item value of the event list item is normally used to hold the priority
208 * of the task to which it belongs (coded to allow it to be held in reverse
209 * priority order). However, it is occasionally borrowed for other purposes. It
210 * is important its value is not updated due to a task priority change while it is
211 * being used for another purpose. The following bit definition is used to inform
212 * the scheduler that the value should not be changed - in which case it is the
213 * responsibility of whichever module is using the value to ensure it gets set back
214 * to its original value when it is released. */
215 #if ( configUSE_16_BIT_TICKS == 1 )
216 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U
218 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
221 /* Indicates that the task is not actively running on any core. */
222 #define taskTASK_NOT_RUNNING ( TaskRunning_t ) ( -1 )
224 /* Indicates that the task is actively running but scheduled to yield. */
225 #define taskTASK_YIELDING ( TaskRunning_t ) ( -2 )
227 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
228 #define taskTASK_IS_RUNNING( xTaskRunState ) ( ( 0 <= xTaskRunState ) && ( xTaskRunState < configNUM_CORES ) )
230 typedef BaseType_t TaskRunning_t;
233 * Task control block. A task control block (TCB) is allocated for each task,
234 * and stores task state information, including a pointer to the task's context
235 * (the task's run time environment, including register values)
237 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
239 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. */
241 #if ( portUSING_MPU_WRAPPERS == 1 )
242 xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
245 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 ). */
246 ListItem_t xEventListItem; /*< Used to reference a task from an event list. */
247 UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */
248 StackType_t * pxStack; /*< Points to the start of the stack. */
249 volatile TaskRunning_t xTaskRunState; /*< Used to identify the core the task is running on, if any. */
250 BaseType_t xIsIdle; /*< Used to identify the idle tasks. */
251 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. */
253 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
254 BaseType_t xPreemptionDisable; /*< Used to prevent the task from being preempted */
257 #if ( configUSE_CORE_AFFINITY == 1 && configNUM_CORES > 1 )
258 UBaseType_t uxCoreAffinityMask; /*< Used to link the task to certain cores. UBaseType_t must have >= the same number of bits as SMP confNUM_CORES */
259 UBaseType_t uxCoreAffinityInheritanceMask; /*< Used to allow a task to inherit the affinity of its parent */
262 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
263 StackType_t * pxEndOfStack; /*< Points to the highest valid address for the stack. */
266 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
267 UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
270 #if ( configUSE_TRACE_FACILITY == 1 )
271 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. */
272 UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
275 #if ( configUSE_MUTEXES == 1 )
276 UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
277 UBaseType_t uxMutexesHeld;
280 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
281 TaskHookFunction_t pxTaskTag;
284 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
285 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
288 #if ( configGENERATE_RUN_TIME_STATS == 1 )
289 uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
292 #if ( configUSE_NEWLIB_REENTRANT == 1 )
294 /* Allocate a Newlib reent structure that is specific to this task.
295 * Note Newlib support has been included by popular demand, but is not
296 * used by the FreeRTOS maintainers themselves. FreeRTOS is not
297 * responsible for resulting newlib operation. User must be familiar with
298 * newlib and must provide system-wide implementations of the necessary
299 * stubs. Be warned that (at the time of writing) the current newlib design
300 * implements a system-wide malloc() that must be provided with locks.
302 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
303 * for additional information. */
304 struct _reent xNewLib_reent;
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 PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUM_CORES ] = { NULL };
334 #define pxCurrentTCB xTaskGetCurrentTaskHandle()
336 /* Lists for ready and blocked tasks. --------------------
337 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
338 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
339 * the static qualifier. */
340 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */
341 PRIVILEGED_DATA static List_t xDelayedTaskList1; /*< Delayed tasks. */
342 PRIVILEGED_DATA static List_t xDelayedTaskList2; /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
343 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /*< Points to the delayed task list currently being used. */
344 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. */
345 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. */
347 #if ( INCLUDE_vTaskDelete == 1 )
349 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /*< Tasks that have been deleted - but their memory not yet freed. */
350 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
354 #if ( INCLUDE_vTaskSuspend == 1 )
356 PRIVILEGED_DATA static List_t xSuspendedTaskList; /*< Tasks that are currently suspended. */
360 /* Global POSIX errno. Its value is changed upon context switching to match
361 * the errno of the currently running task. */
362 #if ( configUSE_POSIX_ERRNO == 1 )
363 int FreeRTOS_errno = 0;
366 /* Other file private variables. --------------------------------*/
367 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
368 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
369 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
370 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
371 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
372 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUM_CORES ] = { pdFALSE };
373 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
374 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
375 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
376 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandle[ configNUM_CORES ] = { NULL }; /*< Holds the handle of the idle task. The idle task is created automatically when the scheduler is started. */
378 #define xYieldPending prvGetCurrentYieldPending()
380 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
381 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
382 * to determine the number of priority lists to read back from the remote target. */
383 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
385 /* Context switches are held pending while the scheduler is suspended. Also,
386 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
387 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
388 * If an interrupt needs to unblock a task while the scheduler is suspended then it
389 * moves the task's event list item into the xPendingReadyList, ready for the
390 * kernel to move the task from the pending ready list into the real ready list
391 * when the scheduler is unsuspended. The pending ready list itself can only be
392 * accessed from a critical section.
394 * Updates to uxSchedulerSuspended must be protected by both the task and ISR locks and
395 * must not be done by an ISR. Reads must be protected by either lock and may be done by
396 * either an ISR or a task. */
397 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) pdFALSE;
399 #if ( configGENERATE_RUN_TIME_STATS == 1 )
401 /* Do not move these variables to function scope as doing so prevents the
402 * code working with debuggers that need to remove the static qualifier. */
403 PRIVILEGED_DATA static uint32_t ulTaskSwitchedInTime = 0UL; /*< Holds the value of a timer/counter the last time a task was switched in. */
404 PRIVILEGED_DATA static volatile uint32_t ulTotalRunTime = 0UL; /*< Holds the total amount of execution time as defined by the run time counter clock. */
410 /*-----------------------------------------------------------*/
412 /* File private functions. --------------------------------*/
415 * Creates the idle tasks during scheduler start
417 static BaseType_t prvCreateIdleTasks( void );
420 * Returns the yield pending count for the calling core.
422 static BaseType_t prvGetCurrentYieldPending( void );
425 * Checks to see if another task moved the current task out of the ready
426 * list while it was waiting to enter a critical section and yields if so.
428 static void prvCheckForRunStateChange( void );
431 * Yields the given core.
433 static void prvYieldCore( BaseType_t xCoreID );
436 * Yields a core, or cores if multiple priorities are not allowed to run
437 * simultaneously, to allow the task pxTCB to run.
439 static void prvYieldForTask( TCB_t * pxTCB,
440 const BaseType_t xPreemptEqualPriority );
443 * Selects the highest priority available task
445 static BaseType_t prvSelectHighestPriorityTask( const BaseType_t xCoreID );
448 * Utility task that simply returns pdTRUE if the task referenced by xTask is
449 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
450 * is in any other state.
452 #if ( INCLUDE_vTaskSuspend == 1 )
454 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
456 #endif /* INCLUDE_vTaskSuspend */
459 * Utility to ready all the lists used by the scheduler. This is called
460 * automatically upon the creation of the first task.
462 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
465 * The idle task, which as all tasks is implemented as a never ending loop.
466 * The idle task is automatically created and added to the ready lists upon
467 * creation of the first user task.
470 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
471 #if ( configNUM_CORES > 1 )
472 static portTASK_FUNCTION_PROTO( prvMinimalIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
476 * Utility to free all memory allocated by the scheduler to hold a TCB,
477 * including the stack pointed to by the TCB.
479 * This does not free memory allocated by the task itself (i.e. memory
480 * allocated by calls to pvPortMalloc from within the tasks application code).
482 #if ( INCLUDE_vTaskDelete == 1 )
484 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
489 * Used only by the idle task. This checks to see if anything has been placed
490 * in the list of tasks waiting to be deleted. If so the task is cleaned up
491 * and its TCB deleted.
493 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
496 * The currently executing task is entering the Blocked state. Add the task to
497 * either the current or the overflow delayed task list.
499 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
500 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
503 * Fills an TaskStatus_t structure with information on each task that is
504 * referenced from the pxList list (which may be a ready list, a delayed list,
505 * a suspended list, etc.).
507 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
508 * NORMAL APPLICATION CODE.
510 #if ( configUSE_TRACE_FACILITY == 1 )
512 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
514 eTaskState eState ) PRIVILEGED_FUNCTION;
519 * Searches pxList for a task with name pcNameToQuery - returning a handle to
520 * the task if it is found, or NULL if the task is not found.
522 #if ( INCLUDE_xTaskGetHandle == 1 )
524 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
525 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
530 * When a task is created, the stack of the task is filled with a known value.
531 * This function determines the 'high water mark' of the task stack by
532 * determining how much of the stack remains at the original preset value.
534 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
536 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
541 * Return the amount of time, in ticks, that will pass before the kernel will
542 * next move a task from the Blocked state to the Running state.
544 * This conditional compilation should use inequality to 0, not equality to 1.
545 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
546 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
547 * set to a value other than 1.
549 #if ( configUSE_TICKLESS_IDLE != 0 )
551 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
556 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
557 * will exit the Blocked state.
559 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
561 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
564 * Helper function used to pad task names with spaces when printing out
565 * human readable tables of task information.
567 static char * prvWriteNameToBuffer( char * pcBuffer,
568 const char * pcTaskName ) PRIVILEGED_FUNCTION;
573 * Called after a Task_t structure has been allocated either statically or
574 * dynamically to fill in the structure's members.
576 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
577 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
578 const uint32_t ulStackDepth,
579 void * const pvParameters,
580 UBaseType_t uxPriority,
581 TaskHandle_t * const pxCreatedTask,
583 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
586 * Called after a new task has been created and initialised to place the task
587 * under the control of the scheduler.
589 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
592 * freertos_tasks_c_additions_init() should only be called if the user definable
593 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
594 * called by the function.
596 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
598 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
602 /*-----------------------------------------------------------*/
604 static BaseType_t prvGetCurrentYieldPending( void )
609 ulState = portDISABLE_INTERRUPTS();
610 xReturn = xYieldPendings[ portGET_CORE_ID() ];
611 portRESTORE_INTERRUPTS( ulState );
616 /*-----------------------------------------------------------*/
618 static void prvCheckForRunStateChange( void )
620 UBaseType_t uxPrevCriticalNesting;
621 UBaseType_t uxPrevSchedulerSuspended;
624 /* This should be skipped when entering a critical section within
625 * an ISR. If the task on the current core is no longer running, then
626 * vTaskSwitchContext() probably should be run before returning, but
627 * we don't have a way to force that to happen from here. */
628 if( portCHECK_IF_IN_ISR() == pdFALSE )
630 /* This function is always called with interrupts disabled
631 * so this is safe. */
632 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
634 while( pxThisTCB->xTaskRunState == taskTASK_YIELDING )
636 /* We are only here if we just entered a critical section
637 * or if we just suspended the scheduler, and another task
638 * has requested that we yield.
640 * This is slightly complicated since we need to save and restore
641 * the suspension and critical nesting counts, as well as release
642 * and reacquire the correct locks. And then do it all over again
643 * if our state changed again during the reacquisition. */
645 uxPrevCriticalNesting = pxThisTCB->uxCriticalNesting;
646 uxPrevSchedulerSuspended = uxSchedulerSuspended;
648 /* this must only be called the first time we enter into a critical
649 * section, otherwise it could context switch in the middle of a
650 * critical section. */
651 configASSERT( uxPrevCriticalNesting + uxPrevSchedulerSuspended == 1U );
653 uxSchedulerSuspended = 0U;
655 if( uxPrevCriticalNesting > 0U )
657 pxThisTCB->uxCriticalNesting = 0U;
658 portRELEASE_ISR_LOCK();
659 portRELEASE_TASK_LOCK();
663 /* uxPrevSchedulerSuspended must be 1 */
664 portRELEASE_TASK_LOCK();
667 portMEMORY_BARRIER();
668 configASSERT( pxThisTCB->xTaskRunState == taskTASK_YIELDING );
670 portENABLE_INTERRUPTS();
672 /* Enabling interrupts should cause this core to immediately
673 * service the pending interrupt and yield. If the run state is still
674 * yielding here then that is a problem. */
675 configASSERT( pxThisTCB->xTaskRunState != taskTASK_YIELDING );
677 portDISABLE_INTERRUPTS();
680 pxCurrentTCB->uxCriticalNesting = uxPrevCriticalNesting;
681 uxSchedulerSuspended = uxPrevSchedulerSuspended;
683 if( uxPrevCriticalNesting == 0U )
685 /* uxPrevSchedulerSuspended must be 1 */
686 configASSERT( uxPrevSchedulerSuspended != ( UBaseType_t ) pdFALSE );
687 portRELEASE_ISR_LOCK();
693 /*-----------------------------------------------------------*/
695 static void prvYieldCore( BaseType_t xCoreID )
697 /* This must be called from a critical section and
698 * xCoreID must be valid. */
700 if( portCHECK_IF_IN_ISR() && ( xCoreID == portGET_CORE_ID() ) )
702 xYieldPendings[ xCoreID ] = pdTRUE;
704 else if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_YIELDING )
706 if( xCoreID == portGET_CORE_ID() )
708 xYieldPendings[ xCoreID ] = pdTRUE;
712 portYIELD_CORE( xCoreID );
713 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_YIELDING;
718 /*-----------------------------------------------------------*/
720 static void prvYieldForTask( TCB_t * pxTCB,
721 const BaseType_t xPreemptEqualPriority )
723 BaseType_t xLowestPriority;
724 BaseType_t xTaskPriority;
725 BaseType_t xLowestPriorityCore = -1;
726 BaseType_t xYieldCount = 0;
728 TaskRunning_t xTaskRunState;
730 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION */
732 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
734 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
736 /* No task should yield for this one if it is a lower priority
737 * than priority level of currently ready tasks. */
738 if( pxTCB->uxPriority < uxTopReadyPriority )
745 xLowestPriority = ( BaseType_t ) pxTCB->uxPriority;
747 if( xPreemptEqualPriority == pdFALSE )
749 /* xLowestPriority will be decremented to -1 if the priority of pxTCB
750 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
754 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUM_CORES; x++ )
756 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here */
757 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ x ]->uxPriority - pxCurrentTCBs[ x ]->xIsIdle;
758 xTaskRunState = pxCurrentTCBs[ x ]->xTaskRunState;
760 if( ( taskTASK_IS_RUNNING( xTaskRunState ) != pdFALSE ) && ( xYieldPendings[ x ] == pdFALSE ) )
762 if( xTaskPriority <= xLowestPriority )
764 #if ( configUSE_CORE_AFFINITY == 1 )
765 if( ( pxTCB->uxCoreAffinityMask & ( 1 << x ) ) == 1 )
768 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
769 if( pxCurrentTCBs[ x ]->xPreemptionDisable == pdFALSE )
772 xLowestPriority = xTaskPriority;
773 xLowestPriorityCore = x;
779 mtCOVERAGE_TEST_MARKER();
782 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) && 1
784 /* Yield all currently running non-idle tasks with a priority lower than
785 * the task that needs to run. */
786 if( ( ( BaseType_t ) tskIDLE_PRIORITY - 1 < xTaskPriority ) && ( xTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
793 mtCOVERAGE_TEST_MARKER();
796 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) && 1 */
800 mtCOVERAGE_TEST_MARKER();
804 if( ( xYieldCount == 0 ) && taskVALID_CORE_ID( xLowestPriorityCore ) )
806 prvYieldCore( xLowestPriorityCore );
810 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
811 /* Verify that the calling core always yields to higher priority tasks */
812 if( !pxCurrentTCBs[ portGET_CORE_ID() ]->xIsIdle && ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
814 configASSERT( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE || taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ]->xTaskRunState ) == pdFALSE );
818 /*-----------------------------------------------------------*/
820 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
822 static BaseType_t prvSelectHighestPriorityTask( const BaseType_t xCoreID )
824 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
825 BaseType_t xTaskScheduled = pdFALSE;
826 BaseType_t xDecrementTopPriority = pdTRUE;
828 #if ( configUSE_CORE_AFFINITY == 1 )
829 TCB_t * pxPreviousTCB = NULL;
831 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
832 BaseType_t xPriorityDropped = pdFALSE;
835 while( xTaskScheduled == pdFALSE )
837 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
839 if( uxCurrentPriority < uxTopReadyPriority )
841 /* We can't schedule any tasks, other than idle, that have a
842 * priority lower than the priority of a task currently running
843 * on another core. */
844 uxCurrentPriority = tskIDLE_PRIORITY;
849 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
851 List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
852 ListItem_t * pxLastTaskItem = pxReadyList->pxIndex->pxPrevious;
853 ListItem_t * pxTaskItem = pxLastTaskItem;
855 if( ( void * ) pxLastTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
857 pxLastTaskItem = pxLastTaskItem->pxPrevious;
860 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
861 * must not be decremented any further */
862 xDecrementTopPriority = pdFALSE;
868 pxTaskItem = pxTaskItem->pxNext;
870 if( ( void * ) pxTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
872 pxTaskItem = pxTaskItem->pxNext;
875 pxTCB = pxTaskItem->pvOwner;
877 /*debug_printf("Attempting to schedule %s on core %d\n", pxTCB->pcTaskName, portGET_CORE_ID() ); */
879 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
881 /* When falling back to the idle priority because only one priority
882 * level is allowed to run at a time, we should ONLY schedule the true
883 * idle tasks, not user tasks at the idle priority. */
884 if( uxCurrentPriority < uxTopReadyPriority )
886 if( pxTCB->xIsIdle == pdFALSE )
892 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
894 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
896 #if ( configUSE_CORE_AFFINITY == 1 )
897 if( ( pxTCB->uxCoreAffinityMask & ( 1 << xCoreID ) ) == 1 )
900 /* If the task is not being executed by any core swap it in */
901 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
902 #if ( configUSE_CORE_AFFINITY == 1 )
903 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
905 pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
906 pxCurrentTCBs[ xCoreID ] = pxTCB;
907 xTaskScheduled = pdTRUE;
910 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
912 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_YIELDING ) );
913 #if ( configUSE_CORE_AFFINITY == 1 )
914 if( ( pxTCB->uxCoreAffinityMask & ( 1 << xCoreID ) ) == 1 )
917 /* The task is already running on this core, mark it as scheduled */
918 pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
919 xTaskScheduled = pdTRUE;
923 if( xTaskScheduled != pdFALSE )
925 /* Once a task has been selected to run on this core,
926 * move it to the end of the ready task list. */
927 uxListRemove( pxTaskItem );
928 vListInsertEnd( pxReadyList, pxTaskItem );
931 } while( pxTaskItem != pxLastTaskItem );
935 if( xDecrementTopPriority != pdFALSE )
937 uxTopReadyPriority--;
938 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
940 xPriorityDropped = pdTRUE;
946 /* This function can get called by vTaskSuspend() before the scheduler is started.
947 * In that case, since the idle tasks have not yet been created it is possible that we
948 * won't find a new task to schedule. Return pdFALSE in this case. */
949 if( ( xSchedulerRunning == pdFALSE ) && ( uxCurrentPriority == tskIDLE_PRIORITY ) && ( xTaskScheduled == pdFALSE ) )
954 configASSERT( ( uxCurrentPriority > tskIDLE_PRIORITY ) || ( xTaskScheduled == pdTRUE ) );
958 configASSERT( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ]->xTaskRunState ) );
960 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
961 if( xPriorityDropped != pdFALSE )
963 /* There may be several ready tasks that were being prevented from running because there was
964 * a higher priority task running. Now that the last of the higher priority tasks is no longer
965 * running, make sure all the other idle tasks yield. */
968 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUM_CORES; x++ )
970 if( pxCurrentTCBs[ x ]->xIsIdle != pdFALSE )
976 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
978 #if ( configUSE_CORE_AFFINITY == 1 )
979 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
981 /* A ready task was just bumped off this core. Look at the cores it can run from
982 * from to see if it is able to run on any of them */
983 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
984 BaseType_t xLowestPriority = pxPreviousTCB->uxPriority - pxPreviousTCB->xIsIdle;
985 BaseType_t xLowestPriorityCore = -1;
987 if( ( uxCoreMap & ( 1 << xCoreID ) ) != 0 )
989 /* The ready task that was removed from this core is not excluded from it.
990 * Only look at the intersection of the cores the removed task is allowed to run
991 * on with the cores that the new task is excluded from. It is possible that the
992 * new task was only placed onto this core because it is excluded from another.
993 * Check to see if the previous task could run on one of those cores. */
994 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
998 /* The ready task that was removed from this core is excluded from it.
999 * @todo See if we can schedule it on any of the cores where it is not excluded from. */
1002 uxCoreMap &= ( ( 1 << configNUM_CORES ) - 1 );
1004 while( uxCoreMap != 0 )
1006 int uxCore = 31UL - ( uint32_t ) __builtin_clz( uxCoreMap );
1008 xassert( taskVALID_CORE_ID( uxCore ) );
1010 uxCoreMap &= ~( 1 << uxCore );
1012 BaseType_t xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority - pxCurrentTCBs[ uxCore ]->xIsIdle;
1014 if( ( xTaskPriority < xLowestPriority ) && ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ]->xTaskRunState ) != pdFALSE ) && ( xYieldPendings[ uxCore ] == pdFALSE ) )
1016 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1017 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1020 xLowestPriority = xTaskPriority;
1021 xLowestPriorityCore = uxCore;
1026 if( taskVALID_CORE_ID( xLowestPriorityCore ) )
1028 prvYieldCore( xLowestPriorityCore );
1031 #endif /* if ( configUSE_CORE_AFFINITY == 1 ) */
1036 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1038 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
1040 UBaseType_t uxTopPriority;
1042 /* Find the highest priority list that contains ready tasks. */
1043 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority );
1044 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 );
1045 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) );
1048 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1049 /*-----------------------------------------------------------*/
1051 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1053 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1054 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1055 const uint32_t ulStackDepth,
1056 void * const pvParameters,
1057 UBaseType_t uxPriority,
1058 StackType_t * const puxStackBuffer,
1059 StaticTask_t * const pxTaskBuffer )
1062 TaskHandle_t xReturn;
1064 configASSERT( puxStackBuffer != NULL );
1065 configASSERT( pxTaskBuffer != NULL );
1067 #if ( configASSERT_DEFINED == 1 )
1069 /* Sanity check that the size of the structure used to declare a
1070 * variable of type StaticTask_t equals the size of the real task
1072 volatile size_t xSize = sizeof( StaticTask_t );
1073 configASSERT( xSize == sizeof( TCB_t ) );
1074 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1076 #endif /* configASSERT_DEFINED */
1078 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1080 prvTaskCreator( pxTaskCode, ulStack)
1081 /* The memory used for the task's TCB and stack are passed into this
1082 * function - use them. */
1083 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. */
1084 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1086 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1088 /* Tasks can be created statically or dynamically, so note this
1089 * task was created statically in case the task is later deleted. */
1090 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1092 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1094 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1095 prvAddNewTaskToReadyList( pxNewTCB );
1105 #endif /* SUPPORT_STATIC_ALLOCATION */
1106 /*-----------------------------------------------------------*/
1108 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1110 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1111 TaskHandle_t * pxCreatedTask )
1114 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1116 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1117 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1119 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1121 /* Allocate space for the TCB. Where the memory comes from depends
1122 * on the implementation of the port malloc function and whether or
1123 * not static allocation is being used. */
1124 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1126 /* Store the stack location in the TCB. */
1127 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1129 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1131 /* Tasks can be created statically or dynamically, so note this
1132 * task was created statically in case the task is later deleted. */
1133 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1135 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1137 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1138 pxTaskDefinition->pcName,
1139 ( uint32_t ) pxTaskDefinition->usStackDepth,
1140 pxTaskDefinition->pvParameters,
1141 pxTaskDefinition->uxPriority,
1142 pxCreatedTask, pxNewTCB,
1143 pxTaskDefinition->xRegions );
1145 prvAddNewTaskToReadyList( pxNewTCB );
1152 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1153 /*-----------------------------------------------------------*/
1155 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1157 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1158 TaskHandle_t * pxCreatedTask )
1161 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1163 configASSERT( pxTaskDefinition->puxStackBuffer );
1165 if( pxTaskDefinition->puxStackBuffer != NULL )
1167 /* Allocate space for the TCB. Where the memory comes from depends
1168 * on the implementation of the port malloc function and whether or
1169 * not static allocation is being used. */
1170 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1172 if( pxNewTCB != NULL )
1174 /* Store the stack location in the TCB. */
1175 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1177 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1179 /* Tasks can be created statically or dynamically, so note
1180 * this task had a statically allocated stack in case it is
1181 * later deleted. The TCB was allocated dynamically. */
1182 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1184 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1186 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1187 pxTaskDefinition->pcName,
1188 ( uint32_t ) pxTaskDefinition->usStackDepth,
1189 pxTaskDefinition->pvParameters,
1190 pxTaskDefinition->uxPriority,
1191 pxCreatedTask, pxNewTCB,
1192 pxTaskDefinition->xRegions );
1194 prvAddNewTaskToReadyList( pxNewTCB );
1202 #endif /* portUSING_MPU_WRAPPERS */
1203 /*-----------------------------------------------------------*/
1205 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1207 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1208 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1209 const configSTACK_DEPTH_TYPE usStackDepth,
1210 void * const pvParameters,
1211 UBaseType_t uxPriority,
1212 TaskHandle_t * const pxCreatedTask )
1217 /* If the stack grows down then allocate the stack then the TCB so the stack
1218 * does not grow into the TCB. Likewise if the stack grows up then allocate
1219 * the TCB then the stack. */
1220 #if ( portSTACK_GROWTH > 0 )
1222 /* Allocate space for the TCB. Where the memory comes from depends on
1223 * the implementation of the port malloc function and whether or not static
1224 * allocation is being used. */
1225 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1227 if( pxNewTCB != NULL )
1229 /* Allocate space for the stack used by the task being created.
1230 * The base of the stack memory stored in the TCB so the task can
1231 * be deleted later if required. */
1232 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1234 if( pxNewTCB->pxStack == NULL )
1236 /* Could not allocate the stack. Delete the allocated TCB. */
1237 vPortFree( pxNewTCB );
1242 #else /* portSTACK_GROWTH */
1244 StackType_t * pxStack;
1246 /* Allocate space for the stack used by the task being created. */
1247 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. */
1249 if( pxStack != NULL )
1251 /* Allocate space for the TCB. */
1252 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. */
1254 if( pxNewTCB != NULL )
1256 /* Store the stack location in the TCB. */
1257 pxNewTCB->pxStack = pxStack;
1261 /* The stack cannot be used as the TCB was not created. Free
1263 vPortFreeStack( pxStack );
1271 #endif /* portSTACK_GROWTH */
1273 if( pxNewTCB != NULL )
1275 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1277 /* Tasks can be created statically or dynamically, so note this
1278 * task was created dynamically in case it is later deleted. */
1279 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1281 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1283 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1284 prvAddNewTaskToReadyList( pxNewTCB );
1289 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1295 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1296 /*-----------------------------------------------------------*/
1298 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1299 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1300 const uint32_t ulStackDepth,
1301 void * const pvParameters,
1302 UBaseType_t uxPriority,
1303 TaskHandle_t * const pxCreatedTask,
1305 const MemoryRegion_t * const xRegions )
1307 StackType_t * pxTopOfStack;
1310 #if ( portUSING_MPU_WRAPPERS == 1 )
1311 /* Should the task be created in privileged mode? */
1312 BaseType_t xRunPrivileged;
1314 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1316 xRunPrivileged = pdTRUE;
1320 xRunPrivileged = pdFALSE;
1322 uxPriority &= ~portPRIVILEGE_BIT;
1323 #endif /* portUSING_MPU_WRAPPERS == 1 */
1325 /* Avoid dependency on memset() if it is not required. */
1326 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1328 /* Fill the stack with a known value to assist debugging. */
1329 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1331 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1333 /* Calculate the top of stack address. This depends on whether the stack
1334 * grows from high memory to low (as per the 80x86) or vice versa.
1335 * portSTACK_GROWTH is used to make the result positive or negative as required
1337 #if ( portSTACK_GROWTH < 0 )
1339 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1340 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(). */
1342 /* Check the alignment of the calculated top of stack is correct. */
1343 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1345 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1347 /* Also record the stack's high address, which may assist
1349 pxNewTCB->pxEndOfStack = pxTopOfStack;
1351 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1353 #else /* portSTACK_GROWTH */
1355 pxTopOfStack = pxNewTCB->pxStack;
1357 /* Check the alignment of the stack buffer is correct. */
1358 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1360 /* The other extreme of the stack space is required if stack checking is
1362 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1364 #endif /* portSTACK_GROWTH */
1366 /* Store the task name in the TCB. */
1367 if( pcName != NULL )
1369 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1371 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1373 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1374 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1375 * string is not accessible (extremely unlikely). */
1376 if( pcName[ x ] == ( char ) 0x00 )
1382 mtCOVERAGE_TEST_MARKER();
1386 /* Ensure the name string is terminated in the case that the string length
1387 * was greater or equal to configMAX_TASK_NAME_LEN. */
1388 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1392 /* The task has not been given a name, so just ensure there is a NULL
1393 * terminator when it is read out. */
1394 pxNewTCB->pcTaskName[ 0 ] = 0x00;
1397 /* This is used as an array index so must ensure it's not too large. First
1398 * remove the privilege bit if one is present. */
1399 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1401 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1405 mtCOVERAGE_TEST_MARKER();
1408 pxNewTCB->uxPriority = uxPriority;
1409 #if ( configUSE_MUTEXES == 1 )
1411 pxNewTCB->uxBasePriority = uxPriority;
1412 pxNewTCB->uxMutexesHeld = 0;
1414 #endif /* configUSE_MUTEXES */
1416 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1417 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1419 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1420 * back to the containing TCB from a generic item in a list. */
1421 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1423 /* Event lists are always in priority order. */
1424 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. */
1425 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1427 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
1429 pxNewTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
1431 #endif /* portCRITICAL_NESTING_IN_TCB */
1433 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
1435 pxNewTCB->pxTaskTag = NULL;
1437 #endif /* configUSE_APPLICATION_TASK_TAG */
1439 #if ( configGENERATE_RUN_TIME_STATS == 1 )
1441 pxNewTCB->ulRunTimeCounter = 0UL;
1443 #endif /* configGENERATE_RUN_TIME_STATS */
1445 #if ( portUSING_MPU_WRAPPERS == 1 )
1447 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1451 /* Avoid compiler warning about unreferenced parameter. */
1456 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
1458 memset( ( void * ) &( pxNewTCB->pvThreadLocalStoragePointers[ 0 ] ), 0x00, sizeof( pxNewTCB->pvThreadLocalStoragePointers ) );
1462 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1464 memset( ( void * ) &( pxNewTCB->ulNotifiedValue[ 0 ] ), 0x00, sizeof( pxNewTCB->ulNotifiedValue ) );
1465 memset( ( void * ) &( pxNewTCB->ucNotifyState[ 0 ] ), 0x00, sizeof( pxNewTCB->ucNotifyState ) );
1469 #if ( configUSE_NEWLIB_REENTRANT == 1 )
1471 /* Initialise this task's Newlib reent structure.
1472 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
1473 * for additional information. */
1474 _REENT_INIT_PTR( ( &( pxNewTCB->xNewLib_reent ) ) );
1478 #if ( INCLUDE_xTaskAbortDelay == 1 )
1480 pxNewTCB->ucDelayAborted = pdFALSE;
1484 #if ( configUSE_CORE_AFFINITY == 1 )
1486 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1489 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1491 pxNewTCB->xPreemptionDisable = 0;
1495 /* Initialize the TCB stack to look as if the task was already running,
1496 * but had been interrupted by the scheduler. The return address is set
1497 * to the start of the task function. Once the stack has been initialised
1498 * the top of stack variable is updated. */
1499 #if ( portUSING_MPU_WRAPPERS == 1 )
1501 /* If the port has capability to detect stack overflow,
1502 * pass the stack end address to the stack initialization
1503 * function as well. */
1504 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1506 #if ( portSTACK_GROWTH < 0 )
1508 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
1510 #else /* portSTACK_GROWTH */
1512 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1514 #endif /* portSTACK_GROWTH */
1516 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1518 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1520 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1522 #else /* portUSING_MPU_WRAPPERS */
1524 /* If the port has capability to detect stack overflow,
1525 * pass the stack end address to the stack initialization
1526 * function as well. */
1527 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1529 #if ( portSTACK_GROWTH < 0 )
1531 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1533 #else /* portSTACK_GROWTH */
1535 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1537 #endif /* portSTACK_GROWTH */
1539 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1541 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1543 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1545 #endif /* portUSING_MPU_WRAPPERS */
1547 /* Initialize to not running */
1548 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1550 /* Is this an idle task? */
1551 if(pxTaskCode == prvIdleTask)
1553 pxNewTCB->xIsIdle = pdTRUE;
1555 #if(configNUM_CORES > 1)
1556 else if(pxTaskCode == prvMinimalIdleTask)
1558 pxNewTCB->xIsIdle = pdTRUE;
1563 pxNewTCB->xIsIdle = pdFALSE;
1566 if( pxCreatedTask != NULL )
1568 /* Pass the handle out in an anonymous way. The handle can be used to
1569 * change the created task's priority, delete the created task, etc.*/
1570 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1574 mtCOVERAGE_TEST_MARKER();
1577 /*-----------------------------------------------------------*/
1579 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1581 /* Ensure interrupts don't access the task lists while the lists are being
1583 taskENTER_CRITICAL();
1585 uxCurrentNumberOfTasks++;
1587 if( xSchedulerRunning == pdFALSE )
1589 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1591 /* This is the first task to be created so do the preliminary
1592 * initialisation required. We will not recover if this call
1593 * fails, but we will report the failure. */
1594 prvInitialiseTaskLists();
1598 mtCOVERAGE_TEST_MARKER();
1601 if( pxNewTCB->xIsIdle != pdFALSE )
1605 /* Check if a core is free. */
1606 for( xCoreID = ( UBaseType_t ) 0; xCoreID < ( UBaseType_t ) configNUM_CORES; xCoreID++ )
1608 if( pxCurrentTCBs[ xCoreID ] == NULL )
1610 pxNewTCB->xTaskRunState = xCoreID;
1611 #if ( configUSE_CORE_AFFINITY == 1 )
1613 pxNewTCB->uxCoreAffinityMask = ( 1 << xCoreID );
1616 pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1624 mtCOVERAGE_TEST_MARKER();
1629 #if ( configUSE_TRACE_FACILITY == 1 )
1631 /* Add a counter into the TCB for tracing only. */
1632 pxNewTCB->uxTCBNumber = uxTaskNumber;
1634 #endif /* configUSE_TRACE_FACILITY */
1635 traceTASK_CREATE( pxNewTCB );
1637 prvAddTaskToReadyList( pxNewTCB );
1639 portSETUP_TCB( pxNewTCB );
1641 if( xSchedulerRunning != pdFALSE )
1643 /* If the created task is of a higher priority than another
1644 * currently running task and preemption is on then it should
1646 #if ( configUSE_PREEMPTION == 1 )
1647 prvYieldForTask( pxNewTCB, pdFALSE );
1652 mtCOVERAGE_TEST_MARKER();
1655 taskEXIT_CRITICAL();
1657 /*-----------------------------------------------------------*/
1659 #if ( INCLUDE_vTaskDelete == 1 )
1661 void vTaskDelete( TaskHandle_t xTaskToDelete )
1664 TaskRunning_t xTaskRunningOnCore;
1666 taskENTER_CRITICAL();
1668 /* If null is passed in here then it is the calling task that is
1670 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1672 xTaskRunningOnCore = pxTCB->xTaskRunState;
1674 /* Remove task from the ready/delayed list. */
1675 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1677 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1681 mtCOVERAGE_TEST_MARKER();
1684 /* Is the task waiting on an event also? */
1685 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1687 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1691 mtCOVERAGE_TEST_MARKER();
1694 /* Increment the uxTaskNumber also so kernel aware debuggers can
1695 * detect that the task lists need re-generating. This is done before
1696 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1700 /* If the task is running (or yielding), we must add it to the
1701 * termination list so that an idle task can delete it when it is
1702 * no longer running. */
1703 if( xTaskRunningOnCore != taskTASK_NOT_RUNNING )
1706 /* A running task is being deleted. This cannot complete within the
1707 * task itself, as a context switch to another task is required.
1708 * Place the task in the termination list. The idle task will
1709 * check the termination list and free up any memory allocated by
1710 * the scheduler for the TCB and stack of the deleted task. */
1711 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1713 /* Increment the ucTasksDeleted variable so the idle task knows
1714 * there is a task that has been deleted and that it should therefore
1715 * check the xTasksWaitingTermination list. */
1716 ++uxDeletedTasksWaitingCleanUp;
1718 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1719 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1720 traceTASK_DELETE( pxTCB );
1722 /* The pre-delete hook is primarily for the Windows simulator,
1723 * in which Windows specific clean up operations are performed,
1724 * after which it is not possible to yield away from this task -
1725 * hence xYieldPending is used to latch that a context switch is
1727 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPendings[ pxTCB->xTaskRunState ] );
1731 --uxCurrentNumberOfTasks;
1732 traceTASK_DELETE( pxTCB );
1733 prvDeleteTCB( pxTCB );
1735 /* Reset the next expected unblock time in case it referred to
1736 * the task that has just been deleted. */
1737 prvResetNextTaskUnblockTime();
1740 /* Force a reschedule if the task that has just been deleted was running. */
1741 if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( xTaskRunningOnCore ) ) )
1745 xCoreID = portGET_CORE_ID();
1748 if( xTaskRunningOnCore == xCoreID )
1750 configASSERT( uxSchedulerSuspended == 0 );
1751 vTaskYieldWithinAPI();
1755 prvYieldCore( xTaskRunningOnCore );
1759 taskEXIT_CRITICAL();
1762 #endif /* INCLUDE_vTaskDelete */
1763 /*-----------------------------------------------------------*/
1765 #if ( INCLUDE_xTaskDelayUntil == 1 )
1767 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
1768 const TickType_t xTimeIncrement )
1770 TickType_t xTimeToWake;
1771 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
1773 configASSERT( pxPreviousWakeTime );
1774 configASSERT( ( xTimeIncrement > 0U ) );
1778 configASSERT( uxSchedulerSuspended == 1 );
1780 /* Minor optimisation. The tick count cannot change in this
1782 const TickType_t xConstTickCount = xTickCount;
1784 /* Generate the tick time at which the task wants to wake. */
1785 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
1787 if( xConstTickCount < *pxPreviousWakeTime )
1789 /* The tick count has overflowed since this function was
1790 * lasted called. In this case the only time we should ever
1791 * actually delay is if the wake time has also overflowed,
1792 * and the wake time is greater than the tick time. When this
1793 * is the case it is as if neither time had overflowed. */
1794 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1796 xShouldDelay = pdTRUE;
1800 mtCOVERAGE_TEST_MARKER();
1805 /* The tick time has not overflowed. In this case we will
1806 * delay if either the wake time has overflowed, and/or the
1807 * tick time is less than the wake time. */
1808 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1810 xShouldDelay = pdTRUE;
1814 mtCOVERAGE_TEST_MARKER();
1818 /* Update the wake time ready for the next call. */
1819 *pxPreviousWakeTime = xTimeToWake;
1821 if( xShouldDelay != pdFALSE )
1823 traceTASK_DELAY_UNTIL( xTimeToWake );
1825 /* prvAddCurrentTaskToDelayedList() needs the block time, not
1826 * the time to wake, so subtract the current tick count. */
1827 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
1831 mtCOVERAGE_TEST_MARKER();
1834 xAlreadyYielded = xTaskResumeAll();
1836 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1837 * have put ourselves to sleep. */
1838 if( xAlreadyYielded == pdFALSE )
1840 vTaskYieldWithinAPI();
1844 mtCOVERAGE_TEST_MARKER();
1847 return xShouldDelay;
1850 #endif /* INCLUDE_xTaskDelayUntil */
1851 /*-----------------------------------------------------------*/
1853 #if ( INCLUDE_vTaskDelay == 1 )
1855 void vTaskDelay( const TickType_t xTicksToDelay )
1857 BaseType_t xAlreadyYielded = pdFALSE;
1859 /* A delay time of zero just forces a reschedule. */
1860 if( xTicksToDelay > ( TickType_t ) 0U )
1864 configASSERT( uxSchedulerSuspended == 1 );
1867 /* A task that is removed from the event list while the
1868 * scheduler is suspended will not get placed in the ready
1869 * list or removed from the blocked list until the scheduler
1872 * This task cannot be in an event list as it is the currently
1873 * executing task. */
1874 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
1876 xAlreadyYielded = xTaskResumeAll();
1880 mtCOVERAGE_TEST_MARKER();
1883 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1884 * have put ourselves to sleep. */
1885 if( xAlreadyYielded == pdFALSE )
1887 vTaskYieldWithinAPI();
1891 mtCOVERAGE_TEST_MARKER();
1895 #endif /* INCLUDE_vTaskDelay */
1896 /*-----------------------------------------------------------*/
1898 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
1900 eTaskState eTaskGetState( TaskHandle_t xTask )
1903 List_t const * pxStateList, * pxDelayedList, * pxOverflowedDelayedList;
1904 const TCB_t * const pxTCB = xTask;
1906 configASSERT( pxTCB );
1908 taskENTER_CRITICAL();
1910 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
1911 pxDelayedList = pxDelayedTaskList;
1912 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
1914 taskEXIT_CRITICAL();
1916 if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
1918 /* The task being queried is referenced from one of the Blocked
1923 #if ( INCLUDE_vTaskSuspend == 1 )
1924 else if( pxStateList == &xSuspendedTaskList )
1926 /* The task being queried is referenced from the suspended
1927 * list. Is it genuinely suspended or is it blocked
1929 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
1931 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1935 /* The task does not appear on the event list item of
1936 * and of the RTOS objects, but could still be in the
1937 * blocked state if it is waiting on its notification
1938 * rather than waiting on an object. If not, is
1940 eReturn = eSuspended;
1942 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1944 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1951 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1953 eReturn = eSuspended;
1955 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1962 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
1964 #if ( INCLUDE_vTaskDelete == 1 )
1965 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
1967 /* The task being queried is referenced from the deleted
1968 * tasks list, or it is not referenced from any lists at
1974 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
1976 /* If the task is not in any other state, it must be in the
1977 * Ready (including pending ready) state. */
1978 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
1980 /* Is it actively running on a core? */
1990 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
1992 #endif /* INCLUDE_eTaskGetState */
1993 /*-----------------------------------------------------------*/
1995 #if ( INCLUDE_uxTaskPriorityGet == 1 )
1997 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
1999 TCB_t const * pxTCB;
2000 UBaseType_t uxReturn;
2002 taskENTER_CRITICAL();
2004 /* If null is passed in here then it is the priority of the task
2005 * that called uxTaskPriorityGet() that is being queried. */
2006 pxTCB = prvGetTCBFromHandle( xTask );
2007 uxReturn = pxTCB->uxPriority;
2009 taskEXIT_CRITICAL();
2014 #endif /* INCLUDE_uxTaskPriorityGet */
2015 /*-----------------------------------------------------------*/
2017 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2019 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2021 TCB_t const * pxTCB;
2022 UBaseType_t uxReturn, uxSavedInterruptState;
2024 /* RTOS ports that support interrupt nesting have the concept of a
2025 * maximum system call (or maximum API call) interrupt priority.
2026 * Interrupts that are above the maximum system call priority are keep
2027 * permanently enabled, even when the RTOS kernel is in a critical section,
2028 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2029 * is defined in FreeRTOSConfig.h then
2030 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2031 * failure if a FreeRTOS API function is called from an interrupt that has
2032 * been assigned a priority above the configured maximum system call
2033 * priority. Only FreeRTOS functions that end in FromISR can be called
2034 * from interrupts that have been assigned a priority at or (logically)
2035 * below the maximum system call interrupt priority. FreeRTOS maintains a
2036 * separate interrupt safe API to ensure interrupt entry is as fast and as
2037 * simple as possible. More information (albeit Cortex-M specific) is
2038 * provided on the following link:
2039 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2040 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2042 uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
2044 /* If null is passed in here then it is the priority of the calling
2045 * task that is being queried. */
2046 pxTCB = prvGetTCBFromHandle( xTask );
2047 uxReturn = pxTCB->uxPriority;
2049 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
2054 #endif /* INCLUDE_uxTaskPriorityGet */
2055 /*-----------------------------------------------------------*/
2057 #if ( INCLUDE_vTaskPrioritySet == 1 )
2059 void vTaskPrioritySet( TaskHandle_t xTask,
2060 UBaseType_t uxNewPriority )
2063 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2064 BaseType_t xYieldRequired = pdFALSE;
2065 BaseType_t xYieldForTask = pdFALSE;
2068 configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
2070 /* Ensure the new priority is valid. */
2071 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2073 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2077 mtCOVERAGE_TEST_MARKER();
2080 taskENTER_CRITICAL();
2082 /* If null is passed in here then it is the priority of the calling
2083 * task that is being changed. */
2084 pxTCB = prvGetTCBFromHandle( xTask );
2086 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2088 #if ( configUSE_MUTEXES == 1 )
2090 uxCurrentBasePriority = pxTCB->uxBasePriority;
2094 uxCurrentBasePriority = pxTCB->uxPriority;
2098 if( uxCurrentBasePriority != uxNewPriority )
2100 /* The priority change may have readied a task of higher
2101 * priority than a running task. */
2102 if( uxNewPriority > uxCurrentBasePriority )
2104 /* The priority of a task is being raised so
2105 * perform a yield for this task later. */
2106 xYieldForTask = pdTRUE;
2108 else if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2110 /* Setting the priority of a running task down means
2111 * there may now be another task of higher priority that
2112 * is ready to execute. */
2113 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2114 if( pxTCB->xPreemptionDisable == pdFALSE )
2117 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2118 xYieldRequired = pdTRUE;
2123 /* Setting the priority of any other task down does not
2124 * require a yield as the running task must be above the
2125 * new priority of the task being modified. */
2128 /* Remember the ready list the task might be referenced from
2129 * before its uxPriority member is changed so the
2130 * taskRESET_READY_PRIORITY() macro can function correctly. */
2131 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2133 #if ( configUSE_MUTEXES == 1 )
2135 /* Only change the priority being used if the task is not
2136 * currently using an inherited priority. */
2137 if( pxTCB->uxBasePriority == pxTCB->uxPriority )
2139 pxTCB->uxPriority = uxNewPriority;
2143 mtCOVERAGE_TEST_MARKER();
2146 /* The base priority gets set whatever. */
2147 pxTCB->uxBasePriority = uxNewPriority;
2149 #else /* if ( configUSE_MUTEXES == 1 ) */
2151 pxTCB->uxPriority = uxNewPriority;
2153 #endif /* if ( configUSE_MUTEXES == 1 ) */
2155 /* Only reset the event list item value if the value is not
2156 * being used for anything else. */
2157 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2159 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. */
2163 mtCOVERAGE_TEST_MARKER();
2166 /* If the task is in the blocked or suspended list we need do
2167 * nothing more than change its priority variable. However, if
2168 * the task is in a ready list it needs to be removed and placed
2169 * in the list appropriate to its new priority. */
2170 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2172 /* The task is currently in its ready list - remove before
2173 * adding it to its new ready list. As we are in a critical
2174 * section we can do this even if the scheduler is suspended. */
2175 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2177 /* It is known that the task is in its ready list so
2178 * there is no need to check again and the port level
2179 * reset macro can be called directly. */
2180 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2184 mtCOVERAGE_TEST_MARKER();
2187 prvAddTaskToReadyList( pxTCB );
2191 /* It's possible that xYieldForTask was already set to pdTRUE because
2192 * its priority is being raised. However, since it is not in a ready list
2193 * we don't actually need to yield for it. */
2194 xYieldForTask = pdFALSE;
2197 #if ( configUSE_PREEMPTION == 1 )
2198 if( xYieldRequired != pdFALSE )
2200 prvYieldCore( xCoreID );
2202 else if( xYieldForTask != pdFALSE )
2204 prvYieldForTask( pxTCB, pdTRUE );
2208 mtCOVERAGE_TEST_MARKER();
2210 #endif /* if ( configUSE_PREEMPTION == 1 ) */
2212 /* Remove compiler warning about unused variables when the port
2213 * optimised task selection is not being used. */
2214 ( void ) uxPriorityUsedOnEntry;
2217 taskEXIT_CRITICAL();
2220 #endif /* INCLUDE_vTaskPrioritySet */
2221 /*-----------------------------------------------------------*/
2223 #if ( configUSE_CORE_AFFINITY == 1 )
2225 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2226 UBaseType_t uxCoreAffinityMask )
2231 taskENTER_CRITICAL();
2233 pxTCB = prvGetTCBFromHandle( xTask );
2235 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2237 if( xSchedulerRunning != pdFALSE )
2239 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2241 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2243 if( ( uxCoreAffinityMask & ( 1 << xCoreID ) ) != 1 )
2245 prvYieldCore( xCoreID );
2250 taskEXIT_CRITICAL();
2253 #endif /* configUSE_CORE_AFFINITY */
2254 /*-----------------------------------------------------------*/
2256 #if ( configUSE_CORE_AFFINITY == 1 )
2258 UBaseType_t vTaskCoreAffinityGet( const TaskHandle_t xTask )
2261 UBaseType_t uxCoreAffinityMask;
2263 taskENTER_CRITICAL();
2265 pxTCB = prvGetTCBFromHandle( xTask );
2266 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2268 taskEXIT_CRITICAL();
2270 return uxCoreAffinityMask;
2273 #endif /* configUSE_CORE_AFFINITY */
2274 /*-----------------------------------------------------------*/
2276 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2278 void vTaskPreemptionDisable( const TaskHandle_t xTask )
2282 taskENTER_CRITICAL();
2284 pxTCB = prvGetTCBFromHandle( xTask );
2286 pxTCB->xPreemptionDisable = pdTRUE;
2288 taskEXIT_CRITICAL();
2291 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2292 /*-----------------------------------------------------------*/
2294 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2296 void vTaskPreemptionEnable( const TaskHandle_t xTask )
2301 taskENTER_CRITICAL();
2303 pxTCB = prvGetTCBFromHandle( xTask );
2305 pxTCB->xPreemptionDisable = pdFALSE;
2307 if( xSchedulerRunning != pdFALSE )
2309 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2311 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2312 prvYieldCore( xCoreID );
2316 taskEXIT_CRITICAL();
2319 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2320 /*-----------------------------------------------------------*/
2322 #if ( INCLUDE_vTaskSuspend == 1 )
2324 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2327 TaskRunning_t xTaskRunningOnCore;
2329 taskENTER_CRITICAL();
2331 /* If null is passed in here then it is the running task that is
2332 * being suspended. */
2333 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2335 traceTASK_SUSPEND( pxTCB );
2337 xTaskRunningOnCore = pxTCB->xTaskRunState;
2339 /* Remove task from the ready/delayed list and place in the
2340 * suspended list. */
2341 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2343 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2347 mtCOVERAGE_TEST_MARKER();
2350 /* Is the task waiting on an event also? */
2351 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2353 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2357 mtCOVERAGE_TEST_MARKER();
2360 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2362 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2366 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2368 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2370 /* The task was blocked to wait for a notification, but is
2371 * now suspended, so no notification was received. */
2372 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2376 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2378 if( xSchedulerRunning != pdFALSE )
2380 /* Reset the next expected unblock time in case it referred to the
2381 * task that is now in the Suspended state. */
2382 prvResetNextTaskUnblockTime();
2386 mtCOVERAGE_TEST_MARKER();
2389 if( taskTASK_IS_RUNNING( xTaskRunningOnCore ) )
2391 if( xSchedulerRunning != pdFALSE )
2393 if( xTaskRunningOnCore == portGET_CORE_ID() )
2395 /* The current task has just been suspended. */
2396 configASSERT( uxSchedulerSuspended == 0 );
2397 vTaskYieldWithinAPI();
2401 prvYieldCore( xTaskRunningOnCore );
2404 taskEXIT_CRITICAL();
2408 taskEXIT_CRITICAL();
2410 configASSERT( pxTCB == pxCurrentTCBs[ xTaskRunningOnCore ] );
2412 /* The scheduler is not running, but the task that was pointed
2413 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
2414 * must be adjusted to point to a different task. */
2415 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
2417 /* No other tasks are ready, so set the core's TCB back to
2418 * NULL so when the next task is created the core's TCB will
2419 * be able to be set to point to it no matter what its relative
2421 pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2422 pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2426 /* Attempt to switch in a new task. This could fail since the idle tasks
2427 * haven't been created yet. If it does then set the core's TCB back to
2429 if( prvSelectHighestPriorityTask( xTaskRunningOnCore ) == pdFALSE )
2431 pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2432 pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2439 taskEXIT_CRITICAL();
2441 } /* taskEXIT_CRITICAL() - already exited in one of three cases above */
2444 #endif /* INCLUDE_vTaskSuspend */
2445 /*-----------------------------------------------------------*/
2447 #if ( INCLUDE_vTaskSuspend == 1 )
2449 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
2451 BaseType_t xReturn = pdFALSE;
2452 const TCB_t * const pxTCB = xTask;
2454 /* Accesses xPendingReadyList so must be called from a critical section. */
2456 /* It does not make sense to check if the calling task is suspended. */
2457 configASSERT( xTask );
2459 /* Is the task being resumed actually in the suspended list? */
2460 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
2462 /* Has the task already been resumed from within an ISR? */
2463 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
2465 /* Is it in the suspended list because it is in the Suspended
2466 * state, or because is is blocked with no timeout? */
2467 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
2473 mtCOVERAGE_TEST_MARKER();
2478 mtCOVERAGE_TEST_MARKER();
2483 mtCOVERAGE_TEST_MARKER();
2487 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2489 #endif /* INCLUDE_vTaskSuspend */
2490 /*-----------------------------------------------------------*/
2492 #if ( INCLUDE_vTaskSuspend == 1 )
2494 void vTaskResume( TaskHandle_t xTaskToResume )
2496 TCB_t * const pxTCB = xTaskToResume;
2498 /* It does not make sense to resume the calling task. */
2499 configASSERT( xTaskToResume );
2501 /* The parameter cannot be NULL as it is impossible to resume the
2502 * currently executing task. It is also impossible to resume a task
2503 * that is actively running on another core but it is too dangerous
2504 * to check their run state here. Safer to get into a critical section
2505 * and check if it is actually suspended or not below. */
2508 taskENTER_CRITICAL();
2510 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2512 traceTASK_RESUME( pxTCB );
2514 /* The ready list can be accessed even if the scheduler is
2515 * suspended because this is inside a critical section. */
2516 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2517 prvAddTaskToReadyList( pxTCB );
2519 /* A higher priority task may have just been resumed. */
2520 #if ( configUSE_PREEMPTION == 1 )
2522 prvYieldForTask( pxTCB, pdTRUE );
2528 mtCOVERAGE_TEST_MARKER();
2531 taskEXIT_CRITICAL();
2535 mtCOVERAGE_TEST_MARKER();
2539 #endif /* INCLUDE_vTaskSuspend */
2541 /*-----------------------------------------------------------*/
2543 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
2545 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
2547 BaseType_t xYieldRequired = pdFALSE;
2548 TCB_t * const pxTCB = xTaskToResume;
2549 UBaseType_t uxSavedInterruptStatus;
2551 configASSERT( xTaskToResume );
2553 /* RTOS ports that support interrupt nesting have the concept of a
2554 * maximum system call (or maximum API call) interrupt priority.
2555 * Interrupts that are above the maximum system call priority are keep
2556 * permanently enabled, even when the RTOS kernel is in a critical section,
2557 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2558 * is defined in FreeRTOSConfig.h then
2559 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2560 * failure if a FreeRTOS API function is called from an interrupt that has
2561 * been assigned a priority above the configured maximum system call
2562 * priority. Only FreeRTOS functions that end in FromISR can be called
2563 * from interrupts that have been assigned a priority at or (logically)
2564 * below the maximum system call interrupt priority. FreeRTOS maintains a
2565 * separate interrupt safe API to ensure interrupt entry is as fast and as
2566 * simple as possible. More information (albeit Cortex-M specific) is
2567 * provided on the following link:
2568 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2569 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2571 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2573 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2575 traceTASK_RESUME_FROM_ISR( pxTCB );
2577 /* Check the ready lists can be accessed. */
2578 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2580 /* Ready lists can be accessed so move the task from the
2581 * suspended list to the ready list directly. */
2583 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2584 prvAddTaskToReadyList( pxTCB );
2588 /* The delayed or ready lists cannot be accessed so the task
2589 * is held in the pending ready list until the scheduler is
2591 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
2594 #if ( configUSE_PREEMPTION == 1 )
2595 prvYieldForTask( pxTCB, pdTRUE );
2597 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
2599 xYieldRequired = pdTRUE;
2605 mtCOVERAGE_TEST_MARKER();
2608 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2610 return xYieldRequired;
2613 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
2614 /*-----------------------------------------------------------*/
2616 static BaseType_t prvCreateIdleTasks( void )
2618 BaseType_t xReturn = pdPASS;
2620 char cIdleName[ configMAX_TASK_NAME_LEN ];
2622 /* Add each idle task at the lowest priority. */
2623 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUM_CORES; xCoreID++ )
2627 if( xReturn == pdFAIL )
2633 mtCOVERAGE_TEST_MARKER();
2636 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
2638 cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
2640 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
2641 * configMAX_TASK_NAME_LEN characters just in case the memory after the
2642 * string is not accessible (extremely unlikely). */
2643 if( cIdleName[ x ] == ( char ) 0x00 )
2649 mtCOVERAGE_TEST_MARKER();
2653 /* Append the idle task number to the end of the name if there is space */
2654 if( x < configMAX_TASK_NAME_LEN )
2656 cIdleName[ x++ ] = xCoreID + '0';
2658 /* And append a null character if there is space */
2659 if( x < configMAX_TASK_NAME_LEN )
2661 cIdleName[ x ] = '\0';
2665 mtCOVERAGE_TEST_MARKER();
2670 mtCOVERAGE_TEST_MARKER();
2673 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
2677 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
2678 StackType_t * pxIdleTaskStackBuffer = NULL;
2679 uint32_t ulIdleTaskStackSize;
2681 /* The Idle task is created using user provided RAM - obtain the
2682 * address of the RAM then create the idle task. */
2683 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
2684 xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
2686 ulIdleTaskStackSize,
2687 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
2688 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2689 pxIdleTaskStackBuffer,
2690 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2692 #if( configNUM_CORES > 1)
2695 static StaticTask_t xIdleTCBBuffers[configNUM_CORES-1];
2696 static StackType_t xIdleTaskStackBuffers[configMINIMAL_STACK_SIZE][configNUM_CORES-1];
2698 xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvMinimalIdleTask,
2700 configMINIMAL_STACK_SIZE,
2701 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
2702 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2703 xIdleTaskStackBuffers[xCoreID-1],
2704 &xIdleTCBBuffers[xCoreID-1] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2707 if( xIdleTaskHandle[ xCoreID ] != NULL )
2716 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
2720 /* The Idle task is being created using dynamically allocated RAM. */
2721 xReturn = xTaskCreate( prvIdleTask,
2723 configMINIMAL_STACK_SIZE,
2725 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2726 &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2728 #if( configNUM_CORES > 1 )
2731 xReturn = xTaskCreate( prvMinimalIdleTask,
2733 configMINIMAL_STACK_SIZE,
2735 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2736 &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2740 #endif /* configSUPPORT_STATIC_ALLOCATION */
2745 void vTaskStartScheduler( void )
2749 #if ( configUSE_TIMERS == 1 )
2751 xReturn = xTimerCreateTimerTask();
2753 #endif /* configUSE_TIMERS */
2755 xReturn = prvCreateIdleTasks();
2757 if( xReturn == pdPASS )
2759 /* freertos_tasks_c_additions_init() should only be called if the user
2760 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
2761 * the only macro called by the function. */
2762 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
2764 freertos_tasks_c_additions_init();
2768 /* Interrupts are turned off here, to ensure a tick does not occur
2769 * before or during the call to xPortStartScheduler(). The stacks of
2770 * the created tasks contain a status word with interrupts switched on
2771 * so interrupts will automatically get re-enabled when the first task
2773 portDISABLE_INTERRUPTS();
2775 #if ( configUSE_NEWLIB_REENTRANT == 1 )
2777 /* Switch Newlib's _impure_ptr variable to point to the _reent
2778 * structure specific to the task that will run first.
2779 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
2780 * for additional information. */
2781 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
2783 #endif /* configUSE_NEWLIB_REENTRANT */
2785 xNextTaskUnblockTime = portMAX_DELAY;
2786 xSchedulerRunning = pdTRUE;
2787 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
2789 /* If configGENERATE_RUN_TIME_STATS is defined then the following
2790 * macro must be defined to configure the timer/counter used to generate
2791 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
2792 * is set to 0 and the following line fails to build then ensure you do not
2793 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
2794 * FreeRTOSConfig.h file. */
2795 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
2797 traceTASK_SWITCHED_IN();
2799 /* Setting up the timer tick is hardware specific and thus in the
2800 * portable interface. */
2801 if( xPortStartScheduler() != pdFALSE )
2803 /* Should not reach here as if the scheduler is running the
2804 * function will not return. */
2808 /* Should only reach here if a task calls xTaskEndScheduler(). */
2813 /* This line will only be reached if the kernel could not be started,
2814 * because there was not enough FreeRTOS heap to create the idle task
2815 * or the timer task. */
2816 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
2819 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
2820 * meaning xIdleTaskHandle is not used anywhere else. */
2821 ( void ) xIdleTaskHandle;
2823 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
2824 * from getting optimized out as it is no longer used by the kernel. */
2825 ( void ) uxTopUsedPriority;
2827 /*-----------------------------------------------------------*/
2829 void vTaskEndScheduler( void )
2831 /* Stop the scheduler interrupts and call the portable scheduler end
2832 * routine so the original ISRs can be restored if necessary. The port
2833 * layer must ensure interrupts enable bit is left in the correct state. */
2834 portDISABLE_INTERRUPTS();
2835 xSchedulerRunning = pdFALSE;
2836 vPortEndScheduler();
2838 /*----------------------------------------------------------*/
2840 void vTaskSuspendAll( void )
2842 UBaseType_t ulState;
2844 /* This must only be called from within a task */
2845 portASSERT_IF_IN_ISR();
2847 if( xSchedulerRunning != pdFALSE )
2849 /* writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
2850 * We must disable interrupts before we grab the locks in the event that this task is
2851 * interrupted and switches context before incrementing uxSchedulerSuspended.
2852 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
2853 * uxSchedulerSuspended since that will prevent context switches. */
2854 ulState = portDISABLE_INTERRUPTS();
2856 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
2857 * do not otherwise exhibit real time behaviour. */
2858 portSOFTWARE_BARRIER();
2860 portGET_TASK_LOCK();
2863 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
2864 * is used to allow calls to vTaskSuspendAll() to nest. */
2865 ++uxSchedulerSuspended;
2866 portRELEASE_ISR_LOCK();
2868 if( ( uxSchedulerSuspended == 1U ) && ( pxCurrentTCB->uxCriticalNesting == 0U ) )
2870 prvCheckForRunStateChange();
2873 portRESTORE_INTERRUPTS( ulState );
2877 mtCOVERAGE_TEST_MARKER();
2880 /*----------------------------------------------------------*/
2882 #if ( configUSE_TICKLESS_IDLE != 0 )
2884 static TickType_t prvGetExpectedIdleTime( void )
2887 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
2889 /* uxHigherPriorityReadyTasks takes care of the case where
2890 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
2891 * task that are in the Ready state, even though the idle task is
2893 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
2895 if( uxTopReadyPriority > tskIDLE_PRIORITY )
2897 uxHigherPriorityReadyTasks = pdTRUE;
2902 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
2904 /* When port optimised task selection is used the uxTopReadyPriority
2905 * variable is used as a bit map. If bits other than the least
2906 * significant bit are set then there are tasks that have a priority
2907 * above the idle priority that are in the Ready state. This takes
2908 * care of the case where the co-operative scheduler is in use. */
2909 if( uxTopReadyPriority > uxLeastSignificantBit )
2911 uxHigherPriorityReadyTasks = pdTRUE;
2914 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
2916 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
2920 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
2922 /* There are other idle priority tasks in the ready state. If
2923 * time slicing is used then the very next tick interrupt must be
2927 else if( uxHigherPriorityReadyTasks != pdFALSE )
2929 /* There are tasks in the Ready state that have a priority above the
2930 * idle priority. This path can only be reached if
2931 * configUSE_PREEMPTION is 0. */
2936 xReturn = xNextTaskUnblockTime - xTickCount;
2942 #endif /* configUSE_TICKLESS_IDLE */
2943 /*----------------------------------------------------------*/
2945 BaseType_t xTaskResumeAll( void )
2947 TCB_t * pxTCB = NULL;
2948 BaseType_t xAlreadyYielded = pdFALSE;
2950 if( xSchedulerRunning != pdFALSE )
2952 /* It is possible that an ISR caused a task to be removed from an event
2953 * list while the scheduler was suspended. If this was the case then the
2954 * removed task will have been added to the xPendingReadyList. Once the
2955 * scheduler has been resumed it is safe to move all the pending ready
2956 * tasks from this list into their appropriate ready list. */
2957 taskENTER_CRITICAL();
2961 xCoreID = portGET_CORE_ID();
2963 /* If uxSchedulerSuspended is zero then this function does not match a
2964 * previous call to vTaskSuspendAll(). */
2965 configASSERT( uxSchedulerSuspended );
2967 --uxSchedulerSuspended;
2968 portRELEASE_TASK_LOCK();
2970 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2972 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
2974 /* Move any readied tasks from the pending list into the
2975 * appropriate ready list. */
2976 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
2978 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
2979 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2980 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2981 prvAddTaskToReadyList( pxTCB );
2983 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
2984 * If the current core yielded then vTaskSwitchContext() has already been called
2985 * which sets xYieldPendings for the current core to pdTRUE. */
2990 /* A task was unblocked while the scheduler was suspended,
2991 * which may have prevented the next unblock time from being
2992 * re-calculated, in which case re-calculate it now. Mainly
2993 * important for low power tickless implementations, where
2994 * this can prevent an unnecessary exit from low power
2996 prvResetNextTaskUnblockTime();
2999 /* If any ticks occurred while the scheduler was suspended then
3000 * they should be processed now. This ensures the tick count does
3001 * not slip, and that any delayed tasks are resumed at the correct
3004 * It should be safe to call xTaskIncrementTick here from any core
3005 * since we are in a critical section and xTaskIncrementTick itself
3006 * protects itself within a critical section. Suspending the scheduler
3007 * from any core causes xTaskIncrementTick to increment uxPendedCounts.*/
3009 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3011 if( xPendedCounts > ( TickType_t ) 0U )
3015 if( xTaskIncrementTick() != pdFALSE )
3017 /* other cores are interrupted from
3018 * within xTaskIncrementTick(). */
3019 xYieldPendings[ xCoreID ] = pdTRUE;
3023 mtCOVERAGE_TEST_MARKER();
3027 } while( xPendedCounts > ( TickType_t ) 0U );
3033 mtCOVERAGE_TEST_MARKER();
3037 if( xYieldPendings[ xCoreID ] != pdFALSE )
3039 /* If xYieldPendings is true then taskEXIT_CRITICAL()
3040 * will yield, so make sure we return true to let the
3041 * caller know a yield has already happened. */
3042 xAlreadyYielded = pdTRUE;
3048 mtCOVERAGE_TEST_MARKER();
3051 taskEXIT_CRITICAL();
3055 mtCOVERAGE_TEST_MARKER();
3058 return xAlreadyYielded;
3060 /*-----------------------------------------------------------*/
3062 TickType_t xTaskGetTickCount( void )
3066 /* Critical section required if running on a 16 bit processor. */
3067 portTICK_TYPE_ENTER_CRITICAL();
3069 xTicks = xTickCount;
3071 portTICK_TYPE_EXIT_CRITICAL();
3075 /*-----------------------------------------------------------*/
3077 TickType_t xTaskGetTickCountFromISR( void )
3080 UBaseType_t uxSavedInterruptStatus;
3082 /* RTOS ports that support interrupt nesting have the concept of a maximum
3083 * system call (or maximum API call) interrupt priority. Interrupts that are
3084 * above the maximum system call priority are kept permanently enabled, even
3085 * when the RTOS kernel is in a critical section, but cannot make any calls to
3086 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
3087 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3088 * failure if a FreeRTOS API function is called from an interrupt that has been
3089 * assigned a priority above the configured maximum system call priority.
3090 * Only FreeRTOS functions that end in FromISR can be called from interrupts
3091 * that have been assigned a priority at or (logically) below the maximum
3092 * system call interrupt priority. FreeRTOS maintains a separate interrupt
3093 * safe API to ensure interrupt entry is as fast and as simple as possible.
3094 * More information (albeit Cortex-M specific) is provided on the following
3095 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3096 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3098 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
3100 xReturn = xTickCount;
3102 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3106 /*-----------------------------------------------------------*/
3108 UBaseType_t uxTaskGetNumberOfTasks( void )
3110 /* A critical section is not required because the variables are of type
3112 return uxCurrentNumberOfTasks;
3114 /*-----------------------------------------------------------*/
3116 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3120 /* If null is passed in here then the name of the calling task is being
3122 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3123 configASSERT( pxTCB );
3124 return &( pxTCB->pcTaskName[ 0 ] );
3126 /*-----------------------------------------------------------*/
3128 #if ( INCLUDE_xTaskGetHandle == 1 )
3130 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3131 const char pcNameToQuery[] )
3133 TCB_t * pxNextTCB, * pxFirstTCB, * pxReturn = NULL;
3136 BaseType_t xBreakLoop;
3138 /* This function is called with the scheduler suspended. */
3140 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3142 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3146 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3148 /* Check each character in the name looking for a match or
3150 xBreakLoop = pdFALSE;
3152 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3154 cNextChar = pxNextTCB->pcTaskName[ x ];
3156 if( cNextChar != pcNameToQuery[ x ] )
3158 /* Characters didn't match. */
3159 xBreakLoop = pdTRUE;
3161 else if( cNextChar == ( char ) 0x00 )
3163 /* Both strings terminated, a match must have been
3165 pxReturn = pxNextTCB;
3166 xBreakLoop = pdTRUE;
3170 mtCOVERAGE_TEST_MARKER();
3173 if( xBreakLoop != pdFALSE )
3179 if( pxReturn != NULL )
3181 /* The handle has been found. */
3184 } while( pxNextTCB != pxFirstTCB );
3188 mtCOVERAGE_TEST_MARKER();
3194 #endif /* INCLUDE_xTaskGetHandle */
3195 /*-----------------------------------------------------------*/
3197 #if ( INCLUDE_xTaskGetHandle == 1 )
3199 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3201 UBaseType_t uxQueue = configMAX_PRIORITIES;
3204 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
3205 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
3209 /* Search the ready lists. */
3213 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
3217 /* Found the handle. */
3220 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3222 /* Search the delayed lists. */
3225 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
3230 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
3233 #if ( INCLUDE_vTaskSuspend == 1 )
3237 /* Search the suspended list. */
3238 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
3243 #if ( INCLUDE_vTaskDelete == 1 )
3247 /* Search the deleted list. */
3248 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
3253 ( void ) xTaskResumeAll();
3258 #endif /* INCLUDE_xTaskGetHandle */
3259 /*-----------------------------------------------------------*/
3261 #if ( configUSE_TRACE_FACILITY == 1 )
3263 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
3264 const UBaseType_t uxArraySize,
3265 uint32_t * const pulTotalRunTime )
3267 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
3271 /* Is there a space in the array for each task in the system? */
3272 if( uxArraySize >= uxCurrentNumberOfTasks )
3274 /* Fill in an TaskStatus_t structure with information on each
3275 * task in the Ready state. */
3279 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
3280 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3282 /* Fill in an TaskStatus_t structure with information on each
3283 * task in the Blocked state. */
3284 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
3285 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
3287 #if ( INCLUDE_vTaskDelete == 1 )
3289 /* Fill in an TaskStatus_t structure with information on
3290 * each task that has been deleted but not yet cleaned up. */
3291 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
3295 #if ( INCLUDE_vTaskSuspend == 1 )
3297 /* Fill in an TaskStatus_t structure with information on
3298 * each task in the Suspended state. */
3299 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
3303 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3305 if( pulTotalRunTime != NULL )
3307 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3308 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
3310 *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3314 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3316 if( pulTotalRunTime != NULL )
3318 *pulTotalRunTime = 0;
3321 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3325 mtCOVERAGE_TEST_MARKER();
3328 ( void ) xTaskResumeAll();
3333 #endif /* configUSE_TRACE_FACILITY */
3334 /*----------------------------------------------------------*/
3336 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
3338 TaskHandle_t * xTaskGetIdleTaskHandle( void )
3340 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
3341 * started, then xIdleTaskHandle will be NULL. */
3342 configASSERT( ( xIdleTaskHandle != NULL ) );
3343 return &( xIdleTaskHandle[ 0 ] );
3346 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
3347 /*----------------------------------------------------------*/
3349 /* This conditional compilation should use inequality to 0, not equality to 1.
3350 * This is to ensure vTaskStepTick() is available when user defined low power mode
3351 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
3353 #if ( configUSE_TICKLESS_IDLE != 0 )
3355 void vTaskStepTick( const TickType_t xTicksToJump )
3357 /* Correct the tick count value after a period during which the tick
3358 * was suppressed. Note this does *not* call the tick hook function for
3359 * each stepped tick. */
3360 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
3361 xTickCount += xTicksToJump;
3362 traceINCREASE_TICK_COUNT( xTicksToJump );
3365 #endif /* configUSE_TICKLESS_IDLE */
3366 /*----------------------------------------------------------*/
3368 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
3370 BaseType_t xYieldOccurred;
3372 /* Must not be called with the scheduler suspended as the implementation
3373 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
3374 configASSERT( uxSchedulerSuspended == 0 );
3376 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
3377 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
3379 xPendedTicks += xTicksToCatchUp;
3380 xYieldOccurred = xTaskResumeAll();
3382 return xYieldOccurred;
3384 /*----------------------------------------------------------*/
3386 #if ( INCLUDE_xTaskAbortDelay == 1 )
3388 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
3390 TCB_t * pxTCB = xTask;
3393 configASSERT( pxTCB );
3397 /* A task can only be prematurely removed from the Blocked state if
3398 * it is actually in the Blocked state. */
3399 if( eTaskGetState( xTask ) == eBlocked )
3403 /* Remove the reference to the task from the blocked list. An
3404 * interrupt won't touch the xStateListItem because the
3405 * scheduler is suspended. */
3406 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3408 /* Is the task waiting on an event also? If so remove it from
3409 * the event list too. Interrupts can touch the event list item,
3410 * even though the scheduler is suspended, so a critical section
3412 taskENTER_CRITICAL();
3414 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3416 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3418 /* This lets the task know it was forcibly removed from the
3419 * blocked state so it should not re-evaluate its block time and
3420 * then block again. */
3421 pxTCB->ucDelayAborted = pdTRUE;
3425 mtCOVERAGE_TEST_MARKER();
3428 taskEXIT_CRITICAL();
3430 /* Place the unblocked task into the appropriate ready list. */
3431 prvAddTaskToReadyList( pxTCB );
3433 /* A task being unblocked cannot cause an immediate context
3434 * switch if preemption is turned off. */
3435 #if ( configUSE_PREEMPTION == 1 )
3437 taskENTER_CRITICAL();
3439 prvYieldForTask( pxTCB, pdFALSE );
3441 taskEXIT_CRITICAL();
3443 #endif /* configUSE_PREEMPTION */
3450 ( void ) xTaskResumeAll();
3455 #endif /* INCLUDE_xTaskAbortDelay */
3456 /*----------------------------------------------------------*/
3458 BaseType_t xTaskIncrementTick( void )
3461 TickType_t xItemValue;
3462 BaseType_t xSwitchRequired = pdFALSE;
3464 #if ( configUSE_PREEMPTION == 1 )
3466 BaseType_t xCoreYieldList[ configNUM_CORES ] = { pdFALSE };
3467 #endif /* configUSE_PREEMPTION */
3469 taskENTER_CRITICAL();
3471 /* Called by the portable layer each time a tick interrupt occurs.
3472 * Increments the tick then checks to see if the new tick value will cause any
3473 * tasks to be unblocked. */
3474 traceTASK_INCREMENT_TICK( xTickCount );
3476 /* Tick increment should occur on every kernel timer event. Core 0 has the
3477 * responsibility to increment the tick, or increment the pended ticks if the
3478 * scheduler is suspended. If pended ticks is greater than zero, the core that
3479 * calls xTaskResumeAll has the responsibility to increment the tick. */
3480 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3482 /* Minor optimisation. The tick count cannot change in this
3484 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
3486 /* Increment the RTOS tick, switching the delayed and overflowed
3487 * delayed lists if it wraps to 0. */
3488 xTickCount = xConstTickCount;
3490 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
3492 taskSWITCH_DELAYED_LISTS();
3496 mtCOVERAGE_TEST_MARKER();
3499 /* See if this tick has made a timeout expire. Tasks are stored in
3500 * the queue in the order of their wake time - meaning once one task
3501 * has been found whose block time has not expired there is no need to
3502 * look any further down the list. */
3503 if( xConstTickCount >= xNextTaskUnblockTime )
3507 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
3509 /* The delayed list is empty. Set xNextTaskUnblockTime
3510 * to the maximum possible value so it is extremely
3512 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
3513 * next time through. */
3514 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3519 /* The delayed list is not empty, get the value of the
3520 * item at the head of the delayed list. This is the time
3521 * at which the task at the head of the delayed list must
3522 * be removed from the Blocked state. */
3523 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3524 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
3526 if( xConstTickCount < xItemValue )
3528 /* It is not time to unblock this item yet, but the
3529 * item value is the time at which the task at the head
3530 * of the blocked list must be removed from the Blocked
3531 * state - so record the item value in
3532 * xNextTaskUnblockTime. */
3533 xNextTaskUnblockTime = xItemValue;
3534 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
3538 mtCOVERAGE_TEST_MARKER();
3541 /* It is time to remove the item from the Blocked state. */
3542 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3544 /* Is the task waiting on an event also? If so remove
3545 * it from the event list. */
3546 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3548 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3552 mtCOVERAGE_TEST_MARKER();
3555 /* Place the unblocked task into the appropriate ready
3557 prvAddTaskToReadyList( pxTCB );
3559 /* A task being unblocked cannot cause an immediate
3560 * context switch if preemption is turned off. */
3561 #if ( configUSE_PREEMPTION == 1 )
3563 prvYieldForTask( pxTCB, pdTRUE );
3565 #endif /* configUSE_PREEMPTION */
3570 /* Tasks of equal priority to the currently running task will share
3571 * processing time (time slice) if preemption is on, and the application
3572 * writer has not explicitly turned time slicing off. */
3573 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
3575 /* TODO: If there are fewer "non-IDLE" READY tasks than cores, do not
3576 * force a context switch that would just shuffle tasks around cores */
3577 /* TODO: There are certainly better ways of doing this that would reduce
3578 * the number of interrupts and also potentially help prevent tasks from
3579 * moving between cores as often. This, however, works for now. */
3580 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3582 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ x ]->uxPriority ] ) ) > ( UBaseType_t ) 1 )
3584 xCoreYieldList[ x ] = pdTRUE;
3588 mtCOVERAGE_TEST_MARKER();
3592 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
3594 #if ( configUSE_TICK_HOOK == 1 )
3596 /* Guard against the tick hook being called when the pended tick
3597 * count is being unwound (when the scheduler is being unlocked). */
3598 if( xPendedTicks == ( TickType_t ) 0 )
3600 vApplicationTickHook();
3604 mtCOVERAGE_TEST_MARKER();
3607 #endif /* configUSE_TICK_HOOK */
3609 #if ( configUSE_PREEMPTION == 1 )
3611 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3613 if( xYieldPendings[ x ] != pdFALSE )
3615 xCoreYieldList[ x ] = pdTRUE;
3619 mtCOVERAGE_TEST_MARKER();
3623 #endif /* configUSE_PREEMPTION */
3625 #if ( configUSE_PREEMPTION == 1 )
3629 xCoreID = portGET_CORE_ID();
3631 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3633 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3634 if( pxCurrentTCBs[ x ]->xPreemptionDisable == pdFALSE )
3637 if( xCoreYieldList[ x ] != pdFALSE )
3641 xSwitchRequired = pdTRUE;
3650 mtCOVERAGE_TEST_MARKER();
3655 #endif /* configUSE_PREEMPTION */
3661 /* The tick hook gets called at regular intervals, even if the
3662 * scheduler is locked. */
3663 #if ( configUSE_TICK_HOOK == 1 )
3665 vApplicationTickHook();
3670 taskEXIT_CRITICAL();
3672 return xSwitchRequired;
3674 /*-----------------------------------------------------------*/
3676 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3678 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
3679 TaskHookFunction_t pxHookFunction )
3683 /* If xTask is NULL then it is the task hook of the calling task that is
3687 xTCB = ( TCB_t * ) pxCurrentTCB;
3694 /* Save the hook function in the TCB. A critical section is required as
3695 * the value can be accessed from an interrupt. */
3696 taskENTER_CRITICAL();
3698 xTCB->pxTaskTag = pxHookFunction;
3700 taskEXIT_CRITICAL();
3703 #endif /* configUSE_APPLICATION_TASK_TAG */
3704 /*-----------------------------------------------------------*/
3706 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3708 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
3711 TaskHookFunction_t xReturn;
3713 /* If xTask is NULL then set the calling task's hook. */
3714 pxTCB = prvGetTCBFromHandle( xTask );
3716 /* Save the hook function in the TCB. A critical section is required as
3717 * the value can be accessed from an interrupt. */
3718 taskENTER_CRITICAL();
3720 xReturn = pxTCB->pxTaskTag;
3722 taskEXIT_CRITICAL();
3727 #endif /* configUSE_APPLICATION_TASK_TAG */
3728 /*-----------------------------------------------------------*/
3730 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3732 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
3735 TaskHookFunction_t xReturn;
3736 UBaseType_t uxSavedInterruptStatus;
3738 /* If xTask is NULL then set the calling task's hook. */
3739 pxTCB = prvGetTCBFromHandle( xTask );
3741 /* Save the hook function in the TCB. A critical section is required as
3742 * the value can be accessed from an interrupt. */
3743 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
3745 xReturn = pxTCB->pxTaskTag;
3747 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3752 #endif /* configUSE_APPLICATION_TASK_TAG */
3753 /*-----------------------------------------------------------*/
3755 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3757 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
3758 void * pvParameter )
3763 /* If xTask is NULL then we are calling our own task hook. */
3766 xTCB = pxCurrentTCB;
3773 if( xTCB->pxTaskTag != NULL )
3775 xReturn = xTCB->pxTaskTag( pvParameter );
3785 #endif /* configUSE_APPLICATION_TASK_TAG */
3786 /*-----------------------------------------------------------*/
3788 void vTaskSwitchContext( BaseType_t xCoreID )
3790 /* Acquire both locks:
3791 * - The ISR lock protects the ready list from simultaneous access by
3792 * both other ISRs and tasks.
3793 * - We also take the task lock to pause here in case another core has
3794 * suspended the scheduler. We don't want to simply set xYieldPending
3795 * and move on if another core suspended the scheduler. We should only
3796 * do that if the current core has suspended the scheduler. */
3798 portGET_TASK_LOCK(); /* Must always acquire the task lock first */
3801 /* vTaskSwitchContext() must never be called from within a critical section.
3802 * This is not necessarily true for vanilla FreeRTOS, but it is for this SMP port. */
3803 configASSERT( pxCurrentTCB->uxCriticalNesting == 0 );
3805 if( uxSchedulerSuspended != ( UBaseType_t ) pdFALSE )
3807 /* The scheduler is currently suspended - do not allow a context
3809 xYieldPendings[ xCoreID ] = pdTRUE;
3813 xYieldPendings[ xCoreID ] = pdFALSE;
3814 traceTASK_SWITCHED_OUT();
3816 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3818 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3819 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
3821 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3824 /* Add the amount of time the task has been running to the
3825 * accumulated time so far. The time the task started running was
3826 * stored in ulTaskSwitchedInTime. Note that there is no overflow
3827 * protection here so count values are only valid until the timer
3828 * overflows. The guard against negative values is to protect
3829 * against suspect run time stat counter implementations - which
3830 * are provided by the application, not the kernel. */
3831 if( ulTotalRunTime > ulTaskSwitchedInTime )
3833 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime );
3837 mtCOVERAGE_TEST_MARKER();
3840 ulTaskSwitchedInTime = ulTotalRunTime;
3842 #endif /* configGENERATE_RUN_TIME_STATS */
3844 /* Check for stack overflow, if configured. */
3845 taskCHECK_FOR_STACK_OVERFLOW();
3847 /* Before the currently running task is switched out, save its errno. */
3848 #if ( configUSE_POSIX_ERRNO == 1 )
3850 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
3854 /* Select a new task to run using either the generic C or port
3855 * optimised asm code. */
3856 ( void ) prvSelectHighestPriorityTask( xCoreID );
3857 traceTASK_SWITCHED_IN();
3859 /* After the new task is switched in, update the global errno. */
3860 #if ( configUSE_POSIX_ERRNO == 1 )
3862 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
3866 #if ( configUSE_NEWLIB_REENTRANT == 1 )
3868 /* Switch Newlib's _impure_ptr variable to point to the _reent
3869 * structure specific to this task.
3870 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
3871 * for additional information. */
3872 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
3874 #endif /* configUSE_NEWLIB_REENTRANT */
3877 portRELEASE_ISR_LOCK();
3878 portRELEASE_TASK_LOCK();
3880 /*-----------------------------------------------------------*/
3882 void vTaskPlaceOnEventList( List_t * const pxEventList,
3883 const TickType_t xTicksToWait )
3885 configASSERT( pxEventList );
3887 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
3888 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
3890 /* Place the event list item of the TCB in the appropriate event list.
3891 * This is placed in the list in priority order so the highest priority task
3892 * is the first to be woken by the event. The queue that contains the event
3893 * list is locked, preventing simultaneous access from interrupts. */
3894 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3896 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3898 /*-----------------------------------------------------------*/
3900 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
3901 const TickType_t xItemValue,
3902 const TickType_t xTicksToWait )
3904 configASSERT( pxEventList );
3906 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3907 * the event groups implementation. */
3908 configASSERT( uxSchedulerSuspended != 0 );
3910 /* Store the item value in the event list item. It is safe to access the
3911 * event list item here as interrupts won't access the event list item of a
3912 * task that is not in the Blocked state. */
3913 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
3915 /* Place the event list item of the TCB at the end of the appropriate event
3916 * list. It is safe to access the event list here because it is part of an
3917 * event group implementation - and interrupts don't access event groups
3918 * directly (instead they access them indirectly by pending function calls to
3919 * the task level). */
3920 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3922 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3924 /*-----------------------------------------------------------*/
3926 #if ( configUSE_TIMERS == 1 )
3928 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
3929 TickType_t xTicksToWait,
3930 const BaseType_t xWaitIndefinitely )
3932 configASSERT( pxEventList );
3934 /* This function should not be called by application code hence the
3935 * 'Restricted' in its name. It is not part of the public API. It is
3936 * designed for use by kernel code, and has special calling requirements -
3937 * it should be called with the scheduler suspended. */
3940 /* Place the event list item of the TCB in the appropriate event list.
3941 * In this case it is assume that this is the only task that is going to
3942 * be waiting on this event list, so the faster vListInsertEnd() function
3943 * can be used in place of vListInsert. */
3944 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3946 /* If the task should block indefinitely then set the block time to a
3947 * value that will be recognised as an indefinite delay inside the
3948 * prvAddCurrentTaskToDelayedList() function. */
3949 if( xWaitIndefinitely != pdFALSE )
3951 xTicksToWait = portMAX_DELAY;
3954 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
3955 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
3958 #endif /* configUSE_TIMERS */
3959 /*-----------------------------------------------------------*/
3961 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
3963 TCB_t * pxUnblockedTCB;
3966 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
3967 * called from a critical section within an ISR. */
3969 /* The event list is sorted in priority order, so the first in the list can
3970 * be removed as it is known to be the highest priority. Remove the TCB from
3971 * the delayed list, and add it to the ready list.
3973 * If an event is for a queue that is locked then this function will never
3974 * get called - the lock count on the queue will get modified instead. This
3975 * means exclusive access to the event list is guaranteed here.
3977 * This function assumes that a check has already been made to ensure that
3978 * pxEventList is not empty. */
3979 pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3980 configASSERT( pxUnblockedTCB );
3981 ( void ) uxListRemove( &( pxUnblockedTCB->xEventListItem ) );
3983 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3985 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
3986 prvAddTaskToReadyList( pxUnblockedTCB );
3988 #if ( configUSE_TICKLESS_IDLE != 0 )
3990 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3991 * might be set to the blocked task's time out time. If the task is
3992 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
3993 * normally left unchanged, because it is automatically reset to a new
3994 * value when the tick count equals xNextTaskUnblockTime. However if
3995 * tickless idling is used it might be more important to enter sleep mode
3996 * at the earliest possible time - so reset xNextTaskUnblockTime here to
3997 * ensure it is updated at the earliest possible time. */
3998 prvResetNextTaskUnblockTime();
4004 /* The delayed and ready lists cannot be accessed, so hold this task
4005 * pending until the scheduler is resumed. */
4006 vListInsertEnd( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
4010 #if ( configUSE_PREEMPTION == 1 )
4011 prvYieldForTask( pxUnblockedTCB, pdFALSE );
4013 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
4021 /*-----------------------------------------------------------*/
4023 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
4024 const TickType_t xItemValue )
4026 TCB_t * pxUnblockedTCB;
4028 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
4029 * the event flags implementation. */
4030 configASSERT( uxSchedulerSuspended != pdFALSE );
4032 /* Store the new item value in the event list. */
4033 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4035 /* Remove the event list form the event flag. Interrupts do not access
4037 pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4038 configASSERT( pxUnblockedTCB );
4039 ( void ) uxListRemove( pxEventListItem );
4041 #if ( configUSE_TICKLESS_IDLE != 0 )
4043 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4044 * might be set to the blocked task's time out time. If the task is
4045 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4046 * normally left unchanged, because it is automatically reset to a new
4047 * value when the tick count equals xNextTaskUnblockTime. However if
4048 * tickless idling is used it might be more important to enter sleep mode
4049 * at the earliest possible time - so reset xNextTaskUnblockTime here to
4050 * ensure it is updated at the earliest possible time. */
4051 prvResetNextTaskUnblockTime();
4055 /* Remove the task from the delayed list and add it to the ready list. The
4056 * scheduler is suspended so interrupts will not be accessing the ready
4058 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
4059 prvAddTaskToReadyList( pxUnblockedTCB );
4061 #if ( configUSE_PREEMPTION == 1 )
4062 taskENTER_CRITICAL();
4064 prvYieldForTask( pxUnblockedTCB, pdFALSE );
4066 taskEXIT_CRITICAL();
4069 /*-----------------------------------------------------------*/
4071 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
4073 configASSERT( pxTimeOut );
4074 taskENTER_CRITICAL();
4076 pxTimeOut->xOverflowCount = xNumOfOverflows;
4077 pxTimeOut->xTimeOnEntering = xTickCount;
4079 taskEXIT_CRITICAL();
4081 /*-----------------------------------------------------------*/
4083 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
4085 /* For internal use only as it does not use a critical section. */
4086 pxTimeOut->xOverflowCount = xNumOfOverflows;
4087 pxTimeOut->xTimeOnEntering = xTickCount;
4089 /*-----------------------------------------------------------*/
4091 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
4092 TickType_t * const pxTicksToWait )
4096 configASSERT( pxTimeOut );
4097 configASSERT( pxTicksToWait );
4099 taskENTER_CRITICAL();
4101 /* Minor optimisation. The tick count cannot change in this block. */
4102 const TickType_t xConstTickCount = xTickCount;
4103 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
4105 #if ( INCLUDE_xTaskAbortDelay == 1 )
4106 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
4108 /* The delay was aborted, which is not the same as a time out,
4109 * but has the same result. */
4110 pxCurrentTCB->ucDelayAborted = pdFALSE;
4116 #if ( INCLUDE_vTaskSuspend == 1 )
4117 if( *pxTicksToWait == portMAX_DELAY )
4119 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
4120 * specified is the maximum block time then the task should block
4121 * indefinitely, and therefore never time out. */
4127 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
4129 /* The tick count is greater than the time at which
4130 * vTaskSetTimeout() was called, but has also overflowed since
4131 * vTaskSetTimeOut() was called. It must have wrapped all the way
4132 * around and gone past again. This passed since vTaskSetTimeout()
4135 *pxTicksToWait = ( TickType_t ) 0;
4137 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
4139 /* Not a genuine timeout. Adjust parameters for time remaining. */
4140 *pxTicksToWait -= xElapsedTime;
4141 vTaskInternalSetTimeOutState( pxTimeOut );
4146 *pxTicksToWait = ( TickType_t ) 0;
4150 taskEXIT_CRITICAL();
4154 /*-----------------------------------------------------------*/
4156 void vTaskMissedYield( void )
4158 /* Must be called from within a critical section */
4159 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
4161 /*-----------------------------------------------------------*/
4163 #if ( configUSE_TRACE_FACILITY == 1 )
4165 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
4167 UBaseType_t uxReturn;
4168 TCB_t const * pxTCB;
4173 uxReturn = pxTCB->uxTaskNumber;
4183 #endif /* configUSE_TRACE_FACILITY */
4184 /*-----------------------------------------------------------*/
4186 #if ( configUSE_TRACE_FACILITY == 1 )
4188 void vTaskSetTaskNumber( TaskHandle_t xTask,
4189 const UBaseType_t uxHandle )
4196 pxTCB->uxTaskNumber = uxHandle;
4200 #endif /* configUSE_TRACE_FACILITY */
4203 * -----------------------------------------------------------
4204 * The MinimalIdle task.
4205 * ----------------------------------------------------------
4207 * The minimal idle task is used for all the additional Cores in a SMP system.
4208 * There must be only 1 idle task and the rest are minimal idle tasks.
4210 * @todo additional conditional compiles to remove this function.
4212 #if (configNUM_CORES > 1)
4213 static portTASK_FUNCTION( prvMinimalIdleTask, pvParameters )
4217 #if ( configUSE_PREEMPTION == 0 )
4219 /* If we are not using preemption we keep forcing a task switch to
4220 * see if any other task has become available. If we are using
4221 * preemption we don't need to do this as any task becoming available
4222 * will automatically get the processor anyway. */
4225 #endif /* configUSE_PREEMPTION */
4227 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4229 /* When using preemption tasks of equal priority will be
4230 * timesliced. If a task that is sharing the idle priority is ready
4231 * to run then the idle task should yield before the end of the
4234 * A critical region is not required here as we are just reading from
4235 * the list, and an occasional incorrect value will not matter. If
4236 * the ready list at the idle priority contains one more task than the
4237 * number of idle tasks, which is equal to the configured numbers of cores
4238 * then a task other than the idle task is ready to execute. */
4239 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4245 mtCOVERAGE_TEST_MARKER();
4248 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4253 * -----------------------------------------------------------
4255 * ----------------------------------------------------------
4259 static portTASK_FUNCTION( prvIdleTask, pvParameters )
4261 /* Stop warnings. */
4262 ( void ) pvParameters;
4264 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
4265 * SCHEDULER IS STARTED. **/
4267 /* In case a task that has a secure context deletes itself, in which case
4268 * the idle task is responsible for deleting the task's secure context, if
4270 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
4272 /* All cores start up in the idle task. This initial yield gets the application
4278 /* See if any tasks have deleted themselves - if so then the idle task
4279 * is responsible for freeing the deleted task's TCB and stack. */
4280 prvCheckTasksWaitingTermination();
4282 #if ( configUSE_PREEMPTION == 0 )
4284 /* If we are not using preemption we keep forcing a task switch to
4285 * see if any other task has become available. If we are using
4286 * preemption we don't need to do this as any task becoming available
4287 * will automatically get the processor anyway. */
4290 #endif /* configUSE_PREEMPTION */
4292 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4294 /* When using preemption tasks of equal priority will be
4295 * timesliced. If a task that is sharing the idle priority is ready
4296 * to run then the idle task should yield before the end of the
4299 * A critical region is not required here as we are just reading from
4300 * the list, and an occasional incorrect value will not matter. If
4301 * the ready list at the idle priority contains one more task than the
4302 * number of idle tasks, which is equal to the configured numbers of cores
4303 * then a task other than the idle task is ready to execute. */
4304 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4310 mtCOVERAGE_TEST_MARKER();
4313 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4315 #if ( configUSE_IDLE_HOOK == 1 )
4317 extern void vApplicationIdleHook( void );
4319 /* Call the user defined function from within the idle task. This
4320 * allows the application designer to add background functionality
4321 * without the overhead of a separate task.
4322 * NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
4323 * CALL A FUNCTION THAT MIGHT BLOCK. */
4324 vApplicationIdleHook();
4326 #endif /* configUSE_IDLE_HOOK */
4328 /* This conditional compilation should use inequality to 0, not equality
4329 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
4330 * user defined low power mode implementations require
4331 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
4332 #if ( configUSE_TICKLESS_IDLE != 0 )
4334 TickType_t xExpectedIdleTime;
4336 /* It is not desirable to suspend then resume the scheduler on
4337 * each iteration of the idle task. Therefore, a preliminary
4338 * test of the expected idle time is performed without the
4339 * scheduler suspended. The result here is not necessarily
4341 xExpectedIdleTime = prvGetExpectedIdleTime();
4343 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4347 /* Now the scheduler is suspended, the expected idle
4348 * time can be sampled again, and this time its value can
4350 configASSERT( xNextTaskUnblockTime >= xTickCount );
4351 xExpectedIdleTime = prvGetExpectedIdleTime();
4353 /* Define the following macro to set xExpectedIdleTime to 0
4354 * if the application does not want
4355 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
4356 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
4358 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4360 traceLOW_POWER_IDLE_BEGIN();
4361 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
4362 traceLOW_POWER_IDLE_END();
4366 mtCOVERAGE_TEST_MARKER();
4369 ( void ) xTaskResumeAll();
4373 mtCOVERAGE_TEST_MARKER();
4376 #endif /* configUSE_TICKLESS_IDLE */
4379 /*-----------------------------------------------------------*/
4381 #if ( configUSE_TICKLESS_IDLE != 0 )
4383 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
4385 /* The idle task exists in addition to the application tasks. */
4386 const UBaseType_t uxNonApplicationTasks = 1;
4387 eSleepModeStatus eReturn = eStandardSleep;
4389 /* This function must be called from a critical section. */
4391 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
4393 /* A task was made ready while the scheduler was suspended. */
4394 eReturn = eAbortSleep;
4396 else if( xYieldPending != pdFALSE )
4398 /* A yield was pended while the scheduler was suspended. */
4399 eReturn = eAbortSleep;
4401 else if( xPendedTicks != 0 )
4403 /* A tick interrupt has already occurred but was held pending
4404 * because the scheduler is suspended. */
4405 eReturn = eAbortSleep;
4409 /* If all the tasks are in the suspended list (which might mean they
4410 * have an infinite block time rather than actually being suspended)
4411 * then it is safe to turn all clocks off and just wait for external
4413 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
4415 eReturn = eNoTasksWaitingTimeout;
4419 mtCOVERAGE_TEST_MARKER();
4426 #endif /* configUSE_TICKLESS_IDLE */
4427 /*-----------------------------------------------------------*/
4429 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4431 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
4437 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4439 pxTCB = prvGetTCBFromHandle( xTaskToSet );
4440 configASSERT( pxTCB != NULL );
4441 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
4445 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4446 /*-----------------------------------------------------------*/
4448 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4450 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
4453 void * pvReturn = NULL;
4456 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4458 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4459 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
4469 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4470 /*-----------------------------------------------------------*/
4472 #if ( portUSING_MPU_WRAPPERS == 1 )
4474 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
4475 const MemoryRegion_t * const xRegions )
4479 /* If null is passed in here then we are modifying the MPU settings of
4480 * the calling task. */
4481 pxTCB = prvGetTCBFromHandle( xTaskToModify );
4483 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
4486 #endif /* portUSING_MPU_WRAPPERS */
4487 /*-----------------------------------------------------------*/
4489 static void prvInitialiseTaskLists( void )
4491 UBaseType_t uxPriority;
4493 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
4495 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
4498 vListInitialise( &xDelayedTaskList1 );
4499 vListInitialise( &xDelayedTaskList2 );
4500 vListInitialise( &xPendingReadyList );
4502 #if ( INCLUDE_vTaskDelete == 1 )
4504 vListInitialise( &xTasksWaitingTermination );
4506 #endif /* INCLUDE_vTaskDelete */
4508 #if ( INCLUDE_vTaskSuspend == 1 )
4510 vListInitialise( &xSuspendedTaskList );
4512 #endif /* INCLUDE_vTaskSuspend */
4514 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
4516 pxDelayedTaskList = &xDelayedTaskList1;
4517 pxOverflowDelayedTaskList = &xDelayedTaskList2;
4519 /*-----------------------------------------------------------*/
4521 static void prvCheckTasksWaitingTermination( void )
4523 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
4525 #if ( INCLUDE_vTaskDelete == 1 )
4529 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
4530 * being called too often in the idle task. */
4531 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4533 taskENTER_CRITICAL();
4535 /* Since we are SMP, multiple idles can be running simultaneously
4536 * and we need to check that other idles did not cleanup while we were
4537 * waiting to enter the critical section */
4538 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4540 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4542 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
4544 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4545 --uxCurrentNumberOfTasks;
4546 --uxDeletedTasksWaitingCleanUp;
4547 prvDeleteTCB( pxTCB );
4551 /* The TCB to be deleted still has not yet been switched out
4552 * by the scheduler, so we will just exit this loop early and
4553 * try again next time. */
4554 taskEXIT_CRITICAL();
4559 taskEXIT_CRITICAL();
4562 #endif /* INCLUDE_vTaskDelete */
4564 /*-----------------------------------------------------------*/
4566 #if ( configUSE_TRACE_FACILITY == 1 )
4568 void vTaskGetInfo( TaskHandle_t xTask,
4569 TaskStatus_t * pxTaskStatus,
4570 BaseType_t xGetFreeStackSpace,
4575 /* xTask is NULL then get the state of the calling task. */
4576 pxTCB = prvGetTCBFromHandle( xTask );
4578 pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
4579 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
4580 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
4581 pxTaskStatus->pxStackBase = pxTCB->pxStack;
4582 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
4584 #if ( configUSE_MUTEXES == 1 )
4586 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
4590 pxTaskStatus->uxBasePriority = 0;
4594 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4596 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
4600 pxTaskStatus->ulRunTimeCounter = 0;
4604 /* Obtaining the task state is a little fiddly, so is only done if the
4605 * value of eState passed into this function is eInvalid - otherwise the
4606 * state is just set to whatever is passed in. */
4607 if( eState != eInvalid )
4609 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
4611 pxTaskStatus->eCurrentState = eRunning;
4615 pxTaskStatus->eCurrentState = eState;
4617 #if ( INCLUDE_vTaskSuspend == 1 )
4619 /* If the task is in the suspended list then there is a
4620 * chance it is actually just blocked indefinitely - so really
4621 * it should be reported as being in the Blocked state. */
4622 if( eState == eSuspended )
4626 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4628 pxTaskStatus->eCurrentState = eBlocked;
4631 ( void ) xTaskResumeAll();
4634 #endif /* INCLUDE_vTaskSuspend */
4639 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
4642 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
4643 * parameter is provided to allow it to be skipped. */
4644 if( xGetFreeStackSpace != pdFALSE )
4646 #if ( portSTACK_GROWTH > 0 )
4648 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
4652 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
4658 pxTaskStatus->usStackHighWaterMark = 0;
4662 #endif /* configUSE_TRACE_FACILITY */
4663 /*-----------------------------------------------------------*/
4665 #if ( configUSE_TRACE_FACILITY == 1 )
4667 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
4671 configLIST_VOLATILE TCB_t * pxNextTCB, * pxFirstTCB;
4672 UBaseType_t uxTask = 0;
4674 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4676 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4678 /* Populate an TaskStatus_t structure within the
4679 * pxTaskStatusArray array for each task that is referenced from
4680 * pxList. See the definition of TaskStatus_t in task.h for the
4681 * meaning of each TaskStatus_t structure member. */
4684 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4685 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
4687 } while( pxNextTCB != pxFirstTCB );
4691 mtCOVERAGE_TEST_MARKER();
4697 #endif /* configUSE_TRACE_FACILITY */
4698 /*-----------------------------------------------------------*/
4700 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
4702 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
4704 uint32_t ulCount = 0U;
4706 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
4708 pucStackByte -= portSTACK_GROWTH;
4712 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
4714 return ( configSTACK_DEPTH_TYPE ) ulCount;
4717 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
4718 /*-----------------------------------------------------------*/
4720 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
4722 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
4723 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
4724 * user to determine the return type. It gets around the problem of the value
4725 * overflowing on 8-bit types without breaking backward compatibility for
4726 * applications that expect an 8-bit return type. */
4727 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
4730 uint8_t * pucEndOfStack;
4731 configSTACK_DEPTH_TYPE uxReturn;
4733 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
4734 * the same except for their return type. Using configSTACK_DEPTH_TYPE
4735 * allows the user to determine the return type. It gets around the
4736 * problem of the value overflowing on 8-bit types without breaking
4737 * backward compatibility for applications that expect an 8-bit return
4740 pxTCB = prvGetTCBFromHandle( xTask );
4742 #if portSTACK_GROWTH < 0
4744 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4748 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4752 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
4757 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
4758 /*-----------------------------------------------------------*/
4760 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
4762 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
4765 uint8_t * pucEndOfStack;
4766 UBaseType_t uxReturn;
4768 pxTCB = prvGetTCBFromHandle( xTask );
4770 #if portSTACK_GROWTH < 0
4772 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4776 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4780 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
4785 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
4786 /*-----------------------------------------------------------*/
4788 #if ( INCLUDE_vTaskDelete == 1 )
4790 static void prvDeleteTCB( TCB_t * pxTCB )
4792 /* This call is required specifically for the TriCore port. It must be
4793 * above the vPortFree() calls. The call is also used by ports/demos that
4794 * want to allocate and clean RAM statically. */
4795 portCLEAN_UP_TCB( pxTCB );
4797 /* Free up the memory allocated by the scheduler for the task. It is up
4798 * to the task to free any memory allocated at the application level.
4799 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
4800 * for additional information. */
4801 #if ( configUSE_NEWLIB_REENTRANT == 1 )
4803 _reclaim_reent( &( pxTCB->xNewLib_reent ) );
4805 #endif /* configUSE_NEWLIB_REENTRANT */
4807 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
4809 /* The task can only have been allocated dynamically - free both
4810 * the stack and TCB. */
4811 vPortFreeStack( pxTCB->pxStack );
4814 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
4816 /* The task could have been allocated statically or dynamically, so
4817 * check what was statically allocated before trying to free the
4819 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
4821 /* Both the stack and TCB were allocated dynamically, so both
4823 vPortFreeStack( pxTCB->pxStack );
4826 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4828 /* Only the stack was statically allocated, so the TCB is the
4829 * only memory that must be freed. */
4834 /* Neither the stack nor the TCB were allocated dynamically, so
4835 * nothing needs to be freed. */
4836 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
4837 mtCOVERAGE_TEST_MARKER();
4840 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
4843 #endif /* INCLUDE_vTaskDelete */
4844 /*-----------------------------------------------------------*/
4846 static void prvResetNextTaskUnblockTime( void )
4848 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4850 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
4851 * the maximum possible value so it is extremely unlikely that the
4852 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
4853 * there is an item in the delayed list. */
4854 xNextTaskUnblockTime = portMAX_DELAY;
4858 /* The new current delayed list is not empty, get the value of
4859 * the item at the head of the delayed list. This is the time at
4860 * which the task at the head of the delayed list should be removed
4861 * from the Blocked state. */
4862 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
4865 /*-----------------------------------------------------------*/
4867 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
4869 TaskHandle_t xTaskGetCurrentTaskHandle( void )
4871 TaskHandle_t xReturn;
4874 ulState = portDISABLE_INTERRUPTS();
4875 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
4876 portRESTORE_INTERRUPTS( ulState );
4881 TaskHandle_t xTaskGetCurrentTaskHandleCPU( UBaseType_t xCoreID )
4883 TaskHandle_t xReturn = NULL;
4885 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
4887 xReturn = pxCurrentTCBs[ xCoreID ];
4893 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
4894 /*-----------------------------------------------------------*/
4896 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
4898 BaseType_t xTaskGetSchedulerState( void )
4902 if( xSchedulerRunning == pdFALSE )
4904 xReturn = taskSCHEDULER_NOT_STARTED;
4908 taskENTER_CRITICAL();
4910 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4912 xReturn = taskSCHEDULER_RUNNING;
4916 xReturn = taskSCHEDULER_SUSPENDED;
4919 taskEXIT_CRITICAL();
4925 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
4926 /*-----------------------------------------------------------*/
4928 #if ( configUSE_MUTEXES == 1 )
4930 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
4932 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
4933 BaseType_t xReturn = pdFALSE;
4935 /* If the mutex was given back by an interrupt while the queue was
4936 * locked then the mutex holder might now be NULL. _RB_ Is this still
4937 * needed as interrupts can no longer use mutexes? */
4938 if( pxMutexHolder != NULL )
4940 /* If the holder of the mutex has a priority below the priority of
4941 * the task attempting to obtain the mutex then it will temporarily
4942 * inherit the priority of the task attempting to obtain the mutex. */
4943 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
4945 /* Adjust the mutex holder state to account for its new
4946 * priority. Only reset the event list item value if the value is
4947 * not being used for anything else. */
4948 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4950 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. */
4954 mtCOVERAGE_TEST_MARKER();
4957 /* If the task being modified is in the ready state it will need
4958 * to be moved into a new list. */
4959 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
4961 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4963 /* It is known that the task is in its ready list so
4964 * there is no need to check again and the port level
4965 * reset macro can be called directly. */
4966 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
4970 mtCOVERAGE_TEST_MARKER();
4973 /* Inherit the priority before being moved into the new list. */
4974 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4975 prvAddTaskToReadyList( pxMutexHolderTCB );
4979 /* Just inherit the priority. */
4980 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4983 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
4985 /* Inheritance occurred. */
4990 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
4992 /* The base priority of the mutex holder is lower than the
4993 * priority of the task attempting to take the mutex, but the
4994 * current priority of the mutex holder is not lower than the
4995 * priority of the task attempting to take the mutex.
4996 * Therefore the mutex holder must have already inherited a
4997 * priority, but inheritance would have occurred if that had
4998 * not been the case. */
5003 mtCOVERAGE_TEST_MARKER();
5009 mtCOVERAGE_TEST_MARKER();
5015 #endif /* configUSE_MUTEXES */
5016 /*-----------------------------------------------------------*/
5018 #if ( configUSE_MUTEXES == 1 )
5020 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
5022 TCB_t * const pxTCB = pxMutexHolder;
5023 BaseType_t xReturn = pdFALSE;
5025 if( pxMutexHolder != NULL )
5027 /* A task can only have an inherited priority if it holds the mutex.
5028 * If the mutex is held by a task then it cannot be given from an
5029 * interrupt, and if a mutex is given by the holding task then it must
5030 * be the running state task. */
5031 configASSERT( pxTCB == pxCurrentTCB );
5032 configASSERT( pxTCB->uxMutexesHeld );
5033 ( pxTCB->uxMutexesHeld )--;
5035 /* Has the holder of the mutex inherited the priority of another
5037 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
5039 /* Only disinherit if no other mutexes are held. */
5040 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
5042 /* A task can only have an inherited priority if it holds
5043 * the mutex. If the mutex is held by a task then it cannot be
5044 * given from an interrupt, and if a mutex is given by the
5045 * holding task then it must be the running state task. Remove
5046 * the holding task from the ready list. */
5047 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5049 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5053 mtCOVERAGE_TEST_MARKER();
5056 /* Disinherit the priority before adding the task into the
5057 * new ready list. */
5058 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
5059 pxTCB->uxPriority = pxTCB->uxBasePriority;
5061 /* Reset the event list item value. It cannot be in use for
5062 * any other purpose if this task is running, and it must be
5063 * running to give back the mutex. */
5064 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. */
5065 prvAddTaskToReadyList( pxTCB );
5067 /* Return true to indicate that a context switch is required.
5068 * This is only actually required in the corner case whereby
5069 * multiple mutexes were held and the mutexes were given back
5070 * in an order different to that in which they were taken.
5071 * If a context switch did not occur when the first mutex was
5072 * returned, even if a task was waiting on it, then a context
5073 * switch should occur when the last mutex is returned whether
5074 * a task is waiting on it or not. */
5079 mtCOVERAGE_TEST_MARKER();
5084 mtCOVERAGE_TEST_MARKER();
5089 mtCOVERAGE_TEST_MARKER();
5095 #endif /* configUSE_MUTEXES */
5096 /*-----------------------------------------------------------*/
5098 #if ( configUSE_MUTEXES == 1 )
5100 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
5101 UBaseType_t uxHighestPriorityWaitingTask )
5103 TCB_t * const pxTCB = pxMutexHolder;
5104 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
5105 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
5107 if( pxMutexHolder != NULL )
5109 /* If pxMutexHolder is not NULL then the holder must hold at least
5111 configASSERT( pxTCB->uxMutexesHeld );
5113 /* Determine the priority to which the priority of the task that
5114 * holds the mutex should be set. This will be the greater of the
5115 * holding task's base priority and the priority of the highest
5116 * priority task that is waiting to obtain the mutex. */
5117 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
5119 uxPriorityToUse = uxHighestPriorityWaitingTask;
5123 uxPriorityToUse = pxTCB->uxBasePriority;
5126 /* Does the priority need to change? */
5127 if( pxTCB->uxPriority != uxPriorityToUse )
5129 /* Only disinherit if no other mutexes are held. This is a
5130 * simplification in the priority inheritance implementation. If
5131 * the task that holds the mutex is also holding other mutexes then
5132 * the other mutexes may have caused the priority inheritance. */
5133 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
5135 /* If a task has timed out because it already holds the
5136 * mutex it was trying to obtain then it cannot of inherited
5137 * its own priority. */
5138 configASSERT( pxTCB != pxCurrentTCB );
5140 /* Disinherit the priority, remembering the previous
5141 * priority to facilitate determining the subject task's
5143 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
5144 uxPriorityUsedOnEntry = pxTCB->uxPriority;
5145 pxTCB->uxPriority = uxPriorityToUse;
5147 /* Only reset the event list item value if the value is not
5148 * being used for anything else. */
5149 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
5151 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. */
5155 mtCOVERAGE_TEST_MARKER();
5158 /* If the running task is not the task that holds the mutex
5159 * then the task that holds the mutex could be in either the
5160 * Ready, Blocked or Suspended states. Only remove the task
5161 * from its current state list if it is in the Ready state as
5162 * the task's priority is going to change and there is one
5163 * Ready list per priority. */
5164 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
5166 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5168 /* It is known that the task is in its ready list so
5169 * there is no need to check again and the port level
5170 * reset macro can be called directly. */
5171 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5175 mtCOVERAGE_TEST_MARKER();
5178 prvAddTaskToReadyList( pxTCB );
5182 mtCOVERAGE_TEST_MARKER();
5187 mtCOVERAGE_TEST_MARKER();
5192 mtCOVERAGE_TEST_MARKER();
5197 mtCOVERAGE_TEST_MARKER();
5201 #endif /* configUSE_MUTEXES */
5202 /*-----------------------------------------------------------*/
5205 * If not in a critical section then yield immediately.
5206 * Otherwise set xYieldPending to true to wait to
5207 * yield until exiting the critical section.
5209 void vTaskYieldWithinAPI( void )
5211 if( pxCurrentTCB->uxCriticalNesting == 0U )
5217 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5220 /*-----------------------------------------------------------*/
5222 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5224 void vTaskEnterCritical( void )
5226 portDISABLE_INTERRUPTS();
5228 if( xSchedulerRunning != pdFALSE )
5230 if( pxCurrentTCB->uxCriticalNesting == 0U )
5232 if( portCHECK_IF_IN_ISR() == pdFALSE )
5234 portGET_TASK_LOCK();
5240 ( pxCurrentTCB->uxCriticalNesting )++;
5242 /* This should now be interrupt safe. The only time there would be
5243 * a problem is if this is called before a context switch and
5244 * vTaskExitCritical() is called after pxCurrentTCB changes. Therefore
5245 * this should not be used within vTaskSwitchContext(). */
5247 if( ( uxSchedulerSuspended == 0U ) && ( pxCurrentTCB->uxCriticalNesting == 1U ) )
5249 prvCheckForRunStateChange();
5254 mtCOVERAGE_TEST_MARKER();
5258 #endif /* portCRITICAL_NESTING_IN_TCB */
5259 /*-----------------------------------------------------------*/
5261 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5263 void vTaskExitCritical( void )
5265 if( xSchedulerRunning != pdFALSE )
5267 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
5268 * does not match a previous call to vTaskEnterCritical(). */
5269 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
5271 if( pxCurrentTCB->uxCriticalNesting > 0U )
5273 ( pxCurrentTCB->uxCriticalNesting )--;
5275 if( pxCurrentTCB->uxCriticalNesting == 0U )
5277 portRELEASE_ISR_LOCK();
5279 if( portCHECK_IF_IN_ISR() == pdFALSE )
5281 portRELEASE_TASK_LOCK();
5282 portENABLE_INTERRUPTS();
5284 /* When a task yields in a critical section it just sets
5285 * xYieldPending to true. So now that we have exited the
5286 * critical section check if xYieldPending is true, and
5288 if( xYieldPending != pdFALSE )
5295 /* In an ISR we don't hold the task lock and don't
5296 * need to yield. Yield will happen if necessary when
5297 * the application ISR calls portEND_SWITCHING_ISR() */
5298 mtCOVERAGE_TEST_MARKER();
5303 mtCOVERAGE_TEST_MARKER();
5308 mtCOVERAGE_TEST_MARKER();
5313 mtCOVERAGE_TEST_MARKER();
5317 #endif /* portCRITICAL_NESTING_IN_TCB */
5318 /*-----------------------------------------------------------*/
5320 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
5322 static char * prvWriteNameToBuffer( char * pcBuffer,
5323 const char * pcTaskName )
5327 /* Start by copying the entire string. */
5328 strcpy( pcBuffer, pcTaskName );
5330 /* Pad the end of the string with spaces to ensure columns line up when
5332 for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
5334 pcBuffer[ x ] = ' ';
5338 pcBuffer[ x ] = ( char ) 0x00;
5340 /* Return the new end of string. */
5341 return &( pcBuffer[ x ] );
5344 #endif /* ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
5345 /*-----------------------------------------------------------*/
5347 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5349 void vTaskList( char * pcWriteBuffer )
5351 TaskStatus_t * pxTaskStatusArray;
5352 UBaseType_t uxArraySize, x;
5358 * This function is provided for convenience only, and is used by many
5359 * of the demo applications. Do not consider it to be part of the
5362 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
5363 * uxTaskGetSystemState() output into a human readable table that
5364 * displays task: names, states, priority, stack usage and task number.
5365 * Stack usage specified as the number of unused StackType_t words stack can hold
5366 * on top of stack - not the number of bytes.
5368 * vTaskList() has a dependency on the sprintf() C library function that
5369 * might bloat the code size, use a lot of stack, and provide different
5370 * results on different platforms. An alternative, tiny, third party,
5371 * and limited functionality implementation of sprintf() is provided in
5372 * many of the FreeRTOS/Demo sub-directories in a file called
5373 * printf-stdarg.c (note printf-stdarg.c does not provide a full
5374 * snprintf() implementation!).
5376 * It is recommended that production systems call uxTaskGetSystemState()
5377 * directly to get access to raw stats data, rather than indirectly
5378 * through a call to vTaskList().
5382 /* Make sure the write buffer does not contain a string. */
5383 *pcWriteBuffer = ( char ) 0x00;
5385 /* Take a snapshot of the number of tasks in case it changes while this
5386 * function is executing. */
5387 uxArraySize = uxCurrentNumberOfTasks;
5389 /* Allocate an array index for each task. NOTE! if
5390 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5391 * equate to NULL. */
5392 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. */
5394 if( pxTaskStatusArray != NULL )
5396 /* Generate the (binary) data. */
5397 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
5399 /* Create a human readable table from the binary data. */
5400 for( x = 0; x < uxArraySize; x++ )
5402 switch( pxTaskStatusArray[ x ].eCurrentState )
5405 cStatus = tskRUNNING_CHAR;
5409 cStatus = tskREADY_CHAR;
5413 cStatus = tskBLOCKED_CHAR;
5417 cStatus = tskSUSPENDED_CHAR;
5421 cStatus = tskDELETED_CHAR;
5424 case eInvalid: /* Fall through. */
5425 default: /* Should not get here, but it is included
5426 * to prevent static checking errors. */
5427 cStatus = ( char ) 0x00;
5431 /* Write the task name to the string, padding with spaces so it
5432 * can be printed in tabular form more easily. */
5433 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5435 /* Write the rest of the string. */
5436 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. */
5437 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. */
5440 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
5441 * is 0 then vPortFree() will be #defined to nothing. */
5442 vPortFree( pxTaskStatusArray );
5446 mtCOVERAGE_TEST_MARKER();
5450 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
5451 /*----------------------------------------------------------*/
5453 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5455 void vTaskGetRunTimeStats( char * pcWriteBuffer )
5457 TaskStatus_t * pxTaskStatusArray;
5458 UBaseType_t uxArraySize, x;
5459 uint32_t ulTotalTime, ulStatsAsPercentage;
5461 #if ( configUSE_TRACE_FACILITY != 1 )
5463 #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats().
5470 * This function is provided for convenience only, and is used by many
5471 * of the demo applications. Do not consider it to be part of the
5474 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
5475 * of the uxTaskGetSystemState() output into a human readable table that
5476 * displays the amount of time each task has spent in the Running state
5477 * in both absolute and percentage terms.
5479 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
5480 * function that might bloat the code size, use a lot of stack, and
5481 * provide different results on different platforms. An alternative,
5482 * tiny, third party, and limited functionality implementation of
5483 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
5484 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
5485 * a full snprintf() implementation!).
5487 * It is recommended that production systems call uxTaskGetSystemState()
5488 * directly to get access to raw stats data, rather than indirectly
5489 * through a call to vTaskGetRunTimeStats().
5492 /* Make sure the write buffer does not contain a string. */
5493 *pcWriteBuffer = ( char ) 0x00;
5495 /* Take a snapshot of the number of tasks in case it changes while this
5496 * function is executing. */
5497 uxArraySize = uxCurrentNumberOfTasks;
5499 /* Allocate an array index for each task. NOTE! If
5500 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5501 * equate to NULL. */
5502 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. */
5504 if( pxTaskStatusArray != NULL )
5506 /* Generate the (binary) data. */
5507 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
5509 /* For percentage calculations. */
5510 ulTotalTime /= 100UL;
5512 /* Avoid divide by zero errors. */
5513 if( ulTotalTime > 0UL )
5515 /* Create a human readable table from the binary data. */
5516 for( x = 0; x < uxArraySize; x++ )
5518 /* What percentage of the total run time has the task used?
5519 * This will always be rounded down to the nearest integer.
5520 * ulTotalRunTimeDiv100 has already been divided by 100. */
5521 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
5523 /* Write the task name to the string, padding with
5524 * spaces so it can be printed in tabular form more
5526 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5528 if( ulStatsAsPercentage > 0UL )
5530 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5532 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
5536 /* sizeof( int ) == sizeof( long ) so a smaller
5537 * printf() library can be used. */
5538 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. */
5544 /* If the percentage is zero here then the task has
5545 * consumed less than 1% of the total run time. */
5546 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5548 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
5552 /* sizeof( int ) == sizeof( long ) so a smaller
5553 * printf() library can be used. */
5554 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. */
5559 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. */
5564 mtCOVERAGE_TEST_MARKER();
5567 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
5568 * is 0 then vPortFree() will be #defined to nothing. */
5569 vPortFree( pxTaskStatusArray );
5573 mtCOVERAGE_TEST_MARKER();
5577 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
5578 /*-----------------------------------------------------------*/
5580 TickType_t uxTaskResetEventItemValue( void )
5582 TickType_t uxReturn;
5584 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
5586 /* Reset the event list item to its normal value - so it can be used with
5587 * queues and semaphores. */
5588 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. */
5592 /*-----------------------------------------------------------*/
5594 #if ( configUSE_MUTEXES == 1 )
5596 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
5598 /* If xSemaphoreCreateMutex() is called before any tasks have been created
5599 * then pxCurrentTCB will be NULL. */
5600 if( pxCurrentTCB != NULL )
5602 ( pxCurrentTCB->uxMutexesHeld )++;
5605 return pxCurrentTCB;
5608 #endif /* configUSE_MUTEXES */
5609 /*-----------------------------------------------------------*/
5611 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5613 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
5614 BaseType_t xClearCountOnExit,
5615 TickType_t xTicksToWait )
5619 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5621 taskENTER_CRITICAL();
5623 /* Only block if the notification count is not already non-zero. */
5624 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
5626 /* Mark this task as waiting for a notification. */
5627 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5629 if( xTicksToWait > ( TickType_t ) 0 )
5631 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5632 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
5634 /* All ports are written to allow a yield in a critical
5635 * section (some will yield immediately, others wait until the
5636 * critical section exits) - but it is not something that
5637 * application code should ever do. */
5638 vTaskYieldWithinAPI();
5642 mtCOVERAGE_TEST_MARKER();
5647 mtCOVERAGE_TEST_MARKER();
5650 taskEXIT_CRITICAL();
5652 taskENTER_CRITICAL();
5654 traceTASK_NOTIFY_TAKE( uxIndexToWait );
5655 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5657 if( ulReturn != 0UL )
5659 if( xClearCountOnExit != pdFALSE )
5661 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
5665 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
5670 mtCOVERAGE_TEST_MARKER();
5673 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5675 taskEXIT_CRITICAL();
5680 #endif /* configUSE_TASK_NOTIFICATIONS */
5681 /*-----------------------------------------------------------*/
5683 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5685 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
5686 uint32_t ulBitsToClearOnEntry,
5687 uint32_t ulBitsToClearOnExit,
5688 uint32_t * pulNotificationValue,
5689 TickType_t xTicksToWait )
5693 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5695 taskENTER_CRITICAL();
5697 /* Only block if a notification is not already pending. */
5698 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5700 /* Clear bits in the task's notification value as bits may get
5701 * set by the notifying task or interrupt. This can be used to
5702 * clear the value to zero. */
5703 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
5705 /* Mark this task as waiting for a notification. */
5706 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5708 if( xTicksToWait > ( TickType_t ) 0 )
5710 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5711 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
5713 /* All ports are written to allow a yield in a critical
5714 * section (some will yield immediately, others wait until the
5715 * critical section exits) - but it is not something that
5716 * application code should ever do. */
5717 vTaskYieldWithinAPI();
5721 mtCOVERAGE_TEST_MARKER();
5726 mtCOVERAGE_TEST_MARKER();
5729 taskEXIT_CRITICAL();
5731 taskENTER_CRITICAL();
5733 traceTASK_NOTIFY_WAIT( uxIndexToWait );
5735 if( pulNotificationValue != NULL )
5737 /* Output the current notification value, which may or may not
5739 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5742 /* If ucNotifyValue is set then either the task never entered the
5743 * blocked state (because a notification was already pending) or the
5744 * task unblocked because of a notification. Otherwise the task
5745 * unblocked because of a timeout. */
5746 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5748 /* A notification was not received. */
5753 /* A notification was already pending or a notification was
5754 * received while the task was waiting. */
5755 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
5759 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5761 taskEXIT_CRITICAL();
5766 #endif /* configUSE_TASK_NOTIFICATIONS */
5767 /*-----------------------------------------------------------*/
5769 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5771 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
5772 UBaseType_t uxIndexToNotify,
5774 eNotifyAction eAction,
5775 uint32_t * pulPreviousNotificationValue )
5778 BaseType_t xReturn = pdPASS;
5779 uint8_t ucOriginalNotifyState;
5781 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5782 configASSERT( xTaskToNotify );
5783 pxTCB = xTaskToNotify;
5785 taskENTER_CRITICAL();
5787 if( pulPreviousNotificationValue != NULL )
5789 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5792 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5794 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5799 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5803 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5806 case eSetValueWithOverwrite:
5807 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5810 case eSetValueWithoutOverwrite:
5812 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5814 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5818 /* The value could not be written to the task. */
5826 /* The task is being notified without its notify value being
5832 /* Should not get here if all enums are handled.
5833 * Artificially force an assert by testing a value the
5834 * compiler can't assume is const. */
5835 configASSERT( xTickCount == ( TickType_t ) 0 );
5840 traceTASK_NOTIFY( uxIndexToNotify );
5842 /* If the task is in the blocked state specifically to wait for a
5843 * notification then unblock it now. */
5844 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5846 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5847 prvAddTaskToReadyList( pxTCB );
5849 /* The task should not have been on an event list. */
5850 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5852 #if ( configUSE_TICKLESS_IDLE != 0 )
5854 /* If a task is blocked waiting for a notification then
5855 * xNextTaskUnblockTime might be set to the blocked task's time
5856 * out time. If the task is unblocked for a reason other than
5857 * a timeout xNextTaskUnblockTime is normally left unchanged,
5858 * because it will automatically get reset to a new value when
5859 * the tick count equals xNextTaskUnblockTime. However if
5860 * tickless idling is used it might be more important to enter
5861 * sleep mode at the earliest possible time - so reset
5862 * xNextTaskUnblockTime here to ensure it is updated at the
5863 * earliest possible time. */
5864 prvResetNextTaskUnblockTime();
5868 #if ( configUSE_PREEMPTION == 1 )
5870 prvYieldForTask( pxTCB, pdFALSE );
5876 mtCOVERAGE_TEST_MARKER();
5879 taskEXIT_CRITICAL();
5884 #endif /* configUSE_TASK_NOTIFICATIONS */
5885 /*-----------------------------------------------------------*/
5887 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5889 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
5890 UBaseType_t uxIndexToNotify,
5892 eNotifyAction eAction,
5893 uint32_t * pulPreviousNotificationValue,
5894 BaseType_t * pxHigherPriorityTaskWoken )
5897 uint8_t ucOriginalNotifyState;
5898 BaseType_t xReturn = pdPASS;
5899 UBaseType_t uxSavedInterruptStatus;
5901 configASSERT( xTaskToNotify );
5902 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5904 /* RTOS ports that support interrupt nesting have the concept of a
5905 * maximum system call (or maximum API call) interrupt priority.
5906 * Interrupts that are above the maximum system call priority are keep
5907 * permanently enabled, even when the RTOS kernel is in a critical section,
5908 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
5909 * is defined in FreeRTOSConfig.h then
5910 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
5911 * failure if a FreeRTOS API function is called from an interrupt that has
5912 * been assigned a priority above the configured maximum system call
5913 * priority. Only FreeRTOS functions that end in FromISR can be called
5914 * from interrupts that have been assigned a priority at or (logically)
5915 * below the maximum system call interrupt priority. FreeRTOS maintains a
5916 * separate interrupt safe API to ensure interrupt entry is as fast and as
5917 * simple as possible. More information (albeit Cortex-M specific) is
5918 * provided on the following link:
5919 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5920 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
5922 pxTCB = xTaskToNotify;
5924 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5926 if( pulPreviousNotificationValue != NULL )
5928 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5931 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5932 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5937 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5941 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5944 case eSetValueWithOverwrite:
5945 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5948 case eSetValueWithoutOverwrite:
5950 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5952 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5956 /* The value could not be written to the task. */
5964 /* The task is being notified without its notify value being
5970 /* Should not get here if all enums are handled.
5971 * Artificially force an assert by testing a value the
5972 * compiler can't assume is const. */
5973 configASSERT( xTickCount == ( TickType_t ) 0 );
5977 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
5979 /* If the task is in the blocked state specifically to wait for a
5980 * notification then unblock it now. */
5981 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5983 /* The task should not have been on an event list. */
5984 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5986 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
5988 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5989 prvAddTaskToReadyList( pxTCB );
5993 /* The delayed and ready lists cannot be accessed, so hold
5994 * this task pending until the scheduler is resumed. */
5995 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
5998 #if ( configUSE_PREEMPTION == 1 )
5999 prvYieldForTask( pxTCB, pdFALSE );
6001 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6003 if( pxHigherPriorityTaskWoken != NULL )
6005 *pxHigherPriorityTaskWoken = pdTRUE;
6011 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6016 #endif /* configUSE_TASK_NOTIFICATIONS */
6017 /*-----------------------------------------------------------*/
6019 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6021 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
6022 UBaseType_t uxIndexToNotify,
6023 BaseType_t * pxHigherPriorityTaskWoken )
6026 uint8_t ucOriginalNotifyState;
6027 UBaseType_t uxSavedInterruptStatus;
6029 configASSERT( xTaskToNotify );
6030 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6032 /* RTOS ports that support interrupt nesting have the concept of a
6033 * maximum system call (or maximum API call) interrupt priority.
6034 * Interrupts that are above the maximum system call priority are keep
6035 * permanently enabled, even when the RTOS kernel is in a critical section,
6036 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
6037 * is defined in FreeRTOSConfig.h then
6038 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
6039 * failure if a FreeRTOS API function is called from an interrupt that has
6040 * been assigned a priority above the configured maximum system call
6041 * priority. Only FreeRTOS functions that end in FromISR can be called
6042 * from interrupts that have been assigned a priority at or (logically)
6043 * below the maximum system call interrupt priority. FreeRTOS maintains a
6044 * separate interrupt safe API to ensure interrupt entry is as fast and as
6045 * simple as possible. More information (albeit Cortex-M specific) is
6046 * provided on the following link:
6047 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
6048 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
6050 pxTCB = xTaskToNotify;
6052 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6054 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
6055 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
6057 /* 'Giving' is equivalent to incrementing a count in a counting
6059 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
6061 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
6063 /* If the task is in the blocked state specifically to wait for a
6064 * notification then unblock it now. */
6065 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
6067 /* The task should not have been on an event list. */
6068 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
6070 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
6072 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6073 prvAddTaskToReadyList( pxTCB );
6077 /* The delayed and ready lists cannot be accessed, so hold
6078 * this task pending until the scheduler is resumed. */
6079 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
6082 #if ( configUSE_PREEMPTION == 1 )
6083 prvYieldForTask( pxTCB, pdFALSE );
6085 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6087 if( pxHigherPriorityTaskWoken != NULL )
6089 *pxHigherPriorityTaskWoken = pdTRUE;
6095 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6098 #endif /* configUSE_TASK_NOTIFICATIONS */
6099 /*-----------------------------------------------------------*/
6101 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6103 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
6104 UBaseType_t uxIndexToClear )
6109 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6111 /* If null is passed in here then it is the calling task that is having
6112 * its notification state cleared. */
6113 pxTCB = prvGetTCBFromHandle( xTask );
6115 taskENTER_CRITICAL();
6117 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
6119 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
6127 taskEXIT_CRITICAL();
6132 #endif /* configUSE_TASK_NOTIFICATIONS */
6133 /*-----------------------------------------------------------*/
6135 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6137 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
6138 UBaseType_t uxIndexToClear,
6139 uint32_t ulBitsToClear )
6144 /* If null is passed in here then it is the calling task that is having
6145 * its notification state cleared. */
6146 pxTCB = prvGetTCBFromHandle( xTask );
6148 taskENTER_CRITICAL();
6150 /* Return the notification as it was before the bits were cleared,
6151 * then clear the bit mask. */
6152 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
6153 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
6155 taskEXIT_CRITICAL();
6160 #endif /* configUSE_TASK_NOTIFICATIONS */
6161 /*-----------------------------------------------------------*/
6163 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
6165 uint32_t ulTaskGetIdleRunTimeCounter( void )
6167 uint32_t ulReturn = 0;
6169 for( BaseType_t i = 0; i < configNUM_CORES; i++ )
6171 ulReturn += xIdleTaskHandle[ i ]->ulRunTimeCounter;
6177 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
6178 /*-----------------------------------------------------------*/
6180 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
6181 const BaseType_t xCanBlockIndefinitely )
6183 TickType_t xTimeToWake;
6184 const TickType_t xConstTickCount = xTickCount;
6186 #if ( INCLUDE_xTaskAbortDelay == 1 )
6188 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
6189 * reset to pdFALSE so it can be detected as having been set to pdTRUE
6190 * when the task leaves the Blocked state. */
6191 pxCurrentTCB->ucDelayAborted = pdFALSE;
6195 /* Remove the task from the ready list before adding it to the blocked list
6196 * as the same list item is used for both lists. */
6197 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6199 /* The current task must be in a ready list, so there is no need to
6200 * check, and the port reset macro can be called directly. */
6201 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. */
6205 mtCOVERAGE_TEST_MARKER();
6208 #if ( INCLUDE_vTaskSuspend == 1 )
6210 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
6212 /* Add the task to the suspended task list instead of a delayed task
6213 * list to ensure it is not woken by a timing event. It will block
6215 vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
6219 /* Calculate the time at which the task should be woken if the event
6220 * does not occur. This may overflow but this doesn't matter, the
6221 * kernel will manage it correctly. */
6222 xTimeToWake = xConstTickCount + xTicksToWait;
6224 /* The list item will be inserted in wake time order. */
6225 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6227 if( xTimeToWake < xConstTickCount )
6229 /* Wake time has overflowed. Place this item in the overflow
6231 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6235 /* The wake time has not overflowed, so the current block list
6237 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6239 /* If the task entering the blocked state was placed at the
6240 * head of the list of blocked tasks then xNextTaskUnblockTime
6241 * needs to be updated too. */
6242 if( xTimeToWake < xNextTaskUnblockTime )
6244 xNextTaskUnblockTime = xTimeToWake;
6248 mtCOVERAGE_TEST_MARKER();
6253 #else /* INCLUDE_vTaskSuspend */
6255 /* Calculate the time at which the task should be woken if the event
6256 * does not occur. This may overflow but this doesn't matter, the kernel
6257 * will manage it correctly. */
6258 xTimeToWake = xConstTickCount + xTicksToWait;
6260 /* The list item will be inserted in wake time order. */
6261 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6263 if( xTimeToWake < xConstTickCount )
6265 /* Wake time has overflowed. Place this item in the overflow list. */
6266 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6270 /* The wake time has not overflowed, so the current block list is used. */
6271 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6273 /* If the task entering the blocked state was placed at the head of the
6274 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
6276 if( xTimeToWake < xNextTaskUnblockTime )
6278 xNextTaskUnblockTime = xTimeToWake;
6282 mtCOVERAGE_TEST_MARKER();
6286 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
6287 ( void ) xCanBlockIndefinitely;
6289 #endif /* INCLUDE_vTaskSuspend */
6292 /* Code below here allows additional code to be inserted into this source file,
6293 * especially where access to file scope functions and data is needed (for example
6294 * when performing module tests). */
6296 #ifdef FREERTOS_MODULE_TEST
6297 #include "tasks_test_access_functions.h"
6301 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
6303 #include "freertos_tasks_c_additions.h"
6305 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
6306 static void freertos_tasks_c_additions_init( void )
6308 FREERTOS_TASKS_C_ADDITIONS_INIT();
6312 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */