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( configNUM_CORES > 1 )
765 #if ( configUSE_CORE_AFFINITY == 1 )
766 if( ( pxTCB->uxCoreAffinityMask & ( 1 << x ) ) != 0 )
770 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
771 if( pxCurrentTCBs[ x ]->xPreemptionDisable == pdFALSE )
774 xLowestPriority = xTaskPriority;
775 xLowestPriorityCore = x;
781 mtCOVERAGE_TEST_MARKER();
784 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) && 1
786 /* Yield all currently running non-idle tasks with a priority lower than
787 * the task that needs to run. */
788 if( ( ( BaseType_t ) tskIDLE_PRIORITY - 1 < xTaskPriority ) && ( xTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
795 mtCOVERAGE_TEST_MARKER();
798 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) && 1 */
802 mtCOVERAGE_TEST_MARKER();
806 if( ( xYieldCount == 0 ) && taskVALID_CORE_ID( xLowestPriorityCore ) )
808 prvYieldCore( xLowestPriorityCore );
812 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
813 /* Verify that the calling core always yields to higher priority tasks */
814 if( !pxCurrentTCBs[ portGET_CORE_ID() ]->xIsIdle && ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
816 configASSERT( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE || taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ]->xTaskRunState ) == pdFALSE );
820 /*-----------------------------------------------------------*/
822 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
824 static BaseType_t prvSelectHighestPriorityTask( const BaseType_t xCoreID )
826 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
827 BaseType_t xTaskScheduled = pdFALSE;
828 BaseType_t xDecrementTopPriority = pdTRUE;
830 #if ( configUSE_CORE_AFFINITY == 1 )
831 TCB_t * pxPreviousTCB = NULL;
833 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
834 BaseType_t xPriorityDropped = pdFALSE;
837 while( xTaskScheduled == pdFALSE )
839 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
841 if( uxCurrentPriority < uxTopReadyPriority )
843 /* We can't schedule any tasks, other than idle, that have a
844 * priority lower than the priority of a task currently running
845 * on another core. */
846 uxCurrentPriority = tskIDLE_PRIORITY;
851 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
853 List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
854 ListItem_t * pxLastTaskItem = pxReadyList->pxIndex->pxPrevious;
855 ListItem_t * pxTaskItem = pxLastTaskItem;
857 if( ( void * ) pxLastTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
859 pxLastTaskItem = pxLastTaskItem->pxPrevious;
862 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
863 * must not be decremented any further */
864 xDecrementTopPriority = pdFALSE;
870 pxTaskItem = pxTaskItem->pxNext;
872 if( ( void * ) pxTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
874 pxTaskItem = pxTaskItem->pxNext;
877 pxTCB = pxTaskItem->pvOwner;
879 /*debug_printf("Attempting to schedule %s on core %d\n", pxTCB->pcTaskName, portGET_CORE_ID() ); */
881 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
883 /* When falling back to the idle priority because only one priority
884 * level is allowed to run at a time, we should ONLY schedule the true
885 * idle tasks, not user tasks at the idle priority. */
886 if( uxCurrentPriority < uxTopReadyPriority )
888 if( pxTCB->xIsIdle == pdFALSE )
894 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
896 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
898 #if ( configNUM_CORES > 1 )
899 #if ( configUSE_CORE_AFFINITY == 1 )
900 if( ( pxTCB->uxCoreAffinityMask & ( 1 << xCoreID ) ) != 0 )
904 /* If the task is not being executed by any core swap it in */
905 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
906 #if ( configUSE_CORE_AFFINITY == 1 )
907 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
909 pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
910 pxCurrentTCBs[ xCoreID ] = pxTCB;
911 xTaskScheduled = pdTRUE;
914 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
916 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_YIELDING ) );
917 #if( configNUM_CORES > 1 )
918 #if ( configUSE_CORE_AFFINITY == 1 )
919 if( ( pxTCB->uxCoreAffinityMask & ( 1 << xCoreID ) ) != 0 )
923 /* The task is already running on this core, mark it as scheduled */
924 pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
925 xTaskScheduled = pdTRUE;
929 if( xTaskScheduled != pdFALSE )
931 /* Once a task has been selected to run on this core,
932 * move it to the end of the ready task list. */
933 uxListRemove( pxTaskItem );
934 vListInsertEnd( pxReadyList, pxTaskItem );
937 } while( pxTaskItem != pxLastTaskItem );
941 if( xDecrementTopPriority != pdFALSE )
943 uxTopReadyPriority--;
944 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
946 xPriorityDropped = pdTRUE;
952 /* This function can get called by vTaskSuspend() before the scheduler is started.
953 * In that case, since the idle tasks have not yet been created it is possible that we
954 * won't find a new task to schedule. Return pdFALSE in this case. */
955 if( ( xSchedulerRunning == pdFALSE ) && ( uxCurrentPriority == tskIDLE_PRIORITY ) && ( xTaskScheduled == pdFALSE ) )
960 configASSERT( ( uxCurrentPriority > tskIDLE_PRIORITY ) || ( xTaskScheduled == pdTRUE ) );
964 configASSERT( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ]->xTaskRunState ) );
966 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
967 if( xPriorityDropped != pdFALSE )
969 /* There may be several ready tasks that were being prevented from running because there was
970 * a higher priority task running. Now that the last of the higher priority tasks is no longer
971 * running, make sure all the other idle tasks yield. */
974 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUM_CORES; x++ )
976 if( pxCurrentTCBs[ x ]->xIsIdle != pdFALSE )
982 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
984 #if ( configNUM_CORES > 1 )
985 #if ( configUSE_CORE_AFFINITY == 1 )
986 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
988 /* A ready task was just bumped off this core. Look at the cores it can run from
989 * from to see if it is able to run on any of them */
990 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
991 BaseType_t xLowestPriority = pxPreviousTCB->uxPriority - pxPreviousTCB->xIsIdle;
992 BaseType_t xLowestPriorityCore = -1;
994 if( ( uxCoreMap & ( 1 << xCoreID ) ) != 0 )
996 /* The ready task that was removed from this core is not excluded from it.
997 * Only look at the intersection of the cores the removed task is allowed to run
998 * on with the cores that the new task is excluded from. It is possible that the
999 * new task was only placed onto this core because it is excluded from another.
1000 * Check to see if the previous task could run on one of those cores. */
1001 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1005 /* The ready task that was removed from this core is excluded from it.
1006 * @todo See if we can schedule it on any of the cores where it is not excluded from. */
1009 uxCoreMap &= ( ( 1 << configNUM_CORES ) - 1 );
1011 while( uxCoreMap != 0 )
1013 int uxCore = 31UL - ( uint32_t ) __builtin_clz( uxCoreMap );
1015 xassert( taskVALID_CORE_ID( uxCore ) );
1017 uxCoreMap &= ~( 1 << uxCore );
1019 BaseType_t xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority - pxCurrentTCBs[ uxCore ]->xIsIdle;
1021 if( ( xTaskPriority < xLowestPriority ) && ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ]->xTaskRunState ) != pdFALSE ) && ( xYieldPendings[ uxCore ] == pdFALSE ) )
1023 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1024 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1027 xLowestPriority = xTaskPriority;
1028 xLowestPriorityCore = uxCore;
1033 if( taskVALID_CORE_ID( xLowestPriorityCore ) )
1035 prvYieldCore( xLowestPriorityCore );
1038 #endif /* if ( configUSE_CORE_AFFINITY == 1 ) */
1039 #endif /* if ( configNUM_CORES > 1 ) */
1044 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1046 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
1048 UBaseType_t uxTopPriority;
1050 /* Find the highest priority list that contains ready tasks. */
1051 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority );
1052 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 );
1053 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) );
1056 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1057 /*-----------------------------------------------------------*/
1059 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1061 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1062 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1063 const uint32_t ulStackDepth,
1064 void * const pvParameters,
1065 UBaseType_t uxPriority,
1066 StackType_t * const puxStackBuffer,
1067 StaticTask_t * const pxTaskBuffer )
1070 TaskHandle_t xReturn;
1072 configASSERT( puxStackBuffer != NULL );
1073 configASSERT( pxTaskBuffer != NULL );
1075 #if ( configASSERT_DEFINED == 1 )
1077 /* Sanity check that the size of the structure used to declare a
1078 * variable of type StaticTask_t equals the size of the real task
1080 volatile size_t xSize = sizeof( StaticTask_t );
1081 configASSERT( xSize == sizeof( TCB_t ) );
1082 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1084 #endif /* configASSERT_DEFINED */
1086 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1088 /* The memory used for the task's TCB and stack are passed into this
1089 * function - use them. */
1090 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. */
1091 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1093 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1095 /* Tasks can be created statically or dynamically, so note this
1096 * task was created statically in case the task is later deleted. */
1097 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1099 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1101 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1102 prvAddNewTaskToReadyList( pxNewTCB );
1112 #endif /* SUPPORT_STATIC_ALLOCATION */
1113 /*-----------------------------------------------------------*/
1115 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1117 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1118 TaskHandle_t * pxCreatedTask )
1121 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1123 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1124 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1126 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1128 /* Allocate space for the TCB. Where the memory comes from depends
1129 * on the implementation of the port malloc function and whether or
1130 * not static allocation is being used. */
1131 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1133 /* Store the stack location in the TCB. */
1134 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1136 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1138 /* Tasks can be created statically or dynamically, so note this
1139 * task was created statically in case the task is later deleted. */
1140 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1142 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1144 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1145 pxTaskDefinition->pcName,
1146 ( uint32_t ) pxTaskDefinition->usStackDepth,
1147 pxTaskDefinition->pvParameters,
1148 pxTaskDefinition->uxPriority,
1149 pxCreatedTask, pxNewTCB,
1150 pxTaskDefinition->xRegions );
1152 prvAddNewTaskToReadyList( pxNewTCB );
1159 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1160 /*-----------------------------------------------------------*/
1162 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1164 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1165 TaskHandle_t * pxCreatedTask )
1168 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1170 configASSERT( pxTaskDefinition->puxStackBuffer );
1172 if( pxTaskDefinition->puxStackBuffer != NULL )
1174 /* Allocate space for the TCB. Where the memory comes from depends
1175 * on the implementation of the port malloc function and whether or
1176 * not static allocation is being used. */
1177 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1179 if( pxNewTCB != NULL )
1181 /* Store the stack location in the TCB. */
1182 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1184 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1186 /* Tasks can be created statically or dynamically, so note
1187 * this task had a statically allocated stack in case it is
1188 * later deleted. The TCB was allocated dynamically. */
1189 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1191 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1193 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1194 pxTaskDefinition->pcName,
1195 ( uint32_t ) pxTaskDefinition->usStackDepth,
1196 pxTaskDefinition->pvParameters,
1197 pxTaskDefinition->uxPriority,
1198 pxCreatedTask, pxNewTCB,
1199 pxTaskDefinition->xRegions );
1201 prvAddNewTaskToReadyList( pxNewTCB );
1209 #endif /* portUSING_MPU_WRAPPERS */
1210 /*-----------------------------------------------------------*/
1212 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1214 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1215 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1216 const configSTACK_DEPTH_TYPE usStackDepth,
1217 void * const pvParameters,
1218 UBaseType_t uxPriority,
1219 TaskHandle_t * const pxCreatedTask )
1224 /* If the stack grows down then allocate the stack then the TCB so the stack
1225 * does not grow into the TCB. Likewise if the stack grows up then allocate
1226 * the TCB then the stack. */
1227 #if ( portSTACK_GROWTH > 0 )
1229 /* Allocate space for the TCB. Where the memory comes from depends on
1230 * the implementation of the port malloc function and whether or not static
1231 * allocation is being used. */
1232 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1234 if( pxNewTCB != NULL )
1236 /* Allocate space for the stack used by the task being created.
1237 * The base of the stack memory stored in the TCB so the task can
1238 * be deleted later if required. */
1239 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1241 if( pxNewTCB->pxStack == NULL )
1243 /* Could not allocate the stack. Delete the allocated TCB. */
1244 vPortFree( pxNewTCB );
1249 #else /* portSTACK_GROWTH */
1251 StackType_t * pxStack;
1253 /* Allocate space for the stack used by the task being created. */
1254 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. */
1256 if( pxStack != NULL )
1258 /* Allocate space for the TCB. */
1259 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. */
1261 if( pxNewTCB != NULL )
1263 /* Store the stack location in the TCB. */
1264 pxNewTCB->pxStack = pxStack;
1268 /* The stack cannot be used as the TCB was not created. Free
1270 vPortFreeStack( pxStack );
1278 #endif /* portSTACK_GROWTH */
1280 if( pxNewTCB != NULL )
1282 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1284 /* Tasks can be created statically or dynamically, so note this
1285 * task was created dynamically in case it is later deleted. */
1286 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1288 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1290 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1291 prvAddNewTaskToReadyList( pxNewTCB );
1296 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1302 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1303 /*-----------------------------------------------------------*/
1305 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1306 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1307 const uint32_t ulStackDepth,
1308 void * const pvParameters,
1309 UBaseType_t uxPriority,
1310 TaskHandle_t * const pxCreatedTask,
1312 const MemoryRegion_t * const xRegions )
1314 StackType_t * pxTopOfStack;
1317 #if ( portUSING_MPU_WRAPPERS == 1 )
1318 /* Should the task be created in privileged mode? */
1319 BaseType_t xRunPrivileged;
1321 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1323 xRunPrivileged = pdTRUE;
1327 xRunPrivileged = pdFALSE;
1329 uxPriority &= ~portPRIVILEGE_BIT;
1330 #endif /* portUSING_MPU_WRAPPERS == 1 */
1332 /* Avoid dependency on memset() if it is not required. */
1333 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1335 /* Fill the stack with a known value to assist debugging. */
1336 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1338 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1340 /* Calculate the top of stack address. This depends on whether the stack
1341 * grows from high memory to low (as per the 80x86) or vice versa.
1342 * portSTACK_GROWTH is used to make the result positive or negative as required
1344 #if ( portSTACK_GROWTH < 0 )
1346 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1347 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(). */
1349 /* Check the alignment of the calculated top of stack is correct. */
1350 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1352 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1354 /* Also record the stack's high address, which may assist
1356 pxNewTCB->pxEndOfStack = pxTopOfStack;
1358 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1360 #else /* portSTACK_GROWTH */
1362 pxTopOfStack = pxNewTCB->pxStack;
1364 /* Check the alignment of the stack buffer is correct. */
1365 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1367 /* The other extreme of the stack space is required if stack checking is
1369 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1371 #endif /* portSTACK_GROWTH */
1373 /* Store the task name in the TCB. */
1374 if( pcName != NULL )
1376 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1378 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1380 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1381 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1382 * string is not accessible (extremely unlikely). */
1383 if( pcName[ x ] == ( char ) 0x00 )
1389 mtCOVERAGE_TEST_MARKER();
1393 /* Ensure the name string is terminated in the case that the string length
1394 * was greater or equal to configMAX_TASK_NAME_LEN. */
1395 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1399 /* The task has not been given a name, so just ensure there is a NULL
1400 * terminator when it is read out. */
1401 pxNewTCB->pcTaskName[ 0 ] = 0x00;
1404 /* This is used as an array index so must ensure it's not too large. First
1405 * remove the privilege bit if one is present. */
1406 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1408 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1412 mtCOVERAGE_TEST_MARKER();
1415 pxNewTCB->uxPriority = uxPriority;
1416 #if ( configUSE_MUTEXES == 1 )
1418 pxNewTCB->uxBasePriority = uxPriority;
1419 pxNewTCB->uxMutexesHeld = 0;
1421 #endif /* configUSE_MUTEXES */
1423 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1424 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1426 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1427 * back to the containing TCB from a generic item in a list. */
1428 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1430 /* Event lists are always in priority order. */
1431 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. */
1432 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1434 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
1436 pxNewTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
1438 #endif /* portCRITICAL_NESTING_IN_TCB */
1440 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
1442 pxNewTCB->pxTaskTag = NULL;
1444 #endif /* configUSE_APPLICATION_TASK_TAG */
1446 #if ( configGENERATE_RUN_TIME_STATS == 1 )
1448 pxNewTCB->ulRunTimeCounter = 0UL;
1450 #endif /* configGENERATE_RUN_TIME_STATS */
1452 #if ( portUSING_MPU_WRAPPERS == 1 )
1454 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1458 /* Avoid compiler warning about unreferenced parameter. */
1463 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
1465 memset( ( void * ) &( pxNewTCB->pvThreadLocalStoragePointers[ 0 ] ), 0x00, sizeof( pxNewTCB->pvThreadLocalStoragePointers ) );
1469 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1471 memset( ( void * ) &( pxNewTCB->ulNotifiedValue[ 0 ] ), 0x00, sizeof( pxNewTCB->ulNotifiedValue ) );
1472 memset( ( void * ) &( pxNewTCB->ucNotifyState[ 0 ] ), 0x00, sizeof( pxNewTCB->ucNotifyState ) );
1476 #if ( configUSE_NEWLIB_REENTRANT == 1 )
1478 /* Initialise this task's Newlib reent structure.
1479 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
1480 * for additional information. */
1481 _REENT_INIT_PTR( ( &( pxNewTCB->xNewLib_reent ) ) );
1485 #if ( INCLUDE_xTaskAbortDelay == 1 )
1487 pxNewTCB->ucDelayAborted = pdFALSE;
1491 #if ( configNUM_CORES > 1 )
1492 #if ( configUSE_CORE_AFFINITY == 1 )
1494 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1498 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1500 pxNewTCB->xPreemptionDisable = 0;
1504 /* Initialize the TCB stack to look as if the task was already running,
1505 * but had been interrupted by the scheduler. The return address is set
1506 * to the start of the task function. Once the stack has been initialised
1507 * the top of stack variable is updated. */
1508 #if ( portUSING_MPU_WRAPPERS == 1 )
1510 /* If the port has capability to detect stack overflow,
1511 * pass the stack end address to the stack initialization
1512 * function as well. */
1513 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1515 #if ( portSTACK_GROWTH < 0 )
1517 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
1519 #else /* portSTACK_GROWTH */
1521 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1523 #endif /* portSTACK_GROWTH */
1525 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1527 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1529 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1531 #else /* portUSING_MPU_WRAPPERS */
1533 /* If the port has capability to detect stack overflow,
1534 * pass the stack end address to the stack initialization
1535 * function as well. */
1536 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1538 #if ( portSTACK_GROWTH < 0 )
1540 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1542 #else /* portSTACK_GROWTH */
1544 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1546 #endif /* portSTACK_GROWTH */
1548 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1550 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1552 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1554 #endif /* portUSING_MPU_WRAPPERS */
1556 /* Initialize to not running */
1557 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1559 /* Is this an idle task? */
1560 if(pxTaskCode == prvIdleTask)
1562 pxNewTCB->xIsIdle = pdTRUE;
1564 #if(configNUM_CORES > 1)
1565 else if(pxTaskCode == prvMinimalIdleTask)
1567 pxNewTCB->xIsIdle = pdTRUE;
1572 pxNewTCB->xIsIdle = pdFALSE;
1575 if( pxCreatedTask != NULL )
1577 /* Pass the handle out in an anonymous way. The handle can be used to
1578 * change the created task's priority, delete the created task, etc.*/
1579 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1583 mtCOVERAGE_TEST_MARKER();
1586 /*-----------------------------------------------------------*/
1588 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1590 /* Ensure interrupts don't access the task lists while the lists are being
1592 taskENTER_CRITICAL();
1594 uxCurrentNumberOfTasks++;
1596 if( xSchedulerRunning == pdFALSE )
1598 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1600 /* This is the first task to be created so do the preliminary
1601 * initialisation required. We will not recover if this call
1602 * fails, but we will report the failure. */
1603 prvInitialiseTaskLists();
1607 mtCOVERAGE_TEST_MARKER();
1610 if( pxNewTCB->xIsIdle != pdFALSE )
1614 /* Check if a core is free. */
1615 for( xCoreID = ( UBaseType_t ) 0; xCoreID < ( UBaseType_t ) configNUM_CORES; xCoreID++ )
1617 if( pxCurrentTCBs[ xCoreID ] == NULL )
1619 pxNewTCB->xTaskRunState = xCoreID;
1620 /* This section of code pins the idle tasks to cores.
1621 #if ( configUSE_CORE_AFFINITY == 1 )
1623 pxNewTCB->uxCoreAffinityMask = ( 1 << xCoreID );
1627 pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1635 mtCOVERAGE_TEST_MARKER();
1640 #if ( configUSE_TRACE_FACILITY == 1 )
1642 /* Add a counter into the TCB for tracing only. */
1643 pxNewTCB->uxTCBNumber = uxTaskNumber;
1645 #endif /* configUSE_TRACE_FACILITY */
1646 traceTASK_CREATE( pxNewTCB );
1648 prvAddTaskToReadyList( pxNewTCB );
1650 portSETUP_TCB( pxNewTCB );
1652 if( xSchedulerRunning != pdFALSE )
1654 /* If the created task is of a higher priority than another
1655 * currently running task and preemption is on then it should
1657 #if ( configUSE_PREEMPTION == 1 )
1658 prvYieldForTask( pxNewTCB, pdFALSE );
1663 mtCOVERAGE_TEST_MARKER();
1666 taskEXIT_CRITICAL();
1668 /*-----------------------------------------------------------*/
1670 #if ( INCLUDE_vTaskDelete == 1 )
1672 void vTaskDelete( TaskHandle_t xTaskToDelete )
1675 TaskRunning_t xTaskRunningOnCore;
1677 taskENTER_CRITICAL();
1679 /* If null is passed in here then it is the calling task that is
1681 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1683 xTaskRunningOnCore = pxTCB->xTaskRunState;
1685 /* Remove task from the ready/delayed list. */
1686 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1688 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1692 mtCOVERAGE_TEST_MARKER();
1695 /* Is the task waiting on an event also? */
1696 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1698 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1702 mtCOVERAGE_TEST_MARKER();
1705 /* Increment the uxTaskNumber also so kernel aware debuggers can
1706 * detect that the task lists need re-generating. This is done before
1707 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1711 /* If the task is running (or yielding), we must add it to the
1712 * termination list so that an idle task can delete it when it is
1713 * no longer running. */
1714 if( xTaskRunningOnCore != taskTASK_NOT_RUNNING )
1717 /* A running task is being deleted. This cannot complete within the
1718 * task itself, as a context switch to another task is required.
1719 * Place the task in the termination list. The idle task will
1720 * check the termination list and free up any memory allocated by
1721 * the scheduler for the TCB and stack of the deleted task. */
1722 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1724 /* Increment the ucTasksDeleted variable so the idle task knows
1725 * there is a task that has been deleted and that it should therefore
1726 * check the xTasksWaitingTermination list. */
1727 ++uxDeletedTasksWaitingCleanUp;
1729 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1730 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1731 traceTASK_DELETE( pxTCB );
1733 /* The pre-delete hook is primarily for the Windows simulator,
1734 * in which Windows specific clean up operations are performed,
1735 * after which it is not possible to yield away from this task -
1736 * hence xYieldPending is used to latch that a context switch is
1738 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPendings[ pxTCB->xTaskRunState ] );
1742 --uxCurrentNumberOfTasks;
1743 traceTASK_DELETE( pxTCB );
1744 prvDeleteTCB( pxTCB );
1746 /* Reset the next expected unblock time in case it referred to
1747 * the task that has just been deleted. */
1748 prvResetNextTaskUnblockTime();
1751 /* Force a reschedule if the task that has just been deleted was running. */
1752 if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( xTaskRunningOnCore ) ) )
1756 xCoreID = portGET_CORE_ID();
1759 if( xTaskRunningOnCore == xCoreID )
1761 configASSERT( uxSchedulerSuspended == 0 );
1762 vTaskYieldWithinAPI();
1766 prvYieldCore( xTaskRunningOnCore );
1770 taskEXIT_CRITICAL();
1773 #endif /* INCLUDE_vTaskDelete */
1774 /*-----------------------------------------------------------*/
1776 #if ( INCLUDE_xTaskDelayUntil == 1 )
1778 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
1779 const TickType_t xTimeIncrement )
1781 TickType_t xTimeToWake;
1782 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
1784 configASSERT( pxPreviousWakeTime );
1785 configASSERT( ( xTimeIncrement > 0U ) );
1789 configASSERT( uxSchedulerSuspended == 1 );
1791 /* Minor optimisation. The tick count cannot change in this
1793 const TickType_t xConstTickCount = xTickCount;
1795 /* Generate the tick time at which the task wants to wake. */
1796 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
1798 if( xConstTickCount < *pxPreviousWakeTime )
1800 /* The tick count has overflowed since this function was
1801 * lasted called. In this case the only time we should ever
1802 * actually delay is if the wake time has also overflowed,
1803 * and the wake time is greater than the tick time. When this
1804 * is the case it is as if neither time had overflowed. */
1805 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1807 xShouldDelay = pdTRUE;
1811 mtCOVERAGE_TEST_MARKER();
1816 /* The tick time has not overflowed. In this case we will
1817 * delay if either the wake time has overflowed, and/or the
1818 * tick time is less than the wake time. */
1819 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1821 xShouldDelay = pdTRUE;
1825 mtCOVERAGE_TEST_MARKER();
1829 /* Update the wake time ready for the next call. */
1830 *pxPreviousWakeTime = xTimeToWake;
1832 if( xShouldDelay != pdFALSE )
1834 traceTASK_DELAY_UNTIL( xTimeToWake );
1836 /* prvAddCurrentTaskToDelayedList() needs the block time, not
1837 * the time to wake, so subtract the current tick count. */
1838 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
1842 mtCOVERAGE_TEST_MARKER();
1845 xAlreadyYielded = xTaskResumeAll();
1847 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1848 * have put ourselves to sleep. */
1849 if( xAlreadyYielded == pdFALSE )
1851 vTaskYieldWithinAPI();
1855 mtCOVERAGE_TEST_MARKER();
1858 return xShouldDelay;
1861 #endif /* INCLUDE_xTaskDelayUntil */
1862 /*-----------------------------------------------------------*/
1864 #if ( INCLUDE_vTaskDelay == 1 )
1866 void vTaskDelay( const TickType_t xTicksToDelay )
1868 BaseType_t xAlreadyYielded = pdFALSE;
1870 /* A delay time of zero just forces a reschedule. */
1871 if( xTicksToDelay > ( TickType_t ) 0U )
1875 configASSERT( uxSchedulerSuspended == 1 );
1878 /* A task that is removed from the event list while the
1879 * scheduler is suspended will not get placed in the ready
1880 * list or removed from the blocked list until the scheduler
1883 * This task cannot be in an event list as it is the currently
1884 * executing task. */
1885 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
1887 xAlreadyYielded = xTaskResumeAll();
1891 mtCOVERAGE_TEST_MARKER();
1894 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1895 * have put ourselves to sleep. */
1896 if( xAlreadyYielded == pdFALSE )
1898 vTaskYieldWithinAPI();
1902 mtCOVERAGE_TEST_MARKER();
1906 #endif /* INCLUDE_vTaskDelay */
1907 /*-----------------------------------------------------------*/
1909 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
1911 eTaskState eTaskGetState( TaskHandle_t xTask )
1914 List_t const * pxStateList, * pxDelayedList, * pxOverflowedDelayedList;
1915 const TCB_t * const pxTCB = xTask;
1917 configASSERT( pxTCB );
1919 taskENTER_CRITICAL();
1921 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
1922 pxDelayedList = pxDelayedTaskList;
1923 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
1925 taskEXIT_CRITICAL();
1927 if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
1929 /* The task being queried is referenced from one of the Blocked
1934 #if ( INCLUDE_vTaskSuspend == 1 )
1935 else if( pxStateList == &xSuspendedTaskList )
1937 /* The task being queried is referenced from the suspended
1938 * list. Is it genuinely suspended or is it blocked
1940 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
1942 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1946 /* The task does not appear on the event list item of
1947 * and of the RTOS objects, but could still be in the
1948 * blocked state if it is waiting on its notification
1949 * rather than waiting on an object. If not, is
1951 eReturn = eSuspended;
1953 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1955 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1962 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1964 eReturn = eSuspended;
1966 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1973 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
1975 #if ( INCLUDE_vTaskDelete == 1 )
1976 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
1978 /* The task being queried is referenced from the deleted
1979 * tasks list, or it is not referenced from any lists at
1985 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
1987 /* If the task is not in any other state, it must be in the
1988 * Ready (including pending ready) state. */
1989 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
1991 /* Is it actively running on a core? */
2001 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2003 #endif /* INCLUDE_eTaskGetState */
2004 /*-----------------------------------------------------------*/
2006 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2008 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2010 TCB_t const * pxTCB;
2011 UBaseType_t uxReturn;
2013 taskENTER_CRITICAL();
2015 /* If null is passed in here then it is the priority of the task
2016 * that called uxTaskPriorityGet() that is being queried. */
2017 pxTCB = prvGetTCBFromHandle( xTask );
2018 uxReturn = pxTCB->uxPriority;
2020 taskEXIT_CRITICAL();
2025 #endif /* INCLUDE_uxTaskPriorityGet */
2026 /*-----------------------------------------------------------*/
2028 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2030 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2032 TCB_t const * pxTCB;
2033 UBaseType_t uxReturn, uxSavedInterruptState;
2035 /* RTOS ports that support interrupt nesting have the concept of a
2036 * maximum system call (or maximum API call) interrupt priority.
2037 * Interrupts that are above the maximum system call priority are keep
2038 * permanently enabled, even when the RTOS kernel is in a critical section,
2039 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2040 * is defined in FreeRTOSConfig.h then
2041 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2042 * failure if a FreeRTOS API function is called from an interrupt that has
2043 * been assigned a priority above the configured maximum system call
2044 * priority. Only FreeRTOS functions that end in FromISR can be called
2045 * from interrupts that have been assigned a priority at or (logically)
2046 * below the maximum system call interrupt priority. FreeRTOS maintains a
2047 * separate interrupt safe API to ensure interrupt entry is as fast and as
2048 * simple as possible. More information (albeit Cortex-M specific) is
2049 * provided on the following link:
2050 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2051 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2053 uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
2055 /* If null is passed in here then it is the priority of the calling
2056 * task that is being queried. */
2057 pxTCB = prvGetTCBFromHandle( xTask );
2058 uxReturn = pxTCB->uxPriority;
2060 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
2065 #endif /* INCLUDE_uxTaskPriorityGet */
2066 /*-----------------------------------------------------------*/
2068 #if ( INCLUDE_vTaskPrioritySet == 1 )
2070 void vTaskPrioritySet( TaskHandle_t xTask,
2071 UBaseType_t uxNewPriority )
2074 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2075 BaseType_t xYieldRequired = pdFALSE;
2076 BaseType_t xYieldForTask = pdFALSE;
2079 configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
2081 /* Ensure the new priority is valid. */
2082 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2084 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2088 mtCOVERAGE_TEST_MARKER();
2091 taskENTER_CRITICAL();
2093 /* If null is passed in here then it is the priority of the calling
2094 * task that is being changed. */
2095 pxTCB = prvGetTCBFromHandle( xTask );
2097 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2099 #if ( configUSE_MUTEXES == 1 )
2101 uxCurrentBasePriority = pxTCB->uxBasePriority;
2105 uxCurrentBasePriority = pxTCB->uxPriority;
2109 if( uxCurrentBasePriority != uxNewPriority )
2111 /* The priority change may have readied a task of higher
2112 * priority than a running task. */
2113 if( uxNewPriority > uxCurrentBasePriority )
2115 /* The priority of a task is being raised so
2116 * perform a yield for this task later. */
2117 xYieldForTask = pdTRUE;
2119 else if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2121 /* Setting the priority of a running task down means
2122 * there may now be another task of higher priority that
2123 * is ready to execute. */
2124 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2125 if( pxTCB->xPreemptionDisable == pdFALSE )
2128 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2129 xYieldRequired = pdTRUE;
2134 /* Setting the priority of any other task down does not
2135 * require a yield as the running task must be above the
2136 * new priority of the task being modified. */
2139 /* Remember the ready list the task might be referenced from
2140 * before its uxPriority member is changed so the
2141 * taskRESET_READY_PRIORITY() macro can function correctly. */
2142 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2144 #if ( configUSE_MUTEXES == 1 )
2146 /* Only change the priority being used if the task is not
2147 * currently using an inherited priority. */
2148 if( pxTCB->uxBasePriority == pxTCB->uxPriority )
2150 pxTCB->uxPriority = uxNewPriority;
2154 mtCOVERAGE_TEST_MARKER();
2157 /* The base priority gets set whatever. */
2158 pxTCB->uxBasePriority = uxNewPriority;
2160 #else /* if ( configUSE_MUTEXES == 1 ) */
2162 pxTCB->uxPriority = uxNewPriority;
2164 #endif /* if ( configUSE_MUTEXES == 1 ) */
2166 /* Only reset the event list item value if the value is not
2167 * being used for anything else. */
2168 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2170 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. */
2174 mtCOVERAGE_TEST_MARKER();
2177 /* If the task is in the blocked or suspended list we need do
2178 * nothing more than change its priority variable. However, if
2179 * the task is in a ready list it needs to be removed and placed
2180 * in the list appropriate to its new priority. */
2181 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2183 /* The task is currently in its ready list - remove before
2184 * adding it to its new ready list. As we are in a critical
2185 * section we can do this even if the scheduler is suspended. */
2186 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2188 /* It is known that the task is in its ready list so
2189 * there is no need to check again and the port level
2190 * reset macro can be called directly. */
2191 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2195 mtCOVERAGE_TEST_MARKER();
2198 prvAddTaskToReadyList( pxTCB );
2202 /* It's possible that xYieldForTask was already set to pdTRUE because
2203 * its priority is being raised. However, since it is not in a ready list
2204 * we don't actually need to yield for it. */
2205 xYieldForTask = pdFALSE;
2208 #if ( configUSE_PREEMPTION == 1 )
2209 if( xYieldRequired != pdFALSE )
2211 prvYieldCore( xCoreID );
2213 else if( xYieldForTask != pdFALSE )
2215 prvYieldForTask( pxTCB, pdTRUE );
2219 mtCOVERAGE_TEST_MARKER();
2221 #endif /* if ( configUSE_PREEMPTION == 1 ) */
2223 /* Remove compiler warning about unused variables when the port
2224 * optimised task selection is not being used. */
2225 ( void ) uxPriorityUsedOnEntry;
2228 taskEXIT_CRITICAL();
2231 #endif /* INCLUDE_vTaskPrioritySet */
2232 /*-----------------------------------------------------------*/
2234 #if ( configNUM_CORES > 1 )
2235 #if ( configUSE_CORE_AFFINITY == 1 )
2237 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2238 UBaseType_t uxCoreAffinityMask )
2243 taskENTER_CRITICAL();
2245 pxTCB = prvGetTCBFromHandle( xTask );
2247 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2249 if( xSchedulerRunning != pdFALSE )
2251 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2253 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2255 if( ( uxCoreAffinityMask & ( 1 << xCoreID ) ) != 0 )
2257 prvYieldCore( xCoreID );
2262 taskEXIT_CRITICAL();
2265 #endif /* configUSE_CORE_AFFINITY */
2267 /*-----------------------------------------------------------*/
2269 #if ( configNUM_CORES > 1 )
2270 #if ( configUSE_CORE_AFFINITY == 1 )
2272 UBaseType_t vTaskCoreAffinityGet( const TaskHandle_t xTask )
2275 UBaseType_t uxCoreAffinityMask;
2277 taskENTER_CRITICAL();
2279 pxTCB = prvGetTCBFromHandle( xTask );
2280 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2282 taskEXIT_CRITICAL();
2284 return uxCoreAffinityMask;
2287 #endif /* configUSE_CORE_AFFINITY */
2290 /*-----------------------------------------------------------*/
2292 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2294 void vTaskPreemptionDisable( const TaskHandle_t xTask )
2298 taskENTER_CRITICAL();
2300 pxTCB = prvGetTCBFromHandle( xTask );
2302 pxTCB->xPreemptionDisable = pdTRUE;
2304 taskEXIT_CRITICAL();
2307 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2308 /*-----------------------------------------------------------*/
2310 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2312 void vTaskPreemptionEnable( const TaskHandle_t xTask )
2317 taskENTER_CRITICAL();
2319 pxTCB = prvGetTCBFromHandle( xTask );
2321 pxTCB->xPreemptionDisable = pdFALSE;
2323 if( xSchedulerRunning != pdFALSE )
2325 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2327 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2328 prvYieldCore( xCoreID );
2332 taskEXIT_CRITICAL();
2335 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2336 /*-----------------------------------------------------------*/
2338 #if ( INCLUDE_vTaskSuspend == 1 )
2340 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2343 TaskRunning_t xTaskRunningOnCore;
2345 taskENTER_CRITICAL();
2347 /* If null is passed in here then it is the running task that is
2348 * being suspended. */
2349 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2351 traceTASK_SUSPEND( pxTCB );
2353 xTaskRunningOnCore = pxTCB->xTaskRunState;
2355 /* Remove task from the ready/delayed list and place in the
2356 * suspended list. */
2357 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2359 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2363 mtCOVERAGE_TEST_MARKER();
2366 /* Is the task waiting on an event also? */
2367 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2369 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2373 mtCOVERAGE_TEST_MARKER();
2376 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2378 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2382 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2384 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2386 /* The task was blocked to wait for a notification, but is
2387 * now suspended, so no notification was received. */
2388 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2392 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2394 if( xSchedulerRunning != pdFALSE )
2396 /* Reset the next expected unblock time in case it referred to the
2397 * task that is now in the Suspended state. */
2398 prvResetNextTaskUnblockTime();
2402 mtCOVERAGE_TEST_MARKER();
2405 if( taskTASK_IS_RUNNING( xTaskRunningOnCore ) )
2407 if( xSchedulerRunning != pdFALSE )
2409 if( xTaskRunningOnCore == portGET_CORE_ID() )
2411 /* The current task has just been suspended. */
2412 configASSERT( uxSchedulerSuspended == 0 );
2413 vTaskYieldWithinAPI();
2417 prvYieldCore( xTaskRunningOnCore );
2420 taskEXIT_CRITICAL();
2424 taskEXIT_CRITICAL();
2426 configASSERT( pxTCB == pxCurrentTCBs[ xTaskRunningOnCore ] );
2428 /* The scheduler is not running, but the task that was pointed
2429 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
2430 * must be adjusted to point to a different task. */
2431 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
2433 /* No other tasks are ready, so set the core's TCB back to
2434 * NULL so when the next task is created the core's TCB will
2435 * be able to be set to point to it no matter what its relative
2437 pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2438 pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2442 /* Attempt to switch in a new task. This could fail since the idle tasks
2443 * haven't been created yet. If it does then set the core's TCB back to
2445 if( prvSelectHighestPriorityTask( xTaskRunningOnCore ) == pdFALSE )
2447 pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2448 pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2455 taskEXIT_CRITICAL();
2457 } /* taskEXIT_CRITICAL() - already exited in one of three cases above */
2460 #endif /* INCLUDE_vTaskSuspend */
2461 /*-----------------------------------------------------------*/
2463 #if ( INCLUDE_vTaskSuspend == 1 )
2465 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
2467 BaseType_t xReturn = pdFALSE;
2468 const TCB_t * const pxTCB = xTask;
2470 /* Accesses xPendingReadyList so must be called from a critical section. */
2472 /* It does not make sense to check if the calling task is suspended. */
2473 configASSERT( xTask );
2475 /* Is the task being resumed actually in the suspended list? */
2476 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
2478 /* Has the task already been resumed from within an ISR? */
2479 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
2481 /* Is it in the suspended list because it is in the Suspended
2482 * state, or because is is blocked with no timeout? */
2483 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
2489 mtCOVERAGE_TEST_MARKER();
2494 mtCOVERAGE_TEST_MARKER();
2499 mtCOVERAGE_TEST_MARKER();
2503 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2505 #endif /* INCLUDE_vTaskSuspend */
2506 /*-----------------------------------------------------------*/
2508 #if ( INCLUDE_vTaskSuspend == 1 )
2510 void vTaskResume( TaskHandle_t xTaskToResume )
2512 TCB_t * const pxTCB = xTaskToResume;
2514 /* It does not make sense to resume the calling task. */
2515 configASSERT( xTaskToResume );
2517 /* The parameter cannot be NULL as it is impossible to resume the
2518 * currently executing task. It is also impossible to resume a task
2519 * that is actively running on another core but it is too dangerous
2520 * to check their run state here. Safer to get into a critical section
2521 * and check if it is actually suspended or not below. */
2524 taskENTER_CRITICAL();
2526 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2528 traceTASK_RESUME( pxTCB );
2530 /* The ready list can be accessed even if the scheduler is
2531 * suspended because this is inside a critical section. */
2532 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2533 prvAddTaskToReadyList( pxTCB );
2535 /* A higher priority task may have just been resumed. */
2536 #if ( configUSE_PREEMPTION == 1 )
2538 prvYieldForTask( pxTCB, pdTRUE );
2544 mtCOVERAGE_TEST_MARKER();
2547 taskEXIT_CRITICAL();
2551 mtCOVERAGE_TEST_MARKER();
2555 #endif /* INCLUDE_vTaskSuspend */
2557 /*-----------------------------------------------------------*/
2559 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
2561 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
2563 BaseType_t xYieldRequired = pdFALSE;
2564 TCB_t * const pxTCB = xTaskToResume;
2565 UBaseType_t uxSavedInterruptStatus;
2567 configASSERT( xTaskToResume );
2569 /* RTOS ports that support interrupt nesting have the concept of a
2570 * maximum system call (or maximum API call) interrupt priority.
2571 * Interrupts that are above the maximum system call priority are keep
2572 * permanently enabled, even when the RTOS kernel is in a critical section,
2573 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2574 * is defined in FreeRTOSConfig.h then
2575 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2576 * failure if a FreeRTOS API function is called from an interrupt that has
2577 * been assigned a priority above the configured maximum system call
2578 * priority. Only FreeRTOS functions that end in FromISR can be called
2579 * from interrupts that have been assigned a priority at or (logically)
2580 * below the maximum system call interrupt priority. FreeRTOS maintains a
2581 * separate interrupt safe API to ensure interrupt entry is as fast and as
2582 * simple as possible. More information (albeit Cortex-M specific) is
2583 * provided on the following link:
2584 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2585 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2587 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2589 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2591 traceTASK_RESUME_FROM_ISR( pxTCB );
2593 /* Check the ready lists can be accessed. */
2594 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2596 /* Ready lists can be accessed so move the task from the
2597 * suspended list to the ready list directly. */
2599 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2600 prvAddTaskToReadyList( pxTCB );
2604 /* The delayed or ready lists cannot be accessed so the task
2605 * is held in the pending ready list until the scheduler is
2607 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
2610 #if ( configUSE_PREEMPTION == 1 )
2611 prvYieldForTask( pxTCB, pdTRUE );
2613 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
2615 xYieldRequired = pdTRUE;
2621 mtCOVERAGE_TEST_MARKER();
2624 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2626 return xYieldRequired;
2629 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
2630 /*-----------------------------------------------------------*/
2632 static BaseType_t prvCreateIdleTasks( void )
2634 BaseType_t xReturn = pdPASS;
2636 char cIdleName[ configMAX_TASK_NAME_LEN ];
2638 /* Add each idle task at the lowest priority. */
2639 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUM_CORES; xCoreID++ )
2643 if( xReturn == pdFAIL )
2649 mtCOVERAGE_TEST_MARKER();
2652 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
2654 cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
2656 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
2657 * configMAX_TASK_NAME_LEN characters just in case the memory after the
2658 * string is not accessible (extremely unlikely). */
2659 if( cIdleName[ x ] == ( char ) 0x00 )
2665 mtCOVERAGE_TEST_MARKER();
2669 /* Append the idle task number to the end of the name if there is space */
2670 if( x < configMAX_TASK_NAME_LEN )
2672 cIdleName[ x++ ] = xCoreID + '0';
2674 /* And append a null character if there is space */
2675 if( x < configMAX_TASK_NAME_LEN )
2677 cIdleName[ x ] = '\0';
2681 mtCOVERAGE_TEST_MARKER();
2686 mtCOVERAGE_TEST_MARKER();
2689 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
2693 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
2694 StackType_t * pxIdleTaskStackBuffer = NULL;
2695 uint32_t ulIdleTaskStackSize;
2697 /* The Idle task is created using user provided RAM - obtain the
2698 * address of the RAM then create the idle task. */
2699 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
2700 xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
2702 ulIdleTaskStackSize,
2703 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
2704 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2705 pxIdleTaskStackBuffer,
2706 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2708 #if( configNUM_CORES > 1)
2711 static StaticTask_t xIdleTCBBuffers[configNUM_CORES-1];
2712 static StackType_t xIdleTaskStackBuffers[configNUM_CORES-1][configMINIMAL_STACK_SIZE];
2714 xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvMinimalIdleTask,
2716 configMINIMAL_STACK_SIZE,
2717 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
2718 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2719 xIdleTaskStackBuffers[xCoreID-1],
2720 &xIdleTCBBuffers[xCoreID-1] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2723 if( xIdleTaskHandle[ xCoreID ] != NULL )
2732 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
2736 /* The Idle task is being created using dynamically allocated RAM. */
2737 xReturn = xTaskCreate( prvIdleTask,
2739 configMINIMAL_STACK_SIZE,
2741 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2742 &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2744 #if( configNUM_CORES > 1 )
2747 xReturn = xTaskCreate( prvMinimalIdleTask,
2749 configMINIMAL_STACK_SIZE,
2751 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2752 &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2756 #endif /* configSUPPORT_STATIC_ALLOCATION */
2761 void vTaskStartScheduler( void )
2765 #if ( configUSE_TIMERS == 1 )
2767 xReturn = xTimerCreateTimerTask();
2769 #endif /* configUSE_TIMERS */
2771 xReturn = prvCreateIdleTasks();
2773 if( xReturn == pdPASS )
2775 /* freertos_tasks_c_additions_init() should only be called if the user
2776 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
2777 * the only macro called by the function. */
2778 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
2780 freertos_tasks_c_additions_init();
2784 /* Interrupts are turned off here, to ensure a tick does not occur
2785 * before or during the call to xPortStartScheduler(). The stacks of
2786 * the created tasks contain a status word with interrupts switched on
2787 * so interrupts will automatically get re-enabled when the first task
2789 portDISABLE_INTERRUPTS();
2791 #if ( configUSE_NEWLIB_REENTRANT == 1 )
2793 /* Switch Newlib's _impure_ptr variable to point to the _reent
2794 * structure specific to the task that will run first.
2795 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
2796 * for additional information. */
2797 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
2799 #endif /* configUSE_NEWLIB_REENTRANT */
2801 xNextTaskUnblockTime = portMAX_DELAY;
2802 xSchedulerRunning = pdTRUE;
2803 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
2805 /* If configGENERATE_RUN_TIME_STATS is defined then the following
2806 * macro must be defined to configure the timer/counter used to generate
2807 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
2808 * is set to 0 and the following line fails to build then ensure you do not
2809 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
2810 * FreeRTOSConfig.h file. */
2811 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
2813 traceTASK_SWITCHED_IN();
2815 /* Setting up the timer tick is hardware specific and thus in the
2816 * portable interface. */
2817 if( xPortStartScheduler() != pdFALSE )
2819 /* Should not reach here as if the scheduler is running the
2820 * function will not return. */
2824 /* Should only reach here if a task calls xTaskEndScheduler(). */
2829 /* This line will only be reached if the kernel could not be started,
2830 * because there was not enough FreeRTOS heap to create the idle task
2831 * or the timer task. */
2832 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
2835 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
2836 * meaning xIdleTaskHandle is not used anywhere else. */
2837 ( void ) xIdleTaskHandle;
2839 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
2840 * from getting optimized out as it is no longer used by the kernel. */
2841 ( void ) uxTopUsedPriority;
2843 /*-----------------------------------------------------------*/
2845 void vTaskEndScheduler( void )
2847 /* Stop the scheduler interrupts and call the portable scheduler end
2848 * routine so the original ISRs can be restored if necessary. The port
2849 * layer must ensure interrupts enable bit is left in the correct state. */
2850 portDISABLE_INTERRUPTS();
2851 xSchedulerRunning = pdFALSE;
2852 vPortEndScheduler();
2854 /*----------------------------------------------------------*/
2856 void vTaskSuspendAll( void )
2858 UBaseType_t ulState;
2860 /* This must only be called from within a task */
2861 portASSERT_IF_IN_ISR();
2863 if( xSchedulerRunning != pdFALSE )
2865 /* writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
2866 * We must disable interrupts before we grab the locks in the event that this task is
2867 * interrupted and switches context before incrementing uxSchedulerSuspended.
2868 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
2869 * uxSchedulerSuspended since that will prevent context switches. */
2870 ulState = portDISABLE_INTERRUPTS();
2872 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
2873 * do not otherwise exhibit real time behaviour. */
2874 portSOFTWARE_BARRIER();
2876 portGET_TASK_LOCK();
2879 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
2880 * is used to allow calls to vTaskSuspendAll() to nest. */
2881 ++uxSchedulerSuspended;
2882 portRELEASE_ISR_LOCK();
2884 if( ( uxSchedulerSuspended == 1U ) && ( pxCurrentTCB->uxCriticalNesting == 0U ) )
2886 prvCheckForRunStateChange();
2889 portRESTORE_INTERRUPTS( ulState );
2893 mtCOVERAGE_TEST_MARKER();
2896 /*----------------------------------------------------------*/
2898 #if ( configUSE_TICKLESS_IDLE != 0 )
2900 static TickType_t prvGetExpectedIdleTime( void )
2903 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
2905 /* uxHigherPriorityReadyTasks takes care of the case where
2906 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
2907 * task that are in the Ready state, even though the idle task is
2909 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
2911 if( uxTopReadyPriority > tskIDLE_PRIORITY )
2913 uxHigherPriorityReadyTasks = pdTRUE;
2918 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
2920 /* When port optimised task selection is used the uxTopReadyPriority
2921 * variable is used as a bit map. If bits other than the least
2922 * significant bit are set then there are tasks that have a priority
2923 * above the idle priority that are in the Ready state. This takes
2924 * care of the case where the co-operative scheduler is in use. */
2925 if( uxTopReadyPriority > uxLeastSignificantBit )
2927 uxHigherPriorityReadyTasks = pdTRUE;
2930 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
2932 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
2936 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
2938 /* There are other idle priority tasks in the ready state. If
2939 * time slicing is used then the very next tick interrupt must be
2943 else if( uxHigherPriorityReadyTasks != pdFALSE )
2945 /* There are tasks in the Ready state that have a priority above the
2946 * idle priority. This path can only be reached if
2947 * configUSE_PREEMPTION is 0. */
2952 xReturn = xNextTaskUnblockTime - xTickCount;
2958 #endif /* configUSE_TICKLESS_IDLE */
2959 /*----------------------------------------------------------*/
2961 BaseType_t xTaskResumeAll( void )
2963 TCB_t * pxTCB = NULL;
2964 BaseType_t xAlreadyYielded = pdFALSE;
2966 if( xSchedulerRunning != pdFALSE )
2968 /* It is possible that an ISR caused a task to be removed from an event
2969 * list while the scheduler was suspended. If this was the case then the
2970 * removed task will have been added to the xPendingReadyList. Once the
2971 * scheduler has been resumed it is safe to move all the pending ready
2972 * tasks from this list into their appropriate ready list. */
2973 taskENTER_CRITICAL();
2977 xCoreID = portGET_CORE_ID();
2979 /* If uxSchedulerSuspended is zero then this function does not match a
2980 * previous call to vTaskSuspendAll(). */
2981 configASSERT( uxSchedulerSuspended );
2983 --uxSchedulerSuspended;
2984 portRELEASE_TASK_LOCK();
2986 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2988 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
2990 /* Move any readied tasks from the pending list into the
2991 * appropriate ready list. */
2992 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
2994 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. */
2995 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2996 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2997 prvAddTaskToReadyList( pxTCB );
2999 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
3000 * If the current core yielded then vTaskSwitchContext() has already been called
3001 * which sets xYieldPendings for the current core to pdTRUE. */
3006 /* A task was unblocked while the scheduler was suspended,
3007 * which may have prevented the next unblock time from being
3008 * re-calculated, in which case re-calculate it now. Mainly
3009 * important for low power tickless implementations, where
3010 * this can prevent an unnecessary exit from low power
3012 prvResetNextTaskUnblockTime();
3015 /* If any ticks occurred while the scheduler was suspended then
3016 * they should be processed now. This ensures the tick count does
3017 * not slip, and that any delayed tasks are resumed at the correct
3020 * It should be safe to call xTaskIncrementTick here from any core
3021 * since we are in a critical section and xTaskIncrementTick itself
3022 * protects itself within a critical section. Suspending the scheduler
3023 * from any core causes xTaskIncrementTick to increment uxPendedCounts.*/
3025 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3027 if( xPendedCounts > ( TickType_t ) 0U )
3031 if( xTaskIncrementTick() != pdFALSE )
3033 /* other cores are interrupted from
3034 * within xTaskIncrementTick(). */
3035 xYieldPendings[ xCoreID ] = pdTRUE;
3039 mtCOVERAGE_TEST_MARKER();
3043 } while( xPendedCounts > ( TickType_t ) 0U );
3049 mtCOVERAGE_TEST_MARKER();
3053 if( xYieldPendings[ xCoreID ] != pdFALSE )
3055 /* If xYieldPendings is true then taskEXIT_CRITICAL()
3056 * will yield, so make sure we return true to let the
3057 * caller know a yield has already happened. */
3058 xAlreadyYielded = pdTRUE;
3064 mtCOVERAGE_TEST_MARKER();
3067 taskEXIT_CRITICAL();
3071 mtCOVERAGE_TEST_MARKER();
3074 return xAlreadyYielded;
3076 /*-----------------------------------------------------------*/
3078 TickType_t xTaskGetTickCount( void )
3082 /* Critical section required if running on a 16 bit processor. */
3083 portTICK_TYPE_ENTER_CRITICAL();
3085 xTicks = xTickCount;
3087 portTICK_TYPE_EXIT_CRITICAL();
3091 /*-----------------------------------------------------------*/
3093 TickType_t xTaskGetTickCountFromISR( void )
3096 UBaseType_t uxSavedInterruptStatus;
3098 /* RTOS ports that support interrupt nesting have the concept of a maximum
3099 * system call (or maximum API call) interrupt priority. Interrupts that are
3100 * above the maximum system call priority are kept permanently enabled, even
3101 * when the RTOS kernel is in a critical section, but cannot make any calls to
3102 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
3103 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3104 * failure if a FreeRTOS API function is called from an interrupt that has been
3105 * assigned a priority above the configured maximum system call priority.
3106 * Only FreeRTOS functions that end in FromISR can be called from interrupts
3107 * that have been assigned a priority at or (logically) below the maximum
3108 * system call interrupt priority. FreeRTOS maintains a separate interrupt
3109 * safe API to ensure interrupt entry is as fast and as simple as possible.
3110 * More information (albeit Cortex-M specific) is provided on the following
3111 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3112 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3114 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
3116 xReturn = xTickCount;
3118 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3122 /*-----------------------------------------------------------*/
3124 UBaseType_t uxTaskGetNumberOfTasks( void )
3126 /* A critical section is not required because the variables are of type
3128 return uxCurrentNumberOfTasks;
3130 /*-----------------------------------------------------------*/
3132 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3136 /* If null is passed in here then the name of the calling task is being
3138 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3139 configASSERT( pxTCB );
3140 return &( pxTCB->pcTaskName[ 0 ] );
3142 /*-----------------------------------------------------------*/
3144 #if ( INCLUDE_xTaskGetHandle == 1 )
3146 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3147 const char pcNameToQuery[] )
3149 TCB_t * pxNextTCB, * pxFirstTCB, * pxReturn = NULL;
3152 BaseType_t xBreakLoop;
3154 /* This function is called with the scheduler suspended. */
3156 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3158 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. */
3162 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. */
3164 /* Check each character in the name looking for a match or
3166 xBreakLoop = pdFALSE;
3168 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3170 cNextChar = pxNextTCB->pcTaskName[ x ];
3172 if( cNextChar != pcNameToQuery[ x ] )
3174 /* Characters didn't match. */
3175 xBreakLoop = pdTRUE;
3177 else if( cNextChar == ( char ) 0x00 )
3179 /* Both strings terminated, a match must have been
3181 pxReturn = pxNextTCB;
3182 xBreakLoop = pdTRUE;
3186 mtCOVERAGE_TEST_MARKER();
3189 if( xBreakLoop != pdFALSE )
3195 if( pxReturn != NULL )
3197 /* The handle has been found. */
3200 } while( pxNextTCB != pxFirstTCB );
3204 mtCOVERAGE_TEST_MARKER();
3210 #endif /* INCLUDE_xTaskGetHandle */
3211 /*-----------------------------------------------------------*/
3213 #if ( INCLUDE_xTaskGetHandle == 1 )
3215 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3217 UBaseType_t uxQueue = configMAX_PRIORITIES;
3220 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
3221 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
3225 /* Search the ready lists. */
3229 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
3233 /* Found the handle. */
3236 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3238 /* Search the delayed lists. */
3241 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
3246 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
3249 #if ( INCLUDE_vTaskSuspend == 1 )
3253 /* Search the suspended list. */
3254 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
3259 #if ( INCLUDE_vTaskDelete == 1 )
3263 /* Search the deleted list. */
3264 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
3269 ( void ) xTaskResumeAll();
3274 #endif /* INCLUDE_xTaskGetHandle */
3275 /*-----------------------------------------------------------*/
3277 #if ( configUSE_TRACE_FACILITY == 1 )
3279 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
3280 const UBaseType_t uxArraySize,
3281 uint32_t * const pulTotalRunTime )
3283 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
3287 /* Is there a space in the array for each task in the system? */
3288 if( uxArraySize >= uxCurrentNumberOfTasks )
3290 /* Fill in an TaskStatus_t structure with information on each
3291 * task in the Ready state. */
3295 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
3296 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3298 /* Fill in an TaskStatus_t structure with information on each
3299 * task in the Blocked state. */
3300 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
3301 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
3303 #if ( INCLUDE_vTaskDelete == 1 )
3305 /* Fill in an TaskStatus_t structure with information on
3306 * each task that has been deleted but not yet cleaned up. */
3307 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
3311 #if ( INCLUDE_vTaskSuspend == 1 )
3313 /* Fill in an TaskStatus_t structure with information on
3314 * each task in the Suspended state. */
3315 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
3319 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3321 if( pulTotalRunTime != NULL )
3323 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3324 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
3326 *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3330 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3332 if( pulTotalRunTime != NULL )
3334 *pulTotalRunTime = 0;
3337 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3341 mtCOVERAGE_TEST_MARKER();
3344 ( void ) xTaskResumeAll();
3349 #endif /* configUSE_TRACE_FACILITY */
3350 /*----------------------------------------------------------*/
3352 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
3354 TaskHandle_t * xTaskGetIdleTaskHandle( void )
3356 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
3357 * started, then xIdleTaskHandle will be NULL. */
3358 configASSERT( ( xIdleTaskHandle != NULL ) );
3359 return &( xIdleTaskHandle[ 0 ] );
3362 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
3363 /*----------------------------------------------------------*/
3365 /* This conditional compilation should use inequality to 0, not equality to 1.
3366 * This is to ensure vTaskStepTick() is available when user defined low power mode
3367 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
3369 #if ( configUSE_TICKLESS_IDLE != 0 )
3371 void vTaskStepTick( const TickType_t xTicksToJump )
3373 /* Correct the tick count value after a period during which the tick
3374 * was suppressed. Note this does *not* call the tick hook function for
3375 * each stepped tick. */
3376 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
3377 xTickCount += xTicksToJump;
3378 traceINCREASE_TICK_COUNT( xTicksToJump );
3381 #endif /* configUSE_TICKLESS_IDLE */
3382 /*----------------------------------------------------------*/
3384 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
3386 BaseType_t xYieldOccurred;
3388 /* Must not be called with the scheduler suspended as the implementation
3389 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
3390 configASSERT( uxSchedulerSuspended == 0 );
3392 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
3393 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
3395 xPendedTicks += xTicksToCatchUp;
3396 xYieldOccurred = xTaskResumeAll();
3398 return xYieldOccurred;
3400 /*----------------------------------------------------------*/
3402 #if ( INCLUDE_xTaskAbortDelay == 1 )
3404 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
3406 TCB_t * pxTCB = xTask;
3409 configASSERT( pxTCB );
3413 /* A task can only be prematurely removed from the Blocked state if
3414 * it is actually in the Blocked state. */
3415 if( eTaskGetState( xTask ) == eBlocked )
3419 /* Remove the reference to the task from the blocked list. An
3420 * interrupt won't touch the xStateListItem because the
3421 * scheduler is suspended. */
3422 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3424 /* Is the task waiting on an event also? If so remove it from
3425 * the event list too. Interrupts can touch the event list item,
3426 * even though the scheduler is suspended, so a critical section
3428 taskENTER_CRITICAL();
3430 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3432 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3434 /* This lets the task know it was forcibly removed from the
3435 * blocked state so it should not re-evaluate its block time and
3436 * then block again. */
3437 pxTCB->ucDelayAborted = pdTRUE;
3441 mtCOVERAGE_TEST_MARKER();
3444 taskEXIT_CRITICAL();
3446 /* Place the unblocked task into the appropriate ready list. */
3447 prvAddTaskToReadyList( pxTCB );
3449 /* A task being unblocked cannot cause an immediate context
3450 * switch if preemption is turned off. */
3451 #if ( configUSE_PREEMPTION == 1 )
3453 taskENTER_CRITICAL();
3455 prvYieldForTask( pxTCB, pdFALSE );
3457 taskEXIT_CRITICAL();
3459 #endif /* configUSE_PREEMPTION */
3466 ( void ) xTaskResumeAll();
3471 #endif /* INCLUDE_xTaskAbortDelay */
3472 /*----------------------------------------------------------*/
3474 BaseType_t xTaskIncrementTick( void )
3477 TickType_t xItemValue;
3478 BaseType_t xSwitchRequired = pdFALSE;
3480 #if ( configUSE_PREEMPTION == 1 )
3482 BaseType_t xCoreYieldList[ configNUM_CORES ] = { pdFALSE };
3483 #endif /* configUSE_PREEMPTION */
3485 taskENTER_CRITICAL();
3487 /* Called by the portable layer each time a tick interrupt occurs.
3488 * Increments the tick then checks to see if the new tick value will cause any
3489 * tasks to be unblocked. */
3490 traceTASK_INCREMENT_TICK( xTickCount );
3492 /* Tick increment should occur on every kernel timer event. Core 0 has the
3493 * responsibility to increment the tick, or increment the pended ticks if the
3494 * scheduler is suspended. If pended ticks is greater than zero, the core that
3495 * calls xTaskResumeAll has the responsibility to increment the tick. */
3496 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3498 /* Minor optimisation. The tick count cannot change in this
3500 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
3502 /* Increment the RTOS tick, switching the delayed and overflowed
3503 * delayed lists if it wraps to 0. */
3504 xTickCount = xConstTickCount;
3506 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
3508 taskSWITCH_DELAYED_LISTS();
3512 mtCOVERAGE_TEST_MARKER();
3515 /* See if this tick has made a timeout expire. Tasks are stored in
3516 * the queue in the order of their wake time - meaning once one task
3517 * has been found whose block time has not expired there is no need to
3518 * look any further down the list. */
3519 if( xConstTickCount >= xNextTaskUnblockTime )
3523 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
3525 /* The delayed list is empty. Set xNextTaskUnblockTime
3526 * to the maximum possible value so it is extremely
3528 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
3529 * next time through. */
3530 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3535 /* The delayed list is not empty, get the value of the
3536 * item at the head of the delayed list. This is the time
3537 * at which the task at the head of the delayed list must
3538 * be removed from the Blocked state. */
3539 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. */
3540 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
3542 if( xConstTickCount < xItemValue )
3544 /* It is not time to unblock this item yet, but the
3545 * item value is the time at which the task at the head
3546 * of the blocked list must be removed from the Blocked
3547 * state - so record the item value in
3548 * xNextTaskUnblockTime. */
3549 xNextTaskUnblockTime = xItemValue;
3550 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
3554 mtCOVERAGE_TEST_MARKER();
3557 /* It is time to remove the item from the Blocked state. */
3558 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3560 /* Is the task waiting on an event also? If so remove
3561 * it from the event list. */
3562 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3564 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3568 mtCOVERAGE_TEST_MARKER();
3571 /* Place the unblocked task into the appropriate ready
3573 prvAddTaskToReadyList( pxTCB );
3575 /* A task being unblocked cannot cause an immediate
3576 * context switch if preemption is turned off. */
3577 #if ( configUSE_PREEMPTION == 1 )
3579 prvYieldForTask( pxTCB, pdTRUE );
3581 #endif /* configUSE_PREEMPTION */
3586 /* Tasks of equal priority to the currently running task will share
3587 * processing time (time slice) if preemption is on, and the application
3588 * writer has not explicitly turned time slicing off. */
3589 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
3591 /* TODO: If there are fewer "non-IDLE" READY tasks than cores, do not
3592 * force a context switch that would just shuffle tasks around cores */
3593 /* TODO: There are certainly better ways of doing this that would reduce
3594 * the number of interrupts and also potentially help prevent tasks from
3595 * moving between cores as often. This, however, works for now. */
3596 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3598 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ x ]->uxPriority ] ) ) > ( UBaseType_t ) 1 )
3600 xCoreYieldList[ x ] = pdTRUE;
3604 mtCOVERAGE_TEST_MARKER();
3608 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
3610 #if ( configUSE_TICK_HOOK == 1 )
3612 /* Guard against the tick hook being called when the pended tick
3613 * count is being unwound (when the scheduler is being unlocked). */
3614 if( xPendedTicks == ( TickType_t ) 0 )
3616 vApplicationTickHook();
3620 mtCOVERAGE_TEST_MARKER();
3623 #endif /* configUSE_TICK_HOOK */
3625 #if ( configUSE_PREEMPTION == 1 )
3627 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3629 if( xYieldPendings[ x ] != pdFALSE )
3631 xCoreYieldList[ x ] = pdTRUE;
3635 mtCOVERAGE_TEST_MARKER();
3639 #endif /* configUSE_PREEMPTION */
3641 #if ( configUSE_PREEMPTION == 1 )
3645 xCoreID = portGET_CORE_ID();
3647 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3649 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3650 if( pxCurrentTCBs[ x ]->xPreemptionDisable == pdFALSE )
3653 if( xCoreYieldList[ x ] != pdFALSE )
3657 xSwitchRequired = pdTRUE;
3666 mtCOVERAGE_TEST_MARKER();
3671 #endif /* configUSE_PREEMPTION */
3677 /* The tick hook gets called at regular intervals, even if the
3678 * scheduler is locked. */
3679 #if ( configUSE_TICK_HOOK == 1 )
3681 vApplicationTickHook();
3686 taskEXIT_CRITICAL();
3688 return xSwitchRequired;
3690 /*-----------------------------------------------------------*/
3692 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3694 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
3695 TaskHookFunction_t pxHookFunction )
3699 /* If xTask is NULL then it is the task hook of the calling task that is
3703 xTCB = ( TCB_t * ) pxCurrentTCB;
3710 /* Save the hook function in the TCB. A critical section is required as
3711 * the value can be accessed from an interrupt. */
3712 taskENTER_CRITICAL();
3714 xTCB->pxTaskTag = pxHookFunction;
3716 taskEXIT_CRITICAL();
3719 #endif /* configUSE_APPLICATION_TASK_TAG */
3720 /*-----------------------------------------------------------*/
3722 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3724 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
3727 TaskHookFunction_t xReturn;
3729 /* If xTask is NULL then set the calling task's hook. */
3730 pxTCB = prvGetTCBFromHandle( xTask );
3732 /* Save the hook function in the TCB. A critical section is required as
3733 * the value can be accessed from an interrupt. */
3734 taskENTER_CRITICAL();
3736 xReturn = pxTCB->pxTaskTag;
3738 taskEXIT_CRITICAL();
3743 #endif /* configUSE_APPLICATION_TASK_TAG */
3744 /*-----------------------------------------------------------*/
3746 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3748 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
3751 TaskHookFunction_t xReturn;
3752 UBaseType_t uxSavedInterruptStatus;
3754 /* If xTask is NULL then set the calling task's hook. */
3755 pxTCB = prvGetTCBFromHandle( xTask );
3757 /* Save the hook function in the TCB. A critical section is required as
3758 * the value can be accessed from an interrupt. */
3759 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
3761 xReturn = pxTCB->pxTaskTag;
3763 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3768 #endif /* configUSE_APPLICATION_TASK_TAG */
3769 /*-----------------------------------------------------------*/
3771 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3773 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
3774 void * pvParameter )
3779 /* If xTask is NULL then we are calling our own task hook. */
3782 xTCB = pxCurrentTCB;
3789 if( xTCB->pxTaskTag != NULL )
3791 xReturn = xTCB->pxTaskTag( pvParameter );
3801 #endif /* configUSE_APPLICATION_TASK_TAG */
3802 /*-----------------------------------------------------------*/
3804 void vTaskSwitchContext( BaseType_t xCoreID )
3806 /* Acquire both locks:
3807 * - The ISR lock protects the ready list from simultaneous access by
3808 * both other ISRs and tasks.
3809 * - We also take the task lock to pause here in case another core has
3810 * suspended the scheduler. We don't want to simply set xYieldPending
3811 * and move on if another core suspended the scheduler. We should only
3812 * do that if the current core has suspended the scheduler. */
3814 portGET_TASK_LOCK(); /* Must always acquire the task lock first */
3817 /* vTaskSwitchContext() must never be called from within a critical section.
3818 * This is not necessarily true for vanilla FreeRTOS, but it is for this SMP port. */
3819 configASSERT( pxCurrentTCB->uxCriticalNesting == 0 );
3821 if( uxSchedulerSuspended != ( UBaseType_t ) pdFALSE )
3823 /* The scheduler is currently suspended - do not allow a context
3825 xYieldPendings[ xCoreID ] = pdTRUE;
3829 xYieldPendings[ xCoreID ] = pdFALSE;
3830 traceTASK_SWITCHED_OUT();
3832 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3834 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3835 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
3837 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3840 /* Add the amount of time the task has been running to the
3841 * accumulated time so far. The time the task started running was
3842 * stored in ulTaskSwitchedInTime. Note that there is no overflow
3843 * protection here so count values are only valid until the timer
3844 * overflows. The guard against negative values is to protect
3845 * against suspect run time stat counter implementations - which
3846 * are provided by the application, not the kernel. */
3847 if( ulTotalRunTime > ulTaskSwitchedInTime )
3849 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime );
3853 mtCOVERAGE_TEST_MARKER();
3856 ulTaskSwitchedInTime = ulTotalRunTime;
3858 #endif /* configGENERATE_RUN_TIME_STATS */
3860 /* Check for stack overflow, if configured. */
3861 taskCHECK_FOR_STACK_OVERFLOW();
3863 /* Before the currently running task is switched out, save its errno. */
3864 #if ( configUSE_POSIX_ERRNO == 1 )
3866 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
3870 /* Select a new task to run using either the generic C or port
3871 * optimised asm code. */
3872 ( void ) prvSelectHighestPriorityTask( xCoreID );
3873 traceTASK_SWITCHED_IN();
3875 /* After the new task is switched in, update the global errno. */
3876 #if ( configUSE_POSIX_ERRNO == 1 )
3878 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
3882 #if ( configUSE_NEWLIB_REENTRANT == 1 )
3884 /* Switch Newlib's _impure_ptr variable to point to the _reent
3885 * structure specific to this task.
3886 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
3887 * for additional information. */
3888 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
3890 #endif /* configUSE_NEWLIB_REENTRANT */
3893 portRELEASE_ISR_LOCK();
3894 portRELEASE_TASK_LOCK();
3896 /*-----------------------------------------------------------*/
3898 void vTaskPlaceOnEventList( List_t * const pxEventList,
3899 const TickType_t xTicksToWait )
3901 configASSERT( pxEventList );
3903 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
3904 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
3906 /* Place the event list item of the TCB in the appropriate event list.
3907 * This is placed in the list in priority order so the highest priority task
3908 * is the first to be woken by the event. The queue that contains the event
3909 * list is locked, preventing simultaneous access from interrupts. */
3910 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3912 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3914 /*-----------------------------------------------------------*/
3916 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
3917 const TickType_t xItemValue,
3918 const TickType_t xTicksToWait )
3920 configASSERT( pxEventList );
3922 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3923 * the event groups implementation. */
3924 configASSERT( uxSchedulerSuspended != 0 );
3926 /* Store the item value in the event list item. It is safe to access the
3927 * event list item here as interrupts won't access the event list item of a
3928 * task that is not in the Blocked state. */
3929 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
3931 /* Place the event list item of the TCB at the end of the appropriate event
3932 * list. It is safe to access the event list here because it is part of an
3933 * event group implementation - and interrupts don't access event groups
3934 * directly (instead they access them indirectly by pending function calls to
3935 * the task level). */
3936 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3938 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3940 /*-----------------------------------------------------------*/
3942 #if ( configUSE_TIMERS == 1 )
3944 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
3945 TickType_t xTicksToWait,
3946 const BaseType_t xWaitIndefinitely )
3948 configASSERT( pxEventList );
3950 /* This function should not be called by application code hence the
3951 * 'Restricted' in its name. It is not part of the public API. It is
3952 * designed for use by kernel code, and has special calling requirements -
3953 * it should be called with the scheduler suspended. */
3956 /* Place the event list item of the TCB in the appropriate event list.
3957 * In this case it is assume that this is the only task that is going to
3958 * be waiting on this event list, so the faster vListInsertEnd() function
3959 * can be used in place of vListInsert. */
3960 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3962 /* If the task should block indefinitely then set the block time to a
3963 * value that will be recognised as an indefinite delay inside the
3964 * prvAddCurrentTaskToDelayedList() function. */
3965 if( xWaitIndefinitely != pdFALSE )
3967 xTicksToWait = portMAX_DELAY;
3970 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
3971 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
3974 #endif /* configUSE_TIMERS */
3975 /*-----------------------------------------------------------*/
3977 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
3979 TCB_t * pxUnblockedTCB;
3982 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
3983 * called from a critical section within an ISR. */
3985 /* The event list is sorted in priority order, so the first in the list can
3986 * be removed as it is known to be the highest priority. Remove the TCB from
3987 * the delayed list, and add it to the ready list.
3989 * If an event is for a queue that is locked then this function will never
3990 * get called - the lock count on the queue will get modified instead. This
3991 * means exclusive access to the event list is guaranteed here.
3993 * This function assumes that a check has already been made to ensure that
3994 * pxEventList is not empty. */
3995 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. */
3996 configASSERT( pxUnblockedTCB );
3997 ( void ) uxListRemove( &( pxUnblockedTCB->xEventListItem ) );
3999 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4001 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
4002 prvAddTaskToReadyList( pxUnblockedTCB );
4004 #if ( configUSE_TICKLESS_IDLE != 0 )
4006 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4007 * might be set to the blocked task's time out time. If the task is
4008 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4009 * normally left unchanged, because it is automatically reset to a new
4010 * value when the tick count equals xNextTaskUnblockTime. However if
4011 * tickless idling is used it might be more important to enter sleep mode
4012 * at the earliest possible time - so reset xNextTaskUnblockTime here to
4013 * ensure it is updated at the earliest possible time. */
4014 prvResetNextTaskUnblockTime();
4020 /* The delayed and ready lists cannot be accessed, so hold this task
4021 * pending until the scheduler is resumed. */
4022 vListInsertEnd( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
4026 #if ( configUSE_PREEMPTION == 1 )
4027 prvYieldForTask( pxUnblockedTCB, pdFALSE );
4029 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
4037 /*-----------------------------------------------------------*/
4039 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
4040 const TickType_t xItemValue )
4042 TCB_t * pxUnblockedTCB;
4044 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
4045 * the event flags implementation. */
4046 configASSERT( uxSchedulerSuspended != pdFALSE );
4048 /* Store the new item value in the event list. */
4049 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4051 /* Remove the event list form the event flag. Interrupts do not access
4053 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. */
4054 configASSERT( pxUnblockedTCB );
4055 ( void ) uxListRemove( pxEventListItem );
4057 #if ( configUSE_TICKLESS_IDLE != 0 )
4059 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4060 * might be set to the blocked task's time out time. If the task is
4061 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4062 * normally left unchanged, because it is automatically reset to a new
4063 * value when the tick count equals xNextTaskUnblockTime. However if
4064 * tickless idling is used it might be more important to enter sleep mode
4065 * at the earliest possible time - so reset xNextTaskUnblockTime here to
4066 * ensure it is updated at the earliest possible time. */
4067 prvResetNextTaskUnblockTime();
4071 /* Remove the task from the delayed list and add it to the ready list. The
4072 * scheduler is suspended so interrupts will not be accessing the ready
4074 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
4075 prvAddTaskToReadyList( pxUnblockedTCB );
4077 #if ( configUSE_PREEMPTION == 1 )
4078 taskENTER_CRITICAL();
4080 prvYieldForTask( pxUnblockedTCB, pdFALSE );
4082 taskEXIT_CRITICAL();
4085 /*-----------------------------------------------------------*/
4087 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
4089 configASSERT( pxTimeOut );
4090 taskENTER_CRITICAL();
4092 pxTimeOut->xOverflowCount = xNumOfOverflows;
4093 pxTimeOut->xTimeOnEntering = xTickCount;
4095 taskEXIT_CRITICAL();
4097 /*-----------------------------------------------------------*/
4099 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
4101 /* For internal use only as it does not use a critical section. */
4102 pxTimeOut->xOverflowCount = xNumOfOverflows;
4103 pxTimeOut->xTimeOnEntering = xTickCount;
4105 /*-----------------------------------------------------------*/
4107 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
4108 TickType_t * const pxTicksToWait )
4112 configASSERT( pxTimeOut );
4113 configASSERT( pxTicksToWait );
4115 taskENTER_CRITICAL();
4117 /* Minor optimisation. The tick count cannot change in this block. */
4118 const TickType_t xConstTickCount = xTickCount;
4119 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
4121 #if ( INCLUDE_xTaskAbortDelay == 1 )
4122 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
4124 /* The delay was aborted, which is not the same as a time out,
4125 * but has the same result. */
4126 pxCurrentTCB->ucDelayAborted = pdFALSE;
4132 #if ( INCLUDE_vTaskSuspend == 1 )
4133 if( *pxTicksToWait == portMAX_DELAY )
4135 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
4136 * specified is the maximum block time then the task should block
4137 * indefinitely, and therefore never time out. */
4143 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
4145 /* The tick count is greater than the time at which
4146 * vTaskSetTimeout() was called, but has also overflowed since
4147 * vTaskSetTimeOut() was called. It must have wrapped all the way
4148 * around and gone past again. This passed since vTaskSetTimeout()
4151 *pxTicksToWait = ( TickType_t ) 0;
4153 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
4155 /* Not a genuine timeout. Adjust parameters for time remaining. */
4156 *pxTicksToWait -= xElapsedTime;
4157 vTaskInternalSetTimeOutState( pxTimeOut );
4162 *pxTicksToWait = ( TickType_t ) 0;
4166 taskEXIT_CRITICAL();
4170 /*-----------------------------------------------------------*/
4172 void vTaskMissedYield( void )
4174 /* Must be called from within a critical section */
4175 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
4177 /*-----------------------------------------------------------*/
4179 #if ( configUSE_TRACE_FACILITY == 1 )
4181 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
4183 UBaseType_t uxReturn;
4184 TCB_t const * pxTCB;
4189 uxReturn = pxTCB->uxTaskNumber;
4199 #endif /* configUSE_TRACE_FACILITY */
4200 /*-----------------------------------------------------------*/
4202 #if ( configUSE_TRACE_FACILITY == 1 )
4204 void vTaskSetTaskNumber( TaskHandle_t xTask,
4205 const UBaseType_t uxHandle )
4212 pxTCB->uxTaskNumber = uxHandle;
4216 #endif /* configUSE_TRACE_FACILITY */
4219 * -----------------------------------------------------------
4220 * The MinimalIdle task.
4221 * ----------------------------------------------------------
4223 * The minimal idle task is used for all the additional Cores in a SMP system.
4224 * There must be only 1 idle task and the rest are minimal idle tasks.
4226 * @todo additional conditional compiles to remove this function.
4228 #if (configNUM_CORES > 1)
4229 static portTASK_FUNCTION( prvMinimalIdleTask, pvParameters )
4233 #if ( configUSE_PREEMPTION == 0 )
4235 /* If we are not using preemption we keep forcing a task switch to
4236 * see if any other task has become available. If we are using
4237 * preemption we don't need to do this as any task becoming available
4238 * will automatically get the processor anyway. */
4241 #endif /* configUSE_PREEMPTION */
4243 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4245 /* When using preemption tasks of equal priority will be
4246 * timesliced. If a task that is sharing the idle priority is ready
4247 * to run then the idle task should yield before the end of the
4250 * A critical region is not required here as we are just reading from
4251 * the list, and an occasional incorrect value will not matter. If
4252 * the ready list at the idle priority contains one more task than the
4253 * number of idle tasks, which is equal to the configured numbers of cores
4254 * then a task other than the idle task is ready to execute. */
4255 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4261 mtCOVERAGE_TEST_MARKER();
4264 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4269 * -----------------------------------------------------------
4271 * ----------------------------------------------------------
4275 static portTASK_FUNCTION( prvIdleTask, pvParameters )
4277 /* Stop warnings. */
4278 ( void ) pvParameters;
4280 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
4281 * SCHEDULER IS STARTED. **/
4283 /* In case a task that has a secure context deletes itself, in which case
4284 * the idle task is responsible for deleting the task's secure context, if
4286 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
4288 /* All cores start up in the idle task. This initial yield gets the application
4294 /* See if any tasks have deleted themselves - if so then the idle task
4295 * is responsible for freeing the deleted task's TCB and stack. */
4296 prvCheckTasksWaitingTermination();
4298 #if ( configUSE_PREEMPTION == 0 )
4300 /* If we are not using preemption we keep forcing a task switch to
4301 * see if any other task has become available. If we are using
4302 * preemption we don't need to do this as any task becoming available
4303 * will automatically get the processor anyway. */
4306 #endif /* configUSE_PREEMPTION */
4308 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4310 /* When using preemption tasks of equal priority will be
4311 * timesliced. If a task that is sharing the idle priority is ready
4312 * to run then the idle task should yield before the end of the
4315 * A critical region is not required here as we are just reading from
4316 * the list, and an occasional incorrect value will not matter. If
4317 * the ready list at the idle priority contains one more task than the
4318 * number of idle tasks, which is equal to the configured numbers of cores
4319 * then a task other than the idle task is ready to execute. */
4320 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4326 mtCOVERAGE_TEST_MARKER();
4329 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4331 #if ( configUSE_IDLE_HOOK == 1 )
4333 extern void vApplicationIdleHook( void );
4335 /* Call the user defined function from within the idle task. This
4336 * allows the application designer to add background functionality
4337 * without the overhead of a separate task.
4338 * NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
4339 * CALL A FUNCTION THAT MIGHT BLOCK. */
4340 vApplicationIdleHook();
4342 #endif /* configUSE_IDLE_HOOK */
4344 /* This conditional compilation should use inequality to 0, not equality
4345 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
4346 * user defined low power mode implementations require
4347 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
4348 #if ( configUSE_TICKLESS_IDLE != 0 )
4350 TickType_t xExpectedIdleTime;
4352 /* It is not desirable to suspend then resume the scheduler on
4353 * each iteration of the idle task. Therefore, a preliminary
4354 * test of the expected idle time is performed without the
4355 * scheduler suspended. The result here is not necessarily
4357 xExpectedIdleTime = prvGetExpectedIdleTime();
4359 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4363 /* Now the scheduler is suspended, the expected idle
4364 * time can be sampled again, and this time its value can
4366 configASSERT( xNextTaskUnblockTime >= xTickCount );
4367 xExpectedIdleTime = prvGetExpectedIdleTime();
4369 /* Define the following macro to set xExpectedIdleTime to 0
4370 * if the application does not want
4371 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
4372 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
4374 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4376 traceLOW_POWER_IDLE_BEGIN();
4377 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
4378 traceLOW_POWER_IDLE_END();
4382 mtCOVERAGE_TEST_MARKER();
4385 ( void ) xTaskResumeAll();
4389 mtCOVERAGE_TEST_MARKER();
4392 #endif /* configUSE_TICKLESS_IDLE */
4395 /*-----------------------------------------------------------*/
4397 #if ( configUSE_TICKLESS_IDLE != 0 )
4399 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
4401 /* The idle task exists in addition to the application tasks. */
4402 const UBaseType_t uxNonApplicationTasks = 1;
4403 eSleepModeStatus eReturn = eStandardSleep;
4405 /* This function must be called from a critical section. */
4407 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
4409 /* A task was made ready while the scheduler was suspended. */
4410 eReturn = eAbortSleep;
4412 else if( xYieldPending != pdFALSE )
4414 /* A yield was pended while the scheduler was suspended. */
4415 eReturn = eAbortSleep;
4417 else if( xPendedTicks != 0 )
4419 /* A tick interrupt has already occurred but was held pending
4420 * because the scheduler is suspended. */
4421 eReturn = eAbortSleep;
4425 /* If all the tasks are in the suspended list (which might mean they
4426 * have an infinite block time rather than actually being suspended)
4427 * then it is safe to turn all clocks off and just wait for external
4429 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
4431 eReturn = eNoTasksWaitingTimeout;
4435 mtCOVERAGE_TEST_MARKER();
4442 #endif /* configUSE_TICKLESS_IDLE */
4443 /*-----------------------------------------------------------*/
4445 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4447 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
4453 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4455 pxTCB = prvGetTCBFromHandle( xTaskToSet );
4456 configASSERT( pxTCB != NULL );
4457 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
4461 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4462 /*-----------------------------------------------------------*/
4464 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4466 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
4469 void * pvReturn = NULL;
4472 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4474 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4475 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
4485 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4486 /*-----------------------------------------------------------*/
4488 #if ( portUSING_MPU_WRAPPERS == 1 )
4490 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
4491 const MemoryRegion_t * const xRegions )
4495 /* If null is passed in here then we are modifying the MPU settings of
4496 * the calling task. */
4497 pxTCB = prvGetTCBFromHandle( xTaskToModify );
4499 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
4502 #endif /* portUSING_MPU_WRAPPERS */
4503 /*-----------------------------------------------------------*/
4505 static void prvInitialiseTaskLists( void )
4507 UBaseType_t uxPriority;
4509 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
4511 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
4514 vListInitialise( &xDelayedTaskList1 );
4515 vListInitialise( &xDelayedTaskList2 );
4516 vListInitialise( &xPendingReadyList );
4518 #if ( INCLUDE_vTaskDelete == 1 )
4520 vListInitialise( &xTasksWaitingTermination );
4522 #endif /* INCLUDE_vTaskDelete */
4524 #if ( INCLUDE_vTaskSuspend == 1 )
4526 vListInitialise( &xSuspendedTaskList );
4528 #endif /* INCLUDE_vTaskSuspend */
4530 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
4532 pxDelayedTaskList = &xDelayedTaskList1;
4533 pxOverflowDelayedTaskList = &xDelayedTaskList2;
4535 /*-----------------------------------------------------------*/
4537 static void prvCheckTasksWaitingTermination( void )
4539 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
4541 #if ( INCLUDE_vTaskDelete == 1 )
4545 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
4546 * being called too often in the idle task. */
4547 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4549 taskENTER_CRITICAL();
4551 /* Since we are SMP, multiple idles can be running simultaneously
4552 * and we need to check that other idles did not cleanup while we were
4553 * waiting to enter the critical section */
4554 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4556 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. */
4558 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
4560 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4561 --uxCurrentNumberOfTasks;
4562 --uxDeletedTasksWaitingCleanUp;
4563 prvDeleteTCB( pxTCB );
4567 /* The TCB to be deleted still has not yet been switched out
4568 * by the scheduler, so we will just exit this loop early and
4569 * try again next time. */
4570 taskEXIT_CRITICAL();
4575 taskEXIT_CRITICAL();
4578 #endif /* INCLUDE_vTaskDelete */
4580 /*-----------------------------------------------------------*/
4582 #if ( configUSE_TRACE_FACILITY == 1 )
4584 void vTaskGetInfo( TaskHandle_t xTask,
4585 TaskStatus_t * pxTaskStatus,
4586 BaseType_t xGetFreeStackSpace,
4591 /* xTask is NULL then get the state of the calling task. */
4592 pxTCB = prvGetTCBFromHandle( xTask );
4594 pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
4595 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
4596 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
4597 pxTaskStatus->pxStackBase = pxTCB->pxStack;
4598 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
4600 #if ( configUSE_MUTEXES == 1 )
4602 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
4606 pxTaskStatus->uxBasePriority = 0;
4610 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4612 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
4616 pxTaskStatus->ulRunTimeCounter = 0;
4620 /* Obtaining the task state is a little fiddly, so is only done if the
4621 * value of eState passed into this function is eInvalid - otherwise the
4622 * state is just set to whatever is passed in. */
4623 if( eState != eInvalid )
4625 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
4627 pxTaskStatus->eCurrentState = eRunning;
4631 pxTaskStatus->eCurrentState = eState;
4633 #if ( INCLUDE_vTaskSuspend == 1 )
4635 /* If the task is in the suspended list then there is a
4636 * chance it is actually just blocked indefinitely - so really
4637 * it should be reported as being in the Blocked state. */
4638 if( eState == eSuspended )
4642 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4644 pxTaskStatus->eCurrentState = eBlocked;
4647 ( void ) xTaskResumeAll();
4650 #endif /* INCLUDE_vTaskSuspend */
4655 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
4658 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
4659 * parameter is provided to allow it to be skipped. */
4660 if( xGetFreeStackSpace != pdFALSE )
4662 #if ( portSTACK_GROWTH > 0 )
4664 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
4668 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
4674 pxTaskStatus->usStackHighWaterMark = 0;
4678 #endif /* configUSE_TRACE_FACILITY */
4679 /*-----------------------------------------------------------*/
4681 #if ( configUSE_TRACE_FACILITY == 1 )
4683 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
4687 configLIST_VOLATILE TCB_t * pxNextTCB, * pxFirstTCB;
4688 UBaseType_t uxTask = 0;
4690 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4692 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. */
4694 /* Populate an TaskStatus_t structure within the
4695 * pxTaskStatusArray array for each task that is referenced from
4696 * pxList. See the definition of TaskStatus_t in task.h for the
4697 * meaning of each TaskStatus_t structure member. */
4700 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. */
4701 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
4703 } while( pxNextTCB != pxFirstTCB );
4707 mtCOVERAGE_TEST_MARKER();
4713 #endif /* configUSE_TRACE_FACILITY */
4714 /*-----------------------------------------------------------*/
4716 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
4718 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
4720 uint32_t ulCount = 0U;
4722 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
4724 pucStackByte -= portSTACK_GROWTH;
4728 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
4730 return ( configSTACK_DEPTH_TYPE ) ulCount;
4733 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
4734 /*-----------------------------------------------------------*/
4736 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
4738 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
4739 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
4740 * user to determine the return type. It gets around the problem of the value
4741 * overflowing on 8-bit types without breaking backward compatibility for
4742 * applications that expect an 8-bit return type. */
4743 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
4746 uint8_t * pucEndOfStack;
4747 configSTACK_DEPTH_TYPE uxReturn;
4749 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
4750 * the same except for their return type. Using configSTACK_DEPTH_TYPE
4751 * allows the user to determine the return type. It gets around the
4752 * problem of the value overflowing on 8-bit types without breaking
4753 * backward compatibility for applications that expect an 8-bit return
4756 pxTCB = prvGetTCBFromHandle( xTask );
4758 #if portSTACK_GROWTH < 0
4760 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4764 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4768 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
4773 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
4774 /*-----------------------------------------------------------*/
4776 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
4778 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
4781 uint8_t * pucEndOfStack;
4782 UBaseType_t uxReturn;
4784 pxTCB = prvGetTCBFromHandle( xTask );
4786 #if portSTACK_GROWTH < 0
4788 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4792 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4796 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
4801 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
4802 /*-----------------------------------------------------------*/
4804 #if ( INCLUDE_vTaskDelete == 1 )
4806 static void prvDeleteTCB( TCB_t * pxTCB )
4808 /* This call is required specifically for the TriCore port. It must be
4809 * above the vPortFree() calls. The call is also used by ports/demos that
4810 * want to allocate and clean RAM statically. */
4811 portCLEAN_UP_TCB( pxTCB );
4813 /* Free up the memory allocated by the scheduler for the task. It is up
4814 * to the task to free any memory allocated at the application level.
4815 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
4816 * for additional information. */
4817 #if ( configUSE_NEWLIB_REENTRANT == 1 )
4819 _reclaim_reent( &( pxTCB->xNewLib_reent ) );
4821 #endif /* configUSE_NEWLIB_REENTRANT */
4823 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
4825 /* The task can only have been allocated dynamically - free both
4826 * the stack and TCB. */
4827 vPortFreeStack( pxTCB->pxStack );
4830 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
4832 /* The task could have been allocated statically or dynamically, so
4833 * check what was statically allocated before trying to free the
4835 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
4837 /* Both the stack and TCB were allocated dynamically, so both
4839 vPortFreeStack( pxTCB->pxStack );
4842 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4844 /* Only the stack was statically allocated, so the TCB is the
4845 * only memory that must be freed. */
4850 /* Neither the stack nor the TCB were allocated dynamically, so
4851 * nothing needs to be freed. */
4852 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
4853 mtCOVERAGE_TEST_MARKER();
4856 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
4859 #endif /* INCLUDE_vTaskDelete */
4860 /*-----------------------------------------------------------*/
4862 static void prvResetNextTaskUnblockTime( void )
4864 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4866 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
4867 * the maximum possible value so it is extremely unlikely that the
4868 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
4869 * there is an item in the delayed list. */
4870 xNextTaskUnblockTime = portMAX_DELAY;
4874 /* The new current delayed list is not empty, get the value of
4875 * the item at the head of the delayed list. This is the time at
4876 * which the task at the head of the delayed list should be removed
4877 * from the Blocked state. */
4878 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
4881 /*-----------------------------------------------------------*/
4883 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
4885 TaskHandle_t xTaskGetCurrentTaskHandle( void )
4887 TaskHandle_t xReturn;
4890 ulState = portDISABLE_INTERRUPTS();
4891 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
4892 portRESTORE_INTERRUPTS( ulState );
4897 TaskHandle_t xTaskGetCurrentTaskHandleCPU( UBaseType_t xCoreID )
4899 TaskHandle_t xReturn = NULL;
4901 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
4903 xReturn = pxCurrentTCBs[ xCoreID ];
4909 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
4910 /*-----------------------------------------------------------*/
4912 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
4914 BaseType_t xTaskGetSchedulerState( void )
4918 if( xSchedulerRunning == pdFALSE )
4920 xReturn = taskSCHEDULER_NOT_STARTED;
4924 taskENTER_CRITICAL();
4926 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4928 xReturn = taskSCHEDULER_RUNNING;
4932 xReturn = taskSCHEDULER_SUSPENDED;
4935 taskEXIT_CRITICAL();
4941 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
4942 /*-----------------------------------------------------------*/
4944 #if ( configUSE_MUTEXES == 1 )
4946 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
4948 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
4949 BaseType_t xReturn = pdFALSE;
4951 /* If the mutex was given back by an interrupt while the queue was
4952 * locked then the mutex holder might now be NULL. _RB_ Is this still
4953 * needed as interrupts can no longer use mutexes? */
4954 if( pxMutexHolder != NULL )
4956 /* If the holder of the mutex has a priority below the priority of
4957 * the task attempting to obtain the mutex then it will temporarily
4958 * inherit the priority of the task attempting to obtain the mutex. */
4959 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
4961 /* Adjust the mutex holder state to account for its new
4962 * priority. Only reset the event list item value if the value is
4963 * not being used for anything else. */
4964 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4966 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. */
4970 mtCOVERAGE_TEST_MARKER();
4973 /* If the task being modified is in the ready state it will need
4974 * to be moved into a new list. */
4975 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
4977 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4979 /* It is known that the task is in its ready list so
4980 * there is no need to check again and the port level
4981 * reset macro can be called directly. */
4982 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
4986 mtCOVERAGE_TEST_MARKER();
4989 /* Inherit the priority before being moved into the new list. */
4990 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4991 prvAddTaskToReadyList( pxMutexHolderTCB );
4995 /* Just inherit the priority. */
4996 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4999 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
5001 /* Inheritance occurred. */
5006 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
5008 /* The base priority of the mutex holder is lower than the
5009 * priority of the task attempting to take the mutex, but the
5010 * current priority of the mutex holder is not lower than the
5011 * priority of the task attempting to take the mutex.
5012 * Therefore the mutex holder must have already inherited a
5013 * priority, but inheritance would have occurred if that had
5014 * not been the case. */
5019 mtCOVERAGE_TEST_MARKER();
5025 mtCOVERAGE_TEST_MARKER();
5031 #endif /* configUSE_MUTEXES */
5032 /*-----------------------------------------------------------*/
5034 #if ( configUSE_MUTEXES == 1 )
5036 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
5038 TCB_t * const pxTCB = pxMutexHolder;
5039 BaseType_t xReturn = pdFALSE;
5041 if( pxMutexHolder != NULL )
5043 /* A task can only have an inherited priority if it holds the mutex.
5044 * If the mutex is held by a task then it cannot be given from an
5045 * interrupt, and if a mutex is given by the holding task then it must
5046 * be the running state task. */
5047 configASSERT( pxTCB == pxCurrentTCB );
5048 configASSERT( pxTCB->uxMutexesHeld );
5049 ( pxTCB->uxMutexesHeld )--;
5051 /* Has the holder of the mutex inherited the priority of another
5053 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
5055 /* Only disinherit if no other mutexes are held. */
5056 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
5058 /* A task can only have an inherited priority if it holds
5059 * the mutex. If the mutex is held by a task then it cannot be
5060 * given from an interrupt, and if a mutex is given by the
5061 * holding task then it must be the running state task. Remove
5062 * the holding task from the ready list. */
5063 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5065 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5069 mtCOVERAGE_TEST_MARKER();
5072 /* Disinherit the priority before adding the task into the
5073 * new ready list. */
5074 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
5075 pxTCB->uxPriority = pxTCB->uxBasePriority;
5077 /* Reset the event list item value. It cannot be in use for
5078 * any other purpose if this task is running, and it must be
5079 * running to give back the mutex. */
5080 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. */
5081 prvAddTaskToReadyList( pxTCB );
5083 /* Return true to indicate that a context switch is required.
5084 * This is only actually required in the corner case whereby
5085 * multiple mutexes were held and the mutexes were given back
5086 * in an order different to that in which they were taken.
5087 * If a context switch did not occur when the first mutex was
5088 * returned, even if a task was waiting on it, then a context
5089 * switch should occur when the last mutex is returned whether
5090 * a task is waiting on it or not. */
5095 mtCOVERAGE_TEST_MARKER();
5100 mtCOVERAGE_TEST_MARKER();
5105 mtCOVERAGE_TEST_MARKER();
5111 #endif /* configUSE_MUTEXES */
5112 /*-----------------------------------------------------------*/
5114 #if ( configUSE_MUTEXES == 1 )
5116 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
5117 UBaseType_t uxHighestPriorityWaitingTask )
5119 TCB_t * const pxTCB = pxMutexHolder;
5120 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
5121 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
5123 if( pxMutexHolder != NULL )
5125 /* If pxMutexHolder is not NULL then the holder must hold at least
5127 configASSERT( pxTCB->uxMutexesHeld );
5129 /* Determine the priority to which the priority of the task that
5130 * holds the mutex should be set. This will be the greater of the
5131 * holding task's base priority and the priority of the highest
5132 * priority task that is waiting to obtain the mutex. */
5133 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
5135 uxPriorityToUse = uxHighestPriorityWaitingTask;
5139 uxPriorityToUse = pxTCB->uxBasePriority;
5142 /* Does the priority need to change? */
5143 if( pxTCB->uxPriority != uxPriorityToUse )
5145 /* Only disinherit if no other mutexes are held. This is a
5146 * simplification in the priority inheritance implementation. If
5147 * the task that holds the mutex is also holding other mutexes then
5148 * the other mutexes may have caused the priority inheritance. */
5149 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
5151 /* If a task has timed out because it already holds the
5152 * mutex it was trying to obtain then it cannot of inherited
5153 * its own priority. */
5154 configASSERT( pxTCB != pxCurrentTCB );
5156 /* Disinherit the priority, remembering the previous
5157 * priority to facilitate determining the subject task's
5159 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
5160 uxPriorityUsedOnEntry = pxTCB->uxPriority;
5161 pxTCB->uxPriority = uxPriorityToUse;
5163 /* Only reset the event list item value if the value is not
5164 * being used for anything else. */
5165 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
5167 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. */
5171 mtCOVERAGE_TEST_MARKER();
5174 /* If the running task is not the task that holds the mutex
5175 * then the task that holds the mutex could be in either the
5176 * Ready, Blocked or Suspended states. Only remove the task
5177 * from its current state list if it is in the Ready state as
5178 * the task's priority is going to change and there is one
5179 * Ready list per priority. */
5180 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
5182 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5184 /* It is known that the task is in its ready list so
5185 * there is no need to check again and the port level
5186 * reset macro can be called directly. */
5187 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5191 mtCOVERAGE_TEST_MARKER();
5194 prvAddTaskToReadyList( pxTCB );
5198 mtCOVERAGE_TEST_MARKER();
5203 mtCOVERAGE_TEST_MARKER();
5208 mtCOVERAGE_TEST_MARKER();
5213 mtCOVERAGE_TEST_MARKER();
5217 #endif /* configUSE_MUTEXES */
5218 /*-----------------------------------------------------------*/
5221 * If not in a critical section then yield immediately.
5222 * Otherwise set xYieldPending to true to wait to
5223 * yield until exiting the critical section.
5225 void vTaskYieldWithinAPI( void )
5227 if( pxCurrentTCB->uxCriticalNesting == 0U )
5233 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5236 /*-----------------------------------------------------------*/
5238 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5240 void vTaskEnterCritical( void )
5242 portDISABLE_INTERRUPTS();
5244 if( xSchedulerRunning != pdFALSE )
5246 if( pxCurrentTCB->uxCriticalNesting == 0U )
5248 if( portCHECK_IF_IN_ISR() == pdFALSE )
5250 portGET_TASK_LOCK();
5256 ( pxCurrentTCB->uxCriticalNesting )++;
5258 /* This should now be interrupt safe. The only time there would be
5259 * a problem is if this is called before a context switch and
5260 * vTaskExitCritical() is called after pxCurrentTCB changes. Therefore
5261 * this should not be used within vTaskSwitchContext(). */
5263 if( ( uxSchedulerSuspended == 0U ) && ( pxCurrentTCB->uxCriticalNesting == 1U ) )
5265 prvCheckForRunStateChange();
5270 mtCOVERAGE_TEST_MARKER();
5274 #endif /* portCRITICAL_NESTING_IN_TCB */
5275 /*-----------------------------------------------------------*/
5277 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5279 void vTaskExitCritical( void )
5281 if( xSchedulerRunning != pdFALSE )
5283 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
5284 * does not match a previous call to vTaskEnterCritical(). */
5285 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
5287 if( pxCurrentTCB->uxCriticalNesting > 0U )
5289 ( pxCurrentTCB->uxCriticalNesting )--;
5291 if( pxCurrentTCB->uxCriticalNesting == 0U )
5293 portRELEASE_ISR_LOCK();
5295 if( portCHECK_IF_IN_ISR() == pdFALSE )
5297 portRELEASE_TASK_LOCK();
5298 portENABLE_INTERRUPTS();
5300 /* When a task yields in a critical section it just sets
5301 * xYieldPending to true. So now that we have exited the
5302 * critical section check if xYieldPending is true, and
5304 if( xYieldPending != pdFALSE )
5311 /* In an ISR we don't hold the task lock and don't
5312 * need to yield. Yield will happen if necessary when
5313 * the application ISR calls portEND_SWITCHING_ISR() */
5314 mtCOVERAGE_TEST_MARKER();
5319 mtCOVERAGE_TEST_MARKER();
5324 mtCOVERAGE_TEST_MARKER();
5329 mtCOVERAGE_TEST_MARKER();
5333 #endif /* portCRITICAL_NESTING_IN_TCB */
5334 /*-----------------------------------------------------------*/
5336 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
5338 static char * prvWriteNameToBuffer( char * pcBuffer,
5339 const char * pcTaskName )
5343 /* Start by copying the entire string. */
5344 strcpy( pcBuffer, pcTaskName );
5346 /* Pad the end of the string with spaces to ensure columns line up when
5348 for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
5350 pcBuffer[ x ] = ' ';
5354 pcBuffer[ x ] = ( char ) 0x00;
5356 /* Return the new end of string. */
5357 return &( pcBuffer[ x ] );
5360 #endif /* ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
5361 /*-----------------------------------------------------------*/
5363 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5365 void vTaskList( char * pcWriteBuffer )
5367 TaskStatus_t * pxTaskStatusArray;
5368 UBaseType_t uxArraySize, x;
5374 * This function is provided for convenience only, and is used by many
5375 * of the demo applications. Do not consider it to be part of the
5378 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
5379 * uxTaskGetSystemState() output into a human readable table that
5380 * displays task: names, states, priority, stack usage and task number.
5381 * Stack usage specified as the number of unused StackType_t words stack can hold
5382 * on top of stack - not the number of bytes.
5384 * vTaskList() has a dependency on the sprintf() C library function that
5385 * might bloat the code size, use a lot of stack, and provide different
5386 * results on different platforms. An alternative, tiny, third party,
5387 * and limited functionality implementation of sprintf() is provided in
5388 * many of the FreeRTOS/Demo sub-directories in a file called
5389 * printf-stdarg.c (note printf-stdarg.c does not provide a full
5390 * snprintf() implementation!).
5392 * It is recommended that production systems call uxTaskGetSystemState()
5393 * directly to get access to raw stats data, rather than indirectly
5394 * through a call to vTaskList().
5398 /* Make sure the write buffer does not contain a string. */
5399 *pcWriteBuffer = ( char ) 0x00;
5401 /* Take a snapshot of the number of tasks in case it changes while this
5402 * function is executing. */
5403 uxArraySize = uxCurrentNumberOfTasks;
5405 /* Allocate an array index for each task. NOTE! if
5406 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5407 * equate to NULL. */
5408 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. */
5410 if( pxTaskStatusArray != NULL )
5412 /* Generate the (binary) data. */
5413 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
5415 /* Create a human readable table from the binary data. */
5416 for( x = 0; x < uxArraySize; x++ )
5418 switch( pxTaskStatusArray[ x ].eCurrentState )
5421 cStatus = tskRUNNING_CHAR;
5425 cStatus = tskREADY_CHAR;
5429 cStatus = tskBLOCKED_CHAR;
5433 cStatus = tskSUSPENDED_CHAR;
5437 cStatus = tskDELETED_CHAR;
5440 case eInvalid: /* Fall through. */
5441 default: /* Should not get here, but it is included
5442 * to prevent static checking errors. */
5443 cStatus = ( char ) 0x00;
5447 /* Write the task name to the string, padding with spaces so it
5448 * can be printed in tabular form more easily. */
5449 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5451 /* Write the rest of the string. */
5452 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. */
5453 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. */
5456 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
5457 * is 0 then vPortFree() will be #defined to nothing. */
5458 vPortFree( pxTaskStatusArray );
5462 mtCOVERAGE_TEST_MARKER();
5466 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
5467 /*----------------------------------------------------------*/
5469 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5471 void vTaskGetRunTimeStats( char * pcWriteBuffer )
5473 TaskStatus_t * pxTaskStatusArray;
5474 UBaseType_t uxArraySize, x;
5475 uint32_t ulTotalTime, ulStatsAsPercentage;
5477 #if ( configUSE_TRACE_FACILITY != 1 )
5479 #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats().
5486 * This function is provided for convenience only, and is used by many
5487 * of the demo applications. Do not consider it to be part of the
5490 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
5491 * of the uxTaskGetSystemState() output into a human readable table that
5492 * displays the amount of time each task has spent in the Running state
5493 * in both absolute and percentage terms.
5495 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
5496 * function that might bloat the code size, use a lot of stack, and
5497 * provide different results on different platforms. An alternative,
5498 * tiny, third party, and limited functionality implementation of
5499 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
5500 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
5501 * a full snprintf() implementation!).
5503 * It is recommended that production systems call uxTaskGetSystemState()
5504 * directly to get access to raw stats data, rather than indirectly
5505 * through a call to vTaskGetRunTimeStats().
5508 /* Make sure the write buffer does not contain a string. */
5509 *pcWriteBuffer = ( char ) 0x00;
5511 /* Take a snapshot of the number of tasks in case it changes while this
5512 * function is executing. */
5513 uxArraySize = uxCurrentNumberOfTasks;
5515 /* Allocate an array index for each task. NOTE! If
5516 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5517 * equate to NULL. */
5518 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. */
5520 if( pxTaskStatusArray != NULL )
5522 /* Generate the (binary) data. */
5523 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
5525 /* For percentage calculations. */
5526 ulTotalTime /= 100UL;
5528 /* Avoid divide by zero errors. */
5529 if( ulTotalTime > 0UL )
5531 /* Create a human readable table from the binary data. */
5532 for( x = 0; x < uxArraySize; x++ )
5534 /* What percentage of the total run time has the task used?
5535 * This will always be rounded down to the nearest integer.
5536 * ulTotalRunTimeDiv100 has already been divided by 100. */
5537 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
5539 /* Write the task name to the string, padding with
5540 * spaces so it can be printed in tabular form more
5542 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5544 if( ulStatsAsPercentage > 0UL )
5546 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5548 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
5552 /* sizeof( int ) == sizeof( long ) so a smaller
5553 * printf() library can be used. */
5554 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. */
5560 /* If the percentage is zero here then the task has
5561 * consumed less than 1% of the total run time. */
5562 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5564 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
5568 /* sizeof( int ) == sizeof( long ) so a smaller
5569 * printf() library can be used. */
5570 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. */
5575 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. */
5580 mtCOVERAGE_TEST_MARKER();
5583 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
5584 * is 0 then vPortFree() will be #defined to nothing. */
5585 vPortFree( pxTaskStatusArray );
5589 mtCOVERAGE_TEST_MARKER();
5593 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
5594 /*-----------------------------------------------------------*/
5596 TickType_t uxTaskResetEventItemValue( void )
5598 TickType_t uxReturn;
5600 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
5602 /* Reset the event list item to its normal value - so it can be used with
5603 * queues and semaphores. */
5604 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. */
5608 /*-----------------------------------------------------------*/
5610 #if ( configUSE_MUTEXES == 1 )
5612 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
5614 /* If xSemaphoreCreateMutex() is called before any tasks have been created
5615 * then pxCurrentTCB will be NULL. */
5616 if( pxCurrentTCB != NULL )
5618 ( pxCurrentTCB->uxMutexesHeld )++;
5621 return pxCurrentTCB;
5624 #endif /* configUSE_MUTEXES */
5625 /*-----------------------------------------------------------*/
5627 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5629 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
5630 BaseType_t xClearCountOnExit,
5631 TickType_t xTicksToWait )
5635 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5637 taskENTER_CRITICAL();
5639 /* Only block if the notification count is not already non-zero. */
5640 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
5642 /* Mark this task as waiting for a notification. */
5643 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5645 if( xTicksToWait > ( TickType_t ) 0 )
5647 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5648 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
5650 /* All ports are written to allow a yield in a critical
5651 * section (some will yield immediately, others wait until the
5652 * critical section exits) - but it is not something that
5653 * application code should ever do. */
5654 vTaskYieldWithinAPI();
5658 mtCOVERAGE_TEST_MARKER();
5663 mtCOVERAGE_TEST_MARKER();
5666 taskEXIT_CRITICAL();
5668 taskENTER_CRITICAL();
5670 traceTASK_NOTIFY_TAKE( uxIndexToWait );
5671 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5673 if( ulReturn != 0UL )
5675 if( xClearCountOnExit != pdFALSE )
5677 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
5681 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
5686 mtCOVERAGE_TEST_MARKER();
5689 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5691 taskEXIT_CRITICAL();
5696 #endif /* configUSE_TASK_NOTIFICATIONS */
5697 /*-----------------------------------------------------------*/
5699 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5701 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
5702 uint32_t ulBitsToClearOnEntry,
5703 uint32_t ulBitsToClearOnExit,
5704 uint32_t * pulNotificationValue,
5705 TickType_t xTicksToWait )
5709 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5711 taskENTER_CRITICAL();
5713 /* Only block if a notification is not already pending. */
5714 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5716 /* Clear bits in the task's notification value as bits may get
5717 * set by the notifying task or interrupt. This can be used to
5718 * clear the value to zero. */
5719 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
5721 /* Mark this task as waiting for a notification. */
5722 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5724 if( xTicksToWait > ( TickType_t ) 0 )
5726 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5727 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
5729 /* All ports are written to allow a yield in a critical
5730 * section (some will yield immediately, others wait until the
5731 * critical section exits) - but it is not something that
5732 * application code should ever do. */
5733 vTaskYieldWithinAPI();
5737 mtCOVERAGE_TEST_MARKER();
5742 mtCOVERAGE_TEST_MARKER();
5745 taskEXIT_CRITICAL();
5747 taskENTER_CRITICAL();
5749 traceTASK_NOTIFY_WAIT( uxIndexToWait );
5751 if( pulNotificationValue != NULL )
5753 /* Output the current notification value, which may or may not
5755 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5758 /* If ucNotifyValue is set then either the task never entered the
5759 * blocked state (because a notification was already pending) or the
5760 * task unblocked because of a notification. Otherwise the task
5761 * unblocked because of a timeout. */
5762 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5764 /* A notification was not received. */
5769 /* A notification was already pending or a notification was
5770 * received while the task was waiting. */
5771 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
5775 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5777 taskEXIT_CRITICAL();
5782 #endif /* configUSE_TASK_NOTIFICATIONS */
5783 /*-----------------------------------------------------------*/
5785 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5787 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
5788 UBaseType_t uxIndexToNotify,
5790 eNotifyAction eAction,
5791 uint32_t * pulPreviousNotificationValue )
5794 BaseType_t xReturn = pdPASS;
5795 uint8_t ucOriginalNotifyState;
5797 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5798 configASSERT( xTaskToNotify );
5799 pxTCB = xTaskToNotify;
5801 taskENTER_CRITICAL();
5803 if( pulPreviousNotificationValue != NULL )
5805 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5808 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5810 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5815 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5819 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5822 case eSetValueWithOverwrite:
5823 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5826 case eSetValueWithoutOverwrite:
5828 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5830 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5834 /* The value could not be written to the task. */
5842 /* The task is being notified without its notify value being
5848 /* Should not get here if all enums are handled.
5849 * Artificially force an assert by testing a value the
5850 * compiler can't assume is const. */
5851 configASSERT( xTickCount == ( TickType_t ) 0 );
5856 traceTASK_NOTIFY( uxIndexToNotify );
5858 /* If the task is in the blocked state specifically to wait for a
5859 * notification then unblock it now. */
5860 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5862 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5863 prvAddTaskToReadyList( pxTCB );
5865 /* The task should not have been on an event list. */
5866 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5868 #if ( configUSE_TICKLESS_IDLE != 0 )
5870 /* If a task is blocked waiting for a notification then
5871 * xNextTaskUnblockTime might be set to the blocked task's time
5872 * out time. If the task is unblocked for a reason other than
5873 * a timeout xNextTaskUnblockTime is normally left unchanged,
5874 * because it will automatically get reset to a new value when
5875 * the tick count equals xNextTaskUnblockTime. However if
5876 * tickless idling is used it might be more important to enter
5877 * sleep mode at the earliest possible time - so reset
5878 * xNextTaskUnblockTime here to ensure it is updated at the
5879 * earliest possible time. */
5880 prvResetNextTaskUnblockTime();
5884 #if ( configUSE_PREEMPTION == 1 )
5886 prvYieldForTask( pxTCB, pdFALSE );
5892 mtCOVERAGE_TEST_MARKER();
5895 taskEXIT_CRITICAL();
5900 #endif /* configUSE_TASK_NOTIFICATIONS */
5901 /*-----------------------------------------------------------*/
5903 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5905 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
5906 UBaseType_t uxIndexToNotify,
5908 eNotifyAction eAction,
5909 uint32_t * pulPreviousNotificationValue,
5910 BaseType_t * pxHigherPriorityTaskWoken )
5913 uint8_t ucOriginalNotifyState;
5914 BaseType_t xReturn = pdPASS;
5915 UBaseType_t uxSavedInterruptStatus;
5917 configASSERT( xTaskToNotify );
5918 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5920 /* RTOS ports that support interrupt nesting have the concept of a
5921 * maximum system call (or maximum API call) interrupt priority.
5922 * Interrupts that are above the maximum system call priority are keep
5923 * permanently enabled, even when the RTOS kernel is in a critical section,
5924 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
5925 * is defined in FreeRTOSConfig.h then
5926 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
5927 * failure if a FreeRTOS API function is called from an interrupt that has
5928 * been assigned a priority above the configured maximum system call
5929 * priority. Only FreeRTOS functions that end in FromISR can be called
5930 * from interrupts that have been assigned a priority at or (logically)
5931 * below the maximum system call interrupt priority. FreeRTOS maintains a
5932 * separate interrupt safe API to ensure interrupt entry is as fast and as
5933 * simple as possible. More information (albeit Cortex-M specific) is
5934 * provided on the following link:
5935 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5936 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
5938 pxTCB = xTaskToNotify;
5940 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5942 if( pulPreviousNotificationValue != NULL )
5944 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5947 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5948 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5953 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5957 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5960 case eSetValueWithOverwrite:
5961 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5964 case eSetValueWithoutOverwrite:
5966 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5968 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5972 /* The value could not be written to the task. */
5980 /* The task is being notified without its notify value being
5986 /* Should not get here if all enums are handled.
5987 * Artificially force an assert by testing a value the
5988 * compiler can't assume is const. */
5989 configASSERT( xTickCount == ( TickType_t ) 0 );
5993 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
5995 /* If the task is in the blocked state specifically to wait for a
5996 * notification then unblock it now. */
5997 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5999 /* The task should not have been on an event list. */
6000 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
6002 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
6004 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6005 prvAddTaskToReadyList( pxTCB );
6009 /* The delayed and ready lists cannot be accessed, so hold
6010 * this task pending until the scheduler is resumed. */
6011 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
6014 #if ( configUSE_PREEMPTION == 1 )
6015 prvYieldForTask( pxTCB, pdFALSE );
6017 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6019 if( pxHigherPriorityTaskWoken != NULL )
6021 *pxHigherPriorityTaskWoken = pdTRUE;
6027 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6032 #endif /* configUSE_TASK_NOTIFICATIONS */
6033 /*-----------------------------------------------------------*/
6035 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6037 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
6038 UBaseType_t uxIndexToNotify,
6039 BaseType_t * pxHigherPriorityTaskWoken )
6042 uint8_t ucOriginalNotifyState;
6043 UBaseType_t uxSavedInterruptStatus;
6045 configASSERT( xTaskToNotify );
6046 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6048 /* RTOS ports that support interrupt nesting have the concept of a
6049 * maximum system call (or maximum API call) interrupt priority.
6050 * Interrupts that are above the maximum system call priority are keep
6051 * permanently enabled, even when the RTOS kernel is in a critical section,
6052 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
6053 * is defined in FreeRTOSConfig.h then
6054 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
6055 * failure if a FreeRTOS API function is called from an interrupt that has
6056 * been assigned a priority above the configured maximum system call
6057 * priority. Only FreeRTOS functions that end in FromISR can be called
6058 * from interrupts that have been assigned a priority at or (logically)
6059 * below the maximum system call interrupt priority. FreeRTOS maintains a
6060 * separate interrupt safe API to ensure interrupt entry is as fast and as
6061 * simple as possible. More information (albeit Cortex-M specific) is
6062 * provided on the following link:
6063 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
6064 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
6066 pxTCB = xTaskToNotify;
6068 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6070 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
6071 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
6073 /* 'Giving' is equivalent to incrementing a count in a counting
6075 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
6077 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
6079 /* If the task is in the blocked state specifically to wait for a
6080 * notification then unblock it now. */
6081 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
6083 /* The task should not have been on an event list. */
6084 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
6086 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
6088 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6089 prvAddTaskToReadyList( pxTCB );
6093 /* The delayed and ready lists cannot be accessed, so hold
6094 * this task pending until the scheduler is resumed. */
6095 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
6098 #if ( configUSE_PREEMPTION == 1 )
6099 prvYieldForTask( pxTCB, pdFALSE );
6101 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6103 if( pxHigherPriorityTaskWoken != NULL )
6105 *pxHigherPriorityTaskWoken = pdTRUE;
6111 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6114 #endif /* configUSE_TASK_NOTIFICATIONS */
6115 /*-----------------------------------------------------------*/
6117 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6119 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
6120 UBaseType_t uxIndexToClear )
6125 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6127 /* If null is passed in here then it is the calling task that is having
6128 * its notification state cleared. */
6129 pxTCB = prvGetTCBFromHandle( xTask );
6131 taskENTER_CRITICAL();
6133 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
6135 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
6143 taskEXIT_CRITICAL();
6148 #endif /* configUSE_TASK_NOTIFICATIONS */
6149 /*-----------------------------------------------------------*/
6151 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6153 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
6154 UBaseType_t uxIndexToClear,
6155 uint32_t ulBitsToClear )
6160 /* If null is passed in here then it is the calling task that is having
6161 * its notification state cleared. */
6162 pxTCB = prvGetTCBFromHandle( xTask );
6164 taskENTER_CRITICAL();
6166 /* Return the notification as it was before the bits were cleared,
6167 * then clear the bit mask. */
6168 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
6169 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
6171 taskEXIT_CRITICAL();
6176 #endif /* configUSE_TASK_NOTIFICATIONS */
6177 /*-----------------------------------------------------------*/
6179 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
6181 uint32_t ulTaskGetIdleRunTimeCounter( void )
6183 uint32_t ulReturn = 0;
6185 for( BaseType_t i = 0; i < configNUM_CORES; i++ )
6187 ulReturn += xIdleTaskHandle[ i ]->ulRunTimeCounter;
6193 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
6194 /*-----------------------------------------------------------*/
6196 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
6197 const BaseType_t xCanBlockIndefinitely )
6199 TickType_t xTimeToWake;
6200 const TickType_t xConstTickCount = xTickCount;
6202 #if ( INCLUDE_xTaskAbortDelay == 1 )
6204 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
6205 * reset to pdFALSE so it can be detected as having been set to pdTRUE
6206 * when the task leaves the Blocked state. */
6207 pxCurrentTCB->ucDelayAborted = pdFALSE;
6211 /* Remove the task from the ready list before adding it to the blocked list
6212 * as the same list item is used for both lists. */
6213 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6215 /* The current task must be in a ready list, so there is no need to
6216 * check, and the port reset macro can be called directly. */
6217 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. */
6221 mtCOVERAGE_TEST_MARKER();
6224 #if ( INCLUDE_vTaskSuspend == 1 )
6226 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
6228 /* Add the task to the suspended task list instead of a delayed task
6229 * list to ensure it is not woken by a timing event. It will block
6231 vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
6235 /* Calculate the time at which the task should be woken if the event
6236 * does not occur. This may overflow but this doesn't matter, the
6237 * kernel will manage it correctly. */
6238 xTimeToWake = xConstTickCount + xTicksToWait;
6240 /* The list item will be inserted in wake time order. */
6241 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6243 if( xTimeToWake < xConstTickCount )
6245 /* Wake time has overflowed. Place this item in the overflow
6247 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6251 /* The wake time has not overflowed, so the current block list
6253 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6255 /* If the task entering the blocked state was placed at the
6256 * head of the list of blocked tasks then xNextTaskUnblockTime
6257 * needs to be updated too. */
6258 if( xTimeToWake < xNextTaskUnblockTime )
6260 xNextTaskUnblockTime = xTimeToWake;
6264 mtCOVERAGE_TEST_MARKER();
6269 #else /* INCLUDE_vTaskSuspend */
6271 /* Calculate the time at which the task should be woken if the event
6272 * does not occur. This may overflow but this doesn't matter, the kernel
6273 * will manage it correctly. */
6274 xTimeToWake = xConstTickCount + xTicksToWait;
6276 /* The list item will be inserted in wake time order. */
6277 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6279 if( xTimeToWake < xConstTickCount )
6281 /* Wake time has overflowed. Place this item in the overflow list. */
6282 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6286 /* The wake time has not overflowed, so the current block list is used. */
6287 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6289 /* If the task entering the blocked state was placed at the head of the
6290 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
6292 if( xTimeToWake < xNextTaskUnblockTime )
6294 xNextTaskUnblockTime = xTimeToWake;
6298 mtCOVERAGE_TEST_MARKER();
6302 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
6303 ( void ) xCanBlockIndefinitely;
6305 #endif /* INCLUDE_vTaskSuspend */
6308 /* Code below here allows additional code to be inserted into this source file,
6309 * especially where access to file scope functions and data is needed (for example
6310 * when performing module tests). */
6312 #ifdef FREERTOS_MODULE_TEST
6313 #include "tasks_test_access_functions.h"
6317 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
6319 #include "freertos_tasks_c_additions.h"
6321 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
6322 static void freertos_tasks_c_additions_init( void )
6324 FREERTOS_TASKS_C_ADDITIONS_INIT();
6328 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */