2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /* Standard includes. */
33 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
34 * all the API functions to use the MPU wrappers. That should only be done when
35 * task.h is included from an application file. */
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
38 /* FreeRTOS includes. */
42 #include "stack_macros.h"
44 /* The default definitions are only available for non-MPU ports. The
45 * reason is that the stack alignment requirements vary for different
47 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS != 0 ) )
48 #error configKERNEL_PROVIDED_STATIC_MEMORY cannot be set to 1 when using an MPU port. The vApplicationGet*TaskMemory() functions must be provided manually.
51 /* The MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
52 * for the header files above, but not in this file, in order to generate the
53 * correct privileged Vs unprivileged linkage and placement. */
54 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
56 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
57 * functions but without including stdio.h here. */
58 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
60 /* At the bottom of this file are two optional functions that can be used
61 * to generate human readable text from the raw data generated by the
62 * uxTaskGetSystemState() function. Note the formatting functions are provided
63 * for convenience only, and are NOT considered part of the kernel. */
65 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
67 #if ( configUSE_PREEMPTION == 0 )
69 /* If the cooperative scheduler is being used then a yield should not be
70 * performed just because a higher priority task has been woken. */
71 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB )
72 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB )
75 #if ( configNUMBER_OF_CORES == 1 )
77 /* This macro requests the running task pxTCB to yield. In single core
78 * scheduler, a running task always runs on core 0 and portYIELD_WITHIN_API()
79 * can be used to request the task running on core 0 to yield. Therefore, pxTCB
80 * is not used in this macro. */
81 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) \
84 portYIELD_WITHIN_API(); \
87 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \
89 if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \
91 portYIELD_WITHIN_API(); \
95 mtCOVERAGE_TEST_MARKER(); \
99 #else /* if ( configNUMBER_OF_CORES == 1 ) */
101 /* Yield the core on which this task is running. */
102 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldCore( ( pxTCB )->xTaskRunState )
104 /* Yield for the task if a running task has priority lower than this task. */
105 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldForTask( pxTCB )
107 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
109 #endif /* if ( configUSE_PREEMPTION == 0 ) */
111 /* Values that can be assigned to the ucNotifyState member of the TCB. */
112 #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
113 #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
114 #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
117 * The value used to fill the stack of a task when the task is created. This
118 * is used purely for checking the high water mark for tasks.
120 #define tskSTACK_FILL_BYTE ( 0xa5U )
122 /* Bits used to record how a task's stack and TCB were allocated. */
123 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
124 #define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
125 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
127 /* If any of the following are set then task stacks are filled with a known
128 * value so the high water mark can be determined. If none of the following are
129 * set then don't fill the stack so there is no unnecessary dependency on memset. */
130 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
131 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
133 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
137 * Macros used by vListTask to indicate which state a task is in.
139 #define tskRUNNING_CHAR ( 'X' )
140 #define tskBLOCKED_CHAR ( 'B' )
141 #define tskREADY_CHAR ( 'R' )
142 #define tskDELETED_CHAR ( 'D' )
143 #define tskSUSPENDED_CHAR ( 'S' )
146 * Some kernel aware debuggers require the data the debugger needs access to be
147 * global, rather than file scope.
149 #ifdef portREMOVE_STATIC_QUALIFIER
153 /* The name allocated to the Idle task. This can be overridden by defining
154 * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
155 #ifndef configIDLE_TASK_NAME
156 #define configIDLE_TASK_NAME "IDLE"
159 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
161 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
162 * performed in a generic way that is not optimised to any particular
163 * microcontroller architecture. */
165 /* uxTopReadyPriority holds the priority of the highest priority ready
167 #define taskRECORD_READY_PRIORITY( uxPriority ) \
169 if( ( uxPriority ) > uxTopReadyPriority ) \
171 uxTopReadyPriority = ( uxPriority ); \
173 } while( 0 ) /* taskRECORD_READY_PRIORITY */
175 /*-----------------------------------------------------------*/
177 #if ( configNUMBER_OF_CORES == 1 )
178 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
180 UBaseType_t uxTopPriority = uxTopReadyPriority; \
182 /* Find the highest priority queue that contains ready tasks. */ \
183 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) != pdFALSE ) \
185 configASSERT( uxTopPriority ); \
189 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
190 * the same priority get an equal share of the processor time. */ \
191 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
192 uxTopReadyPriority = uxTopPriority; \
193 } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */
194 #else /* if ( configNUMBER_OF_CORES == 1 ) */
196 #define taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID ) prvSelectHighestPriorityTask( xCoreID )
198 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
200 /*-----------------------------------------------------------*/
202 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
203 * they are only required when a port optimised method of task selection is
205 #define taskRESET_READY_PRIORITY( uxPriority )
206 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
208 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
210 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
211 * performed in a way that is tailored to the particular microcontroller
212 * architecture being used. */
214 /* A port optimised version is provided. Call the port defined macros. */
215 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( ( uxPriority ), uxTopReadyPriority )
217 /*-----------------------------------------------------------*/
219 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
221 UBaseType_t uxTopPriority; \
223 /* Find the highest priority list that contains ready tasks. */ \
224 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
225 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
226 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
229 /*-----------------------------------------------------------*/
231 /* A port optimised version is provided, call it only if the TCB being reset
232 * is being referenced from a ready list. If it is referenced from a delayed
233 * or suspended list then it won't be in a ready list. */
234 #define taskRESET_READY_PRIORITY( uxPriority ) \
236 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
238 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
242 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
244 /*-----------------------------------------------------------*/
246 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
247 * count overflows. */
248 #define taskSWITCH_DELAYED_LISTS() \
252 /* The delayed tasks list should be empty when the lists are switched. */ \
253 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
255 pxTemp = pxDelayedTaskList; \
256 pxDelayedTaskList = pxOverflowDelayedTaskList; \
257 pxOverflowDelayedTaskList = pxTemp; \
258 xNumOfOverflows = ( BaseType_t ) ( xNumOfOverflows + 1 ); \
259 prvResetNextTaskUnblockTime(); \
262 /*-----------------------------------------------------------*/
265 * Place the task represented by pxTCB into the appropriate ready list for
266 * the task. It is inserted at the end of the list.
268 #define prvAddTaskToReadyList( pxTCB ) \
270 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
271 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
272 listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
273 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB ); \
275 /*-----------------------------------------------------------*/
278 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
279 * where NULL is used to indicate that the handle of the currently executing
280 * task should be used in place of the parameter. This macro simply checks to
281 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
283 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
285 /* The item value of the event list item is normally used to hold the priority
286 * of the task to which it belongs (coded to allow it to be held in reverse
287 * priority order). However, it is occasionally borrowed for other purposes. It
288 * is important its value is not updated due to a task priority change while it is
289 * being used for another purpose. The following bit definition is used to inform
290 * the scheduler that the value should not be changed - in which case it is the
291 * responsibility of whichever module is using the value to ensure it gets set back
292 * to its original value when it is released. */
293 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
294 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint16_t ) 0x8000U )
295 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
296 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint32_t ) 0x80000000U )
297 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
298 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint64_t ) 0x8000000000000000U )
301 /* Indicates that the task is not actively running on any core. */
302 #define taskTASK_NOT_RUNNING ( ( BaseType_t ) ( -1 ) )
304 /* Indicates that the task is actively running but scheduled to yield. */
305 #define taskTASK_SCHEDULED_TO_YIELD ( ( BaseType_t ) ( -2 ) )
307 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
308 #if ( configNUMBER_OF_CORES == 1 )
309 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
310 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
312 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) )
313 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB )->xTaskRunState != taskTASK_NOT_RUNNING ) ? ( pdTRUE ) : ( pdFALSE ) )
316 /* Indicates that the task is an Idle task. */
317 #define taskATTRIBUTE_IS_IDLE ( UBaseType_t ) ( 1U << 0U )
319 #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) )
320 #define portGET_CRITICAL_NESTING_COUNT( xCoreID ) ( pxCurrentTCBs[ ( xCoreID ) ]->uxCriticalNesting )
321 #define portSET_CRITICAL_NESTING_COUNT( xCoreID, x ) ( pxCurrentTCBs[ ( xCoreID ) ]->uxCriticalNesting = ( x ) )
322 #define portINCREMENT_CRITICAL_NESTING_COUNT( xCoreID ) ( pxCurrentTCBs[ ( xCoreID ) ]->uxCriticalNesting++ )
323 #define portDECREMENT_CRITICAL_NESTING_COUNT( xCoreID ) ( pxCurrentTCBs[ ( xCoreID ) ]->uxCriticalNesting-- )
324 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) */
326 #define taskBITS_PER_BYTE ( ( size_t ) 8 )
328 #if ( configNUMBER_OF_CORES > 1 )
330 /* Yields the given core. This must be called from a critical section and xCoreID
331 * must be valid. This macro is not required in single core since there is only
332 * one core to yield. */
333 #define prvYieldCore( xCoreID ) \
335 if( ( xCoreID ) == ( BaseType_t ) portGET_CORE_ID() ) \
337 /* Pending a yield for this core since it is in the critical section. */ \
338 xYieldPendings[ ( xCoreID ) ] = pdTRUE; \
342 /* Request other core to yield if it is not requested before. */ \
343 if( pxCurrentTCBs[ ( xCoreID ) ]->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD ) \
345 portYIELD_CORE( xCoreID ); \
346 pxCurrentTCBs[ ( xCoreID ) ]->xTaskRunState = taskTASK_SCHEDULED_TO_YIELD; \
350 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
351 /*-----------------------------------------------------------*/
354 * Task control block. A task control block (TCB) is allocated for each task,
355 * and stores task state information, including a pointer to the task's context
356 * (the task's run time environment, including register values)
358 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
360 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. */
362 #if ( portUSING_MPU_WRAPPERS == 1 )
363 xMPU_SETTINGS xMPUSettings; /**< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
366 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
367 UBaseType_t uxCoreAffinityMask; /**< Used to link the task to certain cores. UBaseType_t must have greater than or equal to the number of bits as configNUMBER_OF_CORES. */
370 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 ). */
371 ListItem_t xEventListItem; /**< Used to reference a task from an event list. */
372 UBaseType_t uxPriority; /**< The priority of the task. 0 is the lowest priority. */
373 StackType_t * pxStack; /**< Points to the start of the stack. */
374 #if ( configNUMBER_OF_CORES > 1 )
375 volatile BaseType_t xTaskRunState; /**< Used to identify the core the task is running on, if the task is running. Otherwise, identifies the task's state - not running or yielding. */
376 UBaseType_t uxTaskAttributes; /**< Task's attributes - currently used to identify the idle tasks. */
378 char pcTaskName[ configMAX_TASK_NAME_LEN ]; /**< Descriptive name given to the task when created. Facilitates debugging only. */
380 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
381 BaseType_t xPreemptionDisable; /**< Used to prevent the task from being preempted. */
384 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
385 StackType_t * pxEndOfStack; /**< Points to the highest valid address for the stack. */
388 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
389 UBaseType_t uxCriticalNesting; /**< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
392 #if ( configUSE_TRACE_FACILITY == 1 )
393 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. */
394 UBaseType_t uxTaskNumber; /**< Stores a number specifically for use by third party trace code. */
397 #if ( configUSE_MUTEXES == 1 )
398 UBaseType_t uxBasePriority; /**< The priority last assigned to the task - used by the priority inheritance mechanism. */
399 UBaseType_t uxMutexesHeld;
402 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
403 TaskHookFunction_t pxTaskTag;
406 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
407 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
410 #if ( configGENERATE_RUN_TIME_STATS == 1 )
411 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /**< Stores the amount of time the task has spent in the Running state. */
414 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
415 configTLS_BLOCK_TYPE xTLSBlock; /**< Memory block used as Thread Local Storage (TLS) Block for the task. */
418 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
419 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
420 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
423 /* See the comments in FreeRTOS.h with the definition of
424 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
425 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
426 uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
429 #if ( INCLUDE_xTaskAbortDelay == 1 )
430 uint8_t ucDelayAborted;
433 #if ( configUSE_POSIX_ERRNO == 1 )
438 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
439 * below to enable the use of older kernel aware debuggers. */
440 typedef tskTCB TCB_t;
442 #if ( configNUMBER_OF_CORES == 1 )
443 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
444 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
445 /* coverity[misra_c_2012_rule_8_4_violation] */
446 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
448 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
449 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
450 /* coverity[misra_c_2012_rule_8_4_violation] */
451 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ];
452 #define pxCurrentTCB xTaskGetCurrentTaskHandle()
455 /* Lists for ready and blocked tasks. --------------------
456 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
457 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
458 * the static qualifier. */
459 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
460 PRIVILEGED_DATA static List_t xDelayedTaskList1; /**< Delayed tasks. */
461 PRIVILEGED_DATA static List_t xDelayedTaskList2; /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
462 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /**< Points to the delayed task list currently being used. */
463 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. */
464 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. */
466 #if ( INCLUDE_vTaskDelete == 1 )
468 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
469 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
473 #if ( INCLUDE_vTaskSuspend == 1 )
475 PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
479 /* Global POSIX errno. Its value is changed upon context switching to match
480 * the errno of the currently running task. */
481 #if ( configUSE_POSIX_ERRNO == 1 )
482 int FreeRTOS_errno = 0;
485 /* Other file private variables. --------------------------------*/
486 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
487 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
488 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
489 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
490 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
491 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
492 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
493 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
494 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
495 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandles[ configNUMBER_OF_CORES ]; /**< Holds the handles of the idle tasks. The idle tasks are created automatically when the scheduler is started. */
497 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
498 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
499 * to determine the number of priority lists to read back from the remote target. */
500 static const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
502 /* Context switches are held pending while the scheduler is suspended. Also,
503 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
504 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
505 * If an interrupt needs to unblock a task while the scheduler is suspended then it
506 * moves the task's event list item into the xPendingReadyList, ready for the
507 * kernel to move the task from the pending ready list into the real ready list
508 * when the scheduler is unsuspended. The pending ready list itself can only be
509 * accessed from a critical section.
511 * Updates to uxSchedulerSuspended must be protected by both the task lock and the ISR lock
512 * and must not be done from an ISR. Reads must be protected by either lock and may be done
513 * from either an ISR or a task. */
514 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
516 #if ( configGENERATE_RUN_TIME_STATS == 1 )
518 /* Do not move these variables to function scope as doing so prevents the
519 * code working with debuggers that need to remove the static qualifier. */
520 PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the value of a timer/counter the last time a task was switched in. */
521 PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the total amount of execution time as defined by the run time counter clock. */
525 /*-----------------------------------------------------------*/
527 /* File private functions. --------------------------------*/
530 * Creates the idle tasks during scheduler start.
532 static BaseType_t prvCreateIdleTasks( void );
534 #if ( configNUMBER_OF_CORES > 1 )
537 * Checks to see if another task moved the current task out of the ready
538 * list while it was waiting to enter a critical section and yields, if so.
540 static void prvCheckForRunStateChange( void );
541 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
543 #if ( configNUMBER_OF_CORES > 1 )
546 * Yields a core, or cores if multiple priorities are not allowed to run
547 * simultaneously, to allow the task pxTCB to run.
549 static void prvYieldForTask( const TCB_t * pxTCB );
550 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
552 #if ( configNUMBER_OF_CORES > 1 )
555 * Selects the highest priority available task for the given core.
557 static void prvSelectHighestPriorityTask( BaseType_t xCoreID );
558 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
561 * Utility task that simply returns pdTRUE if the task referenced by xTask is
562 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
563 * is in any other state.
565 #if ( INCLUDE_vTaskSuspend == 1 )
567 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
569 #endif /* INCLUDE_vTaskSuspend */
572 * Utility to ready all the lists used by the scheduler. This is called
573 * automatically upon the creation of the first task.
575 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
578 * The idle task, which as all tasks is implemented as a never ending loop.
579 * The idle task is automatically created and added to the ready lists upon
580 * creation of the first user task.
582 * In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks are also
583 * created to ensure that each core has an idle task to run when no other
584 * task is available to run.
586 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
587 * language extensions. The equivalent prototype for these functions are:
589 * void prvIdleTask( void *pvParameters );
590 * void prvPassiveIdleTask( void *pvParameters );
593 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
594 #if ( configNUMBER_OF_CORES > 1 )
595 static portTASK_FUNCTION_PROTO( prvPassiveIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
599 * Utility to free all memory allocated by the scheduler to hold a TCB,
600 * including the stack pointed to by the TCB.
602 * This does not free memory allocated by the task itself (i.e. memory
603 * allocated by calls to pvPortMalloc from within the tasks application code).
605 #if ( INCLUDE_vTaskDelete == 1 )
607 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
612 * Used only by the idle task. This checks to see if anything has been placed
613 * in the list of tasks waiting to be deleted. If so the task is cleaned up
614 * and its TCB deleted.
616 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
619 * The currently executing task is entering the Blocked state. Add the task to
620 * either the current or the overflow delayed task list.
622 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
623 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
626 * Fills an TaskStatus_t structure with information on each task that is
627 * referenced from the pxList list (which may be a ready list, a delayed list,
628 * a suspended list, etc.).
630 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
631 * NORMAL APPLICATION CODE.
633 #if ( configUSE_TRACE_FACILITY == 1 )
635 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
637 eTaskState eState ) PRIVILEGED_FUNCTION;
642 * Searches pxList for a task with name pcNameToQuery - returning a handle to
643 * the task if it is found, or NULL if the task is not found.
645 #if ( INCLUDE_xTaskGetHandle == 1 )
647 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
648 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
653 * When a task is created, the stack of the task is filled with a known value.
654 * This function determines the 'high water mark' of the task stack by
655 * determining how much of the stack remains at the original preset value.
657 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
659 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
664 * Return the amount of time, in ticks, that will pass before the kernel will
665 * next move a task from the Blocked state to the Running state or before the
666 * tick count overflows (whichever is earlier).
668 * This conditional compilation should use inequality to 0, not equality to 1.
669 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
670 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
671 * set to a value other than 1.
673 #if ( configUSE_TICKLESS_IDLE != 0 )
675 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
680 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
681 * will exit the Blocked state.
683 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
685 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
688 * Helper function used to pad task names with spaces when printing out
689 * human readable tables of task information.
691 static char * prvWriteNameToBuffer( char * pcBuffer,
692 const char * pcTaskName ) PRIVILEGED_FUNCTION;
697 * Called after a Task_t structure has been allocated either statically or
698 * dynamically to fill in the structure's members.
700 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
701 const char * const pcName,
702 const configSTACK_DEPTH_TYPE uxStackDepth,
703 void * const pvParameters,
704 UBaseType_t uxPriority,
705 TaskHandle_t * const pxCreatedTask,
707 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
710 * Called after a new task has been created and initialised to place the task
711 * under the control of the scheduler.
713 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
716 * Create a task with static buffer for both TCB and stack. Returns a handle to
717 * the task if it is created successfully. Otherwise, returns NULL.
719 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
720 static TCB_t * prvCreateStaticTask( TaskFunction_t pxTaskCode,
721 const char * const pcName,
722 const configSTACK_DEPTH_TYPE uxStackDepth,
723 void * const pvParameters,
724 UBaseType_t uxPriority,
725 StackType_t * const puxStackBuffer,
726 StaticTask_t * const pxTaskBuffer,
727 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
728 #endif /* #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
731 * Create a restricted task with static buffer for both TCB and stack. Returns
732 * a handle to the task if it is created successfully. Otherwise, returns NULL.
734 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
735 static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
736 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
737 #endif /* #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
740 * Create a restricted task with static buffer for task stack and allocated buffer
741 * for TCB. Returns a handle to the task if it is created successfully. Otherwise,
744 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
745 static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
746 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
747 #endif /* #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
750 * Create a task with allocated buffer for both TCB and stack. Returns a handle to
751 * the task if it is created successfully. Otherwise, returns NULL.
753 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
754 static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
755 const char * const pcName,
756 const configSTACK_DEPTH_TYPE uxStackDepth,
757 void * const pvParameters,
758 UBaseType_t uxPriority,
759 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
760 #endif /* #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) */
763 * freertos_tasks_c_additions_init() should only be called if the user definable
764 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
765 * called by the function.
767 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
769 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
773 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
774 extern void vApplicationPassiveIdleHook( void );
775 #endif /* #if ( configUSE_PASSIVE_IDLE_HOOK == 1 ) */
777 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
780 * Convert the snprintf return value to the number of characters
781 * written. The following are the possible cases:
783 * 1. The buffer supplied to snprintf is large enough to hold the
784 * generated string. The return value in this case is the number
785 * of characters actually written, not counting the terminating
787 * 2. The buffer supplied to snprintf is NOT large enough to hold
788 * the generated string. The return value in this case is the
789 * number of characters that would have been written if the
790 * buffer had been sufficiently large, not counting the
791 * terminating null character.
792 * 3. Encoding error. The return value in this case is a negative
795 * From 1 and 2 above ==> Only when the return value is non-negative
796 * and less than the supplied buffer length, the string has been
797 * completely written.
799 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
802 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
803 /*-----------------------------------------------------------*/
805 #if ( configNUMBER_OF_CORES > 1 )
806 static void prvCheckForRunStateChange( void )
808 UBaseType_t uxPrevCriticalNesting;
809 const TCB_t * pxThisTCB;
810 BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
812 /* This must only be called from within a task. */
813 portASSERT_IF_IN_ISR();
815 /* This function is always called with interrupts disabled
816 * so this is safe. */
817 pxThisTCB = pxCurrentTCBs[ xCoreID ];
819 while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD )
821 /* We are only here if we just entered a critical section
822 * or if we just suspended the scheduler, and another task
823 * has requested that we yield.
825 * This is slightly complicated since we need to save and restore
826 * the suspension and critical nesting counts, as well as release
827 * and reacquire the correct locks. And then, do it all over again
828 * if our state changed again during the reacquisition. */
829 uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT( xCoreID );
831 if( uxPrevCriticalNesting > 0U )
833 portSET_CRITICAL_NESTING_COUNT( xCoreID, 0U );
834 portRELEASE_ISR_LOCK( xCoreID );
838 /* The scheduler is suspended. uxSchedulerSuspended is updated
839 * only when the task is not requested to yield. */
840 mtCOVERAGE_TEST_MARKER();
843 portRELEASE_TASK_LOCK( xCoreID );
844 portMEMORY_BARRIER();
845 configASSERT( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD );
847 portENABLE_INTERRUPTS();
849 /* Enabling interrupts should cause this core to immediately service
850 * the pending interrupt and yield. After servicing the pending interrupt,
851 * the task needs to re-evaluate its run state within this loop, as
852 * other cores may have requested this task to yield, potentially altering
855 portDISABLE_INTERRUPTS();
857 xCoreID = ( BaseType_t ) portGET_CORE_ID();
858 portGET_TASK_LOCK( xCoreID );
859 portGET_ISR_LOCK( xCoreID );
861 portSET_CRITICAL_NESTING_COUNT( xCoreID, uxPrevCriticalNesting );
863 if( uxPrevCriticalNesting == 0U )
865 portRELEASE_ISR_LOCK( xCoreID );
869 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
871 /*-----------------------------------------------------------*/
873 #if ( configNUMBER_OF_CORES > 1 )
874 static void prvYieldForTask( const TCB_t * pxTCB )
876 BaseType_t xLowestPriorityToPreempt;
877 BaseType_t xCurrentCoreTaskPriority;
878 BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
880 const BaseType_t xCurrentCoreID = portGET_CORE_ID();
882 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
883 BaseType_t xYieldCount = 0;
884 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
886 /* This must be called from a critical section. */
887 configASSERT( portGET_CRITICAL_NESTING_COUNT( xCurrentCoreID ) > 0U );
889 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
891 /* No task should yield for this one if it is a lower priority
892 * than priority level of currently ready tasks. */
893 if( pxTCB->uxPriority >= uxTopReadyPriority )
895 /* Yield is not required for a task which is already running. */
896 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
899 xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
901 /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
902 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
903 --xLowestPriorityToPreempt;
905 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
907 xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
909 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
910 if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
912 xCurrentCoreTaskPriority = ( BaseType_t ) ( xCurrentCoreTaskPriority - 1 );
915 if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
917 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
918 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
921 if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
923 #if ( configUSE_CORE_AFFINITY == 1 )
924 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
927 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
928 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
931 xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
932 xLowestPriorityCore = xCoreID;
938 mtCOVERAGE_TEST_MARKER();
942 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
944 /* Yield all currently running non-idle tasks with a priority lower than
945 * the task that needs to run. */
946 if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
947 ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
949 prvYieldCore( xCoreID );
954 mtCOVERAGE_TEST_MARKER();
957 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
961 mtCOVERAGE_TEST_MARKER();
965 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
966 if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
967 #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
968 if( xLowestPriorityCore >= 0 )
969 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
971 prvYieldCore( xLowestPriorityCore );
974 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
975 /* Verify that the calling core always yields to higher priority tasks. */
976 if( ( ( pxCurrentTCBs[ xCurrentCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) &&
977 ( pxTCB->uxPriority > pxCurrentTCBs[ xCurrentCoreID ]->uxPriority ) )
979 configASSERT( ( xYieldPendings[ xCurrentCoreID ] == pdTRUE ) ||
980 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCurrentCoreID ] ) == pdFALSE ) );
985 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
986 /*-----------------------------------------------------------*/
988 #if ( configNUMBER_OF_CORES > 1 )
989 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
991 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
992 BaseType_t xTaskScheduled = pdFALSE;
993 BaseType_t xDecrementTopPriority = pdTRUE;
994 TCB_t * pxTCB = NULL;
996 #if ( configUSE_CORE_AFFINITY == 1 )
997 const TCB_t * pxPreviousTCB = NULL;
999 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1000 BaseType_t xPriorityDropped = pdFALSE;
1003 /* This function should be called when scheduler is running. */
1004 configASSERT( xSchedulerRunning == pdTRUE );
1006 /* A new task is created and a running task with the same priority yields
1007 * itself to run the new task. When a running task yields itself, it is still
1008 * in the ready list. This running task will be selected before the new task
1009 * since the new task is always added to the end of the ready list.
1010 * The other problem is that the running task still in the same position of
1011 * the ready list when it yields itself. It is possible that it will be selected
1012 * earlier then other tasks which waits longer than this task.
1014 * To fix these problems, the running task should be put to the end of the
1015 * ready list before searching for the ready task in the ready list. */
1016 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
1017 &pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
1019 ( void ) uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
1020 vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
1021 &pxCurrentTCBs[ xCoreID ]->xStateListItem );
1024 while( xTaskScheduled == pdFALSE )
1026 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1028 if( uxCurrentPriority < uxTopReadyPriority )
1030 /* We can't schedule any tasks, other than idle, that have a
1031 * priority lower than the priority of a task currently running
1032 * on another core. */
1033 uxCurrentPriority = tskIDLE_PRIORITY;
1038 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
1040 const List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
1041 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxReadyList );
1042 ListItem_t * pxIterator;
1044 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
1045 * must not be decremented any further. */
1046 xDecrementTopPriority = pdFALSE;
1048 for( pxIterator = listGET_HEAD_ENTRY( pxReadyList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
1050 /* MISRA Ref 11.5.3 [Void pointer assignment] */
1051 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1052 /* coverity[misra_c_2012_rule_11_5_violation] */
1053 pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
1055 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1057 /* When falling back to the idle priority because only one priority
1058 * level is allowed to run at a time, we should ONLY schedule the true
1059 * idle tasks, not user tasks at the idle priority. */
1060 if( uxCurrentPriority < uxTopReadyPriority )
1062 if( ( pxTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U )
1068 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1070 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
1072 #if ( configUSE_CORE_AFFINITY == 1 )
1073 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1076 /* If the task is not being executed by any core swap it in. */
1077 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
1078 #if ( configUSE_CORE_AFFINITY == 1 )
1079 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
1081 pxTCB->xTaskRunState = xCoreID;
1082 pxCurrentTCBs[ xCoreID ] = pxTCB;
1083 xTaskScheduled = pdTRUE;
1086 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
1088 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) );
1090 #if ( configUSE_CORE_AFFINITY == 1 )
1091 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1094 /* The task is already running on this core, mark it as scheduled. */
1095 pxTCB->xTaskRunState = xCoreID;
1096 xTaskScheduled = pdTRUE;
1101 /* This task is running on the core other than xCoreID. */
1102 mtCOVERAGE_TEST_MARKER();
1105 if( xTaskScheduled != pdFALSE )
1107 /* A task has been selected to run on this core. */
1114 if( xDecrementTopPriority != pdFALSE )
1116 uxTopReadyPriority--;
1117 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1119 xPriorityDropped = pdTRUE;
1125 /* There are configNUMBER_OF_CORES Idle tasks created when scheduler started.
1126 * The scheduler should be able to select a task to run when uxCurrentPriority
1127 * is tskIDLE_PRIORITY. uxCurrentPriority is never decreased to value blow
1128 * tskIDLE_PRIORITY. */
1129 if( uxCurrentPriority > tskIDLE_PRIORITY )
1131 uxCurrentPriority--;
1135 /* This function is called when idle task is not created. Break the
1136 * loop to prevent uxCurrentPriority overrun. */
1141 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1143 if( xTaskScheduled == pdTRUE )
1145 if( xPriorityDropped != pdFALSE )
1147 /* There may be several ready tasks that were being prevented from running because there was
1148 * a higher priority task running. Now that the last of the higher priority tasks is no longer
1149 * running, make sure all the other idle tasks yield. */
1152 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUMBER_OF_CORES; x++ )
1154 if( ( pxCurrentTCBs[ x ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1162 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1164 #if ( configUSE_CORE_AFFINITY == 1 )
1166 if( xTaskScheduled == pdTRUE )
1168 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
1170 /* A ready task was just evicted from this core. See if it can be
1171 * scheduled on any other core. */
1172 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
1173 BaseType_t xLowestPriority = ( BaseType_t ) pxPreviousTCB->uxPriority;
1174 BaseType_t xLowestPriorityCore = -1;
1177 if( ( pxPreviousTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1179 xLowestPriority = xLowestPriority - 1;
1182 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1184 /* pxPreviousTCB was removed from this core and this core is not excluded
1185 * from it's core affinity mask.
1187 * pxPreviousTCB is preempted by the new higher priority task
1188 * pxCurrentTCBs[ xCoreID ]. When searching a new core for pxPreviousTCB,
1189 * we do not need to look at the cores on which pxCurrentTCBs[ xCoreID ]
1190 * is allowed to run. The reason is - when more than one cores are
1191 * eligible for an incoming task, we preempt the core with the minimum
1192 * priority task. Because this core (i.e. xCoreID) was preempted for
1193 * pxCurrentTCBs[ xCoreID ], this means that all the others cores
1194 * where pxCurrentTCBs[ xCoreID ] can run, are running tasks with priority
1195 * no lower than pxPreviousTCB's priority. Therefore, the only cores where
1196 * which can be preempted for pxPreviousTCB are the ones where
1197 * pxCurrentTCBs[ xCoreID ] is not allowed to run (and obviously,
1198 * pxPreviousTCB is allowed to run).
1200 * This is an optimization which reduces the number of cores needed to be
1201 * searched for pxPreviousTCB to run. */
1202 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1206 /* pxPreviousTCB's core affinity mask is changed and it is no longer
1207 * allowed to run on this core. Searching all the cores in pxPreviousTCB's
1208 * new core affinity mask to find a core on which it can run. */
1211 uxCoreMap &= ( ( 1U << configNUMBER_OF_CORES ) - 1U );
1213 for( x = ( ( BaseType_t ) configNUMBER_OF_CORES - 1 ); x >= ( BaseType_t ) 0; x-- )
1215 UBaseType_t uxCore = ( UBaseType_t ) x;
1216 BaseType_t xTaskPriority;
1218 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << uxCore ) ) != 0U )
1220 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority;
1222 if( ( pxCurrentTCBs[ uxCore ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1224 xTaskPriority = xTaskPriority - ( BaseType_t ) 1;
1227 uxCoreMap &= ~( ( UBaseType_t ) 1U << uxCore );
1229 if( ( xTaskPriority < xLowestPriority ) &&
1230 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ] ) != pdFALSE ) &&
1231 ( xYieldPendings[ uxCore ] == pdFALSE ) )
1233 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1234 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1237 xLowestPriority = xTaskPriority;
1238 xLowestPriorityCore = ( BaseType_t ) uxCore;
1244 if( xLowestPriorityCore >= 0 )
1246 prvYieldCore( xLowestPriorityCore );
1251 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) */
1254 #endif /* ( configNUMBER_OF_CORES > 1 ) */
1256 /*-----------------------------------------------------------*/
1258 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1260 static TCB_t * prvCreateStaticTask( TaskFunction_t pxTaskCode,
1261 const char * const pcName,
1262 const configSTACK_DEPTH_TYPE uxStackDepth,
1263 void * const pvParameters,
1264 UBaseType_t uxPriority,
1265 StackType_t * const puxStackBuffer,
1266 StaticTask_t * const pxTaskBuffer,
1267 TaskHandle_t * const pxCreatedTask )
1271 configASSERT( puxStackBuffer != NULL );
1272 configASSERT( pxTaskBuffer != NULL );
1274 #if ( configASSERT_DEFINED == 1 )
1276 /* Sanity check that the size of the structure used to declare a
1277 * variable of type StaticTask_t equals the size of the real task
1279 volatile size_t xSize = sizeof( StaticTask_t );
1280 configASSERT( xSize == sizeof( TCB_t ) );
1281 ( void ) xSize; /* Prevent unused variable warning when configASSERT() is not used. */
1283 #endif /* configASSERT_DEFINED */
1285 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1287 /* The memory used for the task's TCB and stack are passed into this
1288 * function - use them. */
1289 /* MISRA Ref 11.3.1 [Misaligned access] */
1290 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
1291 /* coverity[misra_c_2012_rule_11_3_violation] */
1292 pxNewTCB = ( TCB_t * ) pxTaskBuffer;
1293 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1294 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1296 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1298 /* Tasks can be created statically or dynamically, so note this
1299 * task was created statically in case the task is later deleted. */
1300 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1302 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1304 prvInitialiseNewTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1313 /*-----------------------------------------------------------*/
1315 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1316 const char * const pcName,
1317 const configSTACK_DEPTH_TYPE uxStackDepth,
1318 void * const pvParameters,
1319 UBaseType_t uxPriority,
1320 StackType_t * const puxStackBuffer,
1321 StaticTask_t * const pxTaskBuffer )
1323 TaskHandle_t xReturn = NULL;
1326 traceENTER_xTaskCreateStatic( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer );
1328 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1330 if( pxNewTCB != NULL )
1332 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1334 /* Set the task's affinity before scheduling it. */
1335 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1339 prvAddNewTaskToReadyList( pxNewTCB );
1343 mtCOVERAGE_TEST_MARKER();
1346 traceRETURN_xTaskCreateStatic( xReturn );
1350 /*-----------------------------------------------------------*/
1352 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1353 TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1354 const char * const pcName,
1355 const configSTACK_DEPTH_TYPE uxStackDepth,
1356 void * const pvParameters,
1357 UBaseType_t uxPriority,
1358 StackType_t * const puxStackBuffer,
1359 StaticTask_t * const pxTaskBuffer,
1360 UBaseType_t uxCoreAffinityMask )
1362 TaskHandle_t xReturn = NULL;
1365 traceENTER_xTaskCreateStaticAffinitySet( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, uxCoreAffinityMask );
1367 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1369 if( pxNewTCB != NULL )
1371 /* Set the task's affinity before scheduling it. */
1372 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1374 prvAddNewTaskToReadyList( pxNewTCB );
1378 mtCOVERAGE_TEST_MARKER();
1381 traceRETURN_xTaskCreateStaticAffinitySet( xReturn );
1385 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1387 #endif /* SUPPORT_STATIC_ALLOCATION */
1388 /*-----------------------------------------------------------*/
1390 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1391 static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
1392 TaskHandle_t * const pxCreatedTask )
1396 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1397 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1399 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1401 /* Allocate space for the TCB. Where the memory comes from depends
1402 * on the implementation of the port malloc function and whether or
1403 * not static allocation is being used. */
1404 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1405 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1407 /* Store the stack location in the TCB. */
1408 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1410 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1412 /* Tasks can be created statically or dynamically, so note this
1413 * task was created statically in case the task is later deleted. */
1414 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1416 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1418 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1419 pxTaskDefinition->pcName,
1420 pxTaskDefinition->usStackDepth,
1421 pxTaskDefinition->pvParameters,
1422 pxTaskDefinition->uxPriority,
1423 pxCreatedTask, pxNewTCB,
1424 pxTaskDefinition->xRegions );
1433 /*-----------------------------------------------------------*/
1435 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1436 TaskHandle_t * pxCreatedTask )
1441 traceENTER_xTaskCreateRestrictedStatic( pxTaskDefinition, pxCreatedTask );
1443 configASSERT( pxTaskDefinition != NULL );
1445 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1447 if( pxNewTCB != NULL )
1449 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1451 /* Set the task's affinity before scheduling it. */
1452 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1456 prvAddNewTaskToReadyList( pxNewTCB );
1461 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1464 traceRETURN_xTaskCreateRestrictedStatic( xReturn );
1468 /*-----------------------------------------------------------*/
1470 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1471 BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1472 UBaseType_t uxCoreAffinityMask,
1473 TaskHandle_t * pxCreatedTask )
1478 traceENTER_xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1480 configASSERT( pxTaskDefinition != NULL );
1482 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1484 if( pxNewTCB != NULL )
1486 /* Set the task's affinity before scheduling it. */
1487 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1489 prvAddNewTaskToReadyList( pxNewTCB );
1494 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1497 traceRETURN_xTaskCreateRestrictedStaticAffinitySet( xReturn );
1501 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1503 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1504 /*-----------------------------------------------------------*/
1506 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1507 static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
1508 TaskHandle_t * const pxCreatedTask )
1512 configASSERT( pxTaskDefinition->puxStackBuffer );
1514 if( pxTaskDefinition->puxStackBuffer != NULL )
1516 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1517 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1518 /* coverity[misra_c_2012_rule_11_5_violation] */
1519 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1521 if( pxNewTCB != NULL )
1523 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1525 /* Store the stack location in the TCB. */
1526 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1528 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1530 /* Tasks can be created statically or dynamically, so note
1531 * this task had a statically allocated stack in case it is
1532 * later deleted. The TCB was allocated dynamically. */
1533 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1535 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1537 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1538 pxTaskDefinition->pcName,
1539 pxTaskDefinition->usStackDepth,
1540 pxTaskDefinition->pvParameters,
1541 pxTaskDefinition->uxPriority,
1542 pxCreatedTask, pxNewTCB,
1543 pxTaskDefinition->xRegions );
1553 /*-----------------------------------------------------------*/
1555 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1556 TaskHandle_t * pxCreatedTask )
1561 traceENTER_xTaskCreateRestricted( pxTaskDefinition, pxCreatedTask );
1563 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1565 if( pxNewTCB != NULL )
1567 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1569 /* Set the task's affinity before scheduling it. */
1570 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1572 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1574 prvAddNewTaskToReadyList( pxNewTCB );
1580 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1583 traceRETURN_xTaskCreateRestricted( xReturn );
1587 /*-----------------------------------------------------------*/
1589 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1590 BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1591 UBaseType_t uxCoreAffinityMask,
1592 TaskHandle_t * pxCreatedTask )
1597 traceENTER_xTaskCreateRestrictedAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1599 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1601 if( pxNewTCB != NULL )
1603 /* Set the task's affinity before scheduling it. */
1604 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1606 prvAddNewTaskToReadyList( pxNewTCB );
1612 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1615 traceRETURN_xTaskCreateRestrictedAffinitySet( xReturn );
1619 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1622 #endif /* portUSING_MPU_WRAPPERS */
1623 /*-----------------------------------------------------------*/
1625 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1626 static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
1627 const char * const pcName,
1628 const configSTACK_DEPTH_TYPE uxStackDepth,
1629 void * const pvParameters,
1630 UBaseType_t uxPriority,
1631 TaskHandle_t * const pxCreatedTask )
1635 /* If the stack grows down then allocate the stack then the TCB so the stack
1636 * does not grow into the TCB. Likewise if the stack grows up then allocate
1637 * the TCB then the stack. */
1638 #if ( portSTACK_GROWTH > 0 )
1640 /* Allocate space for the TCB. Where the memory comes from depends on
1641 * the implementation of the port malloc function and whether or not static
1642 * allocation is being used. */
1643 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1644 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1645 /* coverity[misra_c_2012_rule_11_5_violation] */
1646 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1648 if( pxNewTCB != NULL )
1650 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1652 /* Allocate space for the stack used by the task being created.
1653 * The base of the stack memory stored in the TCB so the task can
1654 * be deleted later if required. */
1655 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1656 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1657 /* coverity[misra_c_2012_rule_11_5_violation] */
1658 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) uxStackDepth ) * sizeof( StackType_t ) ) );
1660 if( pxNewTCB->pxStack == NULL )
1662 /* Could not allocate the stack. Delete the allocated TCB. */
1663 vPortFree( pxNewTCB );
1668 #else /* portSTACK_GROWTH */
1670 StackType_t * pxStack;
1672 /* Allocate space for the stack used by the task being created. */
1673 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1674 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1675 /* coverity[misra_c_2012_rule_11_5_violation] */
1676 pxStack = pvPortMallocStack( ( ( ( size_t ) uxStackDepth ) * sizeof( StackType_t ) ) );
1678 if( pxStack != NULL )
1680 /* Allocate space for the TCB. */
1681 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1682 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1683 /* coverity[misra_c_2012_rule_11_5_violation] */
1684 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1686 if( pxNewTCB != NULL )
1688 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1690 /* Store the stack location in the TCB. */
1691 pxNewTCB->pxStack = pxStack;
1695 /* The stack cannot be used as the TCB was not created. Free
1697 vPortFreeStack( pxStack );
1705 #endif /* portSTACK_GROWTH */
1707 if( pxNewTCB != NULL )
1709 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1711 /* Tasks can be created statically or dynamically, so note this
1712 * task was created dynamically in case it is later deleted. */
1713 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1715 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1717 prvInitialiseNewTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1722 /*-----------------------------------------------------------*/
1724 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1725 const char * const pcName,
1726 const configSTACK_DEPTH_TYPE uxStackDepth,
1727 void * const pvParameters,
1728 UBaseType_t uxPriority,
1729 TaskHandle_t * const pxCreatedTask )
1734 traceENTER_xTaskCreate( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
1736 pxNewTCB = prvCreateTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
1738 if( pxNewTCB != NULL )
1740 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1742 /* Set the task's affinity before scheduling it. */
1743 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1747 prvAddNewTaskToReadyList( pxNewTCB );
1752 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1755 traceRETURN_xTaskCreate( xReturn );
1759 /*-----------------------------------------------------------*/
1761 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1762 BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1763 const char * const pcName,
1764 const configSTACK_DEPTH_TYPE uxStackDepth,
1765 void * const pvParameters,
1766 UBaseType_t uxPriority,
1767 UBaseType_t uxCoreAffinityMask,
1768 TaskHandle_t * const pxCreatedTask )
1773 traceENTER_xTaskCreateAffinitySet( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, uxCoreAffinityMask, pxCreatedTask );
1775 pxNewTCB = prvCreateTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
1777 if( pxNewTCB != NULL )
1779 /* Set the task's affinity before scheduling it. */
1780 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1782 prvAddNewTaskToReadyList( pxNewTCB );
1787 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1790 traceRETURN_xTaskCreateAffinitySet( xReturn );
1794 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1796 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1797 /*-----------------------------------------------------------*/
1799 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1800 const char * const pcName,
1801 const configSTACK_DEPTH_TYPE uxStackDepth,
1802 void * const pvParameters,
1803 UBaseType_t uxPriority,
1804 TaskHandle_t * const pxCreatedTask,
1806 const MemoryRegion_t * const xRegions )
1808 StackType_t * pxTopOfStack;
1811 #if ( portUSING_MPU_WRAPPERS == 1 )
1812 /* Should the task be created in privileged mode? */
1813 BaseType_t xRunPrivileged;
1815 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1817 xRunPrivileged = pdTRUE;
1821 xRunPrivileged = pdFALSE;
1823 uxPriority &= ~portPRIVILEGE_BIT;
1824 #endif /* portUSING_MPU_WRAPPERS == 1 */
1826 /* Avoid dependency on memset() if it is not required. */
1827 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1829 /* Fill the stack with a known value to assist debugging. */
1830 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) uxStackDepth * sizeof( StackType_t ) );
1832 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1834 /* Calculate the top of stack address. This depends on whether the stack
1835 * grows from high memory to low (as per the 80x86) or vice versa.
1836 * portSTACK_GROWTH is used to make the result positive or negative as required
1838 #if ( portSTACK_GROWTH < 0 )
1840 pxTopOfStack = &( pxNewTCB->pxStack[ uxStackDepth - ( configSTACK_DEPTH_TYPE ) 1 ] );
1841 pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
1843 /* Check the alignment of the calculated top of stack is correct. */
1844 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0U ) );
1846 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1848 /* Also record the stack's high address, which may assist
1850 pxNewTCB->pxEndOfStack = pxTopOfStack;
1852 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1854 #else /* portSTACK_GROWTH */
1856 pxTopOfStack = pxNewTCB->pxStack;
1857 pxTopOfStack = ( StackType_t * ) ( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) + portBYTE_ALIGNMENT_MASK ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
1859 /* Check the alignment of the calculated top of stack is correct. */
1860 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0U ) );
1862 /* The other extreme of the stack space is required if stack checking is
1864 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( uxStackDepth - ( configSTACK_DEPTH_TYPE ) 1 );
1866 #endif /* portSTACK_GROWTH */
1868 /* Store the task name in the TCB. */
1869 if( pcName != NULL )
1871 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1873 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1875 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1876 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1877 * string is not accessible (extremely unlikely). */
1878 if( pcName[ x ] == ( char ) 0x00 )
1884 mtCOVERAGE_TEST_MARKER();
1888 /* Ensure the name string is terminated in the case that the string length
1889 * was greater or equal to configMAX_TASK_NAME_LEN. */
1890 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1U ] = '\0';
1894 mtCOVERAGE_TEST_MARKER();
1897 /* This is used as an array index so must ensure it's not too large. */
1898 configASSERT( uxPriority < configMAX_PRIORITIES );
1900 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1902 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1906 mtCOVERAGE_TEST_MARKER();
1909 pxNewTCB->uxPriority = uxPriority;
1910 #if ( configUSE_MUTEXES == 1 )
1912 pxNewTCB->uxBasePriority = uxPriority;
1914 #endif /* configUSE_MUTEXES */
1916 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1917 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1919 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1920 * back to the containing TCB from a generic item in a list. */
1921 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1923 /* Event lists are always in priority order. */
1924 listSET_LIST_ITEM_VALUE( &( pxNewTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority );
1925 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1927 #if ( portUSING_MPU_WRAPPERS == 1 )
1929 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, uxStackDepth );
1933 /* Avoid compiler warning about unreferenced parameter. */
1938 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
1940 /* Allocate and initialize memory for the task's TLS Block. */
1941 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
1945 /* Initialize the TCB stack to look as if the task was already running,
1946 * but had been interrupted by the scheduler. The return address is set
1947 * to the start of the task function. Once the stack has been initialised
1948 * the top of stack variable is updated. */
1949 #if ( portUSING_MPU_WRAPPERS == 1 )
1951 /* If the port has capability to detect stack overflow,
1952 * pass the stack end address to the stack initialization
1953 * function as well. */
1954 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1956 #if ( portSTACK_GROWTH < 0 )
1958 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1960 #else /* portSTACK_GROWTH */
1962 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1964 #endif /* portSTACK_GROWTH */
1966 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1968 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1970 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1972 #else /* portUSING_MPU_WRAPPERS */
1974 /* If the port has capability to detect stack overflow,
1975 * pass the stack end address to the stack initialization
1976 * function as well. */
1977 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1979 #if ( portSTACK_GROWTH < 0 )
1981 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1983 #else /* portSTACK_GROWTH */
1985 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1987 #endif /* portSTACK_GROWTH */
1989 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1991 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1993 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1995 #endif /* portUSING_MPU_WRAPPERS */
1997 /* Initialize task state and task attributes. */
1998 #if ( configNUMBER_OF_CORES > 1 )
2000 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2002 /* Is this an idle task? */
2003 if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvPassiveIdleTask ) )
2005 pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
2008 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
2010 if( pxCreatedTask != NULL )
2012 /* Pass the handle out in an anonymous way. The handle can be used to
2013 * change the created task's priority, delete the created task, etc.*/
2014 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
2018 mtCOVERAGE_TEST_MARKER();
2021 /*-----------------------------------------------------------*/
2023 #if ( configNUMBER_OF_CORES == 1 )
2025 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
2027 /* Ensure interrupts don't access the task lists while the lists are being
2029 taskENTER_CRITICAL();
2031 uxCurrentNumberOfTasks = ( UBaseType_t ) ( uxCurrentNumberOfTasks + 1U );
2033 if( pxCurrentTCB == NULL )
2035 /* There are no other tasks, or all the other tasks are in
2036 * the suspended state - make this the current task. */
2037 pxCurrentTCB = pxNewTCB;
2039 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2041 /* This is the first task to be created so do the preliminary
2042 * initialisation required. We will not recover if this call
2043 * fails, but we will report the failure. */
2044 prvInitialiseTaskLists();
2048 mtCOVERAGE_TEST_MARKER();
2053 /* If the scheduler is not already running, make this task the
2054 * current task if it is the highest priority task to be created
2056 if( xSchedulerRunning == pdFALSE )
2058 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
2060 pxCurrentTCB = pxNewTCB;
2064 mtCOVERAGE_TEST_MARKER();
2069 mtCOVERAGE_TEST_MARKER();
2075 #if ( configUSE_TRACE_FACILITY == 1 )
2077 /* Add a counter into the TCB for tracing only. */
2078 pxNewTCB->uxTCBNumber = uxTaskNumber;
2080 #endif /* configUSE_TRACE_FACILITY */
2081 traceTASK_CREATE( pxNewTCB );
2083 prvAddTaskToReadyList( pxNewTCB );
2085 portSETUP_TCB( pxNewTCB );
2087 taskEXIT_CRITICAL();
2089 if( xSchedulerRunning != pdFALSE )
2091 /* If the created task is of a higher priority than the current task
2092 * then it should run now. */
2093 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2097 mtCOVERAGE_TEST_MARKER();
2101 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2103 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
2105 /* Ensure interrupts don't access the task lists while the lists are being
2107 taskENTER_CRITICAL();
2109 uxCurrentNumberOfTasks++;
2111 if( xSchedulerRunning == pdFALSE )
2113 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2115 /* This is the first task to be created so do the preliminary
2116 * initialisation required. We will not recover if this call
2117 * fails, but we will report the failure. */
2118 prvInitialiseTaskLists();
2122 mtCOVERAGE_TEST_MARKER();
2125 /* All the cores start with idle tasks before the SMP scheduler
2126 * is running. Idle tasks are assigned to cores when they are
2127 * created in prvCreateIdleTasks(). */
2132 #if ( configUSE_TRACE_FACILITY == 1 )
2134 /* Add a counter into the TCB for tracing only. */
2135 pxNewTCB->uxTCBNumber = uxTaskNumber;
2137 #endif /* configUSE_TRACE_FACILITY */
2138 traceTASK_CREATE( pxNewTCB );
2140 prvAddTaskToReadyList( pxNewTCB );
2142 portSETUP_TCB( pxNewTCB );
2144 if( xSchedulerRunning != pdFALSE )
2146 /* If the created task is of a higher priority than another
2147 * currently running task and preemption is on then it should
2149 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2153 mtCOVERAGE_TEST_MARKER();
2156 taskEXIT_CRITICAL();
2159 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2160 /*-----------------------------------------------------------*/
2162 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
2164 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
2167 size_t uxCharsWritten;
2169 if( iSnprintfReturnValue < 0 )
2171 /* Encoding error - Return 0 to indicate that nothing
2172 * was written to the buffer. */
2175 else if( iSnprintfReturnValue >= ( int ) n )
2177 /* This is the case when the supplied buffer is not
2178 * large to hold the generated string. Return the
2179 * number of characters actually written without
2180 * counting the terminating NULL character. */
2181 uxCharsWritten = n - 1U;
2185 /* Complete string was written to the buffer. */
2186 uxCharsWritten = ( size_t ) iSnprintfReturnValue;
2189 return uxCharsWritten;
2192 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
2193 /*-----------------------------------------------------------*/
2195 #if ( INCLUDE_vTaskDelete == 1 )
2197 void vTaskDelete( TaskHandle_t xTaskToDelete )
2200 BaseType_t xDeleteTCBInIdleTask = pdFALSE;
2201 BaseType_t xTaskIsRunningOrYielding;
2203 traceENTER_vTaskDelete( xTaskToDelete );
2205 taskENTER_CRITICAL();
2207 /* If null is passed in here then it is the calling task that is
2209 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
2210 configASSERT( pxTCB != NULL );
2212 /* Remove task from the ready/delayed list. */
2213 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2215 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2219 mtCOVERAGE_TEST_MARKER();
2222 /* Is the task waiting on an event also? */
2223 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2225 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2229 mtCOVERAGE_TEST_MARKER();
2232 /* Increment the uxTaskNumber also so kernel aware debuggers can
2233 * detect that the task lists need re-generating. This is done before
2234 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
2238 /* Use temp variable as distinct sequence points for reading volatile
2239 * variables prior to a logical operator to ensure compliance with
2240 * MISRA C 2012 Rule 13.5. */
2241 xTaskIsRunningOrYielding = taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB );
2243 /* If the task is running (or yielding), we must add it to the
2244 * termination list so that an idle task can delete it when it is
2245 * no longer running. */
2246 if( ( xSchedulerRunning != pdFALSE ) && ( xTaskIsRunningOrYielding != pdFALSE ) )
2248 /* A running task or a task which is scheduled to yield is being
2249 * deleted. This cannot complete when the task is still running
2250 * on a core, as a context switch to another task is required.
2251 * Place the task in the termination list. The idle task will check
2252 * the termination list and free up any memory allocated by the
2253 * scheduler for the TCB and stack of the deleted task. */
2254 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
2256 /* Increment the ucTasksDeleted variable so the idle task knows
2257 * there is a task that has been deleted and that it should therefore
2258 * check the xTasksWaitingTermination list. */
2259 ++uxDeletedTasksWaitingCleanUp;
2261 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
2262 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
2263 traceTASK_DELETE( pxTCB );
2265 /* Delete the task TCB in idle task. */
2266 xDeleteTCBInIdleTask = pdTRUE;
2268 /* The pre-delete hook is primarily for the Windows simulator,
2269 * in which Windows specific clean up operations are performed,
2270 * after which it is not possible to yield away from this task -
2271 * hence xYieldPending is used to latch that a context switch is
2273 #if ( configNUMBER_OF_CORES == 1 )
2274 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
2276 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
2279 /* In the case of SMP, it is possible that the task being deleted
2280 * is running on another core. We must evict the task before
2281 * exiting the critical section to ensure that the task cannot
2282 * take an action which puts it back on ready/state/event list,
2283 * thereby nullifying the delete operation. Once evicted, the
2284 * task won't be scheduled ever as it will no longer be on the
2286 #if ( configNUMBER_OF_CORES > 1 )
2288 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2290 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2292 configASSERT( uxSchedulerSuspended == 0 );
2293 taskYIELD_WITHIN_API();
2297 prvYieldCore( pxTCB->xTaskRunState );
2301 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
2305 --uxCurrentNumberOfTasks;
2306 traceTASK_DELETE( pxTCB );
2308 /* Reset the next expected unblock time in case it referred to
2309 * the task that has just been deleted. */
2310 prvResetNextTaskUnblockTime();
2313 taskEXIT_CRITICAL();
2315 /* If the task is not deleting itself, call prvDeleteTCB from outside of
2316 * critical section. If a task deletes itself, prvDeleteTCB is called
2317 * from prvCheckTasksWaitingTermination which is called from Idle task. */
2318 if( xDeleteTCBInIdleTask != pdTRUE )
2320 prvDeleteTCB( pxTCB );
2323 /* Force a reschedule if it is the currently running task that has just
2325 #if ( configNUMBER_OF_CORES == 1 )
2327 if( xSchedulerRunning != pdFALSE )
2329 if( pxTCB == pxCurrentTCB )
2331 configASSERT( uxSchedulerSuspended == 0 );
2332 taskYIELD_WITHIN_API();
2336 mtCOVERAGE_TEST_MARKER();
2340 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2342 traceRETURN_vTaskDelete();
2345 #endif /* INCLUDE_vTaskDelete */
2346 /*-----------------------------------------------------------*/
2348 #if ( INCLUDE_xTaskDelayUntil == 1 )
2350 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2351 const TickType_t xTimeIncrement )
2353 TickType_t xTimeToWake;
2354 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2356 traceENTER_xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
2358 configASSERT( pxPreviousWakeTime );
2359 configASSERT( ( xTimeIncrement > 0U ) );
2363 /* Minor optimisation. The tick count cannot change in this
2365 const TickType_t xConstTickCount = xTickCount;
2367 configASSERT( uxSchedulerSuspended == 1U );
2369 /* Generate the tick time at which the task wants to wake. */
2370 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2372 if( xConstTickCount < *pxPreviousWakeTime )
2374 /* The tick count has overflowed since this function was
2375 * lasted called. In this case the only time we should ever
2376 * actually delay is if the wake time has also overflowed,
2377 * and the wake time is greater than the tick time. When this
2378 * is the case it is as if neither time had overflowed. */
2379 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2381 xShouldDelay = pdTRUE;
2385 mtCOVERAGE_TEST_MARKER();
2390 /* The tick time has not overflowed. In this case we will
2391 * delay if either the wake time has overflowed, and/or the
2392 * tick time is less than the wake time. */
2393 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2395 xShouldDelay = pdTRUE;
2399 mtCOVERAGE_TEST_MARKER();
2403 /* Update the wake time ready for the next call. */
2404 *pxPreviousWakeTime = xTimeToWake;
2406 if( xShouldDelay != pdFALSE )
2408 traceTASK_DELAY_UNTIL( xTimeToWake );
2410 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2411 * the time to wake, so subtract the current tick count. */
2412 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2416 mtCOVERAGE_TEST_MARKER();
2419 xAlreadyYielded = xTaskResumeAll();
2421 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2422 * have put ourselves to sleep. */
2423 if( xAlreadyYielded == pdFALSE )
2425 taskYIELD_WITHIN_API();
2429 mtCOVERAGE_TEST_MARKER();
2432 traceRETURN_xTaskDelayUntil( xShouldDelay );
2434 return xShouldDelay;
2437 #endif /* INCLUDE_xTaskDelayUntil */
2438 /*-----------------------------------------------------------*/
2440 #if ( INCLUDE_vTaskDelay == 1 )
2442 void vTaskDelay( const TickType_t xTicksToDelay )
2444 BaseType_t xAlreadyYielded = pdFALSE;
2446 traceENTER_vTaskDelay( xTicksToDelay );
2448 /* A delay time of zero just forces a reschedule. */
2449 if( xTicksToDelay > ( TickType_t ) 0U )
2453 configASSERT( uxSchedulerSuspended == 1U );
2457 /* A task that is removed from the event list while the
2458 * scheduler is suspended will not get placed in the ready
2459 * list or removed from the blocked list until the scheduler
2462 * This task cannot be in an event list as it is the currently
2463 * executing task. */
2464 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2466 xAlreadyYielded = xTaskResumeAll();
2470 mtCOVERAGE_TEST_MARKER();
2473 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2474 * have put ourselves to sleep. */
2475 if( xAlreadyYielded == pdFALSE )
2477 taskYIELD_WITHIN_API();
2481 mtCOVERAGE_TEST_MARKER();
2484 traceRETURN_vTaskDelay();
2487 #endif /* INCLUDE_vTaskDelay */
2488 /*-----------------------------------------------------------*/
2490 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2492 eTaskState eTaskGetState( TaskHandle_t xTask )
2495 List_t const * pxStateList;
2496 List_t const * pxEventList;
2497 List_t const * pxDelayedList;
2498 List_t const * pxOverflowedDelayedList;
2499 const TCB_t * const pxTCB = xTask;
2501 traceENTER_eTaskGetState( xTask );
2503 configASSERT( pxTCB != NULL );
2505 #if ( configNUMBER_OF_CORES == 1 )
2506 if( pxTCB == pxCurrentTCB )
2508 /* The task calling this function is querying its own state. */
2514 taskENTER_CRITICAL();
2516 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2517 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2518 pxDelayedList = pxDelayedTaskList;
2519 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2521 taskEXIT_CRITICAL();
2523 if( pxEventList == &xPendingReadyList )
2525 /* The task has been placed on the pending ready list, so its
2526 * state is eReady regardless of what list the task's state list
2527 * item is currently placed on. */
2530 else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2532 /* The task being queried is referenced from one of the Blocked
2537 #if ( INCLUDE_vTaskSuspend == 1 )
2538 else if( pxStateList == &xSuspendedTaskList )
2540 /* The task being queried is referenced from the suspended
2541 * list. Is it genuinely suspended or is it blocked
2543 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2545 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2549 /* The task does not appear on the event list item of
2550 * and of the RTOS objects, but could still be in the
2551 * blocked state if it is waiting on its notification
2552 * rather than waiting on an object. If not, is
2554 eReturn = eSuspended;
2556 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2558 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2565 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2567 eReturn = eSuspended;
2569 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2576 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2578 #if ( INCLUDE_vTaskDelete == 1 )
2579 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2581 /* The task being queried is referenced from the deleted
2582 * tasks list, or it is not referenced from any lists at
2590 #if ( configNUMBER_OF_CORES == 1 )
2592 /* If the task is not in any other state, it must be in the
2593 * Ready (including pending ready) state. */
2596 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2598 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2600 /* Is it actively running on a core? */
2605 /* If the task is not in any other state, it must be in the
2606 * Ready (including pending ready) state. */
2610 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2614 traceRETURN_eTaskGetState( eReturn );
2619 #endif /* INCLUDE_eTaskGetState */
2620 /*-----------------------------------------------------------*/
2622 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2624 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2626 TCB_t const * pxTCB;
2627 UBaseType_t uxReturn;
2629 traceENTER_uxTaskPriorityGet( xTask );
2631 portBASE_TYPE_ENTER_CRITICAL();
2633 /* If null is passed in here then it is the priority of the task
2634 * that called uxTaskPriorityGet() that is being queried. */
2635 pxTCB = prvGetTCBFromHandle( xTask );
2636 configASSERT( pxTCB != NULL );
2638 uxReturn = pxTCB->uxPriority;
2640 portBASE_TYPE_EXIT_CRITICAL();
2642 traceRETURN_uxTaskPriorityGet( uxReturn );
2647 #endif /* INCLUDE_uxTaskPriorityGet */
2648 /*-----------------------------------------------------------*/
2650 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2652 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2654 TCB_t const * pxTCB;
2655 UBaseType_t uxReturn;
2656 UBaseType_t uxSavedInterruptStatus;
2658 traceENTER_uxTaskPriorityGetFromISR( xTask );
2660 /* RTOS ports that support interrupt nesting have the concept of a
2661 * maximum system call (or maximum API call) interrupt priority.
2662 * Interrupts that are above the maximum system call priority are keep
2663 * permanently enabled, even when the RTOS kernel is in a critical section,
2664 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2665 * is defined in FreeRTOSConfig.h then
2666 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2667 * failure if a FreeRTOS API function is called from an interrupt that has
2668 * been assigned a priority above the configured maximum system call
2669 * priority. Only FreeRTOS functions that end in FromISR can be called
2670 * from interrupts that have been assigned a priority at or (logically)
2671 * below the maximum system call interrupt priority. FreeRTOS maintains a
2672 * separate interrupt safe API to ensure interrupt entry is as fast and as
2673 * simple as possible. More information (albeit Cortex-M specific) is
2674 * provided on the following link:
2675 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2676 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2678 /* MISRA Ref 4.7.1 [Return value shall be checked] */
2679 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
2680 /* coverity[misra_c_2012_directive_4_7_violation] */
2681 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
2683 /* If null is passed in here then it is the priority of the calling
2684 * task that is being queried. */
2685 pxTCB = prvGetTCBFromHandle( xTask );
2686 configASSERT( pxTCB != NULL );
2688 uxReturn = pxTCB->uxPriority;
2690 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2692 traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
2697 #endif /* INCLUDE_uxTaskPriorityGet */
2698 /*-----------------------------------------------------------*/
2700 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2702 UBaseType_t uxTaskBasePriorityGet( const TaskHandle_t xTask )
2704 TCB_t const * pxTCB;
2705 UBaseType_t uxReturn;
2707 traceENTER_uxTaskBasePriorityGet( xTask );
2709 portBASE_TYPE_ENTER_CRITICAL();
2711 /* If null is passed in here then it is the base priority of the task
2712 * that called uxTaskBasePriorityGet() that is being queried. */
2713 pxTCB = prvGetTCBFromHandle( xTask );
2714 configASSERT( pxTCB != NULL );
2716 uxReturn = pxTCB->uxBasePriority;
2718 portBASE_TYPE_EXIT_CRITICAL();
2720 traceRETURN_uxTaskBasePriorityGet( uxReturn );
2725 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2726 /*-----------------------------------------------------------*/
2728 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2730 UBaseType_t uxTaskBasePriorityGetFromISR( const TaskHandle_t xTask )
2732 TCB_t const * pxTCB;
2733 UBaseType_t uxReturn;
2734 UBaseType_t uxSavedInterruptStatus;
2736 traceENTER_uxTaskBasePriorityGetFromISR( xTask );
2738 /* RTOS ports that support interrupt nesting have the concept of a
2739 * maximum system call (or maximum API call) interrupt priority.
2740 * Interrupts that are above the maximum system call priority are keep
2741 * permanently enabled, even when the RTOS kernel is in a critical section,
2742 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2743 * is defined in FreeRTOSConfig.h then
2744 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2745 * failure if a FreeRTOS API function is called from an interrupt that has
2746 * been assigned a priority above the configured maximum system call
2747 * priority. Only FreeRTOS functions that end in FromISR can be called
2748 * from interrupts that have been assigned a priority at or (logically)
2749 * below the maximum system call interrupt priority. FreeRTOS maintains a
2750 * separate interrupt safe API to ensure interrupt entry is as fast and as
2751 * simple as possible. More information (albeit Cortex-M specific) is
2752 * provided on the following link:
2753 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2754 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2756 /* MISRA Ref 4.7.1 [Return value shall be checked] */
2757 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
2758 /* coverity[misra_c_2012_directive_4_7_violation] */
2759 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
2761 /* If null is passed in here then it is the base priority of the calling
2762 * task that is being queried. */
2763 pxTCB = prvGetTCBFromHandle( xTask );
2764 configASSERT( pxTCB != NULL );
2766 uxReturn = pxTCB->uxBasePriority;
2768 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2770 traceRETURN_uxTaskBasePriorityGetFromISR( uxReturn );
2775 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2776 /*-----------------------------------------------------------*/
2778 #if ( INCLUDE_vTaskPrioritySet == 1 )
2780 void vTaskPrioritySet( TaskHandle_t xTask,
2781 UBaseType_t uxNewPriority )
2784 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2785 BaseType_t xYieldRequired = pdFALSE;
2787 #if ( configNUMBER_OF_CORES > 1 )
2788 BaseType_t xYieldForTask = pdFALSE;
2791 traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
2793 configASSERT( uxNewPriority < configMAX_PRIORITIES );
2795 /* Ensure the new priority is valid. */
2796 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2798 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2802 mtCOVERAGE_TEST_MARKER();
2805 taskENTER_CRITICAL();
2807 /* If null is passed in here then it is the priority of the calling
2808 * task that is being changed. */
2809 pxTCB = prvGetTCBFromHandle( xTask );
2810 configASSERT( pxTCB != NULL );
2812 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2814 #if ( configUSE_MUTEXES == 1 )
2816 uxCurrentBasePriority = pxTCB->uxBasePriority;
2820 uxCurrentBasePriority = pxTCB->uxPriority;
2824 if( uxCurrentBasePriority != uxNewPriority )
2826 /* The priority change may have readied a task of higher
2827 * priority than a running task. */
2828 if( uxNewPriority > uxCurrentBasePriority )
2830 #if ( configNUMBER_OF_CORES == 1 )
2832 if( pxTCB != pxCurrentTCB )
2834 /* The priority of a task other than the currently
2835 * running task is being raised. Is the priority being
2836 * raised above that of the running task? */
2837 if( uxNewPriority > pxCurrentTCB->uxPriority )
2839 xYieldRequired = pdTRUE;
2843 mtCOVERAGE_TEST_MARKER();
2848 /* The priority of the running task is being raised,
2849 * but the running task must already be the highest
2850 * priority task able to run so no yield is required. */
2853 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2855 /* The priority of a task is being raised so
2856 * perform a yield for this task later. */
2857 xYieldForTask = pdTRUE;
2859 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2861 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2863 /* Setting the priority of a running task down means
2864 * there may now be another task of higher priority that
2865 * is ready to execute. */
2866 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2867 if( pxTCB->xPreemptionDisable == pdFALSE )
2870 xYieldRequired = pdTRUE;
2875 /* Setting the priority of any other task down does not
2876 * require a yield as the running task must be above the
2877 * new priority of the task being modified. */
2880 /* Remember the ready list the task might be referenced from
2881 * before its uxPriority member is changed so the
2882 * taskRESET_READY_PRIORITY() macro can function correctly. */
2883 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2885 #if ( configUSE_MUTEXES == 1 )
2887 /* Only change the priority being used if the task is not
2888 * currently using an inherited priority or the new priority
2889 * is bigger than the inherited priority. */
2890 if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2892 pxTCB->uxPriority = uxNewPriority;
2896 mtCOVERAGE_TEST_MARKER();
2899 /* The base priority gets set whatever. */
2900 pxTCB->uxBasePriority = uxNewPriority;
2902 #else /* if ( configUSE_MUTEXES == 1 ) */
2904 pxTCB->uxPriority = uxNewPriority;
2906 #endif /* if ( configUSE_MUTEXES == 1 ) */
2908 /* Only reset the event list item value if the value is not
2909 * being used for anything else. */
2910 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
2912 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) );
2916 mtCOVERAGE_TEST_MARKER();
2919 /* If the task is in the blocked or suspended list we need do
2920 * nothing more than change its priority variable. However, if
2921 * the task is in a ready list it needs to be removed and placed
2922 * in the list appropriate to its new priority. */
2923 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2925 /* The task is currently in its ready list - remove before
2926 * adding it to its new ready list. As we are in a critical
2927 * section we can do this even if the scheduler is suspended. */
2928 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2930 /* It is known that the task is in its ready list so
2931 * there is no need to check again and the port level
2932 * reset macro can be called directly. */
2933 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2937 mtCOVERAGE_TEST_MARKER();
2940 prvAddTaskToReadyList( pxTCB );
2944 #if ( configNUMBER_OF_CORES == 1 )
2946 mtCOVERAGE_TEST_MARKER();
2950 /* It's possible that xYieldForTask was already set to pdTRUE because
2951 * its priority is being raised. However, since it is not in a ready list
2952 * we don't actually need to yield for it. */
2953 xYieldForTask = pdFALSE;
2958 if( xYieldRequired != pdFALSE )
2960 /* The running task priority is set down. Request the task to yield. */
2961 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2965 #if ( configNUMBER_OF_CORES > 1 )
2966 if( xYieldForTask != pdFALSE )
2968 /* The priority of the task is being raised. If a running
2969 * task has priority lower than this task, it should yield
2971 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2974 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2976 mtCOVERAGE_TEST_MARKER();
2980 /* Remove compiler warning about unused variables when the port
2981 * optimised task selection is not being used. */
2982 ( void ) uxPriorityUsedOnEntry;
2985 taskEXIT_CRITICAL();
2987 traceRETURN_vTaskPrioritySet();
2990 #endif /* INCLUDE_vTaskPrioritySet */
2991 /*-----------------------------------------------------------*/
2993 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2994 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2995 UBaseType_t uxCoreAffinityMask )
3000 traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
3002 taskENTER_CRITICAL();
3004 pxTCB = prvGetTCBFromHandle( xTask );
3005 configASSERT( pxTCB != NULL );
3007 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
3009 if( xSchedulerRunning != pdFALSE )
3011 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3013 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
3015 /* If the task can no longer run on the core it was running,
3016 * request the core to yield. */
3017 if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
3019 prvYieldCore( xCoreID );
3024 #if ( configUSE_PREEMPTION == 1 )
3026 /* The SMP scheduler requests a core to yield when a ready
3027 * task is able to run. It is possible that the core affinity
3028 * of the ready task is changed before the requested core
3029 * can select it to run. In that case, the task may not be
3030 * selected by the previously requested core due to core affinity
3031 * constraint and the SMP scheduler must select a new core to
3032 * yield for the task. */
3033 prvYieldForTask( xTask );
3035 #else /* #if( configUSE_PREEMPTION == 1 ) */
3037 mtCOVERAGE_TEST_MARKER();
3039 #endif /* #if( configUSE_PREEMPTION == 1 ) */
3043 taskEXIT_CRITICAL();
3045 traceRETURN_vTaskCoreAffinitySet();
3047 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3048 /*-----------------------------------------------------------*/
3050 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
3051 UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
3053 const TCB_t * pxTCB;
3054 UBaseType_t uxCoreAffinityMask;
3056 traceENTER_vTaskCoreAffinityGet( xTask );
3058 portBASE_TYPE_ENTER_CRITICAL();
3060 pxTCB = prvGetTCBFromHandle( xTask );
3061 configASSERT( pxTCB != NULL );
3063 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
3065 portBASE_TYPE_EXIT_CRITICAL();
3067 traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
3069 return uxCoreAffinityMask;
3071 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3073 /*-----------------------------------------------------------*/
3075 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3077 void vTaskPreemptionDisable( const TaskHandle_t xTask )
3081 traceENTER_vTaskPreemptionDisable( xTask );
3083 taskENTER_CRITICAL();
3085 pxTCB = prvGetTCBFromHandle( xTask );
3086 configASSERT( pxTCB != NULL );
3088 pxTCB->xPreemptionDisable = pdTRUE;
3090 taskEXIT_CRITICAL();
3092 traceRETURN_vTaskPreemptionDisable();
3095 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3096 /*-----------------------------------------------------------*/
3098 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3100 void vTaskPreemptionEnable( const TaskHandle_t xTask )
3105 traceENTER_vTaskPreemptionEnable( xTask );
3107 taskENTER_CRITICAL();
3109 pxTCB = prvGetTCBFromHandle( xTask );
3110 configASSERT( pxTCB != NULL );
3112 pxTCB->xPreemptionDisable = pdFALSE;
3114 if( xSchedulerRunning != pdFALSE )
3116 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3118 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
3119 prvYieldCore( xCoreID );
3123 taskEXIT_CRITICAL();
3125 traceRETURN_vTaskPreemptionEnable();
3128 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3129 /*-----------------------------------------------------------*/
3131 #if ( INCLUDE_vTaskSuspend == 1 )
3133 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
3137 traceENTER_vTaskSuspend( xTaskToSuspend );
3139 taskENTER_CRITICAL();
3141 /* If null is passed in here then it is the running task that is
3142 * being suspended. */
3143 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
3144 configASSERT( pxTCB != NULL );
3146 traceTASK_SUSPEND( pxTCB );
3148 /* Remove task from the ready/delayed list and place in the
3149 * suspended list. */
3150 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
3152 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
3156 mtCOVERAGE_TEST_MARKER();
3159 /* Is the task waiting on an event also? */
3160 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3162 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3166 mtCOVERAGE_TEST_MARKER();
3169 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
3171 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3175 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3177 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3179 /* The task was blocked to wait for a notification, but is
3180 * now suspended, so no notification was received. */
3181 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
3185 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3187 /* In the case of SMP, it is possible that the task being suspended
3188 * is running on another core. We must evict the task before
3189 * exiting the critical section to ensure that the task cannot
3190 * take an action which puts it back on ready/state/event list,
3191 * thereby nullifying the suspend operation. Once evicted, the
3192 * task won't be scheduled before it is resumed as it will no longer
3193 * be on the ready list. */
3194 #if ( configNUMBER_OF_CORES > 1 )
3196 if( xSchedulerRunning != pdFALSE )
3198 /* Reset the next expected unblock time in case it referred to the
3199 * task that is now in the Suspended state. */
3200 prvResetNextTaskUnblockTime();
3202 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3204 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
3206 /* The current task has just been suspended. */
3207 configASSERT( uxSchedulerSuspended == 0 );
3208 vTaskYieldWithinAPI();
3212 prvYieldCore( pxTCB->xTaskRunState );
3217 mtCOVERAGE_TEST_MARKER();
3222 mtCOVERAGE_TEST_MARKER();
3225 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
3227 taskEXIT_CRITICAL();
3229 #if ( configNUMBER_OF_CORES == 1 )
3231 UBaseType_t uxCurrentListLength;
3233 if( xSchedulerRunning != pdFALSE )
3235 /* Reset the next expected unblock time in case it referred to the
3236 * task that is now in the Suspended state. */
3237 taskENTER_CRITICAL();
3239 prvResetNextTaskUnblockTime();
3241 taskEXIT_CRITICAL();
3245 mtCOVERAGE_TEST_MARKER();
3248 if( pxTCB == pxCurrentTCB )
3250 if( xSchedulerRunning != pdFALSE )
3252 /* The current task has just been suspended. */
3253 configASSERT( uxSchedulerSuspended == 0 );
3254 portYIELD_WITHIN_API();
3258 /* The scheduler is not running, but the task that was pointed
3259 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
3260 * must be adjusted to point to a different task. */
3262 /* Use a temp variable as a distinct sequence point for reading
3263 * volatile variables prior to a comparison to ensure compliance
3264 * with MISRA C 2012 Rule 13.2. */
3265 uxCurrentListLength = listCURRENT_LIST_LENGTH( &xSuspendedTaskList );
3267 if( uxCurrentListLength == uxCurrentNumberOfTasks )
3269 /* No other tasks are ready, so set pxCurrentTCB back to
3270 * NULL so when the next task is created pxCurrentTCB will
3271 * be set to point to it no matter what its relative priority
3273 pxCurrentTCB = NULL;
3277 vTaskSwitchContext();
3283 mtCOVERAGE_TEST_MARKER();
3286 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3288 traceRETURN_vTaskSuspend();
3291 #endif /* INCLUDE_vTaskSuspend */
3292 /*-----------------------------------------------------------*/
3294 #if ( INCLUDE_vTaskSuspend == 1 )
3296 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
3298 BaseType_t xReturn = pdFALSE;
3299 const TCB_t * const pxTCB = xTask;
3301 /* Accesses xPendingReadyList so must be called from a critical
3304 /* It does not make sense to check if the calling task is suspended. */
3305 configASSERT( xTask );
3307 /* Is the task being resumed actually in the suspended list? */
3308 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
3310 /* Has the task already been resumed from within an ISR? */
3311 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
3313 /* Is it in the suspended list because it is in the Suspended
3314 * state, or because it is blocked with no timeout? */
3315 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE )
3317 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3321 /* The task does not appear on the event list item of
3322 * and of the RTOS objects, but could still be in the
3323 * blocked state if it is waiting on its notification
3324 * rather than waiting on an object. If not, is
3328 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3330 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3337 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3341 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3345 mtCOVERAGE_TEST_MARKER();
3350 mtCOVERAGE_TEST_MARKER();
3355 mtCOVERAGE_TEST_MARKER();
3361 #endif /* INCLUDE_vTaskSuspend */
3362 /*-----------------------------------------------------------*/
3364 #if ( INCLUDE_vTaskSuspend == 1 )
3366 void vTaskResume( TaskHandle_t xTaskToResume )
3368 TCB_t * const pxTCB = xTaskToResume;
3370 traceENTER_vTaskResume( xTaskToResume );
3372 /* It does not make sense to resume the calling task. */
3373 configASSERT( xTaskToResume );
3375 #if ( configNUMBER_OF_CORES == 1 )
3377 /* The parameter cannot be NULL as it is impossible to resume the
3378 * currently executing task. */
3379 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
3382 /* The parameter cannot be NULL as it is impossible to resume the
3383 * currently executing task. It is also impossible to resume a task
3384 * that is actively running on another core but it is not safe
3385 * to check their run state here. Therefore, we get into a critical
3386 * section and check if the task is actually suspended or not. */
3390 taskENTER_CRITICAL();
3392 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3394 traceTASK_RESUME( pxTCB );
3396 /* The ready list can be accessed even if the scheduler is
3397 * suspended because this is inside a critical section. */
3398 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3399 prvAddTaskToReadyList( pxTCB );
3401 /* This yield may not cause the task just resumed to run,
3402 * but will leave the lists in the correct state for the
3404 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
3408 mtCOVERAGE_TEST_MARKER();
3411 taskEXIT_CRITICAL();
3415 mtCOVERAGE_TEST_MARKER();
3418 traceRETURN_vTaskResume();
3421 #endif /* INCLUDE_vTaskSuspend */
3423 /*-----------------------------------------------------------*/
3425 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
3427 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
3429 BaseType_t xYieldRequired = pdFALSE;
3430 TCB_t * const pxTCB = xTaskToResume;
3431 UBaseType_t uxSavedInterruptStatus;
3433 traceENTER_xTaskResumeFromISR( xTaskToResume );
3435 configASSERT( xTaskToResume );
3437 /* RTOS ports that support interrupt nesting have the concept of a
3438 * maximum system call (or maximum API call) interrupt priority.
3439 * Interrupts that are above the maximum system call priority are keep
3440 * permanently enabled, even when the RTOS kernel is in a critical section,
3441 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
3442 * is defined in FreeRTOSConfig.h then
3443 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3444 * failure if a FreeRTOS API function is called from an interrupt that has
3445 * been assigned a priority above the configured maximum system call
3446 * priority. Only FreeRTOS functions that end in FromISR can be called
3447 * from interrupts that have been assigned a priority at or (logically)
3448 * below the maximum system call interrupt priority. FreeRTOS maintains a
3449 * separate interrupt safe API to ensure interrupt entry is as fast and as
3450 * simple as possible. More information (albeit Cortex-M specific) is
3451 * provided on the following link:
3452 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3453 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3455 /* MISRA Ref 4.7.1 [Return value shall be checked] */
3456 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
3457 /* coverity[misra_c_2012_directive_4_7_violation] */
3458 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3460 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3462 traceTASK_RESUME_FROM_ISR( pxTCB );
3464 /* Check the ready lists can be accessed. */
3465 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3467 #if ( configNUMBER_OF_CORES == 1 )
3469 /* Ready lists can be accessed so move the task from the
3470 * suspended list to the ready list directly. */
3471 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3473 xYieldRequired = pdTRUE;
3475 /* Mark that a yield is pending in case the user is not
3476 * using the return value to initiate a context switch
3477 * from the ISR using the port specific portYIELD_FROM_ISR(). */
3478 xYieldPendings[ 0 ] = pdTRUE;
3482 mtCOVERAGE_TEST_MARKER();
3485 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3487 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3488 prvAddTaskToReadyList( pxTCB );
3492 /* The delayed or ready lists cannot be accessed so the task
3493 * is held in the pending ready list until the scheduler is
3495 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3498 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3500 prvYieldForTask( pxTCB );
3502 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3504 xYieldRequired = pdTRUE;
3507 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3511 mtCOVERAGE_TEST_MARKER();
3514 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3516 traceRETURN_xTaskResumeFromISR( xYieldRequired );
3518 return xYieldRequired;
3521 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3522 /*-----------------------------------------------------------*/
3524 static BaseType_t prvCreateIdleTasks( void )
3526 BaseType_t xReturn = pdPASS;
3528 char cIdleName[ configMAX_TASK_NAME_LEN ] = { 0 };
3529 TaskFunction_t pxIdleTaskFunction = NULL;
3530 BaseType_t xIdleTaskNameIndex;
3531 BaseType_t xIdleNameLen;
3532 BaseType_t xCopyLen;
3534 configASSERT( ( configIDLE_TASK_NAME != NULL ) && ( configMAX_TASK_NAME_LEN > 3 ) );
3536 /* The length of the idle task name is limited to the minimum of the length
3537 * of configIDLE_TASK_NAME and configMAX_TASK_NAME_LEN - 2, keeping space
3538 * for the core ID suffix and the null-terminator. */
3539 xIdleNameLen = strlen( configIDLE_TASK_NAME );
3540 xCopyLen = xIdleNameLen < ( configMAX_TASK_NAME_LEN - 2 ) ? xIdleNameLen : ( configMAX_TASK_NAME_LEN - 2 );
3542 for( xIdleTaskNameIndex = ( BaseType_t ) 0; xIdleTaskNameIndex < xCopyLen; xIdleTaskNameIndex++ )
3544 cIdleName[ xIdleTaskNameIndex ] = configIDLE_TASK_NAME[ xIdleTaskNameIndex ];
3547 /* Ensure null termination. */
3548 cIdleName[ xIdleTaskNameIndex ] = '\0';
3550 /* Add each idle task at the lowest priority. */
3551 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3553 #if ( configNUMBER_OF_CORES == 1 )
3555 pxIdleTaskFunction = prvIdleTask;
3557 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3559 /* In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks
3560 * are also created to ensure that each core has an idle task to
3561 * run when no other task is available to run. */
3564 pxIdleTaskFunction = prvIdleTask;
3568 pxIdleTaskFunction = prvPassiveIdleTask;
3571 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3573 /* Update the idle task name with suffix to differentiate the idle tasks.
3574 * This function is not required in single core FreeRTOS since there is
3575 * only one idle task. */
3576 #if ( configNUMBER_OF_CORES > 1 )
3578 /* Append the idle task number to the end of the name. */
3579 cIdleName[ xIdleTaskNameIndex ] = ( char ) ( xCoreID + '0' );
3580 cIdleName[ xIdleTaskNameIndex + 1 ] = '\0';
3582 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
3584 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3586 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3587 StackType_t * pxIdleTaskStackBuffer = NULL;
3588 configSTACK_DEPTH_TYPE uxIdleTaskStackSize;
3590 /* The Idle task is created using user provided RAM - obtain the
3591 * address of the RAM then create the idle task. */
3592 #if ( configNUMBER_OF_CORES == 1 )
3594 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
3600 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
3604 vApplicationGetPassiveIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize, ( BaseType_t ) ( xCoreID - 1 ) );
3607 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
3608 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( pxIdleTaskFunction,
3610 uxIdleTaskStackSize,
3612 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3613 pxIdleTaskStackBuffer,
3614 pxIdleTaskTCBBuffer );
3616 if( xIdleTaskHandles[ xCoreID ] != NULL )
3625 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3627 /* The Idle task is being created using dynamically allocated RAM. */
3628 xReturn = xTaskCreate( pxIdleTaskFunction,
3630 configMINIMAL_STACK_SIZE,
3632 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3633 &xIdleTaskHandles[ xCoreID ] );
3635 #endif /* configSUPPORT_STATIC_ALLOCATION */
3637 /* Break the loop if any of the idle task is failed to be created. */
3638 if( xReturn != pdPASS )
3644 #if ( configNUMBER_OF_CORES == 1 )
3646 mtCOVERAGE_TEST_MARKER();
3650 /* Assign idle task to each core before SMP scheduler is running. */
3651 xIdleTaskHandles[ xCoreID ]->xTaskRunState = xCoreID;
3652 pxCurrentTCBs[ xCoreID ] = xIdleTaskHandles[ xCoreID ];
3661 /*-----------------------------------------------------------*/
3663 void vTaskStartScheduler( void )
3667 traceENTER_vTaskStartScheduler();
3669 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3671 /* Sanity check that the UBaseType_t must have greater than or equal to
3672 * the number of bits as confNUMBER_OF_CORES. */
3673 configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3675 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3677 xReturn = prvCreateIdleTasks();
3679 #if ( configUSE_TIMERS == 1 )
3681 if( xReturn == pdPASS )
3683 xReturn = xTimerCreateTimerTask();
3687 mtCOVERAGE_TEST_MARKER();
3690 #endif /* configUSE_TIMERS */
3692 if( xReturn == pdPASS )
3694 /* freertos_tasks_c_additions_init() should only be called if the user
3695 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3696 * the only macro called by the function. */
3697 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3699 freertos_tasks_c_additions_init();
3703 /* Interrupts are turned off here, to ensure a tick does not occur
3704 * before or during the call to xPortStartScheduler(). The stacks of
3705 * the created tasks contain a status word with interrupts switched on
3706 * so interrupts will automatically get re-enabled when the first task
3708 portDISABLE_INTERRUPTS();
3710 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3712 /* Switch C-Runtime's TLS Block to point to the TLS
3713 * block specific to the task that will run first. */
3714 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3718 xNextTaskUnblockTime = portMAX_DELAY;
3719 xSchedulerRunning = pdTRUE;
3720 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3722 /* If configGENERATE_RUN_TIME_STATS is defined then the following
3723 * macro must be defined to configure the timer/counter used to generate
3724 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
3725 * is set to 0 and the following line fails to build then ensure you do not
3726 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3727 * FreeRTOSConfig.h file. */
3728 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3730 traceTASK_SWITCHED_IN();
3732 traceSTARTING_SCHEDULER( xIdleTaskHandles );
3734 /* Setting up the timer tick is hardware specific and thus in the
3735 * portable interface. */
3737 /* The return value for xPortStartScheduler is not required
3738 * hence using a void datatype. */
3739 ( void ) xPortStartScheduler();
3741 /* In most cases, xPortStartScheduler() will not return. If it
3742 * returns pdTRUE then there was not enough heap memory available
3743 * to create either the Idle or the Timer task. If it returned
3744 * pdFALSE, then the application called xTaskEndScheduler().
3745 * Most ports don't implement xTaskEndScheduler() as there is
3746 * nothing to return to. */
3750 /* This line will only be reached if the kernel could not be started,
3751 * because there was not enough FreeRTOS heap to create the idle task
3752 * or the timer task. */
3753 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3756 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3757 * meaning xIdleTaskHandles are not used anywhere else. */
3758 ( void ) xIdleTaskHandles;
3760 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3761 * from getting optimized out as it is no longer used by the kernel. */
3762 ( void ) uxTopUsedPriority;
3764 traceRETURN_vTaskStartScheduler();
3766 /*-----------------------------------------------------------*/
3768 void vTaskEndScheduler( void )
3770 traceENTER_vTaskEndScheduler();
3772 #if ( INCLUDE_vTaskDelete == 1 )
3776 #if ( configUSE_TIMERS == 1 )
3778 /* Delete the timer task created by the kernel. */
3779 vTaskDelete( xTimerGetTimerDaemonTaskHandle() );
3781 #endif /* #if ( configUSE_TIMERS == 1 ) */
3783 /* Delete Idle tasks created by the kernel.*/
3784 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3786 vTaskDelete( xIdleTaskHandles[ xCoreID ] );
3789 /* Idle task is responsible for reclaiming the resources of the tasks in
3790 * xTasksWaitingTermination list. Since the idle task is now deleted and
3791 * no longer going to run, we need to reclaim resources of all the tasks
3792 * in the xTasksWaitingTermination list. */
3793 prvCheckTasksWaitingTermination();
3795 #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
3797 /* Stop the scheduler interrupts and call the portable scheduler end
3798 * routine so the original ISRs can be restored if necessary. The port
3799 * layer must ensure interrupts enable bit is left in the correct state. */
3800 portDISABLE_INTERRUPTS();
3801 xSchedulerRunning = pdFALSE;
3803 /* This function must be called from a task and the application is
3804 * responsible for deleting that task after the scheduler is stopped. */
3805 vPortEndScheduler();
3807 traceRETURN_vTaskEndScheduler();
3809 /*----------------------------------------------------------*/
3811 void vTaskSuspendAll( void )
3813 traceENTER_vTaskSuspendAll();
3815 #if ( configNUMBER_OF_CORES == 1 )
3817 /* A critical section is not required as the variable is of type
3818 * BaseType_t. Each task maintains its own context, and a context switch
3819 * cannot occur if the variable is non zero. So, as long as the writing
3820 * from the register back into the memory is atomic, it is not a
3823 * Consider the following scenario, which starts with
3824 * uxSchedulerSuspended at zero.
3826 * 1. load uxSchedulerSuspended into register.
3827 * 2. Now a context switch causes another task to run, and the other
3828 * task uses the same variable. The other task will see the variable
3829 * as zero because the variable has not yet been updated by the
3830 * original task. Eventually the original task runs again. **That can
3831 * only happen when uxSchedulerSuspended is once again zero**. When
3832 * the original task runs again, the contents of the CPU registers
3833 * are restored to exactly how they were when it was switched out -
3834 * therefore the value it read into the register still matches the
3835 * value of the uxSchedulerSuspended variable.
3837 * 3. increment register.
3838 * 4. store register into uxSchedulerSuspended. The value restored to
3839 * uxSchedulerSuspended will be the correct value of 1, even though
3840 * the variable was used by other tasks in the mean time.
3843 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3844 * do not otherwise exhibit real time behaviour. */
3845 portSOFTWARE_BARRIER();
3847 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3848 * is used to allow calls to vTaskSuspendAll() to nest. */
3849 uxSchedulerSuspended = ( UBaseType_t ) ( uxSchedulerSuspended + 1U );
3851 /* Enforces ordering for ports and optimised compilers that may otherwise place
3852 * the above increment elsewhere. */
3853 portMEMORY_BARRIER();
3855 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3857 UBaseType_t ulState;
3860 /* This must only be called from within a task. */
3861 portASSERT_IF_IN_ISR();
3863 if( xSchedulerRunning != pdFALSE )
3865 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3866 * We must disable interrupts before we grab the locks in the event that this task is
3867 * interrupted and switches context before incrementing uxSchedulerSuspended.
3868 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3869 * uxSchedulerSuspended since that will prevent context switches. */
3870 ulState = portSET_INTERRUPT_MASK();
3872 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3874 /* This must never be called from inside a critical section. */
3875 configASSERT( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0 );
3877 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3878 * do not otherwise exhibit real time behaviour. */
3879 portSOFTWARE_BARRIER();
3881 portGET_TASK_LOCK( xCoreID );
3883 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3884 * purpose is to prevent altering the variable when fromISR APIs are readying
3886 if( uxSchedulerSuspended == 0U )
3888 prvCheckForRunStateChange();
3892 mtCOVERAGE_TEST_MARKER();
3895 /* Query the coreID again as prvCheckForRunStateChange may have
3896 * caused the task to get scheduled on a different core. The correct
3897 * task lock for the core is acquired in prvCheckForRunStateChange. */
3898 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3900 portGET_ISR_LOCK( xCoreID );
3902 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3903 * is used to allow calls to vTaskSuspendAll() to nest. */
3904 ++uxSchedulerSuspended;
3905 portRELEASE_ISR_LOCK( xCoreID );
3907 portCLEAR_INTERRUPT_MASK( ulState );
3911 mtCOVERAGE_TEST_MARKER();
3914 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3916 traceRETURN_vTaskSuspendAll();
3919 /*----------------------------------------------------------*/
3921 #if ( configUSE_TICKLESS_IDLE != 0 )
3923 static TickType_t prvGetExpectedIdleTime( void )
3926 BaseType_t xHigherPriorityReadyTasks = pdFALSE;
3928 /* xHigherPriorityReadyTasks takes care of the case where
3929 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3930 * task that are in the Ready state, even though the idle task is
3932 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3934 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3936 xHigherPriorityReadyTasks = pdTRUE;
3941 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3943 /* When port optimised task selection is used the uxTopReadyPriority
3944 * variable is used as a bit map. If bits other than the least
3945 * significant bit are set then there are tasks that have a priority
3946 * above the idle priority that are in the Ready state. This takes
3947 * care of the case where the co-operative scheduler is in use. */
3948 if( uxTopReadyPriority > uxLeastSignificantBit )
3950 xHigherPriorityReadyTasks = pdTRUE;
3953 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3955 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3959 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1U )
3961 /* There are other idle priority tasks in the ready state. If
3962 * time slicing is used then the very next tick interrupt must be
3966 else if( xHigherPriorityReadyTasks != pdFALSE )
3968 /* There are tasks in the Ready state that have a priority above the
3969 * idle priority. This path can only be reached if
3970 * configUSE_PREEMPTION is 0. */
3975 xReturn = xNextTaskUnblockTime;
3976 xReturn -= xTickCount;
3982 #endif /* configUSE_TICKLESS_IDLE */
3983 /*----------------------------------------------------------*/
3985 BaseType_t xTaskResumeAll( void )
3987 TCB_t * pxTCB = NULL;
3988 BaseType_t xAlreadyYielded = pdFALSE;
3990 traceENTER_xTaskResumeAll();
3992 #if ( configNUMBER_OF_CORES > 1 )
3993 if( xSchedulerRunning != pdFALSE )
3996 /* It is possible that an ISR caused a task to be removed from an event
3997 * list while the scheduler was suspended. If this was the case then the
3998 * removed task will have been added to the xPendingReadyList. Once the
3999 * scheduler has been resumed it is safe to move all the pending ready
4000 * tasks from this list into their appropriate ready list. */
4001 taskENTER_CRITICAL();
4003 const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
4005 /* If uxSchedulerSuspended is zero then this function does not match a
4006 * previous call to vTaskSuspendAll(). */
4007 configASSERT( uxSchedulerSuspended != 0U );
4009 uxSchedulerSuspended = ( UBaseType_t ) ( uxSchedulerSuspended - 1U );
4010 portRELEASE_TASK_LOCK( xCoreID );
4012 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4014 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
4016 /* Move any readied tasks from the pending list into the
4017 * appropriate ready list. */
4018 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
4020 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4021 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4022 /* coverity[misra_c_2012_rule_11_5_violation] */
4023 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) );
4024 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4025 portMEMORY_BARRIER();
4026 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4027 prvAddTaskToReadyList( pxTCB );
4029 #if ( configNUMBER_OF_CORES == 1 )
4031 /* If the moved task has a priority higher than the current
4032 * task then a yield must be performed. */
4033 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4035 xYieldPendings[ xCoreID ] = pdTRUE;
4039 mtCOVERAGE_TEST_MARKER();
4042 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4044 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
4045 * If the current core yielded then vTaskSwitchContext() has already been called
4046 * which sets xYieldPendings for the current core to pdTRUE. */
4048 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4053 /* A task was unblocked while the scheduler was suspended,
4054 * which may have prevented the next unblock time from being
4055 * re-calculated, in which case re-calculate it now. Mainly
4056 * important for low power tickless implementations, where
4057 * this can prevent an unnecessary exit from low power
4059 prvResetNextTaskUnblockTime();
4062 /* If any ticks occurred while the scheduler was suspended then
4063 * they should be processed now. This ensures the tick count does
4064 * not slip, and that any delayed tasks are resumed at the correct
4067 * It should be safe to call xTaskIncrementTick here from any core
4068 * since we are in a critical section and xTaskIncrementTick itself
4069 * protects itself within a critical section. Suspending the scheduler
4070 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
4072 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
4074 if( xPendedCounts > ( TickType_t ) 0U )
4078 if( xTaskIncrementTick() != pdFALSE )
4080 /* Other cores are interrupted from
4081 * within xTaskIncrementTick(). */
4082 xYieldPendings[ xCoreID ] = pdTRUE;
4086 mtCOVERAGE_TEST_MARKER();
4090 } while( xPendedCounts > ( TickType_t ) 0U );
4096 mtCOVERAGE_TEST_MARKER();
4100 if( xYieldPendings[ xCoreID ] != pdFALSE )
4102 #if ( configUSE_PREEMPTION != 0 )
4104 xAlreadyYielded = pdTRUE;
4106 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
4108 #if ( configNUMBER_OF_CORES == 1 )
4110 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
4112 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4116 mtCOVERAGE_TEST_MARKER();
4122 mtCOVERAGE_TEST_MARKER();
4125 taskEXIT_CRITICAL();
4128 traceRETURN_xTaskResumeAll( xAlreadyYielded );
4130 return xAlreadyYielded;
4132 /*-----------------------------------------------------------*/
4134 TickType_t xTaskGetTickCount( void )
4138 traceENTER_xTaskGetTickCount();
4140 /* Critical section required if running on a 16 bit processor. */
4141 portTICK_TYPE_ENTER_CRITICAL();
4143 xTicks = xTickCount;
4145 portTICK_TYPE_EXIT_CRITICAL();
4147 traceRETURN_xTaskGetTickCount( xTicks );
4151 /*-----------------------------------------------------------*/
4153 TickType_t xTaskGetTickCountFromISR( void )
4156 UBaseType_t uxSavedInterruptStatus;
4158 traceENTER_xTaskGetTickCountFromISR();
4160 /* RTOS ports that support interrupt nesting have the concept of a maximum
4161 * system call (or maximum API call) interrupt priority. Interrupts that are
4162 * above the maximum system call priority are kept permanently enabled, even
4163 * when the RTOS kernel is in a critical section, but cannot make any calls to
4164 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
4165 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
4166 * failure if a FreeRTOS API function is called from an interrupt that has been
4167 * assigned a priority above the configured maximum system call priority.
4168 * Only FreeRTOS functions that end in FromISR can be called from interrupts
4169 * that have been assigned a priority at or (logically) below the maximum
4170 * system call interrupt priority. FreeRTOS maintains a separate interrupt
4171 * safe API to ensure interrupt entry is as fast and as simple as possible.
4172 * More information (albeit Cortex-M specific) is provided on the following
4173 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
4174 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
4176 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
4178 xReturn = xTickCount;
4180 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
4182 traceRETURN_xTaskGetTickCountFromISR( xReturn );
4186 /*-----------------------------------------------------------*/
4188 UBaseType_t uxTaskGetNumberOfTasks( void )
4190 traceENTER_uxTaskGetNumberOfTasks();
4192 /* A critical section is not required because the variables are of type
4194 traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
4196 return uxCurrentNumberOfTasks;
4198 /*-----------------------------------------------------------*/
4200 char * pcTaskGetName( TaskHandle_t xTaskToQuery )
4204 traceENTER_pcTaskGetName( xTaskToQuery );
4206 /* If null is passed in here then the name of the calling task is being
4208 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4209 configASSERT( pxTCB != NULL );
4211 traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
4213 return &( pxTCB->pcTaskName[ 0 ] );
4215 /*-----------------------------------------------------------*/
4217 #if ( INCLUDE_xTaskGetHandle == 1 )
4218 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4219 const char pcNameToQuery[] )
4221 TCB_t * pxReturn = NULL;
4222 TCB_t * pxTCB = NULL;
4225 BaseType_t xBreakLoop;
4226 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
4227 ListItem_t * pxIterator;
4229 /* This function is called with the scheduler suspended. */
4231 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4233 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
4235 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4236 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4237 /* coverity[misra_c_2012_rule_11_5_violation] */
4238 pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
4240 /* Check each character in the name looking for a match or
4242 xBreakLoop = pdFALSE;
4244 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4246 cNextChar = pxTCB->pcTaskName[ x ];
4248 if( cNextChar != pcNameToQuery[ x ] )
4250 /* Characters didn't match. */
4251 xBreakLoop = pdTRUE;
4253 else if( cNextChar == ( char ) 0x00 )
4255 /* Both strings terminated, a match must have been
4258 xBreakLoop = pdTRUE;
4262 mtCOVERAGE_TEST_MARKER();
4265 if( xBreakLoop != pdFALSE )
4271 if( pxReturn != NULL )
4273 /* The handle has been found. */
4280 mtCOVERAGE_TEST_MARKER();
4286 #endif /* INCLUDE_xTaskGetHandle */
4287 /*-----------------------------------------------------------*/
4289 #if ( INCLUDE_xTaskGetHandle == 1 )
4291 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery )
4293 UBaseType_t uxQueue = configMAX_PRIORITIES;
4296 traceENTER_xTaskGetHandle( pcNameToQuery );
4298 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
4299 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
4303 /* Search the ready lists. */
4307 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
4311 /* Found the handle. */
4314 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4316 /* Search the delayed lists. */
4319 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
4324 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
4327 #if ( INCLUDE_vTaskSuspend == 1 )
4331 /* Search the suspended list. */
4332 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
4337 #if ( INCLUDE_vTaskDelete == 1 )
4341 /* Search the deleted list. */
4342 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
4347 ( void ) xTaskResumeAll();
4349 traceRETURN_xTaskGetHandle( pxTCB );
4354 #endif /* INCLUDE_xTaskGetHandle */
4355 /*-----------------------------------------------------------*/
4357 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4359 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4360 StackType_t ** ppuxStackBuffer,
4361 StaticTask_t ** ppxTaskBuffer )
4366 traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4368 configASSERT( ppuxStackBuffer != NULL );
4369 configASSERT( ppxTaskBuffer != NULL );
4371 pxTCB = prvGetTCBFromHandle( xTask );
4372 configASSERT( pxTCB != NULL );
4374 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4376 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4378 *ppuxStackBuffer = pxTCB->pxStack;
4379 /* MISRA Ref 11.3.1 [Misaligned access] */
4380 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
4381 /* coverity[misra_c_2012_rule_11_3_violation] */
4382 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4385 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4387 *ppuxStackBuffer = pxTCB->pxStack;
4388 *ppxTaskBuffer = NULL;
4396 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4398 *ppuxStackBuffer = pxTCB->pxStack;
4399 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4402 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4404 traceRETURN_xTaskGetStaticBuffers( xReturn );
4409 #endif /* configSUPPORT_STATIC_ALLOCATION */
4410 /*-----------------------------------------------------------*/
4412 #if ( configUSE_TRACE_FACILITY == 1 )
4414 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4415 const UBaseType_t uxArraySize,
4416 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4418 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4420 traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4424 /* Is there a space in the array for each task in the system? */
4425 if( uxArraySize >= uxCurrentNumberOfTasks )
4427 /* Fill in an TaskStatus_t structure with information on each
4428 * task in the Ready state. */
4432 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4433 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4435 /* Fill in an TaskStatus_t structure with information on each
4436 * task in the Blocked state. */
4437 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4438 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4440 #if ( INCLUDE_vTaskDelete == 1 )
4442 /* Fill in an TaskStatus_t structure with information on
4443 * each task that has been deleted but not yet cleaned up. */
4444 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4448 #if ( INCLUDE_vTaskSuspend == 1 )
4450 /* Fill in an TaskStatus_t structure with information on
4451 * each task in the Suspended state. */
4452 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4456 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4458 if( pulTotalRunTime != NULL )
4460 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4461 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4463 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4467 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4469 if( pulTotalRunTime != NULL )
4471 *pulTotalRunTime = 0;
4474 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4478 mtCOVERAGE_TEST_MARKER();
4481 ( void ) xTaskResumeAll();
4483 traceRETURN_uxTaskGetSystemState( uxTask );
4488 #endif /* configUSE_TRACE_FACILITY */
4489 /*----------------------------------------------------------*/
4491 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4493 #if ( configNUMBER_OF_CORES == 1 )
4494 TaskHandle_t xTaskGetIdleTaskHandle( void )
4496 traceENTER_xTaskGetIdleTaskHandle();
4498 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4499 * started, then xIdleTaskHandles will be NULL. */
4500 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4502 traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4504 return xIdleTaskHandles[ 0 ];
4506 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
4508 TaskHandle_t xTaskGetIdleTaskHandleForCore( BaseType_t xCoreID )
4510 traceENTER_xTaskGetIdleTaskHandleForCore( xCoreID );
4512 /* Ensure the core ID is valid. */
4513 configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE );
4515 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4516 * started, then xIdleTaskHandles will be NULL. */
4517 configASSERT( ( xIdleTaskHandles[ xCoreID ] != NULL ) );
4519 traceRETURN_xTaskGetIdleTaskHandleForCore( xIdleTaskHandles[ xCoreID ] );
4521 return xIdleTaskHandles[ xCoreID ];
4524 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4525 /*----------------------------------------------------------*/
4527 /* This conditional compilation should use inequality to 0, not equality to 1.
4528 * This is to ensure vTaskStepTick() is available when user defined low power mode
4529 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4531 #if ( configUSE_TICKLESS_IDLE != 0 )
4533 void vTaskStepTick( TickType_t xTicksToJump )
4535 TickType_t xUpdatedTickCount;
4537 traceENTER_vTaskStepTick( xTicksToJump );
4539 /* Correct the tick count value after a period during which the tick
4540 * was suppressed. Note this does *not* call the tick hook function for
4541 * each stepped tick. */
4542 xUpdatedTickCount = xTickCount + xTicksToJump;
4543 configASSERT( xUpdatedTickCount <= xNextTaskUnblockTime );
4545 if( xUpdatedTickCount == xNextTaskUnblockTime )
4547 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4548 * xTaskIncrementTick() when the scheduler resumes. This ensures
4549 * that any delayed tasks are resumed at the correct time. */
4550 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4551 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4553 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4554 taskENTER_CRITICAL();
4558 taskEXIT_CRITICAL();
4563 mtCOVERAGE_TEST_MARKER();
4566 xTickCount += xTicksToJump;
4568 traceINCREASE_TICK_COUNT( xTicksToJump );
4569 traceRETURN_vTaskStepTick();
4572 #endif /* configUSE_TICKLESS_IDLE */
4573 /*----------------------------------------------------------*/
4575 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4577 BaseType_t xYieldOccurred;
4579 traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4581 /* Must not be called with the scheduler suspended as the implementation
4582 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4583 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4585 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4586 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4589 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4590 taskENTER_CRITICAL();
4592 xPendedTicks += xTicksToCatchUp;
4594 taskEXIT_CRITICAL();
4595 xYieldOccurred = xTaskResumeAll();
4597 traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4599 return xYieldOccurred;
4601 /*----------------------------------------------------------*/
4603 #if ( INCLUDE_xTaskAbortDelay == 1 )
4605 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4607 TCB_t * pxTCB = xTask;
4610 traceENTER_xTaskAbortDelay( xTask );
4612 configASSERT( pxTCB != NULL );
4616 /* A task can only be prematurely removed from the Blocked state if
4617 * it is actually in the Blocked state. */
4618 if( eTaskGetState( xTask ) == eBlocked )
4622 /* Remove the reference to the task from the blocked list. An
4623 * interrupt won't touch the xStateListItem because the
4624 * scheduler is suspended. */
4625 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4627 /* Is the task waiting on an event also? If so remove it from
4628 * the event list too. Interrupts can touch the event list item,
4629 * even though the scheduler is suspended, so a critical section
4631 taskENTER_CRITICAL();
4633 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4635 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4637 /* This lets the task know it was forcibly removed from the
4638 * blocked state so it should not re-evaluate its block time and
4639 * then block again. */
4640 pxTCB->ucDelayAborted = ( uint8_t ) pdTRUE;
4644 mtCOVERAGE_TEST_MARKER();
4647 taskEXIT_CRITICAL();
4649 /* Place the unblocked task into the appropriate ready list. */
4650 prvAddTaskToReadyList( pxTCB );
4652 /* A task being unblocked cannot cause an immediate context
4653 * switch if preemption is turned off. */
4654 #if ( configUSE_PREEMPTION == 1 )
4656 #if ( configNUMBER_OF_CORES == 1 )
4658 /* Preemption is on, but a context switch should only be
4659 * performed if the unblocked task has a priority that is
4660 * higher than the currently executing task. */
4661 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4663 /* Pend the yield to be performed when the scheduler
4664 * is unsuspended. */
4665 xYieldPendings[ 0 ] = pdTRUE;
4669 mtCOVERAGE_TEST_MARKER();
4672 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4674 taskENTER_CRITICAL();
4676 prvYieldForTask( pxTCB );
4678 taskEXIT_CRITICAL();
4680 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4682 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4689 ( void ) xTaskResumeAll();
4691 traceRETURN_xTaskAbortDelay( xReturn );
4696 #endif /* INCLUDE_xTaskAbortDelay */
4697 /*----------------------------------------------------------*/
4699 BaseType_t xTaskIncrementTick( void )
4702 TickType_t xItemValue;
4703 BaseType_t xSwitchRequired = pdFALSE;
4705 traceENTER_xTaskIncrementTick();
4707 /* Called by the portable layer each time a tick interrupt occurs.
4708 * Increments the tick then checks to see if the new tick value will cause any
4709 * tasks to be unblocked. */
4710 traceTASK_INCREMENT_TICK( xTickCount );
4712 /* Tick increment should occur on every kernel timer event. Core 0 has the
4713 * responsibility to increment the tick, or increment the pended ticks if the
4714 * scheduler is suspended. If pended ticks is greater than zero, the core that
4715 * calls xTaskResumeAll has the responsibility to increment the tick. */
4716 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4718 /* Minor optimisation. The tick count cannot change in this
4720 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4722 /* Increment the RTOS tick, switching the delayed and overflowed
4723 * delayed lists if it wraps to 0. */
4724 xTickCount = xConstTickCount;
4726 if( xConstTickCount == ( TickType_t ) 0U )
4728 taskSWITCH_DELAYED_LISTS();
4732 mtCOVERAGE_TEST_MARKER();
4735 /* See if this tick has made a timeout expire. Tasks are stored in
4736 * the queue in the order of their wake time - meaning once one task
4737 * has been found whose block time has not expired there is no need to
4738 * look any further down the list. */
4739 if( xConstTickCount >= xNextTaskUnblockTime )
4743 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4745 /* The delayed list is empty. Set xNextTaskUnblockTime
4746 * to the maximum possible value so it is extremely
4748 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4749 * next time through. */
4750 xNextTaskUnblockTime = portMAX_DELAY;
4755 /* The delayed list is not empty, get the value of the
4756 * item at the head of the delayed list. This is the time
4757 * at which the task at the head of the delayed list must
4758 * be removed from the Blocked state. */
4759 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4760 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4761 /* coverity[misra_c_2012_rule_11_5_violation] */
4762 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
4763 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4765 if( xConstTickCount < xItemValue )
4767 /* It is not time to unblock this item yet, but the
4768 * item value is the time at which the task at the head
4769 * of the blocked list must be removed from the Blocked
4770 * state - so record the item value in
4771 * xNextTaskUnblockTime. */
4772 xNextTaskUnblockTime = xItemValue;
4777 mtCOVERAGE_TEST_MARKER();
4780 /* It is time to remove the item from the Blocked state. */
4781 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4783 /* Is the task waiting on an event also? If so remove
4784 * it from the event list. */
4785 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4787 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4791 mtCOVERAGE_TEST_MARKER();
4794 /* Place the unblocked task into the appropriate ready
4796 prvAddTaskToReadyList( pxTCB );
4798 /* A task being unblocked cannot cause an immediate
4799 * context switch if preemption is turned off. */
4800 #if ( configUSE_PREEMPTION == 1 )
4802 #if ( configNUMBER_OF_CORES == 1 )
4804 /* Preemption is on, but a context switch should
4805 * only be performed if the unblocked task's
4806 * priority is higher than the currently executing
4808 * The case of equal priority tasks sharing
4809 * processing time (which happens when both
4810 * preemption and time slicing are on) is
4812 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4814 xSwitchRequired = pdTRUE;
4818 mtCOVERAGE_TEST_MARKER();
4821 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4823 prvYieldForTask( pxTCB );
4825 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4827 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4832 /* Tasks of equal priority to the currently running task will share
4833 * processing time (time slice) if preemption is on, and the application
4834 * writer has not explicitly turned time slicing off. */
4835 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4837 #if ( configNUMBER_OF_CORES == 1 )
4839 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > 1U )
4841 xSwitchRequired = pdTRUE;
4845 mtCOVERAGE_TEST_MARKER();
4848 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4852 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4854 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1U )
4856 xYieldPendings[ xCoreID ] = pdTRUE;
4860 mtCOVERAGE_TEST_MARKER();
4864 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4866 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4868 #if ( configUSE_TICK_HOOK == 1 )
4870 /* Guard against the tick hook being called when the pended tick
4871 * count is being unwound (when the scheduler is being unlocked). */
4872 if( xPendedTicks == ( TickType_t ) 0 )
4874 vApplicationTickHook();
4878 mtCOVERAGE_TEST_MARKER();
4881 #endif /* configUSE_TICK_HOOK */
4883 #if ( configUSE_PREEMPTION == 1 )
4885 #if ( configNUMBER_OF_CORES == 1 )
4887 /* For single core the core ID is always 0. */
4888 if( xYieldPendings[ 0 ] != pdFALSE )
4890 xSwitchRequired = pdTRUE;
4894 mtCOVERAGE_TEST_MARKER();
4897 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4899 BaseType_t xCoreID, xCurrentCoreID;
4900 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4902 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4904 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4905 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4908 if( xYieldPendings[ xCoreID ] != pdFALSE )
4910 if( xCoreID == xCurrentCoreID )
4912 xSwitchRequired = pdTRUE;
4916 prvYieldCore( xCoreID );
4921 mtCOVERAGE_TEST_MARKER();
4926 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4928 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4934 /* The tick hook gets called at regular intervals, even if the
4935 * scheduler is locked. */
4936 #if ( configUSE_TICK_HOOK == 1 )
4938 vApplicationTickHook();
4943 traceRETURN_xTaskIncrementTick( xSwitchRequired );
4945 return xSwitchRequired;
4947 /*-----------------------------------------------------------*/
4949 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4951 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4952 TaskHookFunction_t pxHookFunction )
4956 traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4958 /* If xTask is NULL then it is the task hook of the calling task that is
4962 xTCB = ( TCB_t * ) pxCurrentTCB;
4969 /* Save the hook function in the TCB. A critical section is required as
4970 * the value can be accessed from an interrupt. */
4971 taskENTER_CRITICAL();
4973 xTCB->pxTaskTag = pxHookFunction;
4975 taskEXIT_CRITICAL();
4977 traceRETURN_vTaskSetApplicationTaskTag();
4980 #endif /* configUSE_APPLICATION_TASK_TAG */
4981 /*-----------------------------------------------------------*/
4983 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4985 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4988 TaskHookFunction_t xReturn;
4990 traceENTER_xTaskGetApplicationTaskTag( xTask );
4992 /* If xTask is NULL then set the calling task's hook. */
4993 pxTCB = prvGetTCBFromHandle( xTask );
4994 configASSERT( pxTCB != NULL );
4996 /* Save the hook function in the TCB. A critical section is required as
4997 * the value can be accessed from an interrupt. */
4998 taskENTER_CRITICAL();
5000 xReturn = pxTCB->pxTaskTag;
5002 taskEXIT_CRITICAL();
5004 traceRETURN_xTaskGetApplicationTaskTag( xReturn );
5009 #endif /* configUSE_APPLICATION_TASK_TAG */
5010 /*-----------------------------------------------------------*/
5012 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5014 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
5017 TaskHookFunction_t xReturn;
5018 UBaseType_t uxSavedInterruptStatus;
5020 traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
5022 /* If xTask is NULL then set the calling task's hook. */
5023 pxTCB = prvGetTCBFromHandle( xTask );
5024 configASSERT( pxTCB != NULL );
5026 /* Save the hook function in the TCB. A critical section is required as
5027 * the value can be accessed from an interrupt. */
5028 /* MISRA Ref 4.7.1 [Return value shall be checked] */
5029 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
5030 /* coverity[misra_c_2012_directive_4_7_violation] */
5031 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
5033 xReturn = pxTCB->pxTaskTag;
5035 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
5037 traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
5042 #endif /* configUSE_APPLICATION_TASK_TAG */
5043 /*-----------------------------------------------------------*/
5045 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5047 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
5048 void * pvParameter )
5053 traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
5055 /* If xTask is NULL then we are calling our own task hook. */
5058 xTCB = pxCurrentTCB;
5065 if( xTCB->pxTaskTag != NULL )
5067 xReturn = xTCB->pxTaskTag( pvParameter );
5074 traceRETURN_xTaskCallApplicationTaskHook( xReturn );
5079 #endif /* configUSE_APPLICATION_TASK_TAG */
5080 /*-----------------------------------------------------------*/
5082 #if ( configNUMBER_OF_CORES == 1 )
5083 void vTaskSwitchContext( void )
5085 traceENTER_vTaskSwitchContext();
5087 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5089 /* The scheduler is currently suspended - do not allow a context
5091 xYieldPendings[ 0 ] = pdTRUE;
5095 xYieldPendings[ 0 ] = pdFALSE;
5096 traceTASK_SWITCHED_OUT();
5098 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5100 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5101 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
5103 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
5106 /* Add the amount of time the task has been running to the
5107 * accumulated time so far. The time the task started running was
5108 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5109 * protection here so count values are only valid until the timer
5110 * overflows. The guard against negative values is to protect
5111 * against suspect run time stat counter implementations - which
5112 * are provided by the application, not the kernel. */
5113 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
5115 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
5119 mtCOVERAGE_TEST_MARKER();
5122 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
5124 #endif /* configGENERATE_RUN_TIME_STATS */
5126 /* Check for stack overflow, if configured. */
5127 taskCHECK_FOR_STACK_OVERFLOW();
5129 /* Before the currently running task is switched out, save its errno. */
5130 #if ( configUSE_POSIX_ERRNO == 1 )
5132 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
5136 /* Select a new task to run using either the generic C or port
5137 * optimised asm code. */
5138 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5139 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5140 /* coverity[misra_c_2012_rule_11_5_violation] */
5141 taskSELECT_HIGHEST_PRIORITY_TASK();
5142 traceTASK_SWITCHED_IN();
5144 /* Macro to inject port specific behaviour immediately after
5145 * switching tasks, such as setting an end of stack watchpoint
5146 * or reconfiguring the MPU. */
5147 portTASK_SWITCH_HOOK( pxCurrentTCB );
5149 /* After the new task is switched in, update the global errno. */
5150 #if ( configUSE_POSIX_ERRNO == 1 )
5152 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
5156 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5158 /* Switch C-Runtime's TLS Block to point to the TLS
5159 * Block specific to this task. */
5160 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
5165 traceRETURN_vTaskSwitchContext();
5167 #else /* if ( configNUMBER_OF_CORES == 1 ) */
5168 void vTaskSwitchContext( BaseType_t xCoreID )
5170 traceENTER_vTaskSwitchContext();
5172 /* Acquire both locks:
5173 * - The ISR lock protects the ready list from simultaneous access by
5174 * both other ISRs and tasks.
5175 * - We also take the task lock to pause here in case another core has
5176 * suspended the scheduler. We don't want to simply set xYieldPending
5177 * and move on if another core suspended the scheduler. We should only
5178 * do that if the current core has suspended the scheduler. */
5180 portGET_TASK_LOCK( xCoreID ); /* Must always acquire the task lock first. */
5181 portGET_ISR_LOCK( xCoreID );
5183 /* vTaskSwitchContext() must never be called from within a critical section.
5184 * This is not necessarily true for single core FreeRTOS, but it is for this
5186 configASSERT( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0 );
5188 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5190 /* The scheduler is currently suspended - do not allow a context
5192 xYieldPendings[ xCoreID ] = pdTRUE;
5196 xYieldPendings[ xCoreID ] = pdFALSE;
5197 traceTASK_SWITCHED_OUT();
5199 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5201 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5202 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
5204 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
5207 /* Add the amount of time the task has been running to the
5208 * accumulated time so far. The time the task started running was
5209 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5210 * protection here so count values are only valid until the timer
5211 * overflows. The guard against negative values is to protect
5212 * against suspect run time stat counter implementations - which
5213 * are provided by the application, not the kernel. */
5214 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
5216 pxCurrentTCBs[ xCoreID ]->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
5220 mtCOVERAGE_TEST_MARKER();
5223 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
5225 #endif /* configGENERATE_RUN_TIME_STATS */
5227 /* Check for stack overflow, if configured. */
5228 taskCHECK_FOR_STACK_OVERFLOW();
5230 /* Before the currently running task is switched out, save its errno. */
5231 #if ( configUSE_POSIX_ERRNO == 1 )
5233 pxCurrentTCBs[ xCoreID ]->iTaskErrno = FreeRTOS_errno;
5237 /* Select a new task to run. */
5238 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
5239 traceTASK_SWITCHED_IN();
5241 /* Macro to inject port specific behaviour immediately after
5242 * switching tasks, such as setting an end of stack watchpoint
5243 * or reconfiguring the MPU. */
5244 portTASK_SWITCH_HOOK( pxCurrentTCBs[ portGET_CORE_ID() ] );
5246 /* After the new task is switched in, update the global errno. */
5247 #if ( configUSE_POSIX_ERRNO == 1 )
5249 FreeRTOS_errno = pxCurrentTCBs[ xCoreID ]->iTaskErrno;
5253 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5255 /* Switch C-Runtime's TLS Block to point to the TLS
5256 * Block specific to this task. */
5257 configSET_TLS_BLOCK( pxCurrentTCBs[ xCoreID ]->xTLSBlock );
5262 portRELEASE_ISR_LOCK( xCoreID );
5263 portRELEASE_TASK_LOCK( xCoreID );
5265 traceRETURN_vTaskSwitchContext();
5267 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
5268 /*-----------------------------------------------------------*/
5270 void vTaskPlaceOnEventList( List_t * const pxEventList,
5271 const TickType_t xTicksToWait )
5273 traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
5275 configASSERT( pxEventList );
5277 /* THIS FUNCTION MUST BE CALLED WITH THE
5278 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
5280 /* Place the event list item of the TCB in the appropriate event list.
5281 * This is placed in the list in priority order so the highest priority task
5282 * is the first to be woken by the event.
5284 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
5285 * Normally, the xItemValue of a TCB's ListItem_t members is:
5286 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
5287 * Therefore, the event list is sorted in descending priority order.
5289 * The queue that contains the event list is locked, preventing
5290 * simultaneous access from interrupts. */
5291 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5293 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5295 traceRETURN_vTaskPlaceOnEventList();
5297 /*-----------------------------------------------------------*/
5299 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
5300 const TickType_t xItemValue,
5301 const TickType_t xTicksToWait )
5303 traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
5305 configASSERT( pxEventList );
5307 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5308 * the event groups implementation. */
5309 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5311 /* Store the item value in the event list item. It is safe to access the
5312 * event list item here as interrupts won't access the event list item of a
5313 * task that is not in the Blocked state. */
5314 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5316 /* Place the event list item of the TCB at the end of the appropriate event
5317 * list. It is safe to access the event list here because it is part of an
5318 * event group implementation - and interrupts don't access event groups
5319 * directly (instead they access them indirectly by pending function calls to
5320 * the task level). */
5321 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5323 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5325 traceRETURN_vTaskPlaceOnUnorderedEventList();
5327 /*-----------------------------------------------------------*/
5329 #if ( configUSE_TIMERS == 1 )
5331 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
5332 TickType_t xTicksToWait,
5333 const BaseType_t xWaitIndefinitely )
5335 traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
5337 configASSERT( pxEventList );
5339 /* This function should not be called by application code hence the
5340 * 'Restricted' in its name. It is not part of the public API. It is
5341 * designed for use by kernel code, and has special calling requirements -
5342 * it should be called with the scheduler suspended. */
5345 /* Place the event list item of the TCB in the appropriate event list.
5346 * In this case it is assume that this is the only task that is going to
5347 * be waiting on this event list, so the faster vListInsertEnd() function
5348 * can be used in place of vListInsert. */
5349 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5351 /* If the task should block indefinitely then set the block time to a
5352 * value that will be recognised as an indefinite delay inside the
5353 * prvAddCurrentTaskToDelayedList() function. */
5354 if( xWaitIndefinitely != pdFALSE )
5356 xTicksToWait = portMAX_DELAY;
5359 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
5360 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
5362 traceRETURN_vTaskPlaceOnEventListRestricted();
5365 #endif /* configUSE_TIMERS */
5366 /*-----------------------------------------------------------*/
5368 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
5370 TCB_t * pxUnblockedTCB;
5373 traceENTER_xTaskRemoveFromEventList( pxEventList );
5375 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
5376 * called from a critical section within an ISR. */
5378 /* The event list is sorted in priority order, so the first in the list can
5379 * be removed as it is known to be the highest priority. Remove the TCB from
5380 * the delayed list, and add it to the ready list.
5382 * If an event is for a queue that is locked then this function will never
5383 * get called - the lock count on the queue will get modified instead. This
5384 * means exclusive access to the event list is guaranteed here.
5386 * This function assumes that a check has already been made to ensure that
5387 * pxEventList is not empty. */
5388 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5389 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5390 /* coverity[misra_c_2012_rule_11_5_violation] */
5391 pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
5392 configASSERT( pxUnblockedTCB );
5393 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5395 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5397 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5398 prvAddTaskToReadyList( pxUnblockedTCB );
5400 #if ( configUSE_TICKLESS_IDLE != 0 )
5402 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5403 * might be set to the blocked task's time out time. If the task is
5404 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5405 * normally left unchanged, because it is automatically reset to a new
5406 * value when the tick count equals xNextTaskUnblockTime. However if
5407 * tickless idling is used it might be more important to enter sleep mode
5408 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5409 * ensure it is updated at the earliest possible time. */
5410 prvResetNextTaskUnblockTime();
5416 /* The delayed and ready lists cannot be accessed, so hold this task
5417 * pending until the scheduler is resumed. */
5418 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5421 #if ( configNUMBER_OF_CORES == 1 )
5423 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5425 /* Return true if the task removed from the event list has a higher
5426 * priority than the calling task. This allows the calling task to know if
5427 * it should force a context switch now. */
5430 /* Mark that a yield is pending in case the user is not using the
5431 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5432 xYieldPendings[ 0 ] = pdTRUE;
5439 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5443 #if ( configUSE_PREEMPTION == 1 )
5445 prvYieldForTask( pxUnblockedTCB );
5447 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5452 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5454 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5456 traceRETURN_xTaskRemoveFromEventList( xReturn );
5459 /*-----------------------------------------------------------*/
5461 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5462 const TickType_t xItemValue )
5464 TCB_t * pxUnblockedTCB;
5466 traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5468 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5469 * the event flags implementation. */
5470 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5472 /* Store the new item value in the event list. */
5473 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5475 /* Remove the event list form the event flag. Interrupts do not access
5477 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5478 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5479 /* coverity[misra_c_2012_rule_11_5_violation] */
5480 pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem );
5481 configASSERT( pxUnblockedTCB );
5482 listREMOVE_ITEM( pxEventListItem );
5484 #if ( configUSE_TICKLESS_IDLE != 0 )
5486 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5487 * might be set to the blocked task's time out time. If the task is
5488 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5489 * normally left unchanged, because it is automatically reset to a new
5490 * value when the tick count equals xNextTaskUnblockTime. However if
5491 * tickless idling is used it might be more important to enter sleep mode
5492 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5493 * ensure it is updated at the earliest possible time. */
5494 prvResetNextTaskUnblockTime();
5498 /* Remove the task from the delayed list and add it to the ready list. The
5499 * scheduler is suspended so interrupts will not be accessing the ready
5501 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5502 prvAddTaskToReadyList( pxUnblockedTCB );
5504 #if ( configNUMBER_OF_CORES == 1 )
5506 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5508 /* The unblocked task has a priority above that of the calling task, so
5509 * a context switch is required. This function is called with the
5510 * scheduler suspended so xYieldPending is set so the context switch
5511 * occurs immediately that the scheduler is resumed (unsuspended). */
5512 xYieldPendings[ 0 ] = pdTRUE;
5515 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5517 #if ( configUSE_PREEMPTION == 1 )
5519 taskENTER_CRITICAL();
5521 prvYieldForTask( pxUnblockedTCB );
5523 taskEXIT_CRITICAL();
5527 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5529 traceRETURN_vTaskRemoveFromUnorderedEventList();
5531 /*-----------------------------------------------------------*/
5533 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5535 traceENTER_vTaskSetTimeOutState( pxTimeOut );
5537 configASSERT( pxTimeOut );
5538 taskENTER_CRITICAL();
5540 pxTimeOut->xOverflowCount = xNumOfOverflows;
5541 pxTimeOut->xTimeOnEntering = xTickCount;
5543 taskEXIT_CRITICAL();
5545 traceRETURN_vTaskSetTimeOutState();
5547 /*-----------------------------------------------------------*/
5549 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5551 traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5553 /* For internal use only as it does not use a critical section. */
5554 pxTimeOut->xOverflowCount = xNumOfOverflows;
5555 pxTimeOut->xTimeOnEntering = xTickCount;
5557 traceRETURN_vTaskInternalSetTimeOutState();
5559 /*-----------------------------------------------------------*/
5561 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5562 TickType_t * const pxTicksToWait )
5566 traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5568 configASSERT( pxTimeOut );
5569 configASSERT( pxTicksToWait );
5571 taskENTER_CRITICAL();
5573 /* Minor optimisation. The tick count cannot change in this block. */
5574 const TickType_t xConstTickCount = xTickCount;
5575 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5577 #if ( INCLUDE_xTaskAbortDelay == 1 )
5578 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5580 /* The delay was aborted, which is not the same as a time out,
5581 * but has the same result. */
5582 pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
5588 #if ( INCLUDE_vTaskSuspend == 1 )
5589 if( *pxTicksToWait == portMAX_DELAY )
5591 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5592 * specified is the maximum block time then the task should block
5593 * indefinitely, and therefore never time out. */
5599 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) )
5601 /* The tick count is greater than the time at which
5602 * vTaskSetTimeout() was called, but has also overflowed since
5603 * vTaskSetTimeOut() was called. It must have wrapped all the way
5604 * around and gone past again. This passed since vTaskSetTimeout()
5607 *pxTicksToWait = ( TickType_t ) 0;
5609 else if( xElapsedTime < *pxTicksToWait )
5611 /* Not a genuine timeout. Adjust parameters for time remaining. */
5612 *pxTicksToWait -= xElapsedTime;
5613 vTaskInternalSetTimeOutState( pxTimeOut );
5618 *pxTicksToWait = ( TickType_t ) 0;
5622 taskEXIT_CRITICAL();
5624 traceRETURN_xTaskCheckForTimeOut( xReturn );
5628 /*-----------------------------------------------------------*/
5630 void vTaskMissedYield( void )
5632 traceENTER_vTaskMissedYield();
5634 /* Must be called from within a critical section. */
5635 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5637 traceRETURN_vTaskMissedYield();
5639 /*-----------------------------------------------------------*/
5641 #if ( configUSE_TRACE_FACILITY == 1 )
5643 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5645 UBaseType_t uxReturn;
5646 TCB_t const * pxTCB;
5648 traceENTER_uxTaskGetTaskNumber( xTask );
5653 uxReturn = pxTCB->uxTaskNumber;
5660 traceRETURN_uxTaskGetTaskNumber( uxReturn );
5665 #endif /* configUSE_TRACE_FACILITY */
5666 /*-----------------------------------------------------------*/
5668 #if ( configUSE_TRACE_FACILITY == 1 )
5670 void vTaskSetTaskNumber( TaskHandle_t xTask,
5671 const UBaseType_t uxHandle )
5675 traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5680 pxTCB->uxTaskNumber = uxHandle;
5683 traceRETURN_vTaskSetTaskNumber();
5686 #endif /* configUSE_TRACE_FACILITY */
5687 /*-----------------------------------------------------------*/
5690 * -----------------------------------------------------------
5691 * The passive idle task.
5692 * ----------------------------------------------------------
5694 * The passive idle task is used for all the additional cores in a SMP
5695 * system. There must be only 1 active idle task and the rest are passive
5698 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5699 * language extensions. The equivalent prototype for this function is:
5701 * void prvPassiveIdleTask( void *pvParameters );
5704 #if ( configNUMBER_OF_CORES > 1 )
5705 static portTASK_FUNCTION( prvPassiveIdleTask, pvParameters )
5707 ( void ) pvParameters;
5711 for( ; configCONTROL_INFINITE_LOOP(); )
5713 #if ( configUSE_PREEMPTION == 0 )
5715 /* If we are not using preemption we keep forcing a task switch to
5716 * see if any other task has become available. If we are using
5717 * preemption we don't need to do this as any task becoming available
5718 * will automatically get the processor anyway. */
5721 #endif /* configUSE_PREEMPTION */
5723 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5725 /* When using preemption tasks of equal priority will be
5726 * timesliced. If a task that is sharing the idle priority is ready
5727 * to run then the idle task should yield before the end of the
5730 * A critical region is not required here as we are just reading from
5731 * the list, and an occasional incorrect value will not matter. If
5732 * the ready list at the idle priority contains one more task than the
5733 * number of idle tasks, which is equal to the configured numbers of cores
5734 * then a task other than the idle task is ready to execute. */
5735 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5741 mtCOVERAGE_TEST_MARKER();
5744 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5746 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
5748 /* Call the user defined function from within the idle task. This
5749 * allows the application designer to add background functionality
5750 * without the overhead of a separate task.
5752 * This hook is intended to manage core activity such as disabling cores that go idle.
5754 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5755 * CALL A FUNCTION THAT MIGHT BLOCK. */
5756 vApplicationPassiveIdleHook();
5758 #endif /* configUSE_PASSIVE_IDLE_HOOK */
5761 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5764 * -----------------------------------------------------------
5766 * ----------------------------------------------------------
5768 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5769 * language extensions. The equivalent prototype for this function is:
5771 * void prvIdleTask( void *pvParameters );
5775 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5777 /* Stop warnings. */
5778 ( void ) pvParameters;
5780 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5781 * SCHEDULER IS STARTED. **/
5783 /* In case a task that has a secure context deletes itself, in which case
5784 * the idle task is responsible for deleting the task's secure context, if
5786 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5788 #if ( configNUMBER_OF_CORES > 1 )
5790 /* SMP all cores start up in the idle task. This initial yield gets the application
5794 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5796 for( ; configCONTROL_INFINITE_LOOP(); )
5798 /* See if any tasks have deleted themselves - if so then the idle task
5799 * is responsible for freeing the deleted task's TCB and stack. */
5800 prvCheckTasksWaitingTermination();
5802 #if ( configUSE_PREEMPTION == 0 )
5804 /* If we are not using preemption we keep forcing a task switch to
5805 * see if any other task has become available. If we are using
5806 * preemption we don't need to do this as any task becoming available
5807 * will automatically get the processor anyway. */
5810 #endif /* configUSE_PREEMPTION */
5812 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5814 /* When using preemption tasks of equal priority will be
5815 * timesliced. If a task that is sharing the idle priority is ready
5816 * to run then the idle task should yield before the end of the
5819 * A critical region is not required here as we are just reading from
5820 * the list, and an occasional incorrect value will not matter. If
5821 * the ready list at the idle priority contains one more task than the
5822 * number of idle tasks, which is equal to the configured numbers of cores
5823 * then a task other than the idle task is ready to execute. */
5824 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5830 mtCOVERAGE_TEST_MARKER();
5833 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5835 #if ( configUSE_IDLE_HOOK == 1 )
5837 /* Call the user defined function from within the idle task. */
5838 vApplicationIdleHook();
5840 #endif /* configUSE_IDLE_HOOK */
5842 /* This conditional compilation should use inequality to 0, not equality
5843 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5844 * user defined low power mode implementations require
5845 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5846 #if ( configUSE_TICKLESS_IDLE != 0 )
5848 TickType_t xExpectedIdleTime;
5850 /* It is not desirable to suspend then resume the scheduler on
5851 * each iteration of the idle task. Therefore, a preliminary
5852 * test of the expected idle time is performed without the
5853 * scheduler suspended. The result here is not necessarily
5855 xExpectedIdleTime = prvGetExpectedIdleTime();
5857 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5861 /* Now the scheduler is suspended, the expected idle
5862 * time can be sampled again, and this time its value can
5864 configASSERT( xNextTaskUnblockTime >= xTickCount );
5865 xExpectedIdleTime = prvGetExpectedIdleTime();
5867 /* Define the following macro to set xExpectedIdleTime to 0
5868 * if the application does not want
5869 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5870 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5872 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5874 traceLOW_POWER_IDLE_BEGIN();
5875 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5876 traceLOW_POWER_IDLE_END();
5880 mtCOVERAGE_TEST_MARKER();
5883 ( void ) xTaskResumeAll();
5887 mtCOVERAGE_TEST_MARKER();
5890 #endif /* configUSE_TICKLESS_IDLE */
5892 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) )
5894 /* Call the user defined function from within the idle task. This
5895 * allows the application designer to add background functionality
5896 * without the overhead of a separate task.
5898 * This hook is intended to manage core activity such as disabling cores that go idle.
5900 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5901 * CALL A FUNCTION THAT MIGHT BLOCK. */
5902 vApplicationPassiveIdleHook();
5904 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) ) */
5907 /*-----------------------------------------------------------*/
5909 #if ( configUSE_TICKLESS_IDLE != 0 )
5911 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5913 #if ( INCLUDE_vTaskSuspend == 1 )
5914 /* The idle task exists in addition to the application tasks. */
5915 const UBaseType_t uxNonApplicationTasks = configNUMBER_OF_CORES;
5916 #endif /* INCLUDE_vTaskSuspend */
5918 eSleepModeStatus eReturn = eStandardSleep;
5920 traceENTER_eTaskConfirmSleepModeStatus();
5922 /* This function must be called from a critical section. */
5924 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0U )
5926 /* A task was made ready while the scheduler was suspended. */
5927 eReturn = eAbortSleep;
5929 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5931 /* A yield was pended while the scheduler was suspended. */
5932 eReturn = eAbortSleep;
5934 else if( xPendedTicks != 0U )
5936 /* A tick interrupt has already occurred but was held pending
5937 * because the scheduler is suspended. */
5938 eReturn = eAbortSleep;
5941 #if ( INCLUDE_vTaskSuspend == 1 )
5942 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5944 /* If all the tasks are in the suspended list (which might mean they
5945 * have an infinite block time rather than actually being suspended)
5946 * then it is safe to turn all clocks off and just wait for external
5948 eReturn = eNoTasksWaitingTimeout;
5950 #endif /* INCLUDE_vTaskSuspend */
5953 mtCOVERAGE_TEST_MARKER();
5956 traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5961 #endif /* configUSE_TICKLESS_IDLE */
5962 /*-----------------------------------------------------------*/
5964 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5966 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5972 traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
5974 if( ( xIndex >= 0 ) &&
5975 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5977 pxTCB = prvGetTCBFromHandle( xTaskToSet );
5978 configASSERT( pxTCB != NULL );
5979 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5982 traceRETURN_vTaskSetThreadLocalStoragePointer();
5985 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5986 /*-----------------------------------------------------------*/
5988 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5990 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5993 void * pvReturn = NULL;
5996 traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
5998 if( ( xIndex >= 0 ) &&
5999 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
6001 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
6002 configASSERT( pxTCB != NULL );
6004 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
6011 traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
6016 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
6017 /*-----------------------------------------------------------*/
6019 #if ( portUSING_MPU_WRAPPERS == 1 )
6021 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
6022 const MemoryRegion_t * const pxRegions )
6026 traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
6028 /* If null is passed in here then we are modifying the MPU settings of
6029 * the calling task. */
6030 pxTCB = prvGetTCBFromHandle( xTaskToModify );
6031 configASSERT( pxTCB != NULL );
6033 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
6035 traceRETURN_vTaskAllocateMPURegions();
6038 #endif /* portUSING_MPU_WRAPPERS */
6039 /*-----------------------------------------------------------*/
6041 static void prvInitialiseTaskLists( void )
6043 UBaseType_t uxPriority;
6045 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
6047 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
6050 vListInitialise( &xDelayedTaskList1 );
6051 vListInitialise( &xDelayedTaskList2 );
6052 vListInitialise( &xPendingReadyList );
6054 #if ( INCLUDE_vTaskDelete == 1 )
6056 vListInitialise( &xTasksWaitingTermination );
6058 #endif /* INCLUDE_vTaskDelete */
6060 #if ( INCLUDE_vTaskSuspend == 1 )
6062 vListInitialise( &xSuspendedTaskList );
6064 #endif /* INCLUDE_vTaskSuspend */
6066 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
6068 pxDelayedTaskList = &xDelayedTaskList1;
6069 pxOverflowDelayedTaskList = &xDelayedTaskList2;
6071 /*-----------------------------------------------------------*/
6073 static void prvCheckTasksWaitingTermination( void )
6075 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
6077 #if ( INCLUDE_vTaskDelete == 1 )
6081 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
6082 * being called too often in the idle task. */
6083 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6085 #if ( configNUMBER_OF_CORES == 1 )
6087 taskENTER_CRITICAL();
6090 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6091 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6092 /* coverity[misra_c_2012_rule_11_5_violation] */
6093 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6094 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6095 --uxCurrentNumberOfTasks;
6096 --uxDeletedTasksWaitingCleanUp;
6099 taskEXIT_CRITICAL();
6101 prvDeleteTCB( pxTCB );
6103 #else /* #if( configNUMBER_OF_CORES == 1 ) */
6107 taskENTER_CRITICAL();
6109 /* For SMP, multiple idles can be running simultaneously
6110 * and we need to check that other idles did not cleanup while we were
6111 * waiting to enter the critical section. */
6112 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6114 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6115 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6116 /* coverity[misra_c_2012_rule_11_5_violation] */
6117 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6119 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
6121 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6122 --uxCurrentNumberOfTasks;
6123 --uxDeletedTasksWaitingCleanUp;
6127 /* The TCB to be deleted still has not yet been switched out
6128 * by the scheduler, so we will just exit this loop early and
6129 * try again next time. */
6130 taskEXIT_CRITICAL();
6135 taskEXIT_CRITICAL();
6139 prvDeleteTCB( pxTCB );
6142 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
6145 #endif /* INCLUDE_vTaskDelete */
6147 /*-----------------------------------------------------------*/
6149 #if ( configUSE_TRACE_FACILITY == 1 )
6151 void vTaskGetInfo( TaskHandle_t xTask,
6152 TaskStatus_t * pxTaskStatus,
6153 BaseType_t xGetFreeStackSpace,
6158 traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
6160 /* xTask is NULL then get the state of the calling task. */
6161 pxTCB = prvGetTCBFromHandle( xTask );
6162 configASSERT( pxTCB != NULL );
6164 pxTaskStatus->xHandle = pxTCB;
6165 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
6166 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
6167 pxTaskStatus->pxStackBase = pxTCB->pxStack;
6168 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
6169 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
6170 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
6172 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
6174 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
6176 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
6180 #if ( configUSE_MUTEXES == 1 )
6182 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
6186 pxTaskStatus->uxBasePriority = 0;
6190 #if ( configGENERATE_RUN_TIME_STATS == 1 )
6192 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
6196 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
6200 /* Obtaining the task state is a little fiddly, so is only done if the
6201 * value of eState passed into this function is eInvalid - otherwise the
6202 * state is just set to whatever is passed in. */
6203 if( eState != eInvalid )
6205 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6207 pxTaskStatus->eCurrentState = eRunning;
6211 pxTaskStatus->eCurrentState = eState;
6213 #if ( INCLUDE_vTaskSuspend == 1 )
6215 /* If the task is in the suspended list then there is a
6216 * chance it is actually just blocked indefinitely - so really
6217 * it should be reported as being in the Blocked state. */
6218 if( eState == eSuspended )
6222 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
6224 pxTaskStatus->eCurrentState = eBlocked;
6228 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6232 /* The task does not appear on the event list item of
6233 * and of the RTOS objects, but could still be in the
6234 * blocked state if it is waiting on its notification
6235 * rather than waiting on an object. If not, is
6237 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
6239 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
6241 pxTaskStatus->eCurrentState = eBlocked;
6246 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
6249 ( void ) xTaskResumeAll();
6252 #endif /* INCLUDE_vTaskSuspend */
6254 /* Tasks can be in pending ready list and other state list at the
6255 * same time. These tasks are in ready state no matter what state
6256 * list the task is in. */
6257 taskENTER_CRITICAL();
6259 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
6261 pxTaskStatus->eCurrentState = eReady;
6264 taskEXIT_CRITICAL();
6269 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
6272 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
6273 * parameter is provided to allow it to be skipped. */
6274 if( xGetFreeStackSpace != pdFALSE )
6276 #if ( portSTACK_GROWTH > 0 )
6278 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
6282 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
6288 pxTaskStatus->usStackHighWaterMark = 0;
6291 traceRETURN_vTaskGetInfo();
6294 #endif /* configUSE_TRACE_FACILITY */
6295 /*-----------------------------------------------------------*/
6297 #if ( configUSE_TRACE_FACILITY == 1 )
6299 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
6303 UBaseType_t uxTask = 0;
6304 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
6305 ListItem_t * pxIterator;
6306 TCB_t * pxTCB = NULL;
6308 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
6310 /* Populate an TaskStatus_t structure within the
6311 * pxTaskStatusArray array for each task that is referenced from
6312 * pxList. See the definition of TaskStatus_t in task.h for the
6313 * meaning of each TaskStatus_t structure member. */
6314 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
6316 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6317 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6318 /* coverity[misra_c_2012_rule_11_5_violation] */
6319 pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
6321 vTaskGetInfo( ( TaskHandle_t ) pxTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
6327 mtCOVERAGE_TEST_MARKER();
6333 #endif /* configUSE_TRACE_FACILITY */
6334 /*-----------------------------------------------------------*/
6336 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
6338 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
6340 configSTACK_DEPTH_TYPE uxCount = 0U;
6342 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
6344 pucStackByte -= portSTACK_GROWTH;
6348 uxCount /= ( configSTACK_DEPTH_TYPE ) sizeof( StackType_t );
6353 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
6354 /*-----------------------------------------------------------*/
6356 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
6358 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
6359 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
6360 * user to determine the return type. It gets around the problem of the value
6361 * overflowing on 8-bit types without breaking backward compatibility for
6362 * applications that expect an 8-bit return type. */
6363 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
6366 uint8_t * pucEndOfStack;
6367 configSTACK_DEPTH_TYPE uxReturn;
6369 traceENTER_uxTaskGetStackHighWaterMark2( xTask );
6371 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
6372 * the same except for their return type. Using configSTACK_DEPTH_TYPE
6373 * allows the user to determine the return type. It gets around the
6374 * problem of the value overflowing on 8-bit types without breaking
6375 * backward compatibility for applications that expect an 8-bit return
6378 pxTCB = prvGetTCBFromHandle( xTask );
6379 configASSERT( pxTCB != NULL );
6381 #if portSTACK_GROWTH < 0
6383 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6387 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6391 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
6393 traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
6398 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
6399 /*-----------------------------------------------------------*/
6401 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
6403 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
6406 uint8_t * pucEndOfStack;
6407 UBaseType_t uxReturn;
6409 traceENTER_uxTaskGetStackHighWaterMark( xTask );
6411 pxTCB = prvGetTCBFromHandle( xTask );
6412 configASSERT( pxTCB != NULL );
6414 #if portSTACK_GROWTH < 0
6416 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6420 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6424 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
6426 traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6431 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6432 /*-----------------------------------------------------------*/
6434 #if ( INCLUDE_vTaskDelete == 1 )
6436 static void prvDeleteTCB( TCB_t * pxTCB )
6438 /* This call is required specifically for the TriCore port. It must be
6439 * above the vPortFree() calls. The call is also used by ports/demos that
6440 * want to allocate and clean RAM statically. */
6441 portCLEAN_UP_TCB( pxTCB );
6443 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6445 /* Free up the memory allocated for the task's TLS Block. */
6446 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6450 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6452 /* The task can only have been allocated dynamically - free both
6453 * the stack and TCB. */
6454 vPortFreeStack( pxTCB->pxStack );
6457 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
6459 /* The task could have been allocated statically or dynamically, so
6460 * check what was statically allocated before trying to free the
6462 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6464 /* Both the stack and TCB were allocated dynamically, so both
6466 vPortFreeStack( pxTCB->pxStack );
6469 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6471 /* Only the stack was statically allocated, so the TCB is the
6472 * only memory that must be freed. */
6477 /* Neither the stack nor the TCB were allocated dynamically, so
6478 * nothing needs to be freed. */
6479 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6480 mtCOVERAGE_TEST_MARKER();
6483 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6486 #endif /* INCLUDE_vTaskDelete */
6487 /*-----------------------------------------------------------*/
6489 static void prvResetNextTaskUnblockTime( void )
6491 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6493 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
6494 * the maximum possible value so it is extremely unlikely that the
6495 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6496 * there is an item in the delayed list. */
6497 xNextTaskUnblockTime = portMAX_DELAY;
6501 /* The new current delayed list is not empty, get the value of
6502 * the item at the head of the delayed list. This is the time at
6503 * which the task at the head of the delayed list should be removed
6504 * from the Blocked state. */
6505 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6508 /*-----------------------------------------------------------*/
6510 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_RECURSIVE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6512 #if ( configNUMBER_OF_CORES == 1 )
6513 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6515 TaskHandle_t xReturn;
6517 traceENTER_xTaskGetCurrentTaskHandle();
6519 /* A critical section is not required as this is not called from
6520 * an interrupt and the current TCB will always be the same for any
6521 * individual execution thread. */
6522 xReturn = pxCurrentTCB;
6524 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6528 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6529 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6531 TaskHandle_t xReturn;
6532 UBaseType_t uxSavedInterruptStatus;
6534 traceENTER_xTaskGetCurrentTaskHandle();
6536 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6538 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6540 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6542 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6546 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6548 TaskHandle_t xTaskGetCurrentTaskHandleForCore( BaseType_t xCoreID )
6550 TaskHandle_t xReturn = NULL;
6552 traceENTER_xTaskGetCurrentTaskHandleForCore( xCoreID );
6554 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6556 #if ( configNUMBER_OF_CORES == 1 )
6557 xReturn = pxCurrentTCB;
6558 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6559 xReturn = pxCurrentTCBs[ xCoreID ];
6560 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6563 traceRETURN_xTaskGetCurrentTaskHandleForCore( xReturn );
6568 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_RECURSIVE_MUTEXES == 1 ) ) */
6569 /*-----------------------------------------------------------*/
6571 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6573 BaseType_t xTaskGetSchedulerState( void )
6577 traceENTER_xTaskGetSchedulerState();
6579 if( xSchedulerRunning == pdFALSE )
6581 xReturn = taskSCHEDULER_NOT_STARTED;
6585 #if ( configNUMBER_OF_CORES > 1 )
6586 taskENTER_CRITICAL();
6589 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6591 xReturn = taskSCHEDULER_RUNNING;
6595 xReturn = taskSCHEDULER_SUSPENDED;
6598 #if ( configNUMBER_OF_CORES > 1 )
6599 taskEXIT_CRITICAL();
6603 traceRETURN_xTaskGetSchedulerState( xReturn );
6608 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6609 /*-----------------------------------------------------------*/
6611 #if ( configUSE_MUTEXES == 1 )
6613 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6615 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6616 BaseType_t xReturn = pdFALSE;
6618 traceENTER_xTaskPriorityInherit( pxMutexHolder );
6620 /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6621 * inheritance is not applied in this scenario. */
6622 if( pxMutexHolder != NULL )
6624 /* If the holder of the mutex has a priority below the priority of
6625 * the task attempting to obtain the mutex then it will temporarily
6626 * inherit the priority of the task attempting to obtain the mutex. */
6627 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6629 /* Adjust the mutex holder state to account for its new
6630 * priority. Only reset the event list item value if the value is
6631 * not being used for anything else. */
6632 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
6634 listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority );
6638 mtCOVERAGE_TEST_MARKER();
6641 /* If the task being modified is in the ready state it will need
6642 * to be moved into a new list. */
6643 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6645 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6647 /* It is known that the task is in its ready list so
6648 * there is no need to check again and the port level
6649 * reset macro can be called directly. */
6650 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6654 mtCOVERAGE_TEST_MARKER();
6657 /* Inherit the priority before being moved into the new list. */
6658 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6659 prvAddTaskToReadyList( pxMutexHolderTCB );
6660 #if ( configNUMBER_OF_CORES > 1 )
6662 /* The priority of the task is raised. Yield for this task
6663 * if it is not running. */
6664 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6666 prvYieldForTask( pxMutexHolderTCB );
6669 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6673 /* Just inherit the priority. */
6674 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6677 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6679 /* Inheritance occurred. */
6684 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6686 /* The base priority of the mutex holder is lower than the
6687 * priority of the task attempting to take the mutex, but the
6688 * current priority of the mutex holder is not lower than the
6689 * priority of the task attempting to take the mutex.
6690 * Therefore the mutex holder must have already inherited a
6691 * priority, but inheritance would have occurred if that had
6692 * not been the case. */
6697 mtCOVERAGE_TEST_MARKER();
6703 mtCOVERAGE_TEST_MARKER();
6706 traceRETURN_xTaskPriorityInherit( xReturn );
6711 #endif /* configUSE_MUTEXES */
6712 /*-----------------------------------------------------------*/
6714 #if ( configUSE_MUTEXES == 1 )
6716 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6718 TCB_t * const pxTCB = pxMutexHolder;
6719 BaseType_t xReturn = pdFALSE;
6721 traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6723 if( pxMutexHolder != NULL )
6725 /* A task can only have an inherited priority if it holds the mutex.
6726 * If the mutex is held by a task then it cannot be given from an
6727 * interrupt, and if a mutex is given by the holding task then it must
6728 * be the running state task. */
6729 configASSERT( pxTCB == pxCurrentTCB );
6730 configASSERT( pxTCB->uxMutexesHeld );
6731 ( pxTCB->uxMutexesHeld )--;
6733 /* Has the holder of the mutex inherited the priority of another
6735 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6737 /* Only disinherit if no other mutexes are held. */
6738 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6740 /* A task can only have an inherited priority if it holds
6741 * the mutex. If the mutex is held by a task then it cannot be
6742 * given from an interrupt, and if a mutex is given by the
6743 * holding task then it must be the running state task. Remove
6744 * the holding task from the ready list. */
6745 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6747 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6751 mtCOVERAGE_TEST_MARKER();
6754 /* Disinherit the priority before adding the task into the
6755 * new ready list. */
6756 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6757 pxTCB->uxPriority = pxTCB->uxBasePriority;
6759 /* Reset the event list item value. It cannot be in use for
6760 * any other purpose if this task is running, and it must be
6761 * running to give back the mutex. */
6762 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority );
6763 prvAddTaskToReadyList( pxTCB );
6764 #if ( configNUMBER_OF_CORES > 1 )
6766 /* The priority of the task is dropped. Yield the core on
6767 * which the task is running. */
6768 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6770 prvYieldCore( pxTCB->xTaskRunState );
6773 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6775 /* Return true to indicate that a context switch is required.
6776 * This is only actually required in the corner case whereby
6777 * multiple mutexes were held and the mutexes were given back
6778 * in an order different to that in which they were taken.
6779 * If a context switch did not occur when the first mutex was
6780 * returned, even if a task was waiting on it, then a context
6781 * switch should occur when the last mutex is returned whether
6782 * a task is waiting on it or not. */
6787 mtCOVERAGE_TEST_MARKER();
6792 mtCOVERAGE_TEST_MARKER();
6797 mtCOVERAGE_TEST_MARKER();
6800 traceRETURN_xTaskPriorityDisinherit( xReturn );
6805 #endif /* configUSE_MUTEXES */
6806 /*-----------------------------------------------------------*/
6808 #if ( configUSE_MUTEXES == 1 )
6810 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6811 UBaseType_t uxHighestPriorityWaitingTask )
6813 TCB_t * const pxTCB = pxMutexHolder;
6814 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6815 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6817 traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6819 if( pxMutexHolder != NULL )
6821 /* If pxMutexHolder is not NULL then the holder must hold at least
6823 configASSERT( pxTCB->uxMutexesHeld );
6825 /* Determine the priority to which the priority of the task that
6826 * holds the mutex should be set. This will be the greater of the
6827 * holding task's base priority and the priority of the highest
6828 * priority task that is waiting to obtain the mutex. */
6829 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6831 uxPriorityToUse = uxHighestPriorityWaitingTask;
6835 uxPriorityToUse = pxTCB->uxBasePriority;
6838 /* Does the priority need to change? */
6839 if( pxTCB->uxPriority != uxPriorityToUse )
6841 /* Only disinherit if no other mutexes are held. This is a
6842 * simplification in the priority inheritance implementation. If
6843 * the task that holds the mutex is also holding other mutexes then
6844 * the other mutexes may have caused the priority inheritance. */
6845 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6847 /* If a task has timed out because it already holds the
6848 * mutex it was trying to obtain then it cannot of inherited
6849 * its own priority. */
6850 configASSERT( pxTCB != pxCurrentTCB );
6852 /* Disinherit the priority, remembering the previous
6853 * priority to facilitate determining the subject task's
6855 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6856 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6857 pxTCB->uxPriority = uxPriorityToUse;
6859 /* Only reset the event list item value if the value is not
6860 * being used for anything else. */
6861 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
6863 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse );
6867 mtCOVERAGE_TEST_MARKER();
6870 /* If the running task is not the task that holds the mutex
6871 * then the task that holds the mutex could be in either the
6872 * Ready, Blocked or Suspended states. Only remove the task
6873 * from its current state list if it is in the Ready state as
6874 * the task's priority is going to change and there is one
6875 * Ready list per priority. */
6876 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6878 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6880 /* It is known that the task is in its ready list so
6881 * there is no need to check again and the port level
6882 * reset macro can be called directly. */
6883 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6887 mtCOVERAGE_TEST_MARKER();
6890 prvAddTaskToReadyList( pxTCB );
6891 #if ( configNUMBER_OF_CORES > 1 )
6893 /* The priority of the task is dropped. Yield the core on
6894 * which the task is running. */
6895 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6897 prvYieldCore( pxTCB->xTaskRunState );
6900 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6904 mtCOVERAGE_TEST_MARKER();
6909 mtCOVERAGE_TEST_MARKER();
6914 mtCOVERAGE_TEST_MARKER();
6919 mtCOVERAGE_TEST_MARKER();
6922 traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6925 #endif /* configUSE_MUTEXES */
6926 /*-----------------------------------------------------------*/
6928 #if ( configNUMBER_OF_CORES > 1 )
6930 /* If not in a critical section then yield immediately.
6931 * Otherwise set xYieldPendings to true to wait to
6932 * yield until exiting the critical section.
6934 void vTaskYieldWithinAPI( void )
6936 UBaseType_t ulState;
6938 traceENTER_vTaskYieldWithinAPI();
6940 ulState = portSET_INTERRUPT_MASK();
6942 const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
6944 if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
6950 xYieldPendings[ xCoreID ] = pdTRUE;
6953 portCLEAR_INTERRUPT_MASK( ulState );
6955 traceRETURN_vTaskYieldWithinAPI();
6957 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6959 /*-----------------------------------------------------------*/
6961 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6963 void vTaskEnterCritical( void )
6965 traceENTER_vTaskEnterCritical();
6967 portDISABLE_INTERRUPTS();
6969 if( xSchedulerRunning != pdFALSE )
6971 ( pxCurrentTCB->uxCriticalNesting )++;
6973 /* This is not the interrupt safe version of the enter critical
6974 * function so assert() if it is being called from an interrupt
6975 * context. Only API functions that end in "FromISR" can be used in an
6976 * interrupt. Only assert if the critical nesting count is 1 to
6977 * protect against recursive calls if the assert function also uses a
6978 * critical section. */
6979 if( pxCurrentTCB->uxCriticalNesting == 1U )
6981 portASSERT_IF_IN_ISR();
6986 mtCOVERAGE_TEST_MARKER();
6989 traceRETURN_vTaskEnterCritical();
6992 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6993 /*-----------------------------------------------------------*/
6995 #if ( configNUMBER_OF_CORES > 1 )
6997 void vTaskEnterCritical( void )
6999 traceENTER_vTaskEnterCritical();
7001 portDISABLE_INTERRUPTS();
7003 const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
7005 if( xSchedulerRunning != pdFALSE )
7007 if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
7009 portGET_TASK_LOCK( xCoreID );
7010 portGET_ISR_LOCK( xCoreID );
7013 portINCREMENT_CRITICAL_NESTING_COUNT( xCoreID );
7015 /* This is not the interrupt safe version of the enter critical
7016 * function so assert() if it is being called from an interrupt
7017 * context. Only API functions that end in "FromISR" can be used in an
7018 * interrupt. Only assert if the critical nesting count is 1 to
7019 * protect against recursive calls if the assert function also uses a
7020 * critical section. */
7021 if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 1U )
7023 portASSERT_IF_IN_ISR();
7025 if( uxSchedulerSuspended == 0U )
7027 /* The only time there would be a problem is if this is called
7028 * before a context switch and vTaskExitCritical() is called
7029 * after pxCurrentTCB changes. Therefore this should not be
7030 * used within vTaskSwitchContext(). */
7031 prvCheckForRunStateChange();
7037 mtCOVERAGE_TEST_MARKER();
7041 traceRETURN_vTaskEnterCritical();
7044 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7046 /*-----------------------------------------------------------*/
7048 #if ( configNUMBER_OF_CORES > 1 )
7050 UBaseType_t vTaskEnterCriticalFromISR( void )
7052 UBaseType_t uxSavedInterruptStatus = 0;
7053 const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
7055 traceENTER_vTaskEnterCriticalFromISR();
7057 if( xSchedulerRunning != pdFALSE )
7059 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
7061 if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
7063 portGET_ISR_LOCK( xCoreID );
7066 portINCREMENT_CRITICAL_NESTING_COUNT( xCoreID );
7070 mtCOVERAGE_TEST_MARKER();
7073 traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
7075 return uxSavedInterruptStatus;
7078 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7079 /*-----------------------------------------------------------*/
7081 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
7083 void vTaskExitCritical( void )
7085 traceENTER_vTaskExitCritical();
7087 if( xSchedulerRunning != pdFALSE )
7089 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
7090 * does not match a previous call to vTaskEnterCritical(). */
7091 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
7093 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7094 * to exit critical section from ISR. */
7095 portASSERT_IF_IN_ISR();
7097 if( pxCurrentTCB->uxCriticalNesting > 0U )
7099 ( pxCurrentTCB->uxCriticalNesting )--;
7101 if( pxCurrentTCB->uxCriticalNesting == 0U )
7103 portENABLE_INTERRUPTS();
7107 mtCOVERAGE_TEST_MARKER();
7112 mtCOVERAGE_TEST_MARKER();
7117 mtCOVERAGE_TEST_MARKER();
7120 traceRETURN_vTaskExitCritical();
7123 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
7124 /*-----------------------------------------------------------*/
7126 #if ( configNUMBER_OF_CORES > 1 )
7128 void vTaskExitCritical( void )
7130 const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
7132 traceENTER_vTaskExitCritical();
7134 if( xSchedulerRunning != pdFALSE )
7136 /* If critical nesting count is zero then this function
7137 * does not match a previous call to vTaskEnterCritical(). */
7138 configASSERT( portGET_CRITICAL_NESTING_COUNT( xCoreID ) > 0U );
7140 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7141 * to exit critical section from ISR. */
7142 portASSERT_IF_IN_ISR();
7144 if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) > 0U )
7146 portDECREMENT_CRITICAL_NESTING_COUNT( xCoreID );
7148 if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
7150 BaseType_t xYieldCurrentTask;
7152 /* Get the xYieldPending stats inside the critical section. */
7153 xYieldCurrentTask = xYieldPendings[ xCoreID ];
7155 portRELEASE_ISR_LOCK( xCoreID );
7156 portRELEASE_TASK_LOCK( xCoreID );
7157 portENABLE_INTERRUPTS();
7159 /* When a task yields in a critical section it just sets
7160 * xYieldPending to true. So now that we have exited the
7161 * critical section check if xYieldPending is true, and
7163 if( xYieldCurrentTask != pdFALSE )
7170 mtCOVERAGE_TEST_MARKER();
7175 mtCOVERAGE_TEST_MARKER();
7180 mtCOVERAGE_TEST_MARKER();
7183 traceRETURN_vTaskExitCritical();
7186 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7187 /*-----------------------------------------------------------*/
7189 #if ( configNUMBER_OF_CORES > 1 )
7191 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
7195 traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
7197 if( xSchedulerRunning != pdFALSE )
7199 xCoreID = ( BaseType_t ) portGET_CORE_ID();
7201 /* If critical nesting count is zero then this function
7202 * does not match a previous call to vTaskEnterCritical(). */
7203 configASSERT( portGET_CRITICAL_NESTING_COUNT( xCoreID ) > 0U );
7205 if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) > 0U )
7207 portDECREMENT_CRITICAL_NESTING_COUNT( xCoreID );
7209 if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
7211 portRELEASE_ISR_LOCK( xCoreID );
7212 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
7216 mtCOVERAGE_TEST_MARKER();
7221 mtCOVERAGE_TEST_MARKER();
7226 mtCOVERAGE_TEST_MARKER();
7229 traceRETURN_vTaskExitCriticalFromISR();
7232 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7233 /*-----------------------------------------------------------*/
7235 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
7237 static char * prvWriteNameToBuffer( char * pcBuffer,
7238 const char * pcTaskName )
7242 /* Start by copying the entire string. */
7243 ( void ) strcpy( pcBuffer, pcTaskName );
7245 /* Pad the end of the string with spaces to ensure columns line up when
7247 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
7249 pcBuffer[ x ] = ' ';
7253 pcBuffer[ x ] = ( char ) 0x00;
7255 /* Return the new end of string. */
7256 return &( pcBuffer[ x ] );
7259 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
7260 /*-----------------------------------------------------------*/
7262 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
7264 void vTaskListTasks( char * pcWriteBuffer,
7265 size_t uxBufferLength )
7267 TaskStatus_t * pxTaskStatusArray;
7268 size_t uxConsumedBufferLength = 0;
7269 size_t uxCharsWrittenBySnprintf;
7270 int iSnprintfReturnValue;
7271 BaseType_t xOutputBufferFull = pdFALSE;
7272 UBaseType_t uxArraySize, x;
7275 traceENTER_vTaskListTasks( pcWriteBuffer, uxBufferLength );
7280 * This function is provided for convenience only, and is used by many
7281 * of the demo applications. Do not consider it to be part of the
7284 * vTaskListTasks() calls uxTaskGetSystemState(), then formats part of the
7285 * uxTaskGetSystemState() output into a human readable table that
7286 * displays task: names, states, priority, stack usage and task number.
7287 * Stack usage specified as the number of unused StackType_t words stack can hold
7288 * on top of stack - not the number of bytes.
7290 * vTaskListTasks() has a dependency on the snprintf() C library function that
7291 * might bloat the code size, use a lot of stack, and provide different
7292 * results on different platforms. An alternative, tiny, third party,
7293 * and limited functionality implementation of snprintf() is provided in
7294 * many of the FreeRTOS/Demo sub-directories in a file called
7295 * printf-stdarg.c (note printf-stdarg.c does not provide a full
7296 * snprintf() implementation!).
7298 * It is recommended that production systems call uxTaskGetSystemState()
7299 * directly to get access to raw stats data, rather than indirectly
7300 * through a call to vTaskListTasks().
7304 /* Make sure the write buffer does not contain a string. */
7305 *pcWriteBuffer = ( char ) 0x00;
7307 /* Take a snapshot of the number of tasks in case it changes while this
7308 * function is executing. */
7309 uxArraySize = uxCurrentNumberOfTasks;
7311 /* Allocate an array index for each task. NOTE! if
7312 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7313 * equate to NULL. */
7314 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7315 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7316 /* coverity[misra_c_2012_rule_11_5_violation] */
7317 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7319 if( pxTaskStatusArray != NULL )
7321 /* Generate the (binary) data. */
7322 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
7324 /* Create a human readable table from the binary data. */
7325 for( x = 0; x < uxArraySize; x++ )
7327 switch( pxTaskStatusArray[ x ].eCurrentState )
7330 cStatus = tskRUNNING_CHAR;
7334 cStatus = tskREADY_CHAR;
7338 cStatus = tskBLOCKED_CHAR;
7342 cStatus = tskSUSPENDED_CHAR;
7346 cStatus = tskDELETED_CHAR;
7349 case eInvalid: /* Fall through. */
7350 default: /* Should not get here, but it is included
7351 * to prevent static checking errors. */
7352 cStatus = ( char ) 0x00;
7356 /* Is there enough space in the buffer to hold task name? */
7357 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7359 /* Write the task name to the string, padding with spaces so it
7360 * can be printed in tabular form more easily. */
7361 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7362 /* Do not count the terminating null character. */
7363 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7365 /* Is there space left in the buffer? -1 is done because snprintf
7366 * writes a terminating null character. So we are essentially
7367 * checking if the buffer has space to write at least one non-null
7369 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7371 /* Write the rest of the string. */
7372 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
7373 /* MISRA Ref 21.6.1 [snprintf for utility] */
7374 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7375 /* coverity[misra_c_2012_rule_21_6_violation] */
7376 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7377 uxBufferLength - uxConsumedBufferLength,
7378 "\t%c\t%u\t%u\t%u\t0x%x\r\n",
7380 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7381 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7382 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber,
7383 ( unsigned int ) pxTaskStatusArray[ x ].uxCoreAffinityMask );
7384 #else /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7385 /* MISRA Ref 21.6.1 [snprintf for utility] */
7386 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7387 /* coverity[misra_c_2012_rule_21_6_violation] */
7388 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7389 uxBufferLength - uxConsumedBufferLength,
7390 "\t%c\t%u\t%u\t%u\r\n",
7392 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7393 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7394 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber );
7395 #endif /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7396 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7398 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7399 pcWriteBuffer += uxCharsWrittenBySnprintf;
7403 xOutputBufferFull = pdTRUE;
7408 xOutputBufferFull = pdTRUE;
7411 if( xOutputBufferFull == pdTRUE )
7417 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7418 * is 0 then vPortFree() will be #defined to nothing. */
7419 vPortFree( pxTaskStatusArray );
7423 mtCOVERAGE_TEST_MARKER();
7426 traceRETURN_vTaskListTasks();
7429 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7430 /*----------------------------------------------------------*/
7432 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
7434 void vTaskGetRunTimeStatistics( char * pcWriteBuffer,
7435 size_t uxBufferLength )
7437 TaskStatus_t * pxTaskStatusArray;
7438 size_t uxConsumedBufferLength = 0;
7439 size_t uxCharsWrittenBySnprintf;
7440 int iSnprintfReturnValue;
7441 BaseType_t xOutputBufferFull = pdFALSE;
7442 UBaseType_t uxArraySize, x;
7443 configRUN_TIME_COUNTER_TYPE ulTotalTime = 0;
7444 configRUN_TIME_COUNTER_TYPE ulStatsAsPercentage;
7446 traceENTER_vTaskGetRunTimeStatistics( pcWriteBuffer, uxBufferLength );
7451 * This function is provided for convenience only, and is used by many
7452 * of the demo applications. Do not consider it to be part of the
7455 * vTaskGetRunTimeStatistics() calls uxTaskGetSystemState(), then formats part
7456 * of the uxTaskGetSystemState() output into a human readable table that
7457 * displays the amount of time each task has spent in the Running state
7458 * in both absolute and percentage terms.
7460 * vTaskGetRunTimeStatistics() has a dependency on the snprintf() C library
7461 * function that might bloat the code size, use a lot of stack, and
7462 * provide different results on different platforms. An alternative,
7463 * tiny, third party, and limited functionality implementation of
7464 * snprintf() is provided in many of the FreeRTOS/Demo sub-directories in
7465 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
7466 * a full snprintf() implementation!).
7468 * It is recommended that production systems call uxTaskGetSystemState()
7469 * directly to get access to raw stats data, rather than indirectly
7470 * through a call to vTaskGetRunTimeStatistics().
7473 /* Make sure the write buffer does not contain a string. */
7474 *pcWriteBuffer = ( char ) 0x00;
7476 /* Take a snapshot of the number of tasks in case it changes while this
7477 * function is executing. */
7478 uxArraySize = uxCurrentNumberOfTasks;
7480 /* Allocate an array index for each task. NOTE! If
7481 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7482 * equate to NULL. */
7483 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7484 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7485 /* coverity[misra_c_2012_rule_11_5_violation] */
7486 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7488 if( pxTaskStatusArray != NULL )
7490 /* Generate the (binary) data. */
7491 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
7493 /* For percentage calculations. */
7494 ulTotalTime /= ( ( configRUN_TIME_COUNTER_TYPE ) 100U );
7496 /* Avoid divide by zero errors. */
7497 if( ulTotalTime > 0U )
7499 /* Create a human readable table from the binary data. */
7500 for( x = 0; x < uxArraySize; x++ )
7502 /* What percentage of the total run time has the task used?
7503 * This will always be rounded down to the nearest integer.
7504 * ulTotalRunTime has already been divided by 100. */
7505 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
7507 /* Is there enough space in the buffer to hold task name? */
7508 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7510 /* Write the task name to the string, padding with
7511 * spaces so it can be printed in tabular form more
7513 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7514 /* Do not count the terminating null character. */
7515 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7517 /* Is there space left in the buffer? -1 is done because snprintf
7518 * writes a terminating null character. So we are essentially
7519 * checking if the buffer has space to write at least one non-null
7521 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7523 if( ulStatsAsPercentage > 0U )
7525 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7527 /* MISRA Ref 21.6.1 [snprintf for utility] */
7528 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7529 /* coverity[misra_c_2012_rule_21_6_violation] */
7530 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7531 uxBufferLength - uxConsumedBufferLength,
7532 "\t%lu\t\t%lu%%\r\n",
7533 pxTaskStatusArray[ x ].ulRunTimeCounter,
7534 ulStatsAsPercentage );
7536 #else /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7538 /* sizeof( int ) == sizeof( long ) so a smaller
7539 * printf() library can be used. */
7540 /* MISRA Ref 21.6.1 [snprintf for utility] */
7541 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7542 /* coverity[misra_c_2012_rule_21_6_violation] */
7543 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7544 uxBufferLength - uxConsumedBufferLength,
7546 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter,
7547 ( unsigned int ) ulStatsAsPercentage );
7549 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7553 /* If the percentage is zero here then the task has
7554 * consumed less than 1% of the total run time. */
7555 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7557 /* MISRA Ref 21.6.1 [snprintf for utility] */
7558 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7559 /* coverity[misra_c_2012_rule_21_6_violation] */
7560 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7561 uxBufferLength - uxConsumedBufferLength,
7562 "\t%lu\t\t<1%%\r\n",
7563 pxTaskStatusArray[ x ].ulRunTimeCounter );
7567 /* sizeof( int ) == sizeof( long ) so a smaller
7568 * printf() library can be used. */
7569 /* MISRA Ref 21.6.1 [snprintf for utility] */
7570 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7571 /* coverity[misra_c_2012_rule_21_6_violation] */
7572 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7573 uxBufferLength - uxConsumedBufferLength,
7575 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter );
7577 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7580 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7581 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7582 pcWriteBuffer += uxCharsWrittenBySnprintf;
7586 xOutputBufferFull = pdTRUE;
7591 xOutputBufferFull = pdTRUE;
7594 if( xOutputBufferFull == pdTRUE )
7602 mtCOVERAGE_TEST_MARKER();
7605 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7606 * is 0 then vPortFree() will be #defined to nothing. */
7607 vPortFree( pxTaskStatusArray );
7611 mtCOVERAGE_TEST_MARKER();
7614 traceRETURN_vTaskGetRunTimeStatistics();
7617 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7618 /*-----------------------------------------------------------*/
7620 TickType_t uxTaskResetEventItemValue( void )
7622 TickType_t uxReturn;
7624 traceENTER_uxTaskResetEventItemValue();
7626 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7628 /* Reset the event list item to its normal value - so it can be used with
7629 * queues and semaphores. */
7630 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) );
7632 traceRETURN_uxTaskResetEventItemValue( uxReturn );
7636 /*-----------------------------------------------------------*/
7638 #if ( configUSE_MUTEXES == 1 )
7640 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7644 traceENTER_pvTaskIncrementMutexHeldCount();
7646 pxTCB = pxCurrentTCB;
7648 /* If xSemaphoreCreateMutex() is called before any tasks have been created
7649 * then pxCurrentTCB will be NULL. */
7652 ( pxTCB->uxMutexesHeld )++;
7655 traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7660 #endif /* configUSE_MUTEXES */
7661 /*-----------------------------------------------------------*/
7663 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7665 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7666 BaseType_t xClearCountOnExit,
7667 TickType_t xTicksToWait )
7670 BaseType_t xAlreadyYielded, xShouldBlock = pdFALSE;
7672 traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7674 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7676 /* If the notification count is zero, and if we are willing to wait for a
7677 * notification, then block the task and wait. */
7678 if( ( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0U ) && ( xTicksToWait > ( TickType_t ) 0 ) )
7680 /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
7681 * non-deterministic operation. */
7684 /* We MUST enter a critical section to atomically check if a notification
7685 * has occurred and set the flag to indicate that we are waiting for
7686 * a notification. If we do not do so, a notification sent from an ISR
7688 taskENTER_CRITICAL();
7690 /* Only block if the notification count is not already non-zero. */
7691 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0U )
7693 /* Mark this task as waiting for a notification. */
7694 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7696 /* Arrange to wait for a notification. */
7697 xShouldBlock = pdTRUE;
7701 mtCOVERAGE_TEST_MARKER();
7704 taskEXIT_CRITICAL();
7706 /* We are now out of the critical section but the scheduler is still
7707 * suspended, so we are safe to do non-deterministic operations such
7708 * as prvAddCurrentTaskToDelayedList. */
7709 if( xShouldBlock == pdTRUE )
7711 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7712 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7716 mtCOVERAGE_TEST_MARKER();
7719 xAlreadyYielded = xTaskResumeAll();
7721 /* Force a reschedule if xTaskResumeAll has not already done so. */
7722 if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
7724 taskYIELD_WITHIN_API();
7728 mtCOVERAGE_TEST_MARKER();
7732 taskENTER_CRITICAL();
7734 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7735 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7737 if( ulReturn != 0U )
7739 if( xClearCountOnExit != pdFALSE )
7741 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ( uint32_t ) 0U;
7745 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7750 mtCOVERAGE_TEST_MARKER();
7753 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7755 taskEXIT_CRITICAL();
7757 traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7762 #endif /* configUSE_TASK_NOTIFICATIONS */
7763 /*-----------------------------------------------------------*/
7765 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7767 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7768 uint32_t ulBitsToClearOnEntry,
7769 uint32_t ulBitsToClearOnExit,
7770 uint32_t * pulNotificationValue,
7771 TickType_t xTicksToWait )
7773 BaseType_t xReturn, xAlreadyYielded, xShouldBlock = pdFALSE;
7775 traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7777 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7779 /* If the task hasn't received a notification, and if we are willing to wait
7780 * for it, then block the task and wait. */
7781 if( ( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED ) && ( xTicksToWait > ( TickType_t ) 0 ) )
7783 /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
7784 * non-deterministic operation. */
7787 /* We MUST enter a critical section to atomically check and update the
7788 * task notification value. If we do not do so, a notification from
7789 * an ISR will get lost. */
7790 taskENTER_CRITICAL();
7792 /* Only block if a notification is not already pending. */
7793 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7795 /* Clear bits in the task's notification value as bits may get
7796 * set by the notifying task or interrupt. This can be used
7797 * to clear the value to zero. */
7798 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7800 /* Mark this task as waiting for a notification. */
7801 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7803 /* Arrange to wait for a notification. */
7804 xShouldBlock = pdTRUE;
7808 mtCOVERAGE_TEST_MARKER();
7811 taskEXIT_CRITICAL();
7813 /* We are now out of the critical section but the scheduler is still
7814 * suspended, so we are safe to do non-deterministic operations such
7815 * as prvAddCurrentTaskToDelayedList. */
7816 if( xShouldBlock == pdTRUE )
7818 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7819 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7823 mtCOVERAGE_TEST_MARKER();
7826 xAlreadyYielded = xTaskResumeAll();
7828 /* Force a reschedule if xTaskResumeAll has not already done so. */
7829 if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
7831 taskYIELD_WITHIN_API();
7835 mtCOVERAGE_TEST_MARKER();
7839 taskENTER_CRITICAL();
7841 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7843 if( pulNotificationValue != NULL )
7845 /* Output the current notification value, which may or may not
7847 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7850 /* If ucNotifyValue is set then either the task never entered the
7851 * blocked state (because a notification was already pending) or the
7852 * task unblocked because of a notification. Otherwise the task
7853 * unblocked because of a timeout. */
7854 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7856 /* A notification was not received. */
7861 /* A notification was already pending or a notification was
7862 * received while the task was waiting. */
7863 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7867 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7869 taskEXIT_CRITICAL();
7871 traceRETURN_xTaskGenericNotifyWait( xReturn );
7876 #endif /* configUSE_TASK_NOTIFICATIONS */
7877 /*-----------------------------------------------------------*/
7879 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7881 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7882 UBaseType_t uxIndexToNotify,
7884 eNotifyAction eAction,
7885 uint32_t * pulPreviousNotificationValue )
7888 BaseType_t xReturn = pdPASS;
7889 uint8_t ucOriginalNotifyState;
7891 traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7893 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7894 configASSERT( xTaskToNotify );
7895 pxTCB = xTaskToNotify;
7897 taskENTER_CRITICAL();
7899 if( pulPreviousNotificationValue != NULL )
7901 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7904 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7906 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7911 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7915 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7918 case eSetValueWithOverwrite:
7919 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7922 case eSetValueWithoutOverwrite:
7924 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7926 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7930 /* The value could not be written to the task. */
7938 /* The task is being notified without its notify value being
7944 /* Should not get here if all enums are handled.
7945 * Artificially force an assert by testing a value the
7946 * compiler can't assume is const. */
7947 configASSERT( xTickCount == ( TickType_t ) 0 );
7952 traceTASK_NOTIFY( uxIndexToNotify );
7954 /* If the task is in the blocked state specifically to wait for a
7955 * notification then unblock it now. */
7956 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7958 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7959 prvAddTaskToReadyList( pxTCB );
7961 /* The task should not have been on an event list. */
7962 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7964 #if ( configUSE_TICKLESS_IDLE != 0 )
7966 /* If a task is blocked waiting for a notification then
7967 * xNextTaskUnblockTime might be set to the blocked task's time
7968 * out time. If the task is unblocked for a reason other than
7969 * a timeout xNextTaskUnblockTime is normally left unchanged,
7970 * because it will automatically get reset to a new value when
7971 * the tick count equals xNextTaskUnblockTime. However if
7972 * tickless idling is used it might be more important to enter
7973 * sleep mode at the earliest possible time - so reset
7974 * xNextTaskUnblockTime here to ensure it is updated at the
7975 * earliest possible time. */
7976 prvResetNextTaskUnblockTime();
7980 /* Check if the notified task has a priority above the currently
7981 * executing task. */
7982 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7986 mtCOVERAGE_TEST_MARKER();
7989 taskEXIT_CRITICAL();
7991 traceRETURN_xTaskGenericNotify( xReturn );
7996 #endif /* configUSE_TASK_NOTIFICATIONS */
7997 /*-----------------------------------------------------------*/
7999 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8001 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
8002 UBaseType_t uxIndexToNotify,
8004 eNotifyAction eAction,
8005 uint32_t * pulPreviousNotificationValue,
8006 BaseType_t * pxHigherPriorityTaskWoken )
8009 uint8_t ucOriginalNotifyState;
8010 BaseType_t xReturn = pdPASS;
8011 UBaseType_t uxSavedInterruptStatus;
8013 traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
8015 configASSERT( xTaskToNotify );
8016 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8018 /* RTOS ports that support interrupt nesting have the concept of a
8019 * maximum system call (or maximum API call) interrupt priority.
8020 * Interrupts that are above the maximum system call priority are keep
8021 * permanently enabled, even when the RTOS kernel is in a critical section,
8022 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
8023 * is defined in FreeRTOSConfig.h then
8024 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
8025 * failure if a FreeRTOS API function is called from an interrupt that has
8026 * been assigned a priority above the configured maximum system call
8027 * priority. Only FreeRTOS functions that end in FromISR can be called
8028 * from interrupts that have been assigned a priority at or (logically)
8029 * below the maximum system call interrupt priority. FreeRTOS maintains a
8030 * separate interrupt safe API to ensure interrupt entry is as fast and as
8031 * simple as possible. More information (albeit Cortex-M specific) is
8032 * provided on the following link:
8033 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
8034 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
8036 pxTCB = xTaskToNotify;
8038 /* MISRA Ref 4.7.1 [Return value shall be checked] */
8039 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
8040 /* coverity[misra_c_2012_directive_4_7_violation] */
8041 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
8043 if( pulPreviousNotificationValue != NULL )
8045 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
8048 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8049 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8054 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
8058 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8061 case eSetValueWithOverwrite:
8062 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8065 case eSetValueWithoutOverwrite:
8067 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
8069 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8073 /* The value could not be written to the task. */
8081 /* The task is being notified without its notify value being
8087 /* Should not get here if all enums are handled.
8088 * Artificially force an assert by testing a value the
8089 * compiler can't assume is const. */
8090 configASSERT( xTickCount == ( TickType_t ) 0 );
8094 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
8096 /* If the task is in the blocked state specifically to wait for a
8097 * notification then unblock it now. */
8098 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8100 /* The task should not have been on an event list. */
8101 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8103 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8105 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8106 prvAddTaskToReadyList( pxTCB );
8108 #if ( configUSE_TICKLESS_IDLE != 0 )
8110 /* If a task is blocked waiting for a notification then
8111 * xNextTaskUnblockTime might be set to the blocked task's time
8112 * out time. If the task is unblocked for a reason other than
8113 * a timeout xNextTaskUnblockTime is normally left unchanged,
8114 * because it will automatically get reset to a new value when
8115 * the tick count equals xNextTaskUnblockTime. However if
8116 * tickless idling is used it might be more important to enter
8117 * sleep mode at the earliest possible time - so reset
8118 * xNextTaskUnblockTime here to ensure it is updated at the
8119 * earliest possible time. */
8120 prvResetNextTaskUnblockTime();
8126 /* The delayed and ready lists cannot be accessed, so hold
8127 * this task pending until the scheduler is resumed. */
8128 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8131 #if ( configNUMBER_OF_CORES == 1 )
8133 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8135 /* The notified task has a priority above the currently
8136 * executing task so a yield is required. */
8137 if( pxHigherPriorityTaskWoken != NULL )
8139 *pxHigherPriorityTaskWoken = pdTRUE;
8142 /* Mark that a yield is pending in case the user is not
8143 * using the "xHigherPriorityTaskWoken" parameter to an ISR
8144 * safe FreeRTOS function. */
8145 xYieldPendings[ 0 ] = pdTRUE;
8149 mtCOVERAGE_TEST_MARKER();
8152 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8154 #if ( configUSE_PREEMPTION == 1 )
8156 prvYieldForTask( pxTCB );
8158 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8160 if( pxHigherPriorityTaskWoken != NULL )
8162 *pxHigherPriorityTaskWoken = pdTRUE;
8166 #endif /* if ( configUSE_PREEMPTION == 1 ) */
8168 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8171 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8173 traceRETURN_xTaskGenericNotifyFromISR( xReturn );
8178 #endif /* configUSE_TASK_NOTIFICATIONS */
8179 /*-----------------------------------------------------------*/
8181 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8183 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
8184 UBaseType_t uxIndexToNotify,
8185 BaseType_t * pxHigherPriorityTaskWoken )
8188 uint8_t ucOriginalNotifyState;
8189 UBaseType_t uxSavedInterruptStatus;
8191 traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
8193 configASSERT( xTaskToNotify );
8194 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8196 /* RTOS ports that support interrupt nesting have the concept of a
8197 * maximum system call (or maximum API call) interrupt priority.
8198 * Interrupts that are above the maximum system call priority are keep
8199 * permanently enabled, even when the RTOS kernel is in a critical section,
8200 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
8201 * is defined in FreeRTOSConfig.h then
8202 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
8203 * failure if a FreeRTOS API function is called from an interrupt that has
8204 * been assigned a priority above the configured maximum system call
8205 * priority. Only FreeRTOS functions that end in FromISR can be called
8206 * from interrupts that have been assigned a priority at or (logically)
8207 * below the maximum system call interrupt priority. FreeRTOS maintains a
8208 * separate interrupt safe API to ensure interrupt entry is as fast and as
8209 * simple as possible. More information (albeit Cortex-M specific) is
8210 * provided on the following link:
8211 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
8212 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
8214 pxTCB = xTaskToNotify;
8216 /* MISRA Ref 4.7.1 [Return value shall be checked] */
8217 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
8218 /* coverity[misra_c_2012_directive_4_7_violation] */
8219 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
8221 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8222 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8224 /* 'Giving' is equivalent to incrementing a count in a counting
8226 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8228 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
8230 /* If the task is in the blocked state specifically to wait for a
8231 * notification then unblock it now. */
8232 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8234 /* The task should not have been on an event list. */
8235 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8237 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8239 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8240 prvAddTaskToReadyList( pxTCB );
8242 #if ( configUSE_TICKLESS_IDLE != 0 )
8244 /* If a task is blocked waiting for a notification then
8245 * xNextTaskUnblockTime might be set to the blocked task's time
8246 * out time. If the task is unblocked for a reason other than
8247 * a timeout xNextTaskUnblockTime is normally left unchanged,
8248 * because it will automatically get reset to a new value when
8249 * the tick count equals xNextTaskUnblockTime. However if
8250 * tickless idling is used it might be more important to enter
8251 * sleep mode at the earliest possible time - so reset
8252 * xNextTaskUnblockTime here to ensure it is updated at the
8253 * earliest possible time. */
8254 prvResetNextTaskUnblockTime();
8260 /* The delayed and ready lists cannot be accessed, so hold
8261 * this task pending until the scheduler is resumed. */
8262 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8265 #if ( configNUMBER_OF_CORES == 1 )
8267 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8269 /* The notified task has a priority above the currently
8270 * executing task so a yield is required. */
8271 if( pxHigherPriorityTaskWoken != NULL )
8273 *pxHigherPriorityTaskWoken = pdTRUE;
8276 /* Mark that a yield is pending in case the user is not
8277 * using the "xHigherPriorityTaskWoken" parameter in an ISR
8278 * safe FreeRTOS function. */
8279 xYieldPendings[ 0 ] = pdTRUE;
8283 mtCOVERAGE_TEST_MARKER();
8286 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8288 #if ( configUSE_PREEMPTION == 1 )
8290 prvYieldForTask( pxTCB );
8292 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8294 if( pxHigherPriorityTaskWoken != NULL )
8296 *pxHigherPriorityTaskWoken = pdTRUE;
8300 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
8302 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8305 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8307 traceRETURN_vTaskGenericNotifyGiveFromISR();
8310 #endif /* configUSE_TASK_NOTIFICATIONS */
8311 /*-----------------------------------------------------------*/
8313 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8315 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
8316 UBaseType_t uxIndexToClear )
8321 traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
8323 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8325 /* If null is passed in here then it is the calling task that is having
8326 * its notification state cleared. */
8327 pxTCB = prvGetTCBFromHandle( xTask );
8328 configASSERT( pxTCB != NULL );
8330 taskENTER_CRITICAL();
8332 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
8334 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
8342 taskEXIT_CRITICAL();
8344 traceRETURN_xTaskGenericNotifyStateClear( xReturn );
8349 #endif /* configUSE_TASK_NOTIFICATIONS */
8350 /*-----------------------------------------------------------*/
8352 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8354 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
8355 UBaseType_t uxIndexToClear,
8356 uint32_t ulBitsToClear )
8361 traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
8363 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8365 /* If null is passed in here then it is the calling task that is having
8366 * its notification state cleared. */
8367 pxTCB = prvGetTCBFromHandle( xTask );
8368 configASSERT( pxTCB != NULL );
8370 taskENTER_CRITICAL();
8372 /* Return the notification as it was before the bits were cleared,
8373 * then clear the bit mask. */
8374 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
8375 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
8377 taskEXIT_CRITICAL();
8379 traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
8384 #endif /* configUSE_TASK_NOTIFICATIONS */
8385 /*-----------------------------------------------------------*/
8387 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8389 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
8393 traceENTER_ulTaskGetRunTimeCounter( xTask );
8395 pxTCB = prvGetTCBFromHandle( xTask );
8396 configASSERT( pxTCB != NULL );
8398 traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
8400 return pxTCB->ulRunTimeCounter;
8403 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8404 /*-----------------------------------------------------------*/
8406 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8408 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
8411 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8413 traceENTER_ulTaskGetRunTimePercent( xTask );
8415 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
8417 /* For percentage calculations. */
8418 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8420 /* Avoid divide by zero errors. */
8421 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8423 pxTCB = prvGetTCBFromHandle( xTask );
8424 configASSERT( pxTCB != NULL );
8426 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
8433 traceRETURN_ulTaskGetRunTimePercent( ulReturn );
8438 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8439 /*-----------------------------------------------------------*/
8441 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8443 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
8445 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
8448 traceENTER_ulTaskGetIdleRunTimeCounter();
8450 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8452 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8455 traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
8460 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8461 /*-----------------------------------------------------------*/
8463 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8465 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
8467 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8468 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
8471 traceENTER_ulTaskGetIdleRunTimePercent();
8473 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
8475 /* For percentage calculations. */
8476 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8478 /* Avoid divide by zero errors. */
8479 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8481 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8483 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8486 ulReturn = ulRunTimeCounter / ulTotalTime;
8493 traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
8498 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8499 /*-----------------------------------------------------------*/
8501 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
8502 const BaseType_t xCanBlockIndefinitely )
8504 TickType_t xTimeToWake;
8505 const TickType_t xConstTickCount = xTickCount;
8506 List_t * const pxDelayedList = pxDelayedTaskList;
8507 List_t * const pxOverflowDelayedList = pxOverflowDelayedTaskList;
8509 #if ( INCLUDE_xTaskAbortDelay == 1 )
8511 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
8512 * reset to pdFALSE so it can be detected as having been set to pdTRUE
8513 * when the task leaves the Blocked state. */
8514 pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
8518 /* Remove the task from the ready list before adding it to the blocked list
8519 * as the same list item is used for both lists. */
8520 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
8522 /* The current task must be in a ready list, so there is no need to
8523 * check, and the port reset macro can be called directly. */
8524 portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
8528 mtCOVERAGE_TEST_MARKER();
8531 #if ( INCLUDE_vTaskSuspend == 1 )
8533 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
8535 /* Add the task to the suspended task list instead of a delayed task
8536 * list to ensure it is not woken by a timing event. It will block
8538 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
8542 /* Calculate the time at which the task should be woken if the event
8543 * does not occur. This may overflow but this doesn't matter, the
8544 * kernel will manage it correctly. */
8545 xTimeToWake = xConstTickCount + xTicksToWait;
8547 /* The list item will be inserted in wake time order. */
8548 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8550 if( xTimeToWake < xConstTickCount )
8552 /* Wake time has overflowed. Place this item in the overflow
8554 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8555 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8559 /* The wake time has not overflowed, so the current block list
8561 traceMOVED_TASK_TO_DELAYED_LIST();
8562 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8564 /* If the task entering the blocked state was placed at the
8565 * head of the list of blocked tasks then xNextTaskUnblockTime
8566 * needs to be updated too. */
8567 if( xTimeToWake < xNextTaskUnblockTime )
8569 xNextTaskUnblockTime = xTimeToWake;
8573 mtCOVERAGE_TEST_MARKER();
8578 #else /* INCLUDE_vTaskSuspend */
8580 /* Calculate the time at which the task should be woken if the event
8581 * does not occur. This may overflow but this doesn't matter, the kernel
8582 * will manage it correctly. */
8583 xTimeToWake = xConstTickCount + xTicksToWait;
8585 /* The list item will be inserted in wake time order. */
8586 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8588 if( xTimeToWake < xConstTickCount )
8590 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8591 /* Wake time has overflowed. Place this item in the overflow list. */
8592 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8596 traceMOVED_TASK_TO_DELAYED_LIST();
8597 /* The wake time has not overflowed, so the current block list is used. */
8598 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8600 /* If the task entering the blocked state was placed at the head of the
8601 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
8603 if( xTimeToWake < xNextTaskUnblockTime )
8605 xNextTaskUnblockTime = xTimeToWake;
8609 mtCOVERAGE_TEST_MARKER();
8613 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
8614 ( void ) xCanBlockIndefinitely;
8616 #endif /* INCLUDE_vTaskSuspend */
8618 /*-----------------------------------------------------------*/
8620 #if ( portUSING_MPU_WRAPPERS == 1 )
8622 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
8626 traceENTER_xTaskGetMPUSettings( xTask );
8628 pxTCB = prvGetTCBFromHandle( xTask );
8629 configASSERT( pxTCB != NULL );
8631 traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
8633 return &( pxTCB->xMPUSettings );
8636 #endif /* portUSING_MPU_WRAPPERS */
8637 /*-----------------------------------------------------------*/
8639 /* Code below here allows additional code to be inserted into this source file,
8640 * especially where access to file scope functions and data is needed (for example
8641 * when performing module tests). */
8643 #ifdef FREERTOS_MODULE_TEST
8644 #include "tasks_test_access_functions.h"
8648 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
8650 #include "freertos_tasks_c_additions.h"
8652 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8653 static void freertos_tasks_c_additions_init( void )
8655 FREERTOS_TASKS_C_ADDITIONS_INIT();
8659 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8660 /*-----------------------------------------------------------*/
8662 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8665 * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8666 * to provide the memory that is used by the Idle task. It is used when
8667 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8668 * it's own implementation of vApplicationGetIdleTaskMemory by setting
8669 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8671 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8672 StackType_t ** ppxIdleTaskStackBuffer,
8673 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize )
8675 static StaticTask_t xIdleTaskTCB;
8676 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8678 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8679 *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8680 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8683 #if ( configNUMBER_OF_CORES > 1 )
8685 void vApplicationGetPassiveIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8686 StackType_t ** ppxIdleTaskStackBuffer,
8687 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize,
8688 BaseType_t xPassiveIdleTaskIndex )
8690 static StaticTask_t xIdleTaskTCBs[ configNUMBER_OF_CORES - 1 ];
8691 static StackType_t uxIdleTaskStacks[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
8693 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCBs[ xPassiveIdleTaskIndex ] );
8694 *ppxIdleTaskStackBuffer = &( uxIdleTaskStacks[ xPassiveIdleTaskIndex ][ 0 ] );
8695 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8698 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
8700 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8701 /*-----------------------------------------------------------*/
8703 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) && ( configUSE_TIMERS == 1 ) )
8706 * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8707 * to provide the memory that is used by the Timer service task. It is used when
8708 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8709 * it's own implementation of vApplicationGetTimerTaskMemory by setting
8710 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8712 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8713 StackType_t ** ppxTimerTaskStackBuffer,
8714 configSTACK_DEPTH_TYPE * puxTimerTaskStackSize )
8716 static StaticTask_t xTimerTaskTCB;
8717 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8719 *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8720 *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8721 *puxTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8724 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) && ( configUSE_TIMERS == 1 ) ) */
8725 /*-----------------------------------------------------------*/
8728 * Reset the state in this file. This state is normally initialized at start up.
8729 * This function must be called by the application before restarting the
8732 void vTaskResetState( void )
8736 /* Task control block. */
8737 #if ( configNUMBER_OF_CORES == 1 )
8739 pxCurrentTCB = NULL;
8741 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8743 #if ( INCLUDE_vTaskDelete == 1 )
8745 uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
8747 #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
8749 #if ( configUSE_POSIX_ERRNO == 1 )
8753 #endif /* #if ( configUSE_POSIX_ERRNO == 1 ) */
8755 /* Other file private variables. */
8756 uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
8757 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
8758 uxTopReadyPriority = tskIDLE_PRIORITY;
8759 xSchedulerRunning = pdFALSE;
8760 xPendedTicks = ( TickType_t ) 0U;
8762 for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
8764 xYieldPendings[ xCoreID ] = pdFALSE;
8767 xNumOfOverflows = ( BaseType_t ) 0;
8768 uxTaskNumber = ( UBaseType_t ) 0U;
8769 xNextTaskUnblockTime = ( TickType_t ) 0U;
8771 uxSchedulerSuspended = ( UBaseType_t ) 0U;
8773 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8775 for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
8777 ulTaskSwitchedInTime[ xCoreID ] = 0U;
8778 ulTotalRunTime[ xCoreID ] = 0U;
8781 #endif /* #if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8783 /*-----------------------------------------------------------*/