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() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting )
321 #define portSET_CRITICAL_NESTING_COUNT( x ) ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting = ( x ) )
322 #define portINCREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting++ )
323 #define portDECREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->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;
811 /* This must only be called from within a task. */
812 portASSERT_IF_IN_ISR();
814 /* This function is always called with interrupts disabled
815 * so this is safe. */
816 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
818 while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD )
820 /* We are only here if we just entered a critical section
821 * or if we just suspended the scheduler, and another task
822 * has requested that we yield.
824 * This is slightly complicated since we need to save and restore
825 * the suspension and critical nesting counts, as well as release
826 * and reacquire the correct locks. And then, do it all over again
827 * if our state changed again during the reacquisition. */
828 uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
830 if( uxPrevCriticalNesting > 0U )
832 portSET_CRITICAL_NESTING_COUNT( 0U );
833 portRELEASE_ISR_LOCK();
837 /* The scheduler is suspended. uxSchedulerSuspended is updated
838 * only when the task is not requested to yield. */
839 mtCOVERAGE_TEST_MARKER();
842 portRELEASE_TASK_LOCK();
843 portMEMORY_BARRIER();
844 configASSERT( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD );
846 portENABLE_INTERRUPTS();
848 /* Enabling interrupts should cause this core to immediately
849 * service the pending interrupt and yield. If the run state is still
850 * yielding here then that is a problem. */
851 configASSERT( pxThisTCB->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD );
853 portDISABLE_INTERRUPTS();
857 portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
859 if( uxPrevCriticalNesting == 0U )
861 portRELEASE_ISR_LOCK();
865 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
867 /*-----------------------------------------------------------*/
869 #if ( configNUMBER_OF_CORES > 1 )
870 static void prvYieldForTask( const TCB_t * pxTCB )
872 BaseType_t xLowestPriorityToPreempt;
873 BaseType_t xCurrentCoreTaskPriority;
874 BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
877 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
878 BaseType_t xYieldCount = 0;
879 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
881 /* This must be called from a critical section. */
882 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
884 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
886 /* No task should yield for this one if it is a lower priority
887 * than priority level of currently ready tasks. */
888 if( pxTCB->uxPriority >= uxTopReadyPriority )
890 /* Yield is not required for a task which is already running. */
891 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
894 xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
896 /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
897 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
898 --xLowestPriorityToPreempt;
900 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
902 xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
904 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
905 if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
907 xCurrentCoreTaskPriority = ( BaseType_t ) ( xCurrentCoreTaskPriority - 1 );
910 if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
912 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
913 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
916 if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
918 #if ( configUSE_CORE_AFFINITY == 1 )
919 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
922 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
923 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
926 xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
927 xLowestPriorityCore = xCoreID;
933 mtCOVERAGE_TEST_MARKER();
937 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
939 /* Yield all currently running non-idle tasks with a priority lower than
940 * the task that needs to run. */
941 if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
942 ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
944 prvYieldCore( xCoreID );
949 mtCOVERAGE_TEST_MARKER();
952 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
956 mtCOVERAGE_TEST_MARKER();
960 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
961 if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
962 #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
963 if( xLowestPriorityCore >= 0 )
964 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
966 prvYieldCore( xLowestPriorityCore );
969 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
970 /* Verify that the calling core always yields to higher priority tasks. */
971 if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) &&
972 ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
974 configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) ||
975 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) );
980 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
981 /*-----------------------------------------------------------*/
983 #if ( configNUMBER_OF_CORES > 1 )
984 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
986 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
987 BaseType_t xTaskScheduled = pdFALSE;
988 BaseType_t xDecrementTopPriority = pdTRUE;
989 TCB_t * pxTCB = NULL;
991 #if ( configUSE_CORE_AFFINITY == 1 )
992 const TCB_t * pxPreviousTCB = NULL;
994 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
995 BaseType_t xPriorityDropped = pdFALSE;
998 /* This function should be called when scheduler is running. */
999 configASSERT( xSchedulerRunning == pdTRUE );
1001 /* A new task is created and a running task with the same priority yields
1002 * itself to run the new task. When a running task yields itself, it is still
1003 * in the ready list. This running task will be selected before the new task
1004 * since the new task is always added to the end of the ready list.
1005 * The other problem is that the running task still in the same position of
1006 * the ready list when it yields itself. It is possible that it will be selected
1007 * earlier then other tasks which waits longer than this task.
1009 * To fix these problems, the running task should be put to the end of the
1010 * ready list before searching for the ready task in the ready list. */
1011 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
1012 &pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
1014 ( void ) uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
1015 vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
1016 &pxCurrentTCBs[ xCoreID ]->xStateListItem );
1019 while( xTaskScheduled == pdFALSE )
1021 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1023 if( uxCurrentPriority < uxTopReadyPriority )
1025 /* We can't schedule any tasks, other than idle, that have a
1026 * priority lower than the priority of a task currently running
1027 * on another core. */
1028 uxCurrentPriority = tskIDLE_PRIORITY;
1033 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
1035 const List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
1036 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxReadyList );
1037 ListItem_t * pxIterator;
1039 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
1040 * must not be decremented any further. */
1041 xDecrementTopPriority = pdFALSE;
1043 for( pxIterator = listGET_HEAD_ENTRY( pxReadyList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
1045 /* MISRA Ref 11.5.3 [Void pointer assignment] */
1046 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1047 /* coverity[misra_c_2012_rule_11_5_violation] */
1048 pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
1050 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1052 /* When falling back to the idle priority because only one priority
1053 * level is allowed to run at a time, we should ONLY schedule the true
1054 * idle tasks, not user tasks at the idle priority. */
1055 if( uxCurrentPriority < uxTopReadyPriority )
1057 if( ( pxTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U )
1063 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1065 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
1067 #if ( configUSE_CORE_AFFINITY == 1 )
1068 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1071 /* If the task is not being executed by any core swap it in. */
1072 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
1073 #if ( configUSE_CORE_AFFINITY == 1 )
1074 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
1076 pxTCB->xTaskRunState = xCoreID;
1077 pxCurrentTCBs[ xCoreID ] = pxTCB;
1078 xTaskScheduled = pdTRUE;
1081 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
1083 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) );
1085 #if ( configUSE_CORE_AFFINITY == 1 )
1086 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1089 /* The task is already running on this core, mark it as scheduled. */
1090 pxTCB->xTaskRunState = xCoreID;
1091 xTaskScheduled = pdTRUE;
1096 /* This task is running on the core other than xCoreID. */
1097 mtCOVERAGE_TEST_MARKER();
1100 if( xTaskScheduled != pdFALSE )
1102 /* A task has been selected to run on this core. */
1109 if( xDecrementTopPriority != pdFALSE )
1111 uxTopReadyPriority--;
1112 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1114 xPriorityDropped = pdTRUE;
1120 /* There are configNUMBER_OF_CORES Idle tasks created when scheduler started.
1121 * The scheduler should be able to select a task to run when uxCurrentPriority
1122 * is tskIDLE_PRIORITY. uxCurrentPriority is never decreased to value blow
1123 * tskIDLE_PRIORITY. */
1124 if( uxCurrentPriority > tskIDLE_PRIORITY )
1126 uxCurrentPriority--;
1130 /* This function is called when idle task is not created. Break the
1131 * loop to prevent uxCurrentPriority overrun. */
1136 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1138 if( xTaskScheduled == pdTRUE )
1140 if( xPriorityDropped != pdFALSE )
1142 /* There may be several ready tasks that were being prevented from running because there was
1143 * a higher priority task running. Now that the last of the higher priority tasks is no longer
1144 * running, make sure all the other idle tasks yield. */
1147 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUMBER_OF_CORES; x++ )
1149 if( ( pxCurrentTCBs[ x ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1157 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1159 #if ( configUSE_CORE_AFFINITY == 1 )
1161 if( xTaskScheduled == pdTRUE )
1163 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
1165 /* A ready task was just evicted from this core. See if it can be
1166 * scheduled on any other core. */
1167 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
1168 BaseType_t xLowestPriority = ( BaseType_t ) pxPreviousTCB->uxPriority;
1169 BaseType_t xLowestPriorityCore = -1;
1172 if( ( pxPreviousTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1174 xLowestPriority = xLowestPriority - 1;
1177 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1179 /* pxPreviousTCB was removed from this core and this core is not excluded
1180 * from it's core affinity mask.
1182 * pxPreviousTCB is preempted by the new higher priority task
1183 * pxCurrentTCBs[ xCoreID ]. When searching a new core for pxPreviousTCB,
1184 * we do not need to look at the cores on which pxCurrentTCBs[ xCoreID ]
1185 * is allowed to run. The reason is - when more than one cores are
1186 * eligible for an incoming task, we preempt the core with the minimum
1187 * priority task. Because this core (i.e. xCoreID) was preempted for
1188 * pxCurrentTCBs[ xCoreID ], this means that all the others cores
1189 * where pxCurrentTCBs[ xCoreID ] can run, are running tasks with priority
1190 * no lower than pxPreviousTCB's priority. Therefore, the only cores where
1191 * which can be preempted for pxPreviousTCB are the ones where
1192 * pxCurrentTCBs[ xCoreID ] is not allowed to run (and obviously,
1193 * pxPreviousTCB is allowed to run).
1195 * This is an optimization which reduces the number of cores needed to be
1196 * searched for pxPreviousTCB to run. */
1197 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1201 /* pxPreviousTCB's core affinity mask is changed and it is no longer
1202 * allowed to run on this core. Searching all the cores in pxPreviousTCB's
1203 * new core affinity mask to find a core on which it can run. */
1206 uxCoreMap &= ( ( 1U << configNUMBER_OF_CORES ) - 1U );
1208 for( x = ( ( BaseType_t ) configNUMBER_OF_CORES - 1 ); x >= ( BaseType_t ) 0; x-- )
1210 UBaseType_t uxCore = ( UBaseType_t ) x;
1211 BaseType_t xTaskPriority;
1213 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << uxCore ) ) != 0U )
1215 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority;
1217 if( ( pxCurrentTCBs[ uxCore ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1219 xTaskPriority = xTaskPriority - ( BaseType_t ) 1;
1222 uxCoreMap &= ~( ( UBaseType_t ) 1U << uxCore );
1224 if( ( xTaskPriority < xLowestPriority ) &&
1225 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ] ) != pdFALSE ) &&
1226 ( xYieldPendings[ uxCore ] == pdFALSE ) )
1228 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1229 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1232 xLowestPriority = xTaskPriority;
1233 xLowestPriorityCore = ( BaseType_t ) uxCore;
1239 if( xLowestPriorityCore >= 0 )
1241 prvYieldCore( xLowestPriorityCore );
1246 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) */
1249 #endif /* ( configNUMBER_OF_CORES > 1 ) */
1251 /*-----------------------------------------------------------*/
1253 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1255 static TCB_t * prvCreateStaticTask( TaskFunction_t pxTaskCode,
1256 const char * const pcName,
1257 const configSTACK_DEPTH_TYPE uxStackDepth,
1258 void * const pvParameters,
1259 UBaseType_t uxPriority,
1260 StackType_t * const puxStackBuffer,
1261 StaticTask_t * const pxTaskBuffer,
1262 TaskHandle_t * const pxCreatedTask )
1266 configASSERT( puxStackBuffer != NULL );
1267 configASSERT( pxTaskBuffer != NULL );
1269 #if ( configASSERT_DEFINED == 1 )
1271 /* Sanity check that the size of the structure used to declare a
1272 * variable of type StaticTask_t equals the size of the real task
1274 volatile size_t xSize = sizeof( StaticTask_t );
1275 configASSERT( xSize == sizeof( TCB_t ) );
1276 ( void ) xSize; /* Prevent unused variable warning when configASSERT() is not used. */
1278 #endif /* configASSERT_DEFINED */
1280 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1282 /* The memory used for the task's TCB and stack are passed into this
1283 * function - use them. */
1284 /* MISRA Ref 11.3.1 [Misaligned access] */
1285 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
1286 /* coverity[misra_c_2012_rule_11_3_violation] */
1287 pxNewTCB = ( TCB_t * ) pxTaskBuffer;
1288 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1289 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1291 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1293 /* Tasks can be created statically or dynamically, so note this
1294 * task was created statically in case the task is later deleted. */
1295 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1297 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1299 prvInitialiseNewTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1308 /*-----------------------------------------------------------*/
1310 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1311 const char * const pcName,
1312 const configSTACK_DEPTH_TYPE uxStackDepth,
1313 void * const pvParameters,
1314 UBaseType_t uxPriority,
1315 StackType_t * const puxStackBuffer,
1316 StaticTask_t * const pxTaskBuffer )
1318 TaskHandle_t xReturn = NULL;
1321 traceENTER_xTaskCreateStatic( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer );
1323 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1325 if( pxNewTCB != NULL )
1327 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1329 /* Set the task's affinity before scheduling it. */
1330 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1334 prvAddNewTaskToReadyList( pxNewTCB );
1338 mtCOVERAGE_TEST_MARKER();
1341 traceRETURN_xTaskCreateStatic( xReturn );
1345 /*-----------------------------------------------------------*/
1347 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1348 TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1349 const char * const pcName,
1350 const configSTACK_DEPTH_TYPE uxStackDepth,
1351 void * const pvParameters,
1352 UBaseType_t uxPriority,
1353 StackType_t * const puxStackBuffer,
1354 StaticTask_t * const pxTaskBuffer,
1355 UBaseType_t uxCoreAffinityMask )
1357 TaskHandle_t xReturn = NULL;
1360 traceENTER_xTaskCreateStaticAffinitySet( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, uxCoreAffinityMask );
1362 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1364 if( pxNewTCB != NULL )
1366 /* Set the task's affinity before scheduling it. */
1367 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1369 prvAddNewTaskToReadyList( pxNewTCB );
1373 mtCOVERAGE_TEST_MARKER();
1376 traceRETURN_xTaskCreateStaticAffinitySet( xReturn );
1380 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1382 #endif /* SUPPORT_STATIC_ALLOCATION */
1383 /*-----------------------------------------------------------*/
1385 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1386 static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
1387 TaskHandle_t * const pxCreatedTask )
1391 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1392 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1394 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1396 /* Allocate space for the TCB. Where the memory comes from depends
1397 * on the implementation of the port malloc function and whether or
1398 * not static allocation is being used. */
1399 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1400 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1402 /* Store the stack location in the TCB. */
1403 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1405 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1407 /* Tasks can be created statically or dynamically, so note this
1408 * task was created statically in case the task is later deleted. */
1409 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1411 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1413 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1414 pxTaskDefinition->pcName,
1415 pxTaskDefinition->usStackDepth,
1416 pxTaskDefinition->pvParameters,
1417 pxTaskDefinition->uxPriority,
1418 pxCreatedTask, pxNewTCB,
1419 pxTaskDefinition->xRegions );
1428 /*-----------------------------------------------------------*/
1430 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1431 TaskHandle_t * pxCreatedTask )
1436 traceENTER_xTaskCreateRestrictedStatic( pxTaskDefinition, pxCreatedTask );
1438 configASSERT( pxTaskDefinition != NULL );
1440 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1442 if( pxNewTCB != NULL )
1444 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1446 /* Set the task's affinity before scheduling it. */
1447 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1451 prvAddNewTaskToReadyList( pxNewTCB );
1456 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1459 traceRETURN_xTaskCreateRestrictedStatic( xReturn );
1463 /*-----------------------------------------------------------*/
1465 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1466 BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1467 UBaseType_t uxCoreAffinityMask,
1468 TaskHandle_t * pxCreatedTask )
1473 traceENTER_xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1475 configASSERT( pxTaskDefinition != NULL );
1477 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1479 if( pxNewTCB != NULL )
1481 /* Set the task's affinity before scheduling it. */
1482 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1484 prvAddNewTaskToReadyList( pxNewTCB );
1489 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1492 traceRETURN_xTaskCreateRestrictedStaticAffinitySet( xReturn );
1496 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1498 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1499 /*-----------------------------------------------------------*/
1501 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1502 static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
1503 TaskHandle_t * const pxCreatedTask )
1507 configASSERT( pxTaskDefinition->puxStackBuffer );
1509 if( pxTaskDefinition->puxStackBuffer != NULL )
1511 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1512 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1513 /* coverity[misra_c_2012_rule_11_5_violation] */
1514 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1516 if( pxNewTCB != NULL )
1518 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1520 /* Store the stack location in the TCB. */
1521 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1523 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1525 /* Tasks can be created statically or dynamically, so note
1526 * this task had a statically allocated stack in case it is
1527 * later deleted. The TCB was allocated dynamically. */
1528 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1530 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1532 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1533 pxTaskDefinition->pcName,
1534 pxTaskDefinition->usStackDepth,
1535 pxTaskDefinition->pvParameters,
1536 pxTaskDefinition->uxPriority,
1537 pxCreatedTask, pxNewTCB,
1538 pxTaskDefinition->xRegions );
1548 /*-----------------------------------------------------------*/
1550 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1551 TaskHandle_t * pxCreatedTask )
1556 traceENTER_xTaskCreateRestricted( pxTaskDefinition, pxCreatedTask );
1558 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1560 if( pxNewTCB != NULL )
1562 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1564 /* Set the task's affinity before scheduling it. */
1565 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1567 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1569 prvAddNewTaskToReadyList( pxNewTCB );
1575 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1578 traceRETURN_xTaskCreateRestricted( xReturn );
1582 /*-----------------------------------------------------------*/
1584 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1585 BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1586 UBaseType_t uxCoreAffinityMask,
1587 TaskHandle_t * pxCreatedTask )
1592 traceENTER_xTaskCreateRestrictedAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1594 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1596 if( pxNewTCB != NULL )
1598 /* Set the task's affinity before scheduling it. */
1599 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1601 prvAddNewTaskToReadyList( pxNewTCB );
1607 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1610 traceRETURN_xTaskCreateRestrictedAffinitySet( xReturn );
1614 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1617 #endif /* portUSING_MPU_WRAPPERS */
1618 /*-----------------------------------------------------------*/
1620 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1621 static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
1622 const char * const pcName,
1623 const configSTACK_DEPTH_TYPE uxStackDepth,
1624 void * const pvParameters,
1625 UBaseType_t uxPriority,
1626 TaskHandle_t * const pxCreatedTask )
1630 /* If the stack grows down then allocate the stack then the TCB so the stack
1631 * does not grow into the TCB. Likewise if the stack grows up then allocate
1632 * the TCB then the stack. */
1633 #if ( portSTACK_GROWTH > 0 )
1635 /* Allocate space for the TCB. Where the memory comes from depends on
1636 * the implementation of the port malloc function and whether or not static
1637 * allocation is being used. */
1638 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1639 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1640 /* coverity[misra_c_2012_rule_11_5_violation] */
1641 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1643 if( pxNewTCB != NULL )
1645 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1647 /* Allocate space for the stack used by the task being created.
1648 * The base of the stack memory stored in the TCB so the task can
1649 * be deleted later if required. */
1650 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1651 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1652 /* coverity[misra_c_2012_rule_11_5_violation] */
1653 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) uxStackDepth ) * sizeof( StackType_t ) ) );
1655 if( pxNewTCB->pxStack == NULL )
1657 /* Could not allocate the stack. Delete the allocated TCB. */
1658 vPortFree( pxNewTCB );
1663 #else /* portSTACK_GROWTH */
1665 StackType_t * pxStack;
1667 /* Allocate space for the stack used by the task being created. */
1668 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1669 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1670 /* coverity[misra_c_2012_rule_11_5_violation] */
1671 pxStack = pvPortMallocStack( ( ( ( size_t ) uxStackDepth ) * sizeof( StackType_t ) ) );
1673 if( pxStack != NULL )
1675 /* Allocate space for the TCB. */
1676 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1677 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1678 /* coverity[misra_c_2012_rule_11_5_violation] */
1679 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1681 if( pxNewTCB != NULL )
1683 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1685 /* Store the stack location in the TCB. */
1686 pxNewTCB->pxStack = pxStack;
1690 /* The stack cannot be used as the TCB was not created. Free
1692 vPortFreeStack( pxStack );
1700 #endif /* portSTACK_GROWTH */
1702 if( pxNewTCB != NULL )
1704 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1706 /* Tasks can be created statically or dynamically, so note this
1707 * task was created dynamically in case it is later deleted. */
1708 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1710 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1712 prvInitialiseNewTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1717 /*-----------------------------------------------------------*/
1719 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1720 const char * const pcName,
1721 const configSTACK_DEPTH_TYPE uxStackDepth,
1722 void * const pvParameters,
1723 UBaseType_t uxPriority,
1724 TaskHandle_t * const pxCreatedTask )
1729 traceENTER_xTaskCreate( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
1731 pxNewTCB = prvCreateTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
1733 if( pxNewTCB != NULL )
1735 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1737 /* Set the task's affinity before scheduling it. */
1738 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1742 prvAddNewTaskToReadyList( pxNewTCB );
1747 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1750 traceRETURN_xTaskCreate( xReturn );
1754 /*-----------------------------------------------------------*/
1756 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1757 BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1758 const char * const pcName,
1759 const configSTACK_DEPTH_TYPE uxStackDepth,
1760 void * const pvParameters,
1761 UBaseType_t uxPriority,
1762 UBaseType_t uxCoreAffinityMask,
1763 TaskHandle_t * const pxCreatedTask )
1768 traceENTER_xTaskCreateAffinitySet( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, uxCoreAffinityMask, pxCreatedTask );
1770 pxNewTCB = prvCreateTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
1772 if( pxNewTCB != NULL )
1774 /* Set the task's affinity before scheduling it. */
1775 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1777 prvAddNewTaskToReadyList( pxNewTCB );
1782 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1785 traceRETURN_xTaskCreateAffinitySet( xReturn );
1789 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1791 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1792 /*-----------------------------------------------------------*/
1794 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1795 const char * const pcName,
1796 const configSTACK_DEPTH_TYPE uxStackDepth,
1797 void * const pvParameters,
1798 UBaseType_t uxPriority,
1799 TaskHandle_t * const pxCreatedTask,
1801 const MemoryRegion_t * const xRegions )
1803 StackType_t * pxTopOfStack;
1806 #if ( portUSING_MPU_WRAPPERS == 1 )
1807 /* Should the task be created in privileged mode? */
1808 BaseType_t xRunPrivileged;
1810 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1812 xRunPrivileged = pdTRUE;
1816 xRunPrivileged = pdFALSE;
1818 uxPriority &= ~portPRIVILEGE_BIT;
1819 #endif /* portUSING_MPU_WRAPPERS == 1 */
1821 /* Avoid dependency on memset() if it is not required. */
1822 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1824 /* Fill the stack with a known value to assist debugging. */
1825 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) uxStackDepth * sizeof( StackType_t ) );
1827 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1829 /* Calculate the top of stack address. This depends on whether the stack
1830 * grows from high memory to low (as per the 80x86) or vice versa.
1831 * portSTACK_GROWTH is used to make the result positive or negative as required
1833 #if ( portSTACK_GROWTH < 0 )
1835 pxTopOfStack = &( pxNewTCB->pxStack[ uxStackDepth - ( configSTACK_DEPTH_TYPE ) 1 ] );
1836 pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
1838 /* Check the alignment of the calculated top of stack is correct. */
1839 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0U ) );
1841 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1843 /* Also record the stack's high address, which may assist
1845 pxNewTCB->pxEndOfStack = pxTopOfStack;
1847 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1849 #else /* portSTACK_GROWTH */
1851 pxTopOfStack = pxNewTCB->pxStack;
1852 pxTopOfStack = ( StackType_t * ) ( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) + portBYTE_ALIGNMENT_MASK ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
1854 /* Check the alignment of the calculated top of stack is correct. */
1855 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0U ) );
1857 /* The other extreme of the stack space is required if stack checking is
1859 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( uxStackDepth - ( configSTACK_DEPTH_TYPE ) 1 );
1861 #endif /* portSTACK_GROWTH */
1863 /* Store the task name in the TCB. */
1864 if( pcName != NULL )
1866 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1868 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1870 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1871 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1872 * string is not accessible (extremely unlikely). */
1873 if( pcName[ x ] == ( char ) 0x00 )
1879 mtCOVERAGE_TEST_MARKER();
1883 /* Ensure the name string is terminated in the case that the string length
1884 * was greater or equal to configMAX_TASK_NAME_LEN. */
1885 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1U ] = '\0';
1889 mtCOVERAGE_TEST_MARKER();
1892 /* This is used as an array index so must ensure it's not too large. */
1893 configASSERT( uxPriority < configMAX_PRIORITIES );
1895 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1897 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1901 mtCOVERAGE_TEST_MARKER();
1904 pxNewTCB->uxPriority = uxPriority;
1905 #if ( configUSE_MUTEXES == 1 )
1907 pxNewTCB->uxBasePriority = uxPriority;
1909 #endif /* configUSE_MUTEXES */
1911 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1912 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1914 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1915 * back to the containing TCB from a generic item in a list. */
1916 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1918 /* Event lists are always in priority order. */
1919 listSET_LIST_ITEM_VALUE( &( pxNewTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority );
1920 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1922 #if ( portUSING_MPU_WRAPPERS == 1 )
1924 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, uxStackDepth );
1928 /* Avoid compiler warning about unreferenced parameter. */
1933 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
1935 /* Allocate and initialize memory for the task's TLS Block. */
1936 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
1940 /* Initialize the TCB stack to look as if the task was already running,
1941 * but had been interrupted by the scheduler. The return address is set
1942 * to the start of the task function. Once the stack has been initialised
1943 * the top of stack variable is updated. */
1944 #if ( portUSING_MPU_WRAPPERS == 1 )
1946 /* If the port has capability to detect stack overflow,
1947 * pass the stack end address to the stack initialization
1948 * function as well. */
1949 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1951 #if ( portSTACK_GROWTH < 0 )
1953 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1955 #else /* portSTACK_GROWTH */
1957 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1959 #endif /* portSTACK_GROWTH */
1961 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1963 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1965 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1967 #else /* portUSING_MPU_WRAPPERS */
1969 /* If the port has capability to detect stack overflow,
1970 * pass the stack end address to the stack initialization
1971 * function as well. */
1972 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1974 #if ( portSTACK_GROWTH < 0 )
1976 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1978 #else /* portSTACK_GROWTH */
1980 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1982 #endif /* portSTACK_GROWTH */
1984 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1986 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1988 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1990 #endif /* portUSING_MPU_WRAPPERS */
1992 /* Initialize task state and task attributes. */
1993 #if ( configNUMBER_OF_CORES > 1 )
1995 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1997 /* Is this an idle task? */
1998 if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvPassiveIdleTask ) )
2000 pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
2003 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
2005 if( pxCreatedTask != NULL )
2007 /* Pass the handle out in an anonymous way. The handle can be used to
2008 * change the created task's priority, delete the created task, etc.*/
2009 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
2013 mtCOVERAGE_TEST_MARKER();
2016 /*-----------------------------------------------------------*/
2018 #if ( configNUMBER_OF_CORES == 1 )
2020 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
2022 /* Ensure interrupts don't access the task lists while the lists are being
2024 taskENTER_CRITICAL();
2026 uxCurrentNumberOfTasks = ( UBaseType_t ) ( uxCurrentNumberOfTasks + 1U );
2028 if( pxCurrentTCB == NULL )
2030 /* There are no other tasks, or all the other tasks are in
2031 * the suspended state - make this the current task. */
2032 pxCurrentTCB = pxNewTCB;
2034 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2036 /* This is the first task to be created so do the preliminary
2037 * initialisation required. We will not recover if this call
2038 * fails, but we will report the failure. */
2039 prvInitialiseTaskLists();
2043 mtCOVERAGE_TEST_MARKER();
2048 /* If the scheduler is not already running, make this task the
2049 * current task if it is the highest priority task to be created
2051 if( xSchedulerRunning == pdFALSE )
2053 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
2055 pxCurrentTCB = pxNewTCB;
2059 mtCOVERAGE_TEST_MARKER();
2064 mtCOVERAGE_TEST_MARKER();
2070 #if ( configUSE_TRACE_FACILITY == 1 )
2072 /* Add a counter into the TCB for tracing only. */
2073 pxNewTCB->uxTCBNumber = uxTaskNumber;
2075 #endif /* configUSE_TRACE_FACILITY */
2076 traceTASK_CREATE( pxNewTCB );
2078 prvAddTaskToReadyList( pxNewTCB );
2080 portSETUP_TCB( pxNewTCB );
2082 taskEXIT_CRITICAL();
2084 if( xSchedulerRunning != pdFALSE )
2086 /* If the created task is of a higher priority than the current task
2087 * then it should run now. */
2088 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2092 mtCOVERAGE_TEST_MARKER();
2096 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2098 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
2100 /* Ensure interrupts don't access the task lists while the lists are being
2102 taskENTER_CRITICAL();
2104 uxCurrentNumberOfTasks++;
2106 if( xSchedulerRunning == pdFALSE )
2108 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2110 /* This is the first task to be created so do the preliminary
2111 * initialisation required. We will not recover if this call
2112 * fails, but we will report the failure. */
2113 prvInitialiseTaskLists();
2117 mtCOVERAGE_TEST_MARKER();
2120 /* All the cores start with idle tasks before the SMP scheduler
2121 * is running. Idle tasks are assigned to cores when they are
2122 * created in prvCreateIdleTasks(). */
2127 #if ( configUSE_TRACE_FACILITY == 1 )
2129 /* Add a counter into the TCB for tracing only. */
2130 pxNewTCB->uxTCBNumber = uxTaskNumber;
2132 #endif /* configUSE_TRACE_FACILITY */
2133 traceTASK_CREATE( pxNewTCB );
2135 prvAddTaskToReadyList( pxNewTCB );
2137 portSETUP_TCB( pxNewTCB );
2139 if( xSchedulerRunning != pdFALSE )
2141 /* If the created task is of a higher priority than another
2142 * currently running task and preemption is on then it should
2144 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2148 mtCOVERAGE_TEST_MARKER();
2151 taskEXIT_CRITICAL();
2154 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2155 /*-----------------------------------------------------------*/
2157 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
2159 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
2162 size_t uxCharsWritten;
2164 if( iSnprintfReturnValue < 0 )
2166 /* Encoding error - Return 0 to indicate that nothing
2167 * was written to the buffer. */
2170 else if( iSnprintfReturnValue >= ( int ) n )
2172 /* This is the case when the supplied buffer is not
2173 * large to hold the generated string. Return the
2174 * number of characters actually written without
2175 * counting the terminating NULL character. */
2176 uxCharsWritten = n - 1U;
2180 /* Complete string was written to the buffer. */
2181 uxCharsWritten = ( size_t ) iSnprintfReturnValue;
2184 return uxCharsWritten;
2187 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
2188 /*-----------------------------------------------------------*/
2190 #if ( INCLUDE_vTaskDelete == 1 )
2192 void vTaskDelete( TaskHandle_t xTaskToDelete )
2195 BaseType_t xDeleteTCBInIdleTask = pdFALSE;
2196 BaseType_t xTaskIsRunningOrYielding;
2198 traceENTER_vTaskDelete( xTaskToDelete );
2200 taskENTER_CRITICAL();
2202 /* If null is passed in here then it is the calling task that is
2204 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
2206 /* Remove task from the ready/delayed list. */
2207 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2209 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2213 mtCOVERAGE_TEST_MARKER();
2216 /* Is the task waiting on an event also? */
2217 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2219 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2223 mtCOVERAGE_TEST_MARKER();
2226 /* Increment the uxTaskNumber also so kernel aware debuggers can
2227 * detect that the task lists need re-generating. This is done before
2228 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
2232 /* Use temp variable as distinct sequence points for reading volatile
2233 * variables prior to a logical operator to ensure compliance with
2234 * MISRA C 2012 Rule 13.5. */
2235 xTaskIsRunningOrYielding = taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB );
2237 /* If the task is running (or yielding), we must add it to the
2238 * termination list so that an idle task can delete it when it is
2239 * no longer running. */
2240 if( ( xSchedulerRunning != pdFALSE ) && ( xTaskIsRunningOrYielding != pdFALSE ) )
2242 /* A running task or a task which is scheduled to yield is being
2243 * deleted. This cannot complete when the task is still running
2244 * on a core, as a context switch to another task is required.
2245 * Place the task in the termination list. The idle task will check
2246 * the termination list and free up any memory allocated by the
2247 * scheduler for the TCB and stack of the deleted task. */
2248 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
2250 /* Increment the ucTasksDeleted variable so the idle task knows
2251 * there is a task that has been deleted and that it should therefore
2252 * check the xTasksWaitingTermination list. */
2253 ++uxDeletedTasksWaitingCleanUp;
2255 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
2256 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
2257 traceTASK_DELETE( pxTCB );
2259 /* Delete the task TCB in idle task. */
2260 xDeleteTCBInIdleTask = pdTRUE;
2262 /* The pre-delete hook is primarily for the Windows simulator,
2263 * in which Windows specific clean up operations are performed,
2264 * after which it is not possible to yield away from this task -
2265 * hence xYieldPending is used to latch that a context switch is
2267 #if ( configNUMBER_OF_CORES == 1 )
2268 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
2270 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
2273 /* In the case of SMP, it is possible that the task being deleted
2274 * is running on another core. We must evict the task before
2275 * exiting the critical section to ensure that the task cannot
2276 * take an action which puts it back on ready/state/event list,
2277 * thereby nullifying the delete operation. Once evicted, the
2278 * task won't be scheduled ever as it will no longer be on the
2280 #if ( configNUMBER_OF_CORES > 1 )
2282 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2284 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2286 configASSERT( uxSchedulerSuspended == 0 );
2287 taskYIELD_WITHIN_API();
2291 prvYieldCore( pxTCB->xTaskRunState );
2295 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
2299 --uxCurrentNumberOfTasks;
2300 traceTASK_DELETE( pxTCB );
2302 /* Reset the next expected unblock time in case it referred to
2303 * the task that has just been deleted. */
2304 prvResetNextTaskUnblockTime();
2307 taskEXIT_CRITICAL();
2309 /* If the task is not deleting itself, call prvDeleteTCB from outside of
2310 * critical section. If a task deletes itself, prvDeleteTCB is called
2311 * from prvCheckTasksWaitingTermination which is called from Idle task. */
2312 if( xDeleteTCBInIdleTask != pdTRUE )
2314 prvDeleteTCB( pxTCB );
2317 /* Force a reschedule if it is the currently running task that has just
2319 #if ( configNUMBER_OF_CORES == 1 )
2321 if( xSchedulerRunning != pdFALSE )
2323 if( pxTCB == pxCurrentTCB )
2325 configASSERT( uxSchedulerSuspended == 0 );
2326 taskYIELD_WITHIN_API();
2330 mtCOVERAGE_TEST_MARKER();
2334 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2336 traceRETURN_vTaskDelete();
2339 #endif /* INCLUDE_vTaskDelete */
2340 /*-----------------------------------------------------------*/
2342 #if ( INCLUDE_xTaskDelayUntil == 1 )
2344 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2345 const TickType_t xTimeIncrement )
2347 TickType_t xTimeToWake;
2348 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2350 traceENTER_xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
2352 configASSERT( pxPreviousWakeTime );
2353 configASSERT( ( xTimeIncrement > 0U ) );
2357 /* Minor optimisation. The tick count cannot change in this
2359 const TickType_t xConstTickCount = xTickCount;
2361 configASSERT( uxSchedulerSuspended == 1U );
2363 /* Generate the tick time at which the task wants to wake. */
2364 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2366 if( xConstTickCount < *pxPreviousWakeTime )
2368 /* The tick count has overflowed since this function was
2369 * lasted called. In this case the only time we should ever
2370 * actually delay is if the wake time has also overflowed,
2371 * and the wake time is greater than the tick time. When this
2372 * is the case it is as if neither time had overflowed. */
2373 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2375 xShouldDelay = pdTRUE;
2379 mtCOVERAGE_TEST_MARKER();
2384 /* The tick time has not overflowed. In this case we will
2385 * delay if either the wake time has overflowed, and/or the
2386 * tick time is less than the wake time. */
2387 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2389 xShouldDelay = pdTRUE;
2393 mtCOVERAGE_TEST_MARKER();
2397 /* Update the wake time ready for the next call. */
2398 *pxPreviousWakeTime = xTimeToWake;
2400 if( xShouldDelay != pdFALSE )
2402 traceTASK_DELAY_UNTIL( xTimeToWake );
2404 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2405 * the time to wake, so subtract the current tick count. */
2406 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2410 mtCOVERAGE_TEST_MARKER();
2413 xAlreadyYielded = xTaskResumeAll();
2415 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2416 * have put ourselves to sleep. */
2417 if( xAlreadyYielded == pdFALSE )
2419 taskYIELD_WITHIN_API();
2423 mtCOVERAGE_TEST_MARKER();
2426 traceRETURN_xTaskDelayUntil( xShouldDelay );
2428 return xShouldDelay;
2431 #endif /* INCLUDE_xTaskDelayUntil */
2432 /*-----------------------------------------------------------*/
2434 #if ( INCLUDE_vTaskDelay == 1 )
2436 void vTaskDelay( const TickType_t xTicksToDelay )
2438 BaseType_t xAlreadyYielded = pdFALSE;
2440 traceENTER_vTaskDelay( xTicksToDelay );
2442 /* A delay time of zero just forces a reschedule. */
2443 if( xTicksToDelay > ( TickType_t ) 0U )
2447 configASSERT( uxSchedulerSuspended == 1U );
2451 /* A task that is removed from the event list while the
2452 * scheduler is suspended will not get placed in the ready
2453 * list or removed from the blocked list until the scheduler
2456 * This task cannot be in an event list as it is the currently
2457 * executing task. */
2458 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2460 xAlreadyYielded = xTaskResumeAll();
2464 mtCOVERAGE_TEST_MARKER();
2467 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2468 * have put ourselves to sleep. */
2469 if( xAlreadyYielded == pdFALSE )
2471 taskYIELD_WITHIN_API();
2475 mtCOVERAGE_TEST_MARKER();
2478 traceRETURN_vTaskDelay();
2481 #endif /* INCLUDE_vTaskDelay */
2482 /*-----------------------------------------------------------*/
2484 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2486 eTaskState eTaskGetState( TaskHandle_t xTask )
2489 List_t const * pxStateList;
2490 List_t const * pxEventList;
2491 List_t const * pxDelayedList;
2492 List_t const * pxOverflowedDelayedList;
2493 const TCB_t * const pxTCB = xTask;
2495 traceENTER_eTaskGetState( xTask );
2497 configASSERT( pxTCB );
2499 #if ( configNUMBER_OF_CORES == 1 )
2500 if( pxTCB == pxCurrentTCB )
2502 /* The task calling this function is querying its own state. */
2508 taskENTER_CRITICAL();
2510 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2511 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2512 pxDelayedList = pxDelayedTaskList;
2513 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2515 taskEXIT_CRITICAL();
2517 if( pxEventList == &xPendingReadyList )
2519 /* The task has been placed on the pending ready list, so its
2520 * state is eReady regardless of what list the task's state list
2521 * item is currently placed on. */
2524 else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2526 /* The task being queried is referenced from one of the Blocked
2531 #if ( INCLUDE_vTaskSuspend == 1 )
2532 else if( pxStateList == &xSuspendedTaskList )
2534 /* The task being queried is referenced from the suspended
2535 * list. Is it genuinely suspended or is it blocked
2537 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2539 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2543 /* The task does not appear on the event list item of
2544 * and of the RTOS objects, but could still be in the
2545 * blocked state if it is waiting on its notification
2546 * rather than waiting on an object. If not, is
2548 eReturn = eSuspended;
2550 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2552 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2559 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2561 eReturn = eSuspended;
2563 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2570 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2572 #if ( INCLUDE_vTaskDelete == 1 )
2573 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2575 /* The task being queried is referenced from the deleted
2576 * tasks list, or it is not referenced from any lists at
2584 #if ( configNUMBER_OF_CORES == 1 )
2586 /* If the task is not in any other state, it must be in the
2587 * Ready (including pending ready) state. */
2590 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2592 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2594 /* Is it actively running on a core? */
2599 /* If the task is not in any other state, it must be in the
2600 * Ready (including pending ready) state. */
2604 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2608 traceRETURN_eTaskGetState( eReturn );
2613 #endif /* INCLUDE_eTaskGetState */
2614 /*-----------------------------------------------------------*/
2616 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2618 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2620 TCB_t const * pxTCB;
2621 UBaseType_t uxReturn;
2623 traceENTER_uxTaskPriorityGet( xTask );
2625 taskENTER_CRITICAL();
2627 /* If null is passed in here then it is the priority of the task
2628 * that called uxTaskPriorityGet() that is being queried. */
2629 pxTCB = prvGetTCBFromHandle( xTask );
2630 uxReturn = pxTCB->uxPriority;
2632 taskEXIT_CRITICAL();
2634 traceRETURN_uxTaskPriorityGet( uxReturn );
2639 #endif /* INCLUDE_uxTaskPriorityGet */
2640 /*-----------------------------------------------------------*/
2642 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2644 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2646 TCB_t const * pxTCB;
2647 UBaseType_t uxReturn;
2648 UBaseType_t uxSavedInterruptStatus;
2650 traceENTER_uxTaskPriorityGetFromISR( xTask );
2652 /* RTOS ports that support interrupt nesting have the concept of a
2653 * maximum system call (or maximum API call) interrupt priority.
2654 * Interrupts that are above the maximum system call priority are keep
2655 * permanently enabled, even when the RTOS kernel is in a critical section,
2656 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2657 * is defined in FreeRTOSConfig.h then
2658 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2659 * failure if a FreeRTOS API function is called from an interrupt that has
2660 * been assigned a priority above the configured maximum system call
2661 * priority. Only FreeRTOS functions that end in FromISR can be called
2662 * from interrupts that have been assigned a priority at or (logically)
2663 * below the maximum system call interrupt priority. FreeRTOS maintains a
2664 * separate interrupt safe API to ensure interrupt entry is as fast and as
2665 * simple as possible. More information (albeit Cortex-M specific) is
2666 * provided on the following link:
2667 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2668 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2670 /* MISRA Ref 4.7.1 [Return value shall be checked] */
2671 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
2672 /* coverity[misra_c_2012_directive_4_7_violation] */
2673 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
2675 /* If null is passed in here then it is the priority of the calling
2676 * task that is being queried. */
2677 pxTCB = prvGetTCBFromHandle( xTask );
2678 uxReturn = pxTCB->uxPriority;
2680 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2682 traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
2687 #endif /* INCLUDE_uxTaskPriorityGet */
2688 /*-----------------------------------------------------------*/
2690 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2692 UBaseType_t uxTaskBasePriorityGet( const TaskHandle_t xTask )
2694 TCB_t const * pxTCB;
2695 UBaseType_t uxReturn;
2697 traceENTER_uxTaskBasePriorityGet( xTask );
2699 taskENTER_CRITICAL();
2701 /* If null is passed in here then it is the base priority of the task
2702 * that called uxTaskBasePriorityGet() that is being queried. */
2703 pxTCB = prvGetTCBFromHandle( xTask );
2704 uxReturn = pxTCB->uxBasePriority;
2706 taskEXIT_CRITICAL();
2708 traceRETURN_uxTaskBasePriorityGet( uxReturn );
2713 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2714 /*-----------------------------------------------------------*/
2716 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2718 UBaseType_t uxTaskBasePriorityGetFromISR( const TaskHandle_t xTask )
2720 TCB_t const * pxTCB;
2721 UBaseType_t uxReturn;
2722 UBaseType_t uxSavedInterruptStatus;
2724 traceENTER_uxTaskBasePriorityGetFromISR( xTask );
2726 /* RTOS ports that support interrupt nesting have the concept of a
2727 * maximum system call (or maximum API call) interrupt priority.
2728 * Interrupts that are above the maximum system call priority are keep
2729 * permanently enabled, even when the RTOS kernel is in a critical section,
2730 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2731 * is defined in FreeRTOSConfig.h then
2732 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2733 * failure if a FreeRTOS API function is called from an interrupt that has
2734 * been assigned a priority above the configured maximum system call
2735 * priority. Only FreeRTOS functions that end in FromISR can be called
2736 * from interrupts that have been assigned a priority at or (logically)
2737 * below the maximum system call interrupt priority. FreeRTOS maintains a
2738 * separate interrupt safe API to ensure interrupt entry is as fast and as
2739 * simple as possible. More information (albeit Cortex-M specific) is
2740 * provided on the following link:
2741 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2742 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2744 /* MISRA Ref 4.7.1 [Return value shall be checked] */
2745 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
2746 /* coverity[misra_c_2012_directive_4_7_violation] */
2747 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
2749 /* If null is passed in here then it is the base priority of the calling
2750 * task that is being queried. */
2751 pxTCB = prvGetTCBFromHandle( xTask );
2752 uxReturn = pxTCB->uxBasePriority;
2754 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2756 traceRETURN_uxTaskBasePriorityGetFromISR( uxReturn );
2761 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2762 /*-----------------------------------------------------------*/
2764 #if ( INCLUDE_vTaskPrioritySet == 1 )
2766 void vTaskPrioritySet( TaskHandle_t xTask,
2767 UBaseType_t uxNewPriority )
2770 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2771 BaseType_t xYieldRequired = pdFALSE;
2773 #if ( configNUMBER_OF_CORES > 1 )
2774 BaseType_t xYieldForTask = pdFALSE;
2777 traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
2779 configASSERT( uxNewPriority < configMAX_PRIORITIES );
2781 /* Ensure the new priority is valid. */
2782 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2784 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2788 mtCOVERAGE_TEST_MARKER();
2791 taskENTER_CRITICAL();
2793 /* If null is passed in here then it is the priority of the calling
2794 * task that is being changed. */
2795 pxTCB = prvGetTCBFromHandle( xTask );
2797 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2799 #if ( configUSE_MUTEXES == 1 )
2801 uxCurrentBasePriority = pxTCB->uxBasePriority;
2805 uxCurrentBasePriority = pxTCB->uxPriority;
2809 if( uxCurrentBasePriority != uxNewPriority )
2811 /* The priority change may have readied a task of higher
2812 * priority than a running task. */
2813 if( uxNewPriority > uxCurrentBasePriority )
2815 #if ( configNUMBER_OF_CORES == 1 )
2817 if( pxTCB != pxCurrentTCB )
2819 /* The priority of a task other than the currently
2820 * running task is being raised. Is the priority being
2821 * raised above that of the running task? */
2822 if( uxNewPriority > pxCurrentTCB->uxPriority )
2824 xYieldRequired = pdTRUE;
2828 mtCOVERAGE_TEST_MARKER();
2833 /* The priority of the running task is being raised,
2834 * but the running task must already be the highest
2835 * priority task able to run so no yield is required. */
2838 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2840 /* The priority of a task is being raised so
2841 * perform a yield for this task later. */
2842 xYieldForTask = pdTRUE;
2844 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2846 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2848 /* Setting the priority of a running task down means
2849 * there may now be another task of higher priority that
2850 * is ready to execute. */
2851 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2852 if( pxTCB->xPreemptionDisable == pdFALSE )
2855 xYieldRequired = pdTRUE;
2860 /* Setting the priority of any other task down does not
2861 * require a yield as the running task must be above the
2862 * new priority of the task being modified. */
2865 /* Remember the ready list the task might be referenced from
2866 * before its uxPriority member is changed so the
2867 * taskRESET_READY_PRIORITY() macro can function correctly. */
2868 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2870 #if ( configUSE_MUTEXES == 1 )
2872 /* Only change the priority being used if the task is not
2873 * currently using an inherited priority or the new priority
2874 * is bigger than the inherited priority. */
2875 if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2877 pxTCB->uxPriority = uxNewPriority;
2881 mtCOVERAGE_TEST_MARKER();
2884 /* The base priority gets set whatever. */
2885 pxTCB->uxBasePriority = uxNewPriority;
2887 #else /* if ( configUSE_MUTEXES == 1 ) */
2889 pxTCB->uxPriority = uxNewPriority;
2891 #endif /* if ( configUSE_MUTEXES == 1 ) */
2893 /* Only reset the event list item value if the value is not
2894 * being used for anything else. */
2895 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
2897 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) );
2901 mtCOVERAGE_TEST_MARKER();
2904 /* If the task is in the blocked or suspended list we need do
2905 * nothing more than change its priority variable. However, if
2906 * the task is in a ready list it needs to be removed and placed
2907 * in the list appropriate to its new priority. */
2908 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2910 /* The task is currently in its ready list - remove before
2911 * adding it to its new ready list. As we are in a critical
2912 * section we can do this even if the scheduler is suspended. */
2913 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2915 /* It is known that the task is in its ready list so
2916 * there is no need to check again and the port level
2917 * reset macro can be called directly. */
2918 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2922 mtCOVERAGE_TEST_MARKER();
2925 prvAddTaskToReadyList( pxTCB );
2929 #if ( configNUMBER_OF_CORES == 1 )
2931 mtCOVERAGE_TEST_MARKER();
2935 /* It's possible that xYieldForTask was already set to pdTRUE because
2936 * its priority is being raised. However, since it is not in a ready list
2937 * we don't actually need to yield for it. */
2938 xYieldForTask = pdFALSE;
2943 if( xYieldRequired != pdFALSE )
2945 /* The running task priority is set down. Request the task to yield. */
2946 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2950 #if ( configNUMBER_OF_CORES > 1 )
2951 if( xYieldForTask != pdFALSE )
2953 /* The priority of the task is being raised. If a running
2954 * task has priority lower than this task, it should yield
2956 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2959 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2961 mtCOVERAGE_TEST_MARKER();
2965 /* Remove compiler warning about unused variables when the port
2966 * optimised task selection is not being used. */
2967 ( void ) uxPriorityUsedOnEntry;
2970 taskEXIT_CRITICAL();
2972 traceRETURN_vTaskPrioritySet();
2975 #endif /* INCLUDE_vTaskPrioritySet */
2976 /*-----------------------------------------------------------*/
2978 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2979 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2980 UBaseType_t uxCoreAffinityMask )
2984 UBaseType_t uxPrevCoreAffinityMask;
2986 #if ( configUSE_PREEMPTION == 1 )
2987 UBaseType_t uxPrevNotAllowedCores;
2990 traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
2992 taskENTER_CRITICAL();
2994 pxTCB = prvGetTCBFromHandle( xTask );
2996 uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2997 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2999 if( xSchedulerRunning != pdFALSE )
3001 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3003 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
3005 /* If the task can no longer run on the core it was running,
3006 * request the core to yield. */
3007 if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
3009 prvYieldCore( xCoreID );
3014 #if ( configUSE_PREEMPTION == 1 )
3016 /* Calculate the cores on which this task was not allowed to
3017 * run previously. */
3018 uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
3020 /* Does the new core mask enables this task to run on any of the
3021 * previously not allowed cores? If yes, check if this task can be
3022 * scheduled on any of those cores. */
3023 if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
3025 prvYieldForTask( pxTCB );
3028 #else /* #if( configUSE_PREEMPTION == 1 ) */
3030 mtCOVERAGE_TEST_MARKER();
3032 #endif /* #if( configUSE_PREEMPTION == 1 ) */
3036 taskEXIT_CRITICAL();
3038 traceRETURN_vTaskCoreAffinitySet();
3040 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3041 /*-----------------------------------------------------------*/
3043 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
3044 UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
3046 const TCB_t * pxTCB;
3047 UBaseType_t uxCoreAffinityMask;
3049 traceENTER_vTaskCoreAffinityGet( xTask );
3051 taskENTER_CRITICAL();
3053 pxTCB = prvGetTCBFromHandle( xTask );
3054 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
3056 taskEXIT_CRITICAL();
3058 traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
3060 return uxCoreAffinityMask;
3062 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3064 /*-----------------------------------------------------------*/
3066 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3068 void vTaskPreemptionDisable( const TaskHandle_t xTask )
3072 traceENTER_vTaskPreemptionDisable( xTask );
3074 taskENTER_CRITICAL();
3076 pxTCB = prvGetTCBFromHandle( xTask );
3078 pxTCB->xPreemptionDisable = pdTRUE;
3080 taskEXIT_CRITICAL();
3082 traceRETURN_vTaskPreemptionDisable();
3085 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3086 /*-----------------------------------------------------------*/
3088 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3090 void vTaskPreemptionEnable( const TaskHandle_t xTask )
3095 traceENTER_vTaskPreemptionEnable( xTask );
3097 taskENTER_CRITICAL();
3099 pxTCB = prvGetTCBFromHandle( xTask );
3101 pxTCB->xPreemptionDisable = pdFALSE;
3103 if( xSchedulerRunning != pdFALSE )
3105 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3107 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
3108 prvYieldCore( xCoreID );
3112 taskEXIT_CRITICAL();
3114 traceRETURN_vTaskPreemptionEnable();
3117 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3118 /*-----------------------------------------------------------*/
3120 #if ( INCLUDE_vTaskSuspend == 1 )
3122 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
3126 traceENTER_vTaskSuspend( xTaskToSuspend );
3128 taskENTER_CRITICAL();
3130 /* If null is passed in here then it is the running task that is
3131 * being suspended. */
3132 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
3134 traceTASK_SUSPEND( pxTCB );
3136 /* Remove task from the ready/delayed list and place in the
3137 * suspended list. */
3138 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
3140 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
3144 mtCOVERAGE_TEST_MARKER();
3147 /* Is the task waiting on an event also? */
3148 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3150 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3154 mtCOVERAGE_TEST_MARKER();
3157 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
3159 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3163 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3165 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3167 /* The task was blocked to wait for a notification, but is
3168 * now suspended, so no notification was received. */
3169 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
3173 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3175 /* In the case of SMP, it is possible that the task being suspended
3176 * is running on another core. We must evict the task before
3177 * exiting the critical section to ensure that the task cannot
3178 * take an action which puts it back on ready/state/event list,
3179 * thereby nullifying the suspend operation. Once evicted, the
3180 * task won't be scheduled before it is resumed as it will no longer
3181 * be on the ready list. */
3182 #if ( configNUMBER_OF_CORES > 1 )
3184 if( xSchedulerRunning != pdFALSE )
3186 /* Reset the next expected unblock time in case it referred to the
3187 * task that is now in the Suspended state. */
3188 prvResetNextTaskUnblockTime();
3190 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3192 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
3194 /* The current task has just been suspended. */
3195 configASSERT( uxSchedulerSuspended == 0 );
3196 vTaskYieldWithinAPI();
3200 prvYieldCore( pxTCB->xTaskRunState );
3205 mtCOVERAGE_TEST_MARKER();
3210 mtCOVERAGE_TEST_MARKER();
3213 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
3215 taskEXIT_CRITICAL();
3217 #if ( configNUMBER_OF_CORES == 1 )
3219 UBaseType_t uxCurrentListLength;
3221 if( xSchedulerRunning != pdFALSE )
3223 /* Reset the next expected unblock time in case it referred to the
3224 * task that is now in the Suspended state. */
3225 taskENTER_CRITICAL();
3227 prvResetNextTaskUnblockTime();
3229 taskEXIT_CRITICAL();
3233 mtCOVERAGE_TEST_MARKER();
3236 if( pxTCB == pxCurrentTCB )
3238 if( xSchedulerRunning != pdFALSE )
3240 /* The current task has just been suspended. */
3241 configASSERT( uxSchedulerSuspended == 0 );
3242 portYIELD_WITHIN_API();
3246 /* The scheduler is not running, but the task that was pointed
3247 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
3248 * must be adjusted to point to a different task. */
3250 /* Use a temp variable as a distinct sequence point for reading
3251 * volatile variables prior to a comparison to ensure compliance
3252 * with MISRA C 2012 Rule 13.2. */
3253 uxCurrentListLength = listCURRENT_LIST_LENGTH( &xSuspendedTaskList );
3255 if( uxCurrentListLength == uxCurrentNumberOfTasks )
3257 /* No other tasks are ready, so set pxCurrentTCB back to
3258 * NULL so when the next task is created pxCurrentTCB will
3259 * be set to point to it no matter what its relative priority
3261 pxCurrentTCB = NULL;
3265 vTaskSwitchContext();
3271 mtCOVERAGE_TEST_MARKER();
3274 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3276 traceRETURN_vTaskSuspend();
3279 #endif /* INCLUDE_vTaskSuspend */
3280 /*-----------------------------------------------------------*/
3282 #if ( INCLUDE_vTaskSuspend == 1 )
3284 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
3286 BaseType_t xReturn = pdFALSE;
3287 const TCB_t * const pxTCB = xTask;
3289 /* Accesses xPendingReadyList so must be called from a critical
3292 /* It does not make sense to check if the calling task is suspended. */
3293 configASSERT( xTask );
3295 /* Is the task being resumed actually in the suspended list? */
3296 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
3298 /* Has the task already been resumed from within an ISR? */
3299 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
3301 /* Is it in the suspended list because it is in the Suspended
3302 * state, or because it is blocked with no timeout? */
3303 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE )
3305 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3309 /* The task does not appear on the event list item of
3310 * and of the RTOS objects, but could still be in the
3311 * blocked state if it is waiting on its notification
3312 * rather than waiting on an object. If not, is
3316 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3318 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3325 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3329 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3333 mtCOVERAGE_TEST_MARKER();
3338 mtCOVERAGE_TEST_MARKER();
3343 mtCOVERAGE_TEST_MARKER();
3349 #endif /* INCLUDE_vTaskSuspend */
3350 /*-----------------------------------------------------------*/
3352 #if ( INCLUDE_vTaskSuspend == 1 )
3354 void vTaskResume( TaskHandle_t xTaskToResume )
3356 TCB_t * const pxTCB = xTaskToResume;
3358 traceENTER_vTaskResume( xTaskToResume );
3360 /* It does not make sense to resume the calling task. */
3361 configASSERT( xTaskToResume );
3363 #if ( configNUMBER_OF_CORES == 1 )
3365 /* The parameter cannot be NULL as it is impossible to resume the
3366 * currently executing task. */
3367 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
3370 /* The parameter cannot be NULL as it is impossible to resume the
3371 * currently executing task. It is also impossible to resume a task
3372 * that is actively running on another core but it is not safe
3373 * to check their run state here. Therefore, we get into a critical
3374 * section and check if the task is actually suspended or not. */
3378 taskENTER_CRITICAL();
3380 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3382 traceTASK_RESUME( pxTCB );
3384 /* The ready list can be accessed even if the scheduler is
3385 * suspended because this is inside a critical section. */
3386 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3387 prvAddTaskToReadyList( pxTCB );
3389 /* This yield may not cause the task just resumed to run,
3390 * but will leave the lists in the correct state for the
3392 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
3396 mtCOVERAGE_TEST_MARKER();
3399 taskEXIT_CRITICAL();
3403 mtCOVERAGE_TEST_MARKER();
3406 traceRETURN_vTaskResume();
3409 #endif /* INCLUDE_vTaskSuspend */
3411 /*-----------------------------------------------------------*/
3413 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
3415 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
3417 BaseType_t xYieldRequired = pdFALSE;
3418 TCB_t * const pxTCB = xTaskToResume;
3419 UBaseType_t uxSavedInterruptStatus;
3421 traceENTER_xTaskResumeFromISR( xTaskToResume );
3423 configASSERT( xTaskToResume );
3425 /* RTOS ports that support interrupt nesting have the concept of a
3426 * maximum system call (or maximum API call) interrupt priority.
3427 * Interrupts that are above the maximum system call priority are keep
3428 * permanently enabled, even when the RTOS kernel is in a critical section,
3429 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
3430 * is defined in FreeRTOSConfig.h then
3431 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3432 * failure if a FreeRTOS API function is called from an interrupt that has
3433 * been assigned a priority above the configured maximum system call
3434 * priority. Only FreeRTOS functions that end in FromISR can be called
3435 * from interrupts that have been assigned a priority at or (logically)
3436 * below the maximum system call interrupt priority. FreeRTOS maintains a
3437 * separate interrupt safe API to ensure interrupt entry is as fast and as
3438 * simple as possible. More information (albeit Cortex-M specific) is
3439 * provided on the following link:
3440 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3441 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3443 /* MISRA Ref 4.7.1 [Return value shall be checked] */
3444 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
3445 /* coverity[misra_c_2012_directive_4_7_violation] */
3446 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3448 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3450 traceTASK_RESUME_FROM_ISR( pxTCB );
3452 /* Check the ready lists can be accessed. */
3453 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3455 #if ( configNUMBER_OF_CORES == 1 )
3457 /* Ready lists can be accessed so move the task from the
3458 * suspended list to the ready list directly. */
3459 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3461 xYieldRequired = pdTRUE;
3463 /* Mark that a yield is pending in case the user is not
3464 * using the return value to initiate a context switch
3465 * from the ISR using the port specific portYIELD_FROM_ISR(). */
3466 xYieldPendings[ 0 ] = pdTRUE;
3470 mtCOVERAGE_TEST_MARKER();
3473 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3475 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3476 prvAddTaskToReadyList( pxTCB );
3480 /* The delayed or ready lists cannot be accessed so the task
3481 * is held in the pending ready list until the scheduler is
3483 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3486 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3488 prvYieldForTask( pxTCB );
3490 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3492 xYieldRequired = pdTRUE;
3495 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3499 mtCOVERAGE_TEST_MARKER();
3502 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3504 traceRETURN_xTaskResumeFromISR( xYieldRequired );
3506 return xYieldRequired;
3509 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3510 /*-----------------------------------------------------------*/
3512 static BaseType_t prvCreateIdleTasks( void )
3514 BaseType_t xReturn = pdPASS;
3516 char cIdleName[ configMAX_TASK_NAME_LEN ];
3517 TaskFunction_t pxIdleTaskFunction = NULL;
3518 BaseType_t xIdleTaskNameIndex;
3520 for( xIdleTaskNameIndex = ( BaseType_t ) 0; xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN; xIdleTaskNameIndex++ )
3522 cIdleName[ xIdleTaskNameIndex ] = configIDLE_TASK_NAME[ xIdleTaskNameIndex ];
3524 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3525 * configMAX_TASK_NAME_LEN characters just in case the memory after the
3526 * string is not accessible (extremely unlikely). */
3527 if( cIdleName[ xIdleTaskNameIndex ] == ( char ) 0x00 )
3533 mtCOVERAGE_TEST_MARKER();
3537 /* Add each idle task at the lowest priority. */
3538 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3540 #if ( configNUMBER_OF_CORES == 1 )
3542 pxIdleTaskFunction = prvIdleTask;
3544 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3546 /* In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks
3547 * are also created to ensure that each core has an idle task to
3548 * run when no other task is available to run. */
3551 pxIdleTaskFunction = prvIdleTask;
3555 pxIdleTaskFunction = prvPassiveIdleTask;
3558 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3560 /* Update the idle task name with suffix to differentiate the idle tasks.
3561 * This function is not required in single core FreeRTOS since there is
3562 * only one idle task. */
3563 #if ( configNUMBER_OF_CORES > 1 )
3565 /* Append the idle task number to the end of the name if there is space. */
3566 if( xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3568 cIdleName[ xIdleTaskNameIndex ] = ( char ) ( xCoreID + '0' );
3570 /* And append a null character if there is space. */
3571 if( ( xIdleTaskNameIndex + 1 ) < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3573 cIdleName[ xIdleTaskNameIndex + 1 ] = '\0';
3577 mtCOVERAGE_TEST_MARKER();
3582 mtCOVERAGE_TEST_MARKER();
3585 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
3587 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3589 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3590 StackType_t * pxIdleTaskStackBuffer = NULL;
3591 configSTACK_DEPTH_TYPE uxIdleTaskStackSize;
3593 /* The Idle task is created using user provided RAM - obtain the
3594 * address of the RAM then create the idle task. */
3595 #if ( configNUMBER_OF_CORES == 1 )
3597 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
3603 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
3607 vApplicationGetPassiveIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize, ( BaseType_t ) ( xCoreID - 1 ) );
3610 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
3611 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( pxIdleTaskFunction,
3613 uxIdleTaskStackSize,
3615 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3616 pxIdleTaskStackBuffer,
3617 pxIdleTaskTCBBuffer );
3619 if( xIdleTaskHandles[ xCoreID ] != NULL )
3628 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3630 /* The Idle task is being created using dynamically allocated RAM. */
3631 xReturn = xTaskCreate( pxIdleTaskFunction,
3633 configMINIMAL_STACK_SIZE,
3635 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3636 &xIdleTaskHandles[ xCoreID ] );
3638 #endif /* configSUPPORT_STATIC_ALLOCATION */
3640 /* Break the loop if any of the idle task is failed to be created. */
3641 if( xReturn == pdFAIL )
3647 #if ( configNUMBER_OF_CORES == 1 )
3649 mtCOVERAGE_TEST_MARKER();
3653 /* Assign idle task to each core before SMP scheduler is running. */
3654 xIdleTaskHandles[ xCoreID ]->xTaskRunState = xCoreID;
3655 pxCurrentTCBs[ xCoreID ] = xIdleTaskHandles[ xCoreID ];
3664 /*-----------------------------------------------------------*/
3666 void vTaskStartScheduler( void )
3670 traceENTER_vTaskStartScheduler();
3672 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3674 /* Sanity check that the UBaseType_t must have greater than or equal to
3675 * the number of bits as confNUMBER_OF_CORES. */
3676 configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3678 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3680 xReturn = prvCreateIdleTasks();
3682 #if ( configUSE_TIMERS == 1 )
3684 if( xReturn == pdPASS )
3686 xReturn = xTimerCreateTimerTask();
3690 mtCOVERAGE_TEST_MARKER();
3693 #endif /* configUSE_TIMERS */
3695 if( xReturn == pdPASS )
3697 /* freertos_tasks_c_additions_init() should only be called if the user
3698 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3699 * the only macro called by the function. */
3700 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3702 freertos_tasks_c_additions_init();
3706 /* Interrupts are turned off here, to ensure a tick does not occur
3707 * before or during the call to xPortStartScheduler(). The stacks of
3708 * the created tasks contain a status word with interrupts switched on
3709 * so interrupts will automatically get re-enabled when the first task
3711 portDISABLE_INTERRUPTS();
3713 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3715 /* Switch C-Runtime's TLS Block to point to the TLS
3716 * block specific to the task that will run first. */
3717 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3721 xNextTaskUnblockTime = portMAX_DELAY;
3722 xSchedulerRunning = pdTRUE;
3723 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3725 /* If configGENERATE_RUN_TIME_STATS is defined then the following
3726 * macro must be defined to configure the timer/counter used to generate
3727 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
3728 * is set to 0 and the following line fails to build then ensure you do not
3729 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3730 * FreeRTOSConfig.h file. */
3731 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3733 traceTASK_SWITCHED_IN();
3735 /* Setting up the timer tick is hardware specific and thus in the
3736 * portable interface. */
3738 /* The return value for xPortStartScheduler is not required
3739 * hence using a void datatype. */
3740 ( void ) xPortStartScheduler();
3742 /* In most cases, xPortStartScheduler() will not return. If it
3743 * returns pdTRUE then there was not enough heap memory available
3744 * to create either the Idle or the Timer task. If it returned
3745 * pdFALSE, then the application called xTaskEndScheduler().
3746 * Most ports don't implement xTaskEndScheduler() as there is
3747 * nothing to return to. */
3751 /* This line will only be reached if the kernel could not be started,
3752 * because there was not enough FreeRTOS heap to create the idle task
3753 * or the timer task. */
3754 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3757 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3758 * meaning xIdleTaskHandles are not used anywhere else. */
3759 ( void ) xIdleTaskHandles;
3761 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3762 * from getting optimized out as it is no longer used by the kernel. */
3763 ( void ) uxTopUsedPriority;
3765 traceRETURN_vTaskStartScheduler();
3767 /*-----------------------------------------------------------*/
3769 void vTaskEndScheduler( void )
3771 traceENTER_vTaskEndScheduler();
3773 #if ( INCLUDE_vTaskDelete == 1 )
3777 #if ( configUSE_TIMERS == 1 )
3779 /* Delete the timer task created by the kernel. */
3780 vTaskDelete( xTimerGetTimerDaemonTaskHandle() );
3782 #endif /* #if ( configUSE_TIMERS == 1 ) */
3784 /* Delete Idle tasks created by the kernel.*/
3785 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3787 vTaskDelete( xIdleTaskHandles[ xCoreID ] );
3790 /* Idle task is responsible for reclaiming the resources of the tasks in
3791 * xTasksWaitingTermination list. Since the idle task is now deleted and
3792 * no longer going to run, we need to reclaim resources of all the tasks
3793 * in the xTasksWaitingTermination list. */
3794 prvCheckTasksWaitingTermination();
3796 #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
3798 /* Stop the scheduler interrupts and call the portable scheduler end
3799 * routine so the original ISRs can be restored if necessary. The port
3800 * layer must ensure interrupts enable bit is left in the correct state. */
3801 portDISABLE_INTERRUPTS();
3802 xSchedulerRunning = pdFALSE;
3804 /* This function must be called from a task and the application is
3805 * responsible for deleting that task after the scheduler is stopped. */
3806 vPortEndScheduler();
3808 traceRETURN_vTaskEndScheduler();
3810 /*----------------------------------------------------------*/
3812 void vTaskSuspendAll( void )
3814 traceENTER_vTaskSuspendAll();
3816 #if ( configNUMBER_OF_CORES == 1 )
3818 /* A critical section is not required as the variable is of type
3819 * BaseType_t. Please read Richard Barry's reply in the following link to a
3820 * post in the FreeRTOS support forum before reporting this as a bug! -
3821 * https://goo.gl/wu4acr */
3823 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3824 * do not otherwise exhibit real time behaviour. */
3825 portSOFTWARE_BARRIER();
3827 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3828 * is used to allow calls to vTaskSuspendAll() to nest. */
3829 uxSchedulerSuspended = ( UBaseType_t ) ( uxSchedulerSuspended + 1U );
3831 /* Enforces ordering for ports and optimised compilers that may otherwise place
3832 * the above increment elsewhere. */
3833 portMEMORY_BARRIER();
3835 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3837 UBaseType_t ulState;
3839 /* This must only be called from within a task. */
3840 portASSERT_IF_IN_ISR();
3842 if( xSchedulerRunning != pdFALSE )
3844 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3845 * We must disable interrupts before we grab the locks in the event that this task is
3846 * interrupted and switches context before incrementing uxSchedulerSuspended.
3847 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3848 * uxSchedulerSuspended since that will prevent context switches. */
3849 ulState = portSET_INTERRUPT_MASK();
3851 /* This must never be called from inside a critical section. */
3852 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
3854 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3855 * do not otherwise exhibit real time behaviour. */
3856 portSOFTWARE_BARRIER();
3858 portGET_TASK_LOCK();
3860 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3861 * purpose is to prevent altering the variable when fromISR APIs are readying
3863 if( uxSchedulerSuspended == 0U )
3865 prvCheckForRunStateChange();
3869 mtCOVERAGE_TEST_MARKER();
3874 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3875 * is used to allow calls to vTaskSuspendAll() to nest. */
3876 ++uxSchedulerSuspended;
3877 portRELEASE_ISR_LOCK();
3879 portCLEAR_INTERRUPT_MASK( ulState );
3883 mtCOVERAGE_TEST_MARKER();
3886 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3888 traceRETURN_vTaskSuspendAll();
3891 /*----------------------------------------------------------*/
3893 #if ( configUSE_TICKLESS_IDLE != 0 )
3895 static TickType_t prvGetExpectedIdleTime( void )
3898 BaseType_t xHigherPriorityReadyTasks = pdFALSE;
3900 /* xHigherPriorityReadyTasks takes care of the case where
3901 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3902 * task that are in the Ready state, even though the idle task is
3904 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3906 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3908 xHigherPriorityReadyTasks = pdTRUE;
3913 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3915 /* When port optimised task selection is used the uxTopReadyPriority
3916 * variable is used as a bit map. If bits other than the least
3917 * significant bit are set then there are tasks that have a priority
3918 * above the idle priority that are in the Ready state. This takes
3919 * care of the case where the co-operative scheduler is in use. */
3920 if( uxTopReadyPriority > uxLeastSignificantBit )
3922 xHigherPriorityReadyTasks = pdTRUE;
3925 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3927 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3931 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1U )
3933 /* There are other idle priority tasks in the ready state. If
3934 * time slicing is used then the very next tick interrupt must be
3938 else if( xHigherPriorityReadyTasks != pdFALSE )
3940 /* There are tasks in the Ready state that have a priority above the
3941 * idle priority. This path can only be reached if
3942 * configUSE_PREEMPTION is 0. */
3947 xReturn = xNextTaskUnblockTime;
3948 xReturn -= xTickCount;
3954 #endif /* configUSE_TICKLESS_IDLE */
3955 /*----------------------------------------------------------*/
3957 BaseType_t xTaskResumeAll( void )
3959 TCB_t * pxTCB = NULL;
3960 BaseType_t xAlreadyYielded = pdFALSE;
3962 traceENTER_xTaskResumeAll();
3964 #if ( configNUMBER_OF_CORES > 1 )
3965 if( xSchedulerRunning != pdFALSE )
3968 /* It is possible that an ISR caused a task to be removed from an event
3969 * list while the scheduler was suspended. If this was the case then the
3970 * removed task will have been added to the xPendingReadyList. Once the
3971 * scheduler has been resumed it is safe to move all the pending ready
3972 * tasks from this list into their appropriate ready list. */
3973 taskENTER_CRITICAL();
3976 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3978 /* If uxSchedulerSuspended is zero then this function does not match a
3979 * previous call to vTaskSuspendAll(). */
3980 configASSERT( uxSchedulerSuspended != 0U );
3982 uxSchedulerSuspended = ( UBaseType_t ) ( uxSchedulerSuspended - 1U );
3983 portRELEASE_TASK_LOCK();
3985 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3987 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3989 /* Move any readied tasks from the pending list into the
3990 * appropriate ready list. */
3991 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3993 /* MISRA Ref 11.5.3 [Void pointer assignment] */
3994 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
3995 /* coverity[misra_c_2012_rule_11_5_violation] */
3996 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) );
3997 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
3998 portMEMORY_BARRIER();
3999 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4000 prvAddTaskToReadyList( pxTCB );
4002 #if ( configNUMBER_OF_CORES == 1 )
4004 /* If the moved task has a priority higher than the current
4005 * task then a yield must be performed. */
4006 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4008 xYieldPendings[ xCoreID ] = pdTRUE;
4012 mtCOVERAGE_TEST_MARKER();
4015 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4017 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
4018 * If the current core yielded then vTaskSwitchContext() has already been called
4019 * which sets xYieldPendings for the current core to pdTRUE. */
4021 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4026 /* A task was unblocked while the scheduler was suspended,
4027 * which may have prevented the next unblock time from being
4028 * re-calculated, in which case re-calculate it now. Mainly
4029 * important for low power tickless implementations, where
4030 * this can prevent an unnecessary exit from low power
4032 prvResetNextTaskUnblockTime();
4035 /* If any ticks occurred while the scheduler was suspended then
4036 * they should be processed now. This ensures the tick count does
4037 * not slip, and that any delayed tasks are resumed at the correct
4040 * It should be safe to call xTaskIncrementTick here from any core
4041 * since we are in a critical section and xTaskIncrementTick itself
4042 * protects itself within a critical section. Suspending the scheduler
4043 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
4045 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
4047 if( xPendedCounts > ( TickType_t ) 0U )
4051 if( xTaskIncrementTick() != pdFALSE )
4053 /* Other cores are interrupted from
4054 * within xTaskIncrementTick(). */
4055 xYieldPendings[ xCoreID ] = pdTRUE;
4059 mtCOVERAGE_TEST_MARKER();
4063 } while( xPendedCounts > ( TickType_t ) 0U );
4069 mtCOVERAGE_TEST_MARKER();
4073 if( xYieldPendings[ xCoreID ] != pdFALSE )
4075 #if ( configUSE_PREEMPTION != 0 )
4077 xAlreadyYielded = pdTRUE;
4079 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
4081 #if ( configNUMBER_OF_CORES == 1 )
4083 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
4085 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4089 mtCOVERAGE_TEST_MARKER();
4095 mtCOVERAGE_TEST_MARKER();
4098 taskEXIT_CRITICAL();
4101 traceRETURN_xTaskResumeAll( xAlreadyYielded );
4103 return xAlreadyYielded;
4105 /*-----------------------------------------------------------*/
4107 TickType_t xTaskGetTickCount( void )
4111 traceENTER_xTaskGetTickCount();
4113 /* Critical section required if running on a 16 bit processor. */
4114 portTICK_TYPE_ENTER_CRITICAL();
4116 xTicks = xTickCount;
4118 portTICK_TYPE_EXIT_CRITICAL();
4120 traceRETURN_xTaskGetTickCount( xTicks );
4124 /*-----------------------------------------------------------*/
4126 TickType_t xTaskGetTickCountFromISR( void )
4129 UBaseType_t uxSavedInterruptStatus;
4131 traceENTER_xTaskGetTickCountFromISR();
4133 /* RTOS ports that support interrupt nesting have the concept of a maximum
4134 * system call (or maximum API call) interrupt priority. Interrupts that are
4135 * above the maximum system call priority are kept permanently enabled, even
4136 * when the RTOS kernel is in a critical section, but cannot make any calls to
4137 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
4138 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
4139 * failure if a FreeRTOS API function is called from an interrupt that has been
4140 * assigned a priority above the configured maximum system call priority.
4141 * Only FreeRTOS functions that end in FromISR can be called from interrupts
4142 * that have been assigned a priority at or (logically) below the maximum
4143 * system call interrupt priority. FreeRTOS maintains a separate interrupt
4144 * safe API to ensure interrupt entry is as fast and as simple as possible.
4145 * More information (albeit Cortex-M specific) is provided on the following
4146 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
4147 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
4149 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
4151 xReturn = xTickCount;
4153 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
4155 traceRETURN_xTaskGetTickCountFromISR( xReturn );
4159 /*-----------------------------------------------------------*/
4161 UBaseType_t uxTaskGetNumberOfTasks( void )
4163 traceENTER_uxTaskGetNumberOfTasks();
4165 /* A critical section is not required because the variables are of type
4167 traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
4169 return uxCurrentNumberOfTasks;
4171 /*-----------------------------------------------------------*/
4173 char * pcTaskGetName( TaskHandle_t xTaskToQuery )
4177 traceENTER_pcTaskGetName( xTaskToQuery );
4179 /* If null is passed in here then the name of the calling task is being
4181 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4182 configASSERT( pxTCB );
4184 traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
4186 return &( pxTCB->pcTaskName[ 0 ] );
4188 /*-----------------------------------------------------------*/
4190 #if ( INCLUDE_xTaskGetHandle == 1 )
4191 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4192 const char pcNameToQuery[] )
4194 TCB_t * pxReturn = NULL;
4195 TCB_t * pxTCB = NULL;
4198 BaseType_t xBreakLoop;
4199 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
4200 ListItem_t * pxIterator;
4202 /* This function is called with the scheduler suspended. */
4204 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4206 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
4208 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4209 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4210 /* coverity[misra_c_2012_rule_11_5_violation] */
4211 pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
4213 /* Check each character in the name looking for a match or
4215 xBreakLoop = pdFALSE;
4217 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4219 cNextChar = pxTCB->pcTaskName[ x ];
4221 if( cNextChar != pcNameToQuery[ x ] )
4223 /* Characters didn't match. */
4224 xBreakLoop = pdTRUE;
4226 else if( cNextChar == ( char ) 0x00 )
4228 /* Both strings terminated, a match must have been
4231 xBreakLoop = pdTRUE;
4235 mtCOVERAGE_TEST_MARKER();
4238 if( xBreakLoop != pdFALSE )
4244 if( pxReturn != NULL )
4246 /* The handle has been found. */
4253 mtCOVERAGE_TEST_MARKER();
4259 #endif /* INCLUDE_xTaskGetHandle */
4260 /*-----------------------------------------------------------*/
4262 #if ( INCLUDE_xTaskGetHandle == 1 )
4264 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery )
4266 UBaseType_t uxQueue = configMAX_PRIORITIES;
4269 traceENTER_xTaskGetHandle( pcNameToQuery );
4271 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
4272 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
4276 /* Search the ready lists. */
4280 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
4284 /* Found the handle. */
4287 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4289 /* Search the delayed lists. */
4292 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
4297 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
4300 #if ( INCLUDE_vTaskSuspend == 1 )
4304 /* Search the suspended list. */
4305 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
4310 #if ( INCLUDE_vTaskDelete == 1 )
4314 /* Search the deleted list. */
4315 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
4320 ( void ) xTaskResumeAll();
4322 traceRETURN_xTaskGetHandle( pxTCB );
4327 #endif /* INCLUDE_xTaskGetHandle */
4328 /*-----------------------------------------------------------*/
4330 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4332 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4333 StackType_t ** ppuxStackBuffer,
4334 StaticTask_t ** ppxTaskBuffer )
4339 traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4341 configASSERT( ppuxStackBuffer != NULL );
4342 configASSERT( ppxTaskBuffer != NULL );
4344 pxTCB = prvGetTCBFromHandle( xTask );
4346 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4348 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4350 *ppuxStackBuffer = pxTCB->pxStack;
4351 /* MISRA Ref 11.3.1 [Misaligned access] */
4352 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
4353 /* coverity[misra_c_2012_rule_11_3_violation] */
4354 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4357 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4359 *ppuxStackBuffer = pxTCB->pxStack;
4360 *ppxTaskBuffer = NULL;
4368 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4370 *ppuxStackBuffer = pxTCB->pxStack;
4371 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4374 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4376 traceRETURN_xTaskGetStaticBuffers( xReturn );
4381 #endif /* configSUPPORT_STATIC_ALLOCATION */
4382 /*-----------------------------------------------------------*/
4384 #if ( configUSE_TRACE_FACILITY == 1 )
4386 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4387 const UBaseType_t uxArraySize,
4388 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4390 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4392 traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4396 /* Is there a space in the array for each task in the system? */
4397 if( uxArraySize >= uxCurrentNumberOfTasks )
4399 /* Fill in an TaskStatus_t structure with information on each
4400 * task in the Ready state. */
4404 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4405 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4407 /* Fill in an TaskStatus_t structure with information on each
4408 * task in the Blocked state. */
4409 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4410 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4412 #if ( INCLUDE_vTaskDelete == 1 )
4414 /* Fill in an TaskStatus_t structure with information on
4415 * each task that has been deleted but not yet cleaned up. */
4416 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4420 #if ( INCLUDE_vTaskSuspend == 1 )
4422 /* Fill in an TaskStatus_t structure with information on
4423 * each task in the Suspended state. */
4424 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4428 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4430 if( pulTotalRunTime != NULL )
4432 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4433 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4435 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4439 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4441 if( pulTotalRunTime != NULL )
4443 *pulTotalRunTime = 0;
4446 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4450 mtCOVERAGE_TEST_MARKER();
4453 ( void ) xTaskResumeAll();
4455 traceRETURN_uxTaskGetSystemState( uxTask );
4460 #endif /* configUSE_TRACE_FACILITY */
4461 /*----------------------------------------------------------*/
4463 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4465 #if ( configNUMBER_OF_CORES == 1 )
4466 TaskHandle_t xTaskGetIdleTaskHandle( void )
4468 traceENTER_xTaskGetIdleTaskHandle();
4470 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4471 * started, then xIdleTaskHandles will be NULL. */
4472 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4474 traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4476 return xIdleTaskHandles[ 0 ];
4478 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
4480 TaskHandle_t xTaskGetIdleTaskHandleForCore( BaseType_t xCoreID )
4482 traceENTER_xTaskGetIdleTaskHandleForCore( xCoreID );
4484 /* Ensure the core ID is valid. */
4485 configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE );
4487 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4488 * started, then xIdleTaskHandles will be NULL. */
4489 configASSERT( ( xIdleTaskHandles[ xCoreID ] != NULL ) );
4491 traceRETURN_xTaskGetIdleTaskHandleForCore( xIdleTaskHandles[ xCoreID ] );
4493 return xIdleTaskHandles[ xCoreID ];
4496 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4497 /*----------------------------------------------------------*/
4499 /* This conditional compilation should use inequality to 0, not equality to 1.
4500 * This is to ensure vTaskStepTick() is available when user defined low power mode
4501 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4503 #if ( configUSE_TICKLESS_IDLE != 0 )
4505 void vTaskStepTick( TickType_t xTicksToJump )
4507 TickType_t xUpdatedTickCount;
4509 traceENTER_vTaskStepTick( xTicksToJump );
4511 /* Correct the tick count value after a period during which the tick
4512 * was suppressed. Note this does *not* call the tick hook function for
4513 * each stepped tick. */
4514 xUpdatedTickCount = xTickCount + xTicksToJump;
4515 configASSERT( xUpdatedTickCount <= xNextTaskUnblockTime );
4517 if( xUpdatedTickCount == xNextTaskUnblockTime )
4519 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4520 * xTaskIncrementTick() when the scheduler resumes. This ensures
4521 * that any delayed tasks are resumed at the correct time. */
4522 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4523 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4525 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4526 taskENTER_CRITICAL();
4530 taskEXIT_CRITICAL();
4535 mtCOVERAGE_TEST_MARKER();
4538 xTickCount += xTicksToJump;
4540 traceINCREASE_TICK_COUNT( xTicksToJump );
4541 traceRETURN_vTaskStepTick();
4544 #endif /* configUSE_TICKLESS_IDLE */
4545 /*----------------------------------------------------------*/
4547 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4549 BaseType_t xYieldOccurred;
4551 traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4553 /* Must not be called with the scheduler suspended as the implementation
4554 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4555 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4557 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4558 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4561 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4562 taskENTER_CRITICAL();
4564 xPendedTicks += xTicksToCatchUp;
4566 taskEXIT_CRITICAL();
4567 xYieldOccurred = xTaskResumeAll();
4569 traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4571 return xYieldOccurred;
4573 /*----------------------------------------------------------*/
4575 #if ( INCLUDE_xTaskAbortDelay == 1 )
4577 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4579 TCB_t * pxTCB = xTask;
4582 traceENTER_xTaskAbortDelay( xTask );
4584 configASSERT( pxTCB );
4588 /* A task can only be prematurely removed from the Blocked state if
4589 * it is actually in the Blocked state. */
4590 if( eTaskGetState( xTask ) == eBlocked )
4594 /* Remove the reference to the task from the blocked list. An
4595 * interrupt won't touch the xStateListItem because the
4596 * scheduler is suspended. */
4597 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4599 /* Is the task waiting on an event also? If so remove it from
4600 * the event list too. Interrupts can touch the event list item,
4601 * even though the scheduler is suspended, so a critical section
4603 taskENTER_CRITICAL();
4605 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4607 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4609 /* This lets the task know it was forcibly removed from the
4610 * blocked state so it should not re-evaluate its block time and
4611 * then block again. */
4612 pxTCB->ucDelayAborted = ( uint8_t ) pdTRUE;
4616 mtCOVERAGE_TEST_MARKER();
4619 taskEXIT_CRITICAL();
4621 /* Place the unblocked task into the appropriate ready list. */
4622 prvAddTaskToReadyList( pxTCB );
4624 /* A task being unblocked cannot cause an immediate context
4625 * switch if preemption is turned off. */
4626 #if ( configUSE_PREEMPTION == 1 )
4628 #if ( configNUMBER_OF_CORES == 1 )
4630 /* Preemption is on, but a context switch should only be
4631 * performed if the unblocked task has a priority that is
4632 * higher than the currently executing task. */
4633 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4635 /* Pend the yield to be performed when the scheduler
4636 * is unsuspended. */
4637 xYieldPendings[ 0 ] = pdTRUE;
4641 mtCOVERAGE_TEST_MARKER();
4644 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4646 taskENTER_CRITICAL();
4648 prvYieldForTask( pxTCB );
4650 taskEXIT_CRITICAL();
4652 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4654 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4661 ( void ) xTaskResumeAll();
4663 traceRETURN_xTaskAbortDelay( xReturn );
4668 #endif /* INCLUDE_xTaskAbortDelay */
4669 /*----------------------------------------------------------*/
4671 BaseType_t xTaskIncrementTick( void )
4674 TickType_t xItemValue;
4675 BaseType_t xSwitchRequired = pdFALSE;
4677 #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4678 BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4679 #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4681 traceENTER_xTaskIncrementTick();
4683 /* Called by the portable layer each time a tick interrupt occurs.
4684 * Increments the tick then checks to see if the new tick value will cause any
4685 * tasks to be unblocked. */
4686 traceTASK_INCREMENT_TICK( xTickCount );
4688 /* Tick increment should occur on every kernel timer event. Core 0 has the
4689 * responsibility to increment the tick, or increment the pended ticks if the
4690 * scheduler is suspended. If pended ticks is greater than zero, the core that
4691 * calls xTaskResumeAll has the responsibility to increment the tick. */
4692 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4694 /* Minor optimisation. The tick count cannot change in this
4696 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4698 /* Increment the RTOS tick, switching the delayed and overflowed
4699 * delayed lists if it wraps to 0. */
4700 xTickCount = xConstTickCount;
4702 if( xConstTickCount == ( TickType_t ) 0U )
4704 taskSWITCH_DELAYED_LISTS();
4708 mtCOVERAGE_TEST_MARKER();
4711 /* See if this tick has made a timeout expire. Tasks are stored in
4712 * the queue in the order of their wake time - meaning once one task
4713 * has been found whose block time has not expired there is no need to
4714 * look any further down the list. */
4715 if( xConstTickCount >= xNextTaskUnblockTime )
4719 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4721 /* The delayed list is empty. Set xNextTaskUnblockTime
4722 * to the maximum possible value so it is extremely
4724 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4725 * next time through. */
4726 xNextTaskUnblockTime = portMAX_DELAY;
4731 /* The delayed list is not empty, get the value of the
4732 * item at the head of the delayed list. This is the time
4733 * at which the task at the head of the delayed list must
4734 * be removed from the Blocked state. */
4735 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4736 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4737 /* coverity[misra_c_2012_rule_11_5_violation] */
4738 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
4739 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4741 if( xConstTickCount < xItemValue )
4743 /* It is not time to unblock this item yet, but the
4744 * item value is the time at which the task at the head
4745 * of the blocked list must be removed from the Blocked
4746 * state - so record the item value in
4747 * xNextTaskUnblockTime. */
4748 xNextTaskUnblockTime = xItemValue;
4753 mtCOVERAGE_TEST_MARKER();
4756 /* It is time to remove the item from the Blocked state. */
4757 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4759 /* Is the task waiting on an event also? If so remove
4760 * it from the event list. */
4761 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4763 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4767 mtCOVERAGE_TEST_MARKER();
4770 /* Place the unblocked task into the appropriate ready
4772 prvAddTaskToReadyList( pxTCB );
4774 /* A task being unblocked cannot cause an immediate
4775 * context switch if preemption is turned off. */
4776 #if ( configUSE_PREEMPTION == 1 )
4778 #if ( configNUMBER_OF_CORES == 1 )
4780 /* Preemption is on, but a context switch should
4781 * only be performed if the unblocked task's
4782 * priority is higher than the currently executing
4784 * The case of equal priority tasks sharing
4785 * processing time (which happens when both
4786 * preemption and time slicing are on) is
4788 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4790 xSwitchRequired = pdTRUE;
4794 mtCOVERAGE_TEST_MARKER();
4797 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4799 prvYieldForTask( pxTCB );
4801 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4803 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4808 /* Tasks of equal priority to the currently running task will share
4809 * processing time (time slice) if preemption is on, and the application
4810 * writer has not explicitly turned time slicing off. */
4811 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4813 #if ( configNUMBER_OF_CORES == 1 )
4815 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > 1U )
4817 xSwitchRequired = pdTRUE;
4821 mtCOVERAGE_TEST_MARKER();
4824 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4828 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4830 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1U )
4832 xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4836 mtCOVERAGE_TEST_MARKER();
4840 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4842 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4844 #if ( configUSE_TICK_HOOK == 1 )
4846 /* Guard against the tick hook being called when the pended tick
4847 * count is being unwound (when the scheduler is being unlocked). */
4848 if( xPendedTicks == ( TickType_t ) 0 )
4850 vApplicationTickHook();
4854 mtCOVERAGE_TEST_MARKER();
4857 #endif /* configUSE_TICK_HOOK */
4859 #if ( configUSE_PREEMPTION == 1 )
4861 #if ( configNUMBER_OF_CORES == 1 )
4863 /* For single core the core ID is always 0. */
4864 if( xYieldPendings[ 0 ] != pdFALSE )
4866 xSwitchRequired = pdTRUE;
4870 mtCOVERAGE_TEST_MARKER();
4873 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4875 BaseType_t xCoreID, xCurrentCoreID;
4876 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4878 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4880 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4881 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4884 if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4886 if( xCoreID == xCurrentCoreID )
4888 xSwitchRequired = pdTRUE;
4892 prvYieldCore( xCoreID );
4897 mtCOVERAGE_TEST_MARKER();
4902 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4904 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4910 /* The tick hook gets called at regular intervals, even if the
4911 * scheduler is locked. */
4912 #if ( configUSE_TICK_HOOK == 1 )
4914 vApplicationTickHook();
4919 traceRETURN_xTaskIncrementTick( xSwitchRequired );
4921 return xSwitchRequired;
4923 /*-----------------------------------------------------------*/
4925 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4927 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4928 TaskHookFunction_t pxHookFunction )
4932 traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4934 /* If xTask is NULL then it is the task hook of the calling task that is
4938 xTCB = ( TCB_t * ) pxCurrentTCB;
4945 /* Save the hook function in the TCB. A critical section is required as
4946 * the value can be accessed from an interrupt. */
4947 taskENTER_CRITICAL();
4949 xTCB->pxTaskTag = pxHookFunction;
4951 taskEXIT_CRITICAL();
4953 traceRETURN_vTaskSetApplicationTaskTag();
4956 #endif /* configUSE_APPLICATION_TASK_TAG */
4957 /*-----------------------------------------------------------*/
4959 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4961 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4964 TaskHookFunction_t xReturn;
4966 traceENTER_xTaskGetApplicationTaskTag( xTask );
4968 /* If xTask is NULL then set the calling task's hook. */
4969 pxTCB = prvGetTCBFromHandle( xTask );
4971 /* Save the hook function in the TCB. A critical section is required as
4972 * the value can be accessed from an interrupt. */
4973 taskENTER_CRITICAL();
4975 xReturn = pxTCB->pxTaskTag;
4977 taskEXIT_CRITICAL();
4979 traceRETURN_xTaskGetApplicationTaskTag( xReturn );
4984 #endif /* configUSE_APPLICATION_TASK_TAG */
4985 /*-----------------------------------------------------------*/
4987 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4989 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
4992 TaskHookFunction_t xReturn;
4993 UBaseType_t uxSavedInterruptStatus;
4995 traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
4997 /* If xTask is NULL then set the calling task's hook. */
4998 pxTCB = prvGetTCBFromHandle( xTask );
5000 /* Save the hook function in the TCB. A critical section is required as
5001 * the value can be accessed from an interrupt. */
5002 /* MISRA Ref 4.7.1 [Return value shall be checked] */
5003 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
5004 /* coverity[misra_c_2012_directive_4_7_violation] */
5005 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
5007 xReturn = pxTCB->pxTaskTag;
5009 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
5011 traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
5016 #endif /* configUSE_APPLICATION_TASK_TAG */
5017 /*-----------------------------------------------------------*/
5019 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5021 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
5022 void * pvParameter )
5027 traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
5029 /* If xTask is NULL then we are calling our own task hook. */
5032 xTCB = pxCurrentTCB;
5039 if( xTCB->pxTaskTag != NULL )
5041 xReturn = xTCB->pxTaskTag( pvParameter );
5048 traceRETURN_xTaskCallApplicationTaskHook( xReturn );
5053 #endif /* configUSE_APPLICATION_TASK_TAG */
5054 /*-----------------------------------------------------------*/
5056 #if ( configNUMBER_OF_CORES == 1 )
5057 void vTaskSwitchContext( void )
5059 traceENTER_vTaskSwitchContext();
5061 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5063 /* The scheduler is currently suspended - do not allow a context
5065 xYieldPendings[ 0 ] = pdTRUE;
5069 xYieldPendings[ 0 ] = pdFALSE;
5070 traceTASK_SWITCHED_OUT();
5072 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5074 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5075 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
5077 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
5080 /* Add the amount of time the task has been running to the
5081 * accumulated time so far. The time the task started running was
5082 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5083 * protection here so count values are only valid until the timer
5084 * overflows. The guard against negative values is to protect
5085 * against suspect run time stat counter implementations - which
5086 * are provided by the application, not the kernel. */
5087 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
5089 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
5093 mtCOVERAGE_TEST_MARKER();
5096 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
5098 #endif /* configGENERATE_RUN_TIME_STATS */
5100 /* Check for stack overflow, if configured. */
5101 taskCHECK_FOR_STACK_OVERFLOW();
5103 /* Before the currently running task is switched out, save its errno. */
5104 #if ( configUSE_POSIX_ERRNO == 1 )
5106 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
5110 /* Select a new task to run using either the generic C or port
5111 * optimised asm code. */
5112 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5113 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5114 /* coverity[misra_c_2012_rule_11_5_violation] */
5115 taskSELECT_HIGHEST_PRIORITY_TASK();
5116 traceTASK_SWITCHED_IN();
5118 /* Macro to inject port specific behaviour immediately after
5119 * switching tasks, such as setting an end of stack watchpoint
5120 * or reconfiguring the MPU. */
5121 portTASK_SWITCH_HOOK( pxCurrentTCB );
5123 /* After the new task is switched in, update the global errno. */
5124 #if ( configUSE_POSIX_ERRNO == 1 )
5126 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
5130 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5132 /* Switch C-Runtime's TLS Block to point to the TLS
5133 * Block specific to this task. */
5134 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
5139 traceRETURN_vTaskSwitchContext();
5141 #else /* if ( configNUMBER_OF_CORES == 1 ) */
5142 void vTaskSwitchContext( BaseType_t xCoreID )
5144 traceENTER_vTaskSwitchContext();
5146 /* Acquire both locks:
5147 * - The ISR lock protects the ready list from simultaneous access by
5148 * both other ISRs and tasks.
5149 * - We also take the task lock to pause here in case another core has
5150 * suspended the scheduler. We don't want to simply set xYieldPending
5151 * and move on if another core suspended the scheduler. We should only
5152 * do that if the current core has suspended the scheduler. */
5154 portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
5157 /* vTaskSwitchContext() must never be called from within a critical section.
5158 * This is not necessarily true for single core FreeRTOS, but it is for this
5160 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
5162 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5164 /* The scheduler is currently suspended - do not allow a context
5166 xYieldPendings[ xCoreID ] = pdTRUE;
5170 xYieldPendings[ xCoreID ] = pdFALSE;
5171 traceTASK_SWITCHED_OUT();
5173 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5175 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5176 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
5178 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
5181 /* Add the amount of time the task has been running to the
5182 * accumulated time so far. The time the task started running was
5183 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5184 * protection here so count values are only valid until the timer
5185 * overflows. The guard against negative values is to protect
5186 * against suspect run time stat counter implementations - which
5187 * are provided by the application, not the kernel. */
5188 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
5190 pxCurrentTCBs[ xCoreID ]->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
5194 mtCOVERAGE_TEST_MARKER();
5197 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
5199 #endif /* configGENERATE_RUN_TIME_STATS */
5201 /* Check for stack overflow, if configured. */
5202 taskCHECK_FOR_STACK_OVERFLOW();
5204 /* Before the currently running task is switched out, save its errno. */
5205 #if ( configUSE_POSIX_ERRNO == 1 )
5207 pxCurrentTCBs[ xCoreID ]->iTaskErrno = FreeRTOS_errno;
5211 /* Select a new task to run. */
5212 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
5213 traceTASK_SWITCHED_IN();
5215 /* Macro to inject port specific behaviour immediately after
5216 * switching tasks, such as setting an end of stack watchpoint
5217 * or reconfiguring the MPU. */
5218 portTASK_SWITCH_HOOK( pxCurrentTCBs[ portGET_CORE_ID() ] );
5220 /* After the new task is switched in, update the global errno. */
5221 #if ( configUSE_POSIX_ERRNO == 1 )
5223 FreeRTOS_errno = pxCurrentTCBs[ xCoreID ]->iTaskErrno;
5227 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5229 /* Switch C-Runtime's TLS Block to point to the TLS
5230 * Block specific to this task. */
5231 configSET_TLS_BLOCK( pxCurrentTCBs[ xCoreID ]->xTLSBlock );
5236 portRELEASE_ISR_LOCK();
5237 portRELEASE_TASK_LOCK();
5239 traceRETURN_vTaskSwitchContext();
5241 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
5242 /*-----------------------------------------------------------*/
5244 void vTaskPlaceOnEventList( List_t * const pxEventList,
5245 const TickType_t xTicksToWait )
5247 traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
5249 configASSERT( pxEventList );
5251 /* THIS FUNCTION MUST BE CALLED WITH THE
5252 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
5254 /* Place the event list item of the TCB in the appropriate event list.
5255 * This is placed in the list in priority order so the highest priority task
5256 * is the first to be woken by the event.
5258 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
5259 * Normally, the xItemValue of a TCB's ListItem_t members is:
5260 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
5261 * Therefore, the event list is sorted in descending priority order.
5263 * The queue that contains the event list is locked, preventing
5264 * simultaneous access from interrupts. */
5265 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5267 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5269 traceRETURN_vTaskPlaceOnEventList();
5271 /*-----------------------------------------------------------*/
5273 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
5274 const TickType_t xItemValue,
5275 const TickType_t xTicksToWait )
5277 traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
5279 configASSERT( pxEventList );
5281 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5282 * the event groups implementation. */
5283 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5285 /* Store the item value in the event list item. It is safe to access the
5286 * event list item here as interrupts won't access the event list item of a
5287 * task that is not in the Blocked state. */
5288 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5290 /* Place the event list item of the TCB at the end of the appropriate event
5291 * list. It is safe to access the event list here because it is part of an
5292 * event group implementation - and interrupts don't access event groups
5293 * directly (instead they access them indirectly by pending function calls to
5294 * the task level). */
5295 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5297 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5299 traceRETURN_vTaskPlaceOnUnorderedEventList();
5301 /*-----------------------------------------------------------*/
5303 #if ( configUSE_TIMERS == 1 )
5305 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
5306 TickType_t xTicksToWait,
5307 const BaseType_t xWaitIndefinitely )
5309 traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
5311 configASSERT( pxEventList );
5313 /* This function should not be called by application code hence the
5314 * 'Restricted' in its name. It is not part of the public API. It is
5315 * designed for use by kernel code, and has special calling requirements -
5316 * it should be called with the scheduler suspended. */
5319 /* Place the event list item of the TCB in the appropriate event list.
5320 * In this case it is assume that this is the only task that is going to
5321 * be waiting on this event list, so the faster vListInsertEnd() function
5322 * can be used in place of vListInsert. */
5323 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5325 /* If the task should block indefinitely then set the block time to a
5326 * value that will be recognised as an indefinite delay inside the
5327 * prvAddCurrentTaskToDelayedList() function. */
5328 if( xWaitIndefinitely != pdFALSE )
5330 xTicksToWait = portMAX_DELAY;
5333 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
5334 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
5336 traceRETURN_vTaskPlaceOnEventListRestricted();
5339 #endif /* configUSE_TIMERS */
5340 /*-----------------------------------------------------------*/
5342 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
5344 TCB_t * pxUnblockedTCB;
5347 traceENTER_xTaskRemoveFromEventList( pxEventList );
5349 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
5350 * called from a critical section within an ISR. */
5352 /* The event list is sorted in priority order, so the first in the list can
5353 * be removed as it is known to be the highest priority. Remove the TCB from
5354 * the delayed list, and add it to the ready list.
5356 * If an event is for a queue that is locked then this function will never
5357 * get called - the lock count on the queue will get modified instead. This
5358 * means exclusive access to the event list is guaranteed here.
5360 * This function assumes that a check has already been made to ensure that
5361 * pxEventList is not empty. */
5362 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5363 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5364 /* coverity[misra_c_2012_rule_11_5_violation] */
5365 pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
5366 configASSERT( pxUnblockedTCB );
5367 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5369 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5371 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5372 prvAddTaskToReadyList( pxUnblockedTCB );
5374 #if ( configUSE_TICKLESS_IDLE != 0 )
5376 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5377 * might be set to the blocked task's time out time. If the task is
5378 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5379 * normally left unchanged, because it is automatically reset to a new
5380 * value when the tick count equals xNextTaskUnblockTime. However if
5381 * tickless idling is used it might be more important to enter sleep mode
5382 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5383 * ensure it is updated at the earliest possible time. */
5384 prvResetNextTaskUnblockTime();
5390 /* The delayed and ready lists cannot be accessed, so hold this task
5391 * pending until the scheduler is resumed. */
5392 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5395 #if ( configNUMBER_OF_CORES == 1 )
5397 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5399 /* Return true if the task removed from the event list has a higher
5400 * priority than the calling task. This allows the calling task to know if
5401 * it should force a context switch now. */
5404 /* Mark that a yield is pending in case the user is not using the
5405 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5406 xYieldPendings[ 0 ] = pdTRUE;
5413 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5417 #if ( configUSE_PREEMPTION == 1 )
5419 prvYieldForTask( pxUnblockedTCB );
5421 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5426 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5428 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5430 traceRETURN_xTaskRemoveFromEventList( xReturn );
5433 /*-----------------------------------------------------------*/
5435 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5436 const TickType_t xItemValue )
5438 TCB_t * pxUnblockedTCB;
5440 traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5442 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5443 * the event flags implementation. */
5444 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5446 /* Store the new item value in the event list. */
5447 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5449 /* Remove the event list form the event flag. Interrupts do not access
5451 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5452 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5453 /* coverity[misra_c_2012_rule_11_5_violation] */
5454 pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem );
5455 configASSERT( pxUnblockedTCB );
5456 listREMOVE_ITEM( pxEventListItem );
5458 #if ( configUSE_TICKLESS_IDLE != 0 )
5460 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5461 * might be set to the blocked task's time out time. If the task is
5462 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5463 * normally left unchanged, because it is automatically reset to a new
5464 * value when the tick count equals xNextTaskUnblockTime. However if
5465 * tickless idling is used it might be more important to enter sleep mode
5466 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5467 * ensure it is updated at the earliest possible time. */
5468 prvResetNextTaskUnblockTime();
5472 /* Remove the task from the delayed list and add it to the ready list. The
5473 * scheduler is suspended so interrupts will not be accessing the ready
5475 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5476 prvAddTaskToReadyList( pxUnblockedTCB );
5478 #if ( configNUMBER_OF_CORES == 1 )
5480 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5482 /* The unblocked task has a priority above that of the calling task, so
5483 * a context switch is required. This function is called with the
5484 * scheduler suspended so xYieldPending is set so the context switch
5485 * occurs immediately that the scheduler is resumed (unsuspended). */
5486 xYieldPendings[ 0 ] = pdTRUE;
5489 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5491 #if ( configUSE_PREEMPTION == 1 )
5493 taskENTER_CRITICAL();
5495 prvYieldForTask( pxUnblockedTCB );
5497 taskEXIT_CRITICAL();
5501 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5503 traceRETURN_vTaskRemoveFromUnorderedEventList();
5505 /*-----------------------------------------------------------*/
5507 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5509 traceENTER_vTaskSetTimeOutState( pxTimeOut );
5511 configASSERT( pxTimeOut );
5512 taskENTER_CRITICAL();
5514 pxTimeOut->xOverflowCount = xNumOfOverflows;
5515 pxTimeOut->xTimeOnEntering = xTickCount;
5517 taskEXIT_CRITICAL();
5519 traceRETURN_vTaskSetTimeOutState();
5521 /*-----------------------------------------------------------*/
5523 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5525 traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5527 /* For internal use only as it does not use a critical section. */
5528 pxTimeOut->xOverflowCount = xNumOfOverflows;
5529 pxTimeOut->xTimeOnEntering = xTickCount;
5531 traceRETURN_vTaskInternalSetTimeOutState();
5533 /*-----------------------------------------------------------*/
5535 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5536 TickType_t * const pxTicksToWait )
5540 traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5542 configASSERT( pxTimeOut );
5543 configASSERT( pxTicksToWait );
5545 taskENTER_CRITICAL();
5547 /* Minor optimisation. The tick count cannot change in this block. */
5548 const TickType_t xConstTickCount = xTickCount;
5549 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5551 #if ( INCLUDE_xTaskAbortDelay == 1 )
5552 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5554 /* The delay was aborted, which is not the same as a time out,
5555 * but has the same result. */
5556 pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
5562 #if ( INCLUDE_vTaskSuspend == 1 )
5563 if( *pxTicksToWait == portMAX_DELAY )
5565 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5566 * specified is the maximum block time then the task should block
5567 * indefinitely, and therefore never time out. */
5573 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) )
5575 /* The tick count is greater than the time at which
5576 * vTaskSetTimeout() was called, but has also overflowed since
5577 * vTaskSetTimeOut() was called. It must have wrapped all the way
5578 * around and gone past again. This passed since vTaskSetTimeout()
5581 *pxTicksToWait = ( TickType_t ) 0;
5583 else if( xElapsedTime < *pxTicksToWait )
5585 /* Not a genuine timeout. Adjust parameters for time remaining. */
5586 *pxTicksToWait -= xElapsedTime;
5587 vTaskInternalSetTimeOutState( pxTimeOut );
5592 *pxTicksToWait = ( TickType_t ) 0;
5596 taskEXIT_CRITICAL();
5598 traceRETURN_xTaskCheckForTimeOut( xReturn );
5602 /*-----------------------------------------------------------*/
5604 void vTaskMissedYield( void )
5606 traceENTER_vTaskMissedYield();
5608 /* Must be called from within a critical section. */
5609 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5611 traceRETURN_vTaskMissedYield();
5613 /*-----------------------------------------------------------*/
5615 #if ( configUSE_TRACE_FACILITY == 1 )
5617 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5619 UBaseType_t uxReturn;
5620 TCB_t const * pxTCB;
5622 traceENTER_uxTaskGetTaskNumber( xTask );
5627 uxReturn = pxTCB->uxTaskNumber;
5634 traceRETURN_uxTaskGetTaskNumber( uxReturn );
5639 #endif /* configUSE_TRACE_FACILITY */
5640 /*-----------------------------------------------------------*/
5642 #if ( configUSE_TRACE_FACILITY == 1 )
5644 void vTaskSetTaskNumber( TaskHandle_t xTask,
5645 const UBaseType_t uxHandle )
5649 traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5654 pxTCB->uxTaskNumber = uxHandle;
5657 traceRETURN_vTaskSetTaskNumber();
5660 #endif /* configUSE_TRACE_FACILITY */
5661 /*-----------------------------------------------------------*/
5664 * -----------------------------------------------------------
5665 * The passive idle task.
5666 * ----------------------------------------------------------
5668 * The passive idle task is used for all the additional cores in a SMP
5669 * system. There must be only 1 active idle task and the rest are passive
5672 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5673 * language extensions. The equivalent prototype for this function is:
5675 * void prvPassiveIdleTask( void *pvParameters );
5678 #if ( configNUMBER_OF_CORES > 1 )
5679 static portTASK_FUNCTION( prvPassiveIdleTask, pvParameters )
5681 ( void ) pvParameters;
5685 for( ; configCONTROL_INFINITE_LOOP(); )
5687 #if ( configUSE_PREEMPTION == 0 )
5689 /* If we are not using preemption we keep forcing a task switch to
5690 * see if any other task has become available. If we are using
5691 * preemption we don't need to do this as any task becoming available
5692 * will automatically get the processor anyway. */
5695 #endif /* configUSE_PREEMPTION */
5697 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5699 /* When using preemption tasks of equal priority will be
5700 * timesliced. If a task that is sharing the idle priority is ready
5701 * to run then the idle task should yield before the end of the
5704 * A critical region is not required here as we are just reading from
5705 * the list, and an occasional incorrect value will not matter. If
5706 * the ready list at the idle priority contains one more task than the
5707 * number of idle tasks, which is equal to the configured numbers of cores
5708 * then a task other than the idle task is ready to execute. */
5709 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5715 mtCOVERAGE_TEST_MARKER();
5718 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5720 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
5722 /* Call the user defined function from within the idle task. This
5723 * allows the application designer to add background functionality
5724 * without the overhead of a separate task.
5726 * This hook is intended to manage core activity such as disabling cores that go idle.
5728 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5729 * CALL A FUNCTION THAT MIGHT BLOCK. */
5730 vApplicationPassiveIdleHook();
5732 #endif /* configUSE_PASSIVE_IDLE_HOOK */
5735 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5738 * -----------------------------------------------------------
5740 * ----------------------------------------------------------
5742 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5743 * language extensions. The equivalent prototype for this function is:
5745 * void prvIdleTask( void *pvParameters );
5749 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5751 /* Stop warnings. */
5752 ( void ) pvParameters;
5754 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5755 * SCHEDULER IS STARTED. **/
5757 /* In case a task that has a secure context deletes itself, in which case
5758 * the idle task is responsible for deleting the task's secure context, if
5760 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5762 #if ( configNUMBER_OF_CORES > 1 )
5764 /* SMP all cores start up in the idle task. This initial yield gets the application
5768 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5770 for( ; configCONTROL_INFINITE_LOOP(); )
5772 /* See if any tasks have deleted themselves - if so then the idle task
5773 * is responsible for freeing the deleted task's TCB and stack. */
5774 prvCheckTasksWaitingTermination();
5776 #if ( configUSE_PREEMPTION == 0 )
5778 /* If we are not using preemption we keep forcing a task switch to
5779 * see if any other task has become available. If we are using
5780 * preemption we don't need to do this as any task becoming available
5781 * will automatically get the processor anyway. */
5784 #endif /* configUSE_PREEMPTION */
5786 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5788 /* When using preemption tasks of equal priority will be
5789 * timesliced. If a task that is sharing the idle priority is ready
5790 * to run then the idle task should yield before the end of the
5793 * A critical region is not required here as we are just reading from
5794 * the list, and an occasional incorrect value will not matter. If
5795 * the ready list at the idle priority contains one more task than the
5796 * number of idle tasks, which is equal to the configured numbers of cores
5797 * then a task other than the idle task is ready to execute. */
5798 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5804 mtCOVERAGE_TEST_MARKER();
5807 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5809 #if ( configUSE_IDLE_HOOK == 1 )
5811 /* Call the user defined function from within the idle task. */
5812 vApplicationIdleHook();
5814 #endif /* configUSE_IDLE_HOOK */
5816 /* This conditional compilation should use inequality to 0, not equality
5817 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5818 * user defined low power mode implementations require
5819 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5820 #if ( configUSE_TICKLESS_IDLE != 0 )
5822 TickType_t xExpectedIdleTime;
5824 /* It is not desirable to suspend then resume the scheduler on
5825 * each iteration of the idle task. Therefore, a preliminary
5826 * test of the expected idle time is performed without the
5827 * scheduler suspended. The result here is not necessarily
5829 xExpectedIdleTime = prvGetExpectedIdleTime();
5831 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5835 /* Now the scheduler is suspended, the expected idle
5836 * time can be sampled again, and this time its value can
5838 configASSERT( xNextTaskUnblockTime >= xTickCount );
5839 xExpectedIdleTime = prvGetExpectedIdleTime();
5841 /* Define the following macro to set xExpectedIdleTime to 0
5842 * if the application does not want
5843 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5844 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5846 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5848 traceLOW_POWER_IDLE_BEGIN();
5849 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5850 traceLOW_POWER_IDLE_END();
5854 mtCOVERAGE_TEST_MARKER();
5857 ( void ) xTaskResumeAll();
5861 mtCOVERAGE_TEST_MARKER();
5864 #endif /* configUSE_TICKLESS_IDLE */
5866 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) )
5868 /* Call the user defined function from within the idle task. This
5869 * allows the application designer to add background functionality
5870 * without the overhead of a separate task.
5872 * This hook is intended to manage core activity such as disabling cores that go idle.
5874 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5875 * CALL A FUNCTION THAT MIGHT BLOCK. */
5876 vApplicationPassiveIdleHook();
5878 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) ) */
5881 /*-----------------------------------------------------------*/
5883 #if ( configUSE_TICKLESS_IDLE != 0 )
5885 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5887 #if ( INCLUDE_vTaskSuspend == 1 )
5888 /* The idle task exists in addition to the application tasks. */
5889 const UBaseType_t uxNonApplicationTasks = configNUMBER_OF_CORES;
5890 #endif /* INCLUDE_vTaskSuspend */
5892 eSleepModeStatus eReturn = eStandardSleep;
5894 traceENTER_eTaskConfirmSleepModeStatus();
5896 /* This function must be called from a critical section. */
5898 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0U )
5900 /* A task was made ready while the scheduler was suspended. */
5901 eReturn = eAbortSleep;
5903 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5905 /* A yield was pended while the scheduler was suspended. */
5906 eReturn = eAbortSleep;
5908 else if( xPendedTicks != 0U )
5910 /* A tick interrupt has already occurred but was held pending
5911 * because the scheduler is suspended. */
5912 eReturn = eAbortSleep;
5915 #if ( INCLUDE_vTaskSuspend == 1 )
5916 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5918 /* If all the tasks are in the suspended list (which might mean they
5919 * have an infinite block time rather than actually being suspended)
5920 * then it is safe to turn all clocks off and just wait for external
5922 eReturn = eNoTasksWaitingTimeout;
5924 #endif /* INCLUDE_vTaskSuspend */
5927 mtCOVERAGE_TEST_MARKER();
5930 traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5935 #endif /* configUSE_TICKLESS_IDLE */
5936 /*-----------------------------------------------------------*/
5938 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5940 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5946 traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
5948 if( ( xIndex >= 0 ) &&
5949 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5951 pxTCB = prvGetTCBFromHandle( xTaskToSet );
5952 configASSERT( pxTCB != NULL );
5953 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5956 traceRETURN_vTaskSetThreadLocalStoragePointer();
5959 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5960 /*-----------------------------------------------------------*/
5962 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5964 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5967 void * pvReturn = NULL;
5970 traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
5972 if( ( xIndex >= 0 ) &&
5973 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5975 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
5976 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
5983 traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
5988 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5989 /*-----------------------------------------------------------*/
5991 #if ( portUSING_MPU_WRAPPERS == 1 )
5993 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
5994 const MemoryRegion_t * const pxRegions )
5998 traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
6000 /* If null is passed in here then we are modifying the MPU settings of
6001 * the calling task. */
6002 pxTCB = prvGetTCBFromHandle( xTaskToModify );
6004 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
6006 traceRETURN_vTaskAllocateMPURegions();
6009 #endif /* portUSING_MPU_WRAPPERS */
6010 /*-----------------------------------------------------------*/
6012 static void prvInitialiseTaskLists( void )
6014 UBaseType_t uxPriority;
6016 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
6018 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
6021 vListInitialise( &xDelayedTaskList1 );
6022 vListInitialise( &xDelayedTaskList2 );
6023 vListInitialise( &xPendingReadyList );
6025 #if ( INCLUDE_vTaskDelete == 1 )
6027 vListInitialise( &xTasksWaitingTermination );
6029 #endif /* INCLUDE_vTaskDelete */
6031 #if ( INCLUDE_vTaskSuspend == 1 )
6033 vListInitialise( &xSuspendedTaskList );
6035 #endif /* INCLUDE_vTaskSuspend */
6037 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
6039 pxDelayedTaskList = &xDelayedTaskList1;
6040 pxOverflowDelayedTaskList = &xDelayedTaskList2;
6042 /*-----------------------------------------------------------*/
6044 static void prvCheckTasksWaitingTermination( void )
6046 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
6048 #if ( INCLUDE_vTaskDelete == 1 )
6052 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
6053 * being called too often in the idle task. */
6054 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6056 #if ( configNUMBER_OF_CORES == 1 )
6058 taskENTER_CRITICAL();
6061 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6062 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6063 /* coverity[misra_c_2012_rule_11_5_violation] */
6064 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6065 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6066 --uxCurrentNumberOfTasks;
6067 --uxDeletedTasksWaitingCleanUp;
6070 taskEXIT_CRITICAL();
6072 prvDeleteTCB( pxTCB );
6074 #else /* #if( configNUMBER_OF_CORES == 1 ) */
6078 taskENTER_CRITICAL();
6080 /* For SMP, multiple idles can be running simultaneously
6081 * and we need to check that other idles did not cleanup while we were
6082 * waiting to enter the critical section. */
6083 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6085 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6086 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6087 /* coverity[misra_c_2012_rule_11_5_violation] */
6088 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6090 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
6092 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6093 --uxCurrentNumberOfTasks;
6094 --uxDeletedTasksWaitingCleanUp;
6098 /* The TCB to be deleted still has not yet been switched out
6099 * by the scheduler, so we will just exit this loop early and
6100 * try again next time. */
6101 taskEXIT_CRITICAL();
6106 taskEXIT_CRITICAL();
6110 prvDeleteTCB( pxTCB );
6113 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
6116 #endif /* INCLUDE_vTaskDelete */
6118 /*-----------------------------------------------------------*/
6120 #if ( configUSE_TRACE_FACILITY == 1 )
6122 void vTaskGetInfo( TaskHandle_t xTask,
6123 TaskStatus_t * pxTaskStatus,
6124 BaseType_t xGetFreeStackSpace,
6129 traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
6131 /* xTask is NULL then get the state of the calling task. */
6132 pxTCB = prvGetTCBFromHandle( xTask );
6134 pxTaskStatus->xHandle = pxTCB;
6135 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
6136 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
6137 pxTaskStatus->pxStackBase = pxTCB->pxStack;
6138 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
6139 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
6140 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
6142 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
6144 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
6146 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
6150 #if ( configUSE_MUTEXES == 1 )
6152 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
6156 pxTaskStatus->uxBasePriority = 0;
6160 #if ( configGENERATE_RUN_TIME_STATS == 1 )
6162 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
6166 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
6170 /* Obtaining the task state is a little fiddly, so is only done if the
6171 * value of eState passed into this function is eInvalid - otherwise the
6172 * state is just set to whatever is passed in. */
6173 if( eState != eInvalid )
6175 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6177 pxTaskStatus->eCurrentState = eRunning;
6181 pxTaskStatus->eCurrentState = eState;
6183 #if ( INCLUDE_vTaskSuspend == 1 )
6185 /* If the task is in the suspended list then there is a
6186 * chance it is actually just blocked indefinitely - so really
6187 * it should be reported as being in the Blocked state. */
6188 if( eState == eSuspended )
6192 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
6194 pxTaskStatus->eCurrentState = eBlocked;
6198 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6202 /* The task does not appear on the event list item of
6203 * and of the RTOS objects, but could still be in the
6204 * blocked state if it is waiting on its notification
6205 * rather than waiting on an object. If not, is
6207 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
6209 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
6211 pxTaskStatus->eCurrentState = eBlocked;
6216 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
6219 ( void ) xTaskResumeAll();
6222 #endif /* INCLUDE_vTaskSuspend */
6224 /* Tasks can be in pending ready list and other state list at the
6225 * same time. These tasks are in ready state no matter what state
6226 * list the task is in. */
6227 taskENTER_CRITICAL();
6229 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
6231 pxTaskStatus->eCurrentState = eReady;
6234 taskEXIT_CRITICAL();
6239 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
6242 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
6243 * parameter is provided to allow it to be skipped. */
6244 if( xGetFreeStackSpace != pdFALSE )
6246 #if ( portSTACK_GROWTH > 0 )
6248 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
6252 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
6258 pxTaskStatus->usStackHighWaterMark = 0;
6261 traceRETURN_vTaskGetInfo();
6264 #endif /* configUSE_TRACE_FACILITY */
6265 /*-----------------------------------------------------------*/
6267 #if ( configUSE_TRACE_FACILITY == 1 )
6269 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
6273 UBaseType_t uxTask = 0;
6274 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
6275 ListItem_t * pxIterator;
6276 TCB_t * pxTCB = NULL;
6278 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
6280 /* Populate an TaskStatus_t structure within the
6281 * pxTaskStatusArray array for each task that is referenced from
6282 * pxList. See the definition of TaskStatus_t in task.h for the
6283 * meaning of each TaskStatus_t structure member. */
6284 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
6286 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6287 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6288 /* coverity[misra_c_2012_rule_11_5_violation] */
6289 pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
6291 vTaskGetInfo( ( TaskHandle_t ) pxTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
6297 mtCOVERAGE_TEST_MARKER();
6303 #endif /* configUSE_TRACE_FACILITY */
6304 /*-----------------------------------------------------------*/
6306 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
6308 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
6310 configSTACK_DEPTH_TYPE uxCount = 0U;
6312 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
6314 pucStackByte -= portSTACK_GROWTH;
6318 uxCount /= ( configSTACK_DEPTH_TYPE ) sizeof( StackType_t );
6323 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
6324 /*-----------------------------------------------------------*/
6326 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
6328 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
6329 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
6330 * user to determine the return type. It gets around the problem of the value
6331 * overflowing on 8-bit types without breaking backward compatibility for
6332 * applications that expect an 8-bit return type. */
6333 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
6336 uint8_t * pucEndOfStack;
6337 configSTACK_DEPTH_TYPE uxReturn;
6339 traceENTER_uxTaskGetStackHighWaterMark2( xTask );
6341 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
6342 * the same except for their return type. Using configSTACK_DEPTH_TYPE
6343 * allows the user to determine the return type. It gets around the
6344 * problem of the value overflowing on 8-bit types without breaking
6345 * backward compatibility for applications that expect an 8-bit return
6348 pxTCB = prvGetTCBFromHandle( xTask );
6350 #if portSTACK_GROWTH < 0
6352 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6356 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6360 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
6362 traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
6367 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
6368 /*-----------------------------------------------------------*/
6370 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
6372 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
6375 uint8_t * pucEndOfStack;
6376 UBaseType_t uxReturn;
6378 traceENTER_uxTaskGetStackHighWaterMark( xTask );
6380 pxTCB = prvGetTCBFromHandle( xTask );
6382 #if portSTACK_GROWTH < 0
6384 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6388 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6392 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
6394 traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6399 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6400 /*-----------------------------------------------------------*/
6402 #if ( INCLUDE_vTaskDelete == 1 )
6404 static void prvDeleteTCB( TCB_t * pxTCB )
6406 /* This call is required specifically for the TriCore port. It must be
6407 * above the vPortFree() calls. The call is also used by ports/demos that
6408 * want to allocate and clean RAM statically. */
6409 portCLEAN_UP_TCB( pxTCB );
6411 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6413 /* Free up the memory allocated for the task's TLS Block. */
6414 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6418 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6420 /* The task can only have been allocated dynamically - free both
6421 * the stack and TCB. */
6422 vPortFreeStack( pxTCB->pxStack );
6425 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
6427 /* The task could have been allocated statically or dynamically, so
6428 * check what was statically allocated before trying to free the
6430 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6432 /* Both the stack and TCB were allocated dynamically, so both
6434 vPortFreeStack( pxTCB->pxStack );
6437 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6439 /* Only the stack was statically allocated, so the TCB is the
6440 * only memory that must be freed. */
6445 /* Neither the stack nor the TCB were allocated dynamically, so
6446 * nothing needs to be freed. */
6447 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6448 mtCOVERAGE_TEST_MARKER();
6451 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6454 #endif /* INCLUDE_vTaskDelete */
6455 /*-----------------------------------------------------------*/
6457 static void prvResetNextTaskUnblockTime( void )
6459 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6461 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
6462 * the maximum possible value so it is extremely unlikely that the
6463 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6464 * there is an item in the delayed list. */
6465 xNextTaskUnblockTime = portMAX_DELAY;
6469 /* The new current delayed list is not empty, get the value of
6470 * the item at the head of the delayed list. This is the time at
6471 * which the task at the head of the delayed list should be removed
6472 * from the Blocked state. */
6473 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6476 /*-----------------------------------------------------------*/
6478 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6480 #if ( configNUMBER_OF_CORES == 1 )
6481 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6483 TaskHandle_t xReturn;
6485 traceENTER_xTaskGetCurrentTaskHandle();
6487 /* A critical section is not required as this is not called from
6488 * an interrupt and the current TCB will always be the same for any
6489 * individual execution thread. */
6490 xReturn = pxCurrentTCB;
6492 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6496 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6497 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6499 TaskHandle_t xReturn;
6500 UBaseType_t uxSavedInterruptStatus;
6502 traceENTER_xTaskGetCurrentTaskHandle();
6504 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6506 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6508 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6510 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6514 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6516 TaskHandle_t xTaskGetCurrentTaskHandleForCore( BaseType_t xCoreID )
6518 TaskHandle_t xReturn = NULL;
6520 traceENTER_xTaskGetCurrentTaskHandleForCore( xCoreID );
6522 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6524 #if ( configNUMBER_OF_CORES == 1 )
6525 xReturn = pxCurrentTCB;
6526 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6527 xReturn = pxCurrentTCBs[ xCoreID ];
6528 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6531 traceRETURN_xTaskGetCurrentTaskHandleForCore( xReturn );
6536 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
6537 /*-----------------------------------------------------------*/
6539 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6541 BaseType_t xTaskGetSchedulerState( void )
6545 traceENTER_xTaskGetSchedulerState();
6547 if( xSchedulerRunning == pdFALSE )
6549 xReturn = taskSCHEDULER_NOT_STARTED;
6553 #if ( configNUMBER_OF_CORES > 1 )
6554 taskENTER_CRITICAL();
6557 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6559 xReturn = taskSCHEDULER_RUNNING;
6563 xReturn = taskSCHEDULER_SUSPENDED;
6566 #if ( configNUMBER_OF_CORES > 1 )
6567 taskEXIT_CRITICAL();
6571 traceRETURN_xTaskGetSchedulerState( xReturn );
6576 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6577 /*-----------------------------------------------------------*/
6579 #if ( configUSE_MUTEXES == 1 )
6581 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6583 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6584 BaseType_t xReturn = pdFALSE;
6586 traceENTER_xTaskPriorityInherit( pxMutexHolder );
6588 /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6589 * inheritance is not applied in this scenario. */
6590 if( pxMutexHolder != NULL )
6592 /* If the holder of the mutex has a priority below the priority of
6593 * the task attempting to obtain the mutex then it will temporarily
6594 * inherit the priority of the task attempting to obtain the mutex. */
6595 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6597 /* Adjust the mutex holder state to account for its new
6598 * priority. Only reset the event list item value if the value is
6599 * not being used for anything else. */
6600 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
6602 listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority );
6606 mtCOVERAGE_TEST_MARKER();
6609 /* If the task being modified is in the ready state it will need
6610 * to be moved into a new list. */
6611 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6613 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6615 /* It is known that the task is in its ready list so
6616 * there is no need to check again and the port level
6617 * reset macro can be called directly. */
6618 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6622 mtCOVERAGE_TEST_MARKER();
6625 /* Inherit the priority before being moved into the new list. */
6626 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6627 prvAddTaskToReadyList( pxMutexHolderTCB );
6628 #if ( configNUMBER_OF_CORES > 1 )
6630 /* The priority of the task is raised. Yield for this task
6631 * if it is not running. */
6632 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6634 prvYieldForTask( pxMutexHolderTCB );
6637 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6641 /* Just inherit the priority. */
6642 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6645 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6647 /* Inheritance occurred. */
6652 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6654 /* The base priority of the mutex holder is lower than the
6655 * priority of the task attempting to take the mutex, but the
6656 * current priority of the mutex holder is not lower than the
6657 * priority of the task attempting to take the mutex.
6658 * Therefore the mutex holder must have already inherited a
6659 * priority, but inheritance would have occurred if that had
6660 * not been the case. */
6665 mtCOVERAGE_TEST_MARKER();
6671 mtCOVERAGE_TEST_MARKER();
6674 traceRETURN_xTaskPriorityInherit( xReturn );
6679 #endif /* configUSE_MUTEXES */
6680 /*-----------------------------------------------------------*/
6682 #if ( configUSE_MUTEXES == 1 )
6684 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6686 TCB_t * const pxTCB = pxMutexHolder;
6687 BaseType_t xReturn = pdFALSE;
6689 traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6691 if( pxMutexHolder != NULL )
6693 /* A task can only have an inherited priority if it holds the mutex.
6694 * If the mutex is held by a task then it cannot be given from an
6695 * interrupt, and if a mutex is given by the holding task then it must
6696 * be the running state task. */
6697 configASSERT( pxTCB == pxCurrentTCB );
6698 configASSERT( pxTCB->uxMutexesHeld );
6699 ( pxTCB->uxMutexesHeld )--;
6701 /* Has the holder of the mutex inherited the priority of another
6703 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6705 /* Only disinherit if no other mutexes are held. */
6706 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6708 /* A task can only have an inherited priority if it holds
6709 * the mutex. If the mutex is held by a task then it cannot be
6710 * given from an interrupt, and if a mutex is given by the
6711 * holding task then it must be the running state task. Remove
6712 * the holding task from the ready list. */
6713 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6715 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6719 mtCOVERAGE_TEST_MARKER();
6722 /* Disinherit the priority before adding the task into the
6723 * new ready list. */
6724 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6725 pxTCB->uxPriority = pxTCB->uxBasePriority;
6727 /* Reset the event list item value. It cannot be in use for
6728 * any other purpose if this task is running, and it must be
6729 * running to give back the mutex. */
6730 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority );
6731 prvAddTaskToReadyList( pxTCB );
6732 #if ( configNUMBER_OF_CORES > 1 )
6734 /* The priority of the task is dropped. Yield the core on
6735 * which the task is running. */
6736 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6738 prvYieldCore( pxTCB->xTaskRunState );
6741 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6743 /* Return true to indicate that a context switch is required.
6744 * This is only actually required in the corner case whereby
6745 * multiple mutexes were held and the mutexes were given back
6746 * in an order different to that in which they were taken.
6747 * If a context switch did not occur when the first mutex was
6748 * returned, even if a task was waiting on it, then a context
6749 * switch should occur when the last mutex is returned whether
6750 * a task is waiting on it or not. */
6755 mtCOVERAGE_TEST_MARKER();
6760 mtCOVERAGE_TEST_MARKER();
6765 mtCOVERAGE_TEST_MARKER();
6768 traceRETURN_xTaskPriorityDisinherit( xReturn );
6773 #endif /* configUSE_MUTEXES */
6774 /*-----------------------------------------------------------*/
6776 #if ( configUSE_MUTEXES == 1 )
6778 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6779 UBaseType_t uxHighestPriorityWaitingTask )
6781 TCB_t * const pxTCB = pxMutexHolder;
6782 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6783 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6785 traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6787 if( pxMutexHolder != NULL )
6789 /* If pxMutexHolder is not NULL then the holder must hold at least
6791 configASSERT( pxTCB->uxMutexesHeld );
6793 /* Determine the priority to which the priority of the task that
6794 * holds the mutex should be set. This will be the greater of the
6795 * holding task's base priority and the priority of the highest
6796 * priority task that is waiting to obtain the mutex. */
6797 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6799 uxPriorityToUse = uxHighestPriorityWaitingTask;
6803 uxPriorityToUse = pxTCB->uxBasePriority;
6806 /* Does the priority need to change? */
6807 if( pxTCB->uxPriority != uxPriorityToUse )
6809 /* Only disinherit if no other mutexes are held. This is a
6810 * simplification in the priority inheritance implementation. If
6811 * the task that holds the mutex is also holding other mutexes then
6812 * the other mutexes may have caused the priority inheritance. */
6813 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6815 /* If a task has timed out because it already holds the
6816 * mutex it was trying to obtain then it cannot of inherited
6817 * its own priority. */
6818 configASSERT( pxTCB != pxCurrentTCB );
6820 /* Disinherit the priority, remembering the previous
6821 * priority to facilitate determining the subject task's
6823 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6824 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6825 pxTCB->uxPriority = uxPriorityToUse;
6827 /* Only reset the event list item value if the value is not
6828 * being used for anything else. */
6829 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
6831 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse );
6835 mtCOVERAGE_TEST_MARKER();
6838 /* If the running task is not the task that holds the mutex
6839 * then the task that holds the mutex could be in either the
6840 * Ready, Blocked or Suspended states. Only remove the task
6841 * from its current state list if it is in the Ready state as
6842 * the task's priority is going to change and there is one
6843 * Ready list per priority. */
6844 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6846 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6848 /* It is known that the task is in its ready list so
6849 * there is no need to check again and the port level
6850 * reset macro can be called directly. */
6851 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6855 mtCOVERAGE_TEST_MARKER();
6858 prvAddTaskToReadyList( pxTCB );
6859 #if ( configNUMBER_OF_CORES > 1 )
6861 /* The priority of the task is dropped. Yield the core on
6862 * which the task is running. */
6863 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6865 prvYieldCore( pxTCB->xTaskRunState );
6868 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6872 mtCOVERAGE_TEST_MARKER();
6877 mtCOVERAGE_TEST_MARKER();
6882 mtCOVERAGE_TEST_MARKER();
6887 mtCOVERAGE_TEST_MARKER();
6890 traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6893 #endif /* configUSE_MUTEXES */
6894 /*-----------------------------------------------------------*/
6896 #if ( configNUMBER_OF_CORES > 1 )
6898 /* If not in a critical section then yield immediately.
6899 * Otherwise set xYieldPendings to true to wait to
6900 * yield until exiting the critical section.
6902 void vTaskYieldWithinAPI( void )
6904 traceENTER_vTaskYieldWithinAPI();
6906 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6912 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6915 traceRETURN_vTaskYieldWithinAPI();
6917 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6919 /*-----------------------------------------------------------*/
6921 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6923 void vTaskEnterCritical( void )
6925 traceENTER_vTaskEnterCritical();
6927 portDISABLE_INTERRUPTS();
6929 if( xSchedulerRunning != pdFALSE )
6931 ( pxCurrentTCB->uxCriticalNesting )++;
6933 /* This is not the interrupt safe version of the enter critical
6934 * function so assert() if it is being called from an interrupt
6935 * context. Only API functions that end in "FromISR" can be used in an
6936 * interrupt. Only assert if the critical nesting count is 1 to
6937 * protect against recursive calls if the assert function also uses a
6938 * critical section. */
6939 if( pxCurrentTCB->uxCriticalNesting == 1U )
6941 portASSERT_IF_IN_ISR();
6946 mtCOVERAGE_TEST_MARKER();
6949 traceRETURN_vTaskEnterCritical();
6952 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6953 /*-----------------------------------------------------------*/
6955 #if ( configNUMBER_OF_CORES > 1 )
6957 void vTaskEnterCritical( void )
6959 traceENTER_vTaskEnterCritical();
6961 portDISABLE_INTERRUPTS();
6963 if( xSchedulerRunning != pdFALSE )
6965 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6967 portGET_TASK_LOCK();
6971 portINCREMENT_CRITICAL_NESTING_COUNT();
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( portGET_CRITICAL_NESTING_COUNT() == 1U )
6981 portASSERT_IF_IN_ISR();
6983 if( uxSchedulerSuspended == 0U )
6985 /* The only time there would be a problem is if this is called
6986 * before a context switch and vTaskExitCritical() is called
6987 * after pxCurrentTCB changes. Therefore this should not be
6988 * used within vTaskSwitchContext(). */
6989 prvCheckForRunStateChange();
6995 mtCOVERAGE_TEST_MARKER();
6998 traceRETURN_vTaskEnterCritical();
7001 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7003 /*-----------------------------------------------------------*/
7005 #if ( configNUMBER_OF_CORES > 1 )
7007 UBaseType_t vTaskEnterCriticalFromISR( void )
7009 UBaseType_t uxSavedInterruptStatus = 0;
7011 traceENTER_vTaskEnterCriticalFromISR();
7013 if( xSchedulerRunning != pdFALSE )
7015 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
7017 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7022 portINCREMENT_CRITICAL_NESTING_COUNT();
7026 mtCOVERAGE_TEST_MARKER();
7029 traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
7031 return uxSavedInterruptStatus;
7034 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7035 /*-----------------------------------------------------------*/
7037 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
7039 void vTaskExitCritical( void )
7041 traceENTER_vTaskExitCritical();
7043 if( xSchedulerRunning != pdFALSE )
7045 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
7046 * does not match a previous call to vTaskEnterCritical(). */
7047 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
7049 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7050 * to exit critical section from ISR. */
7051 portASSERT_IF_IN_ISR();
7053 if( pxCurrentTCB->uxCriticalNesting > 0U )
7055 ( pxCurrentTCB->uxCriticalNesting )--;
7057 if( pxCurrentTCB->uxCriticalNesting == 0U )
7059 portENABLE_INTERRUPTS();
7063 mtCOVERAGE_TEST_MARKER();
7068 mtCOVERAGE_TEST_MARKER();
7073 mtCOVERAGE_TEST_MARKER();
7076 traceRETURN_vTaskExitCritical();
7079 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
7080 /*-----------------------------------------------------------*/
7082 #if ( configNUMBER_OF_CORES > 1 )
7084 void vTaskExitCritical( void )
7086 traceENTER_vTaskExitCritical();
7088 if( xSchedulerRunning != pdFALSE )
7090 /* If critical nesting count is zero then this function
7091 * does not match a previous call to vTaskEnterCritical(). */
7092 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7094 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7095 * to exit critical section from ISR. */
7096 portASSERT_IF_IN_ISR();
7098 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7100 portDECREMENT_CRITICAL_NESTING_COUNT();
7102 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7104 BaseType_t xYieldCurrentTask;
7106 /* Get the xYieldPending stats inside the critical section. */
7107 xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
7109 portRELEASE_ISR_LOCK();
7110 portRELEASE_TASK_LOCK();
7111 portENABLE_INTERRUPTS();
7113 /* When a task yields in a critical section it just sets
7114 * xYieldPending to true. So now that we have exited the
7115 * critical section check if xYieldPending is true, and
7117 if( xYieldCurrentTask != pdFALSE )
7124 mtCOVERAGE_TEST_MARKER();
7129 mtCOVERAGE_TEST_MARKER();
7134 mtCOVERAGE_TEST_MARKER();
7137 traceRETURN_vTaskExitCritical();
7140 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7141 /*-----------------------------------------------------------*/
7143 #if ( configNUMBER_OF_CORES > 1 )
7145 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
7147 traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
7149 if( xSchedulerRunning != pdFALSE )
7151 /* If critical nesting count is zero then this function
7152 * does not match a previous call to vTaskEnterCritical(). */
7153 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7155 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7157 portDECREMENT_CRITICAL_NESTING_COUNT();
7159 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7161 portRELEASE_ISR_LOCK();
7162 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
7166 mtCOVERAGE_TEST_MARKER();
7171 mtCOVERAGE_TEST_MARKER();
7176 mtCOVERAGE_TEST_MARKER();
7179 traceRETURN_vTaskExitCriticalFromISR();
7182 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7183 /*-----------------------------------------------------------*/
7185 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
7187 static char * prvWriteNameToBuffer( char * pcBuffer,
7188 const char * pcTaskName )
7192 /* Start by copying the entire string. */
7193 ( void ) strcpy( pcBuffer, pcTaskName );
7195 /* Pad the end of the string with spaces to ensure columns line up when
7197 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
7199 pcBuffer[ x ] = ' ';
7203 pcBuffer[ x ] = ( char ) 0x00;
7205 /* Return the new end of string. */
7206 return &( pcBuffer[ x ] );
7209 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
7210 /*-----------------------------------------------------------*/
7212 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
7214 void vTaskListTasks( char * pcWriteBuffer,
7215 size_t uxBufferLength )
7217 TaskStatus_t * pxTaskStatusArray;
7218 size_t uxConsumedBufferLength = 0;
7219 size_t uxCharsWrittenBySnprintf;
7220 int iSnprintfReturnValue;
7221 BaseType_t xOutputBufferFull = pdFALSE;
7222 UBaseType_t uxArraySize, x;
7225 traceENTER_vTaskListTasks( pcWriteBuffer, uxBufferLength );
7230 * This function is provided for convenience only, and is used by many
7231 * of the demo applications. Do not consider it to be part of the
7234 * vTaskListTasks() calls uxTaskGetSystemState(), then formats part of the
7235 * uxTaskGetSystemState() output into a human readable table that
7236 * displays task: names, states, priority, stack usage and task number.
7237 * Stack usage specified as the number of unused StackType_t words stack can hold
7238 * on top of stack - not the number of bytes.
7240 * vTaskListTasks() has a dependency on the snprintf() C library function that
7241 * might bloat the code size, use a lot of stack, and provide different
7242 * results on different platforms. An alternative, tiny, third party,
7243 * and limited functionality implementation of snprintf() is provided in
7244 * many of the FreeRTOS/Demo sub-directories in a file called
7245 * printf-stdarg.c (note printf-stdarg.c does not provide a full
7246 * snprintf() implementation!).
7248 * It is recommended that production systems call uxTaskGetSystemState()
7249 * directly to get access to raw stats data, rather than indirectly
7250 * through a call to vTaskListTasks().
7254 /* Make sure the write buffer does not contain a string. */
7255 *pcWriteBuffer = ( char ) 0x00;
7257 /* Take a snapshot of the number of tasks in case it changes while this
7258 * function is executing. */
7259 uxArraySize = uxCurrentNumberOfTasks;
7261 /* Allocate an array index for each task. NOTE! if
7262 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7263 * equate to NULL. */
7264 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7265 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7266 /* coverity[misra_c_2012_rule_11_5_violation] */
7267 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7269 if( pxTaskStatusArray != NULL )
7271 /* Generate the (binary) data. */
7272 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
7274 /* Create a human readable table from the binary data. */
7275 for( x = 0; x < uxArraySize; x++ )
7277 switch( pxTaskStatusArray[ x ].eCurrentState )
7280 cStatus = tskRUNNING_CHAR;
7284 cStatus = tskREADY_CHAR;
7288 cStatus = tskBLOCKED_CHAR;
7292 cStatus = tskSUSPENDED_CHAR;
7296 cStatus = tskDELETED_CHAR;
7299 case eInvalid: /* Fall through. */
7300 default: /* Should not get here, but it is included
7301 * to prevent static checking errors. */
7302 cStatus = ( char ) 0x00;
7306 /* Is there enough space in the buffer to hold task name? */
7307 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7309 /* Write the task name to the string, padding with spaces so it
7310 * can be printed in tabular form more easily. */
7311 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7312 /* Do not count the terminating null character. */
7313 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7315 /* Is there space left in the buffer? -1 is done because snprintf
7316 * writes a terminating null character. So we are essentially
7317 * checking if the buffer has space to write at least one non-null
7319 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7321 /* Write the rest of the string. */
7322 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
7323 /* MISRA Ref 21.6.1 [snprintf for utility] */
7324 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7325 /* coverity[misra_c_2012_rule_21_6_violation] */
7326 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7327 uxBufferLength - uxConsumedBufferLength,
7328 "\t%c\t%u\t%u\t%u\t0x%x\r\n",
7330 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7331 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7332 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber,
7333 ( unsigned int ) pxTaskStatusArray[ x ].uxCoreAffinityMask );
7334 #else /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7335 /* MISRA Ref 21.6.1 [snprintf for utility] */
7336 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7337 /* coverity[misra_c_2012_rule_21_6_violation] */
7338 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7339 uxBufferLength - uxConsumedBufferLength,
7340 "\t%c\t%u\t%u\t%u\r\n",
7342 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7343 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7344 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber );
7345 #endif /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7346 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7348 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7349 pcWriteBuffer += uxCharsWrittenBySnprintf;
7353 xOutputBufferFull = pdTRUE;
7358 xOutputBufferFull = pdTRUE;
7361 if( xOutputBufferFull == pdTRUE )
7367 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7368 * is 0 then vPortFree() will be #defined to nothing. */
7369 vPortFree( pxTaskStatusArray );
7373 mtCOVERAGE_TEST_MARKER();
7376 traceRETURN_vTaskListTasks();
7379 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7380 /*----------------------------------------------------------*/
7382 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
7384 void vTaskGetRunTimeStatistics( char * pcWriteBuffer,
7385 size_t uxBufferLength )
7387 TaskStatus_t * pxTaskStatusArray;
7388 size_t uxConsumedBufferLength = 0;
7389 size_t uxCharsWrittenBySnprintf;
7390 int iSnprintfReturnValue;
7391 BaseType_t xOutputBufferFull = pdFALSE;
7392 UBaseType_t uxArraySize, x;
7393 configRUN_TIME_COUNTER_TYPE ulTotalTime = 0;
7394 configRUN_TIME_COUNTER_TYPE ulStatsAsPercentage;
7396 traceENTER_vTaskGetRunTimeStatistics( pcWriteBuffer, uxBufferLength );
7401 * This function is provided for convenience only, and is used by many
7402 * of the demo applications. Do not consider it to be part of the
7405 * vTaskGetRunTimeStatistics() calls uxTaskGetSystemState(), then formats part
7406 * of the uxTaskGetSystemState() output into a human readable table that
7407 * displays the amount of time each task has spent in the Running state
7408 * in both absolute and percentage terms.
7410 * vTaskGetRunTimeStatistics() has a dependency on the snprintf() C library
7411 * function that might bloat the code size, use a lot of stack, and
7412 * provide different results on different platforms. An alternative,
7413 * tiny, third party, and limited functionality implementation of
7414 * snprintf() is provided in many of the FreeRTOS/Demo sub-directories in
7415 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
7416 * a full snprintf() implementation!).
7418 * It is recommended that production systems call uxTaskGetSystemState()
7419 * directly to get access to raw stats data, rather than indirectly
7420 * through a call to vTaskGetRunTimeStatistics().
7423 /* Make sure the write buffer does not contain a string. */
7424 *pcWriteBuffer = ( char ) 0x00;
7426 /* Take a snapshot of the number of tasks in case it changes while this
7427 * function is executing. */
7428 uxArraySize = uxCurrentNumberOfTasks;
7430 /* Allocate an array index for each task. NOTE! If
7431 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7432 * equate to NULL. */
7433 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7434 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7435 /* coverity[misra_c_2012_rule_11_5_violation] */
7436 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7438 if( pxTaskStatusArray != NULL )
7440 /* Generate the (binary) data. */
7441 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
7443 /* For percentage calculations. */
7444 ulTotalTime /= ( ( configRUN_TIME_COUNTER_TYPE ) 100U );
7446 /* Avoid divide by zero errors. */
7447 if( ulTotalTime > 0U )
7449 /* Create a human readable table from the binary data. */
7450 for( x = 0; x < uxArraySize; x++ )
7452 /* What percentage of the total run time has the task used?
7453 * This will always be rounded down to the nearest integer.
7454 * ulTotalRunTime has already been divided by 100. */
7455 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
7457 /* Is there enough space in the buffer to hold task name? */
7458 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7460 /* Write the task name to the string, padding with
7461 * spaces so it can be printed in tabular form more
7463 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7464 /* Do not count the terminating null character. */
7465 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7467 /* Is there space left in the buffer? -1 is done because snprintf
7468 * writes a terminating null character. So we are essentially
7469 * checking if the buffer has space to write at least one non-null
7471 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7473 if( ulStatsAsPercentage > 0U )
7475 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7477 /* MISRA Ref 21.6.1 [snprintf for utility] */
7478 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7479 /* coverity[misra_c_2012_rule_21_6_violation] */
7480 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7481 uxBufferLength - uxConsumedBufferLength,
7482 "\t%lu\t\t%lu%%\r\n",
7483 pxTaskStatusArray[ x ].ulRunTimeCounter,
7484 ulStatsAsPercentage );
7486 #else /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7488 /* sizeof( int ) == sizeof( long ) so a smaller
7489 * printf() library can be used. */
7490 /* MISRA Ref 21.6.1 [snprintf for utility] */
7491 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7492 /* coverity[misra_c_2012_rule_21_6_violation] */
7493 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7494 uxBufferLength - uxConsumedBufferLength,
7496 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter,
7497 ( unsigned int ) ulStatsAsPercentage );
7499 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7503 /* If the percentage is zero here then the task has
7504 * consumed less than 1% of the total run time. */
7505 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7507 /* MISRA Ref 21.6.1 [snprintf for utility] */
7508 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7509 /* coverity[misra_c_2012_rule_21_6_violation] */
7510 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7511 uxBufferLength - uxConsumedBufferLength,
7512 "\t%lu\t\t<1%%\r\n",
7513 pxTaskStatusArray[ x ].ulRunTimeCounter );
7517 /* sizeof( int ) == sizeof( long ) so a smaller
7518 * printf() library can be used. */
7519 /* MISRA Ref 21.6.1 [snprintf for utility] */
7520 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7521 /* coverity[misra_c_2012_rule_21_6_violation] */
7522 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7523 uxBufferLength - uxConsumedBufferLength,
7525 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter );
7527 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7530 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7531 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7532 pcWriteBuffer += uxCharsWrittenBySnprintf;
7536 xOutputBufferFull = pdTRUE;
7541 xOutputBufferFull = pdTRUE;
7544 if( xOutputBufferFull == pdTRUE )
7552 mtCOVERAGE_TEST_MARKER();
7555 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7556 * is 0 then vPortFree() will be #defined to nothing. */
7557 vPortFree( pxTaskStatusArray );
7561 mtCOVERAGE_TEST_MARKER();
7564 traceRETURN_vTaskGetRunTimeStatistics();
7567 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7568 /*-----------------------------------------------------------*/
7570 TickType_t uxTaskResetEventItemValue( void )
7572 TickType_t uxReturn;
7574 traceENTER_uxTaskResetEventItemValue();
7576 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7578 /* Reset the event list item to its normal value - so it can be used with
7579 * queues and semaphores. */
7580 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) );
7582 traceRETURN_uxTaskResetEventItemValue( uxReturn );
7586 /*-----------------------------------------------------------*/
7588 #if ( configUSE_MUTEXES == 1 )
7590 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7594 traceENTER_pvTaskIncrementMutexHeldCount();
7596 pxTCB = pxCurrentTCB;
7598 /* If xSemaphoreCreateMutex() is called before any tasks have been created
7599 * then pxCurrentTCB will be NULL. */
7602 ( pxTCB->uxMutexesHeld )++;
7605 traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7610 #endif /* configUSE_MUTEXES */
7611 /*-----------------------------------------------------------*/
7613 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7615 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7616 BaseType_t xClearCountOnExit,
7617 TickType_t xTicksToWait )
7620 BaseType_t xAlreadyYielded, xShouldBlock = pdFALSE;
7622 traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7624 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7626 /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
7627 * non-deterministic operation. */
7630 /* We MUST enter a critical section to atomically check if a notification
7631 * has occurred and set the flag to indicate that we are waiting for
7632 * a notification. If we do not do so, a notification sent from an ISR
7634 taskENTER_CRITICAL();
7636 /* Only block if the notification count is not already non-zero. */
7637 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0U )
7639 /* Mark this task as waiting for a notification. */
7640 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7642 if( xTicksToWait > ( TickType_t ) 0 )
7644 xShouldBlock = pdTRUE;
7648 mtCOVERAGE_TEST_MARKER();
7653 mtCOVERAGE_TEST_MARKER();
7656 taskEXIT_CRITICAL();
7658 /* We are now out of the critical section but the scheduler is still
7659 * suspended, so we are safe to do non-deterministic operations such
7660 * as prvAddCurrentTaskToDelayedList. */
7661 if( xShouldBlock == pdTRUE )
7663 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7664 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7668 mtCOVERAGE_TEST_MARKER();
7671 xAlreadyYielded = xTaskResumeAll();
7673 /* Force a reschedule if xTaskResumeAll has not already done so. */
7674 if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
7676 taskYIELD_WITHIN_API();
7680 mtCOVERAGE_TEST_MARKER();
7683 taskENTER_CRITICAL();
7685 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7686 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7688 if( ulReturn != 0U )
7690 if( xClearCountOnExit != pdFALSE )
7692 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ( uint32_t ) 0U;
7696 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7701 mtCOVERAGE_TEST_MARKER();
7704 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7706 taskEXIT_CRITICAL();
7708 traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7713 #endif /* configUSE_TASK_NOTIFICATIONS */
7714 /*-----------------------------------------------------------*/
7716 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7718 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7719 uint32_t ulBitsToClearOnEntry,
7720 uint32_t ulBitsToClearOnExit,
7721 uint32_t * pulNotificationValue,
7722 TickType_t xTicksToWait )
7724 BaseType_t xReturn, xAlreadyYielded, xShouldBlock = pdFALSE;
7726 traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7728 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7730 /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
7731 * non-deterministic operation. */
7734 /* We MUST enter a critical section to atomically check and update the
7735 * task notification value. If we do not do so, a notification from
7736 * an ISR will get lost. */
7737 taskENTER_CRITICAL();
7739 /* Only block if a notification is not already pending. */
7740 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7742 /* Clear bits in the task's notification value as bits may get
7743 * set by the notifying task or interrupt. This can be used
7744 * to clear the value to zero. */
7745 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7747 /* Mark this task as waiting for a notification. */
7748 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7750 if( xTicksToWait > ( TickType_t ) 0 )
7752 xShouldBlock = pdTRUE;
7756 mtCOVERAGE_TEST_MARKER();
7761 mtCOVERAGE_TEST_MARKER();
7764 taskEXIT_CRITICAL();
7766 /* We are now out of the critical section but the scheduler is still
7767 * suspended, so we are safe to do non-deterministic operations such
7768 * as prvAddCurrentTaskToDelayedList. */
7769 if( xShouldBlock == pdTRUE )
7771 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7772 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7776 mtCOVERAGE_TEST_MARKER();
7779 xAlreadyYielded = xTaskResumeAll();
7781 /* Force a reschedule if xTaskResumeAll has not already done so. */
7782 if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
7784 taskYIELD_WITHIN_API();
7788 mtCOVERAGE_TEST_MARKER();
7791 taskENTER_CRITICAL();
7793 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7795 if( pulNotificationValue != NULL )
7797 /* Output the current notification value, which may or may not
7799 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7802 /* If ucNotifyValue is set then either the task never entered the
7803 * blocked state (because a notification was already pending) or the
7804 * task unblocked because of a notification. Otherwise the task
7805 * unblocked because of a timeout. */
7806 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7808 /* A notification was not received. */
7813 /* A notification was already pending or a notification was
7814 * received while the task was waiting. */
7815 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7819 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7821 taskEXIT_CRITICAL();
7823 traceRETURN_xTaskGenericNotifyWait( xReturn );
7828 #endif /* configUSE_TASK_NOTIFICATIONS */
7829 /*-----------------------------------------------------------*/
7831 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7833 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7834 UBaseType_t uxIndexToNotify,
7836 eNotifyAction eAction,
7837 uint32_t * pulPreviousNotificationValue )
7840 BaseType_t xReturn = pdPASS;
7841 uint8_t ucOriginalNotifyState;
7843 traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7845 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7846 configASSERT( xTaskToNotify );
7847 pxTCB = xTaskToNotify;
7849 taskENTER_CRITICAL();
7851 if( pulPreviousNotificationValue != NULL )
7853 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7856 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7858 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7863 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7867 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7870 case eSetValueWithOverwrite:
7871 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7874 case eSetValueWithoutOverwrite:
7876 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7878 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7882 /* The value could not be written to the task. */
7890 /* The task is being notified without its notify value being
7896 /* Should not get here if all enums are handled.
7897 * Artificially force an assert by testing a value the
7898 * compiler can't assume is const. */
7899 configASSERT( xTickCount == ( TickType_t ) 0 );
7904 traceTASK_NOTIFY( uxIndexToNotify );
7906 /* If the task is in the blocked state specifically to wait for a
7907 * notification then unblock it now. */
7908 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7910 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7911 prvAddTaskToReadyList( pxTCB );
7913 /* The task should not have been on an event list. */
7914 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7916 #if ( configUSE_TICKLESS_IDLE != 0 )
7918 /* If a task is blocked waiting for a notification then
7919 * xNextTaskUnblockTime might be set to the blocked task's time
7920 * out time. If the task is unblocked for a reason other than
7921 * a timeout xNextTaskUnblockTime is normally left unchanged,
7922 * because it will automatically get reset to a new value when
7923 * the tick count equals xNextTaskUnblockTime. However if
7924 * tickless idling is used it might be more important to enter
7925 * sleep mode at the earliest possible time - so reset
7926 * xNextTaskUnblockTime here to ensure it is updated at the
7927 * earliest possible time. */
7928 prvResetNextTaskUnblockTime();
7932 /* Check if the notified task has a priority above the currently
7933 * executing task. */
7934 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7938 mtCOVERAGE_TEST_MARKER();
7941 taskEXIT_CRITICAL();
7943 traceRETURN_xTaskGenericNotify( xReturn );
7948 #endif /* configUSE_TASK_NOTIFICATIONS */
7949 /*-----------------------------------------------------------*/
7951 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7953 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
7954 UBaseType_t uxIndexToNotify,
7956 eNotifyAction eAction,
7957 uint32_t * pulPreviousNotificationValue,
7958 BaseType_t * pxHigherPriorityTaskWoken )
7961 uint8_t ucOriginalNotifyState;
7962 BaseType_t xReturn = pdPASS;
7963 UBaseType_t uxSavedInterruptStatus;
7965 traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
7967 configASSERT( xTaskToNotify );
7968 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7970 /* RTOS ports that support interrupt nesting have the concept of a
7971 * maximum system call (or maximum API call) interrupt priority.
7972 * Interrupts that are above the maximum system call priority are keep
7973 * permanently enabled, even when the RTOS kernel is in a critical section,
7974 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7975 * is defined in FreeRTOSConfig.h then
7976 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7977 * failure if a FreeRTOS API function is called from an interrupt that has
7978 * been assigned a priority above the configured maximum system call
7979 * priority. Only FreeRTOS functions that end in FromISR can be called
7980 * from interrupts that have been assigned a priority at or (logically)
7981 * below the maximum system call interrupt priority. FreeRTOS maintains a
7982 * separate interrupt safe API to ensure interrupt entry is as fast and as
7983 * simple as possible. More information (albeit Cortex-M specific) is
7984 * provided on the following link:
7985 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7986 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7988 pxTCB = xTaskToNotify;
7990 /* MISRA Ref 4.7.1 [Return value shall be checked] */
7991 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
7992 /* coverity[misra_c_2012_directive_4_7_violation] */
7993 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
7995 if( pulPreviousNotificationValue != NULL )
7997 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
8000 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8001 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8006 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
8010 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8013 case eSetValueWithOverwrite:
8014 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8017 case eSetValueWithoutOverwrite:
8019 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
8021 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8025 /* The value could not be written to the task. */
8033 /* The task is being notified without its notify value being
8039 /* Should not get here if all enums are handled.
8040 * Artificially force an assert by testing a value the
8041 * compiler can't assume is const. */
8042 configASSERT( xTickCount == ( TickType_t ) 0 );
8046 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
8048 /* If the task is in the blocked state specifically to wait for a
8049 * notification then unblock it now. */
8050 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8052 /* The task should not have been on an event list. */
8053 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8055 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8057 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8058 prvAddTaskToReadyList( pxTCB );
8062 /* The delayed and ready lists cannot be accessed, so hold
8063 * this task pending until the scheduler is resumed. */
8064 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8067 #if ( configNUMBER_OF_CORES == 1 )
8069 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8071 /* The notified task has a priority above the currently
8072 * executing task so a yield is required. */
8073 if( pxHigherPriorityTaskWoken != NULL )
8075 *pxHigherPriorityTaskWoken = pdTRUE;
8078 /* Mark that a yield is pending in case the user is not
8079 * using the "xHigherPriorityTaskWoken" parameter to an ISR
8080 * safe FreeRTOS function. */
8081 xYieldPendings[ 0 ] = pdTRUE;
8085 mtCOVERAGE_TEST_MARKER();
8088 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8090 #if ( configUSE_PREEMPTION == 1 )
8092 prvYieldForTask( pxTCB );
8094 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8096 if( pxHigherPriorityTaskWoken != NULL )
8098 *pxHigherPriorityTaskWoken = pdTRUE;
8102 #endif /* if ( configUSE_PREEMPTION == 1 ) */
8104 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8107 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8109 traceRETURN_xTaskGenericNotifyFromISR( xReturn );
8114 #endif /* configUSE_TASK_NOTIFICATIONS */
8115 /*-----------------------------------------------------------*/
8117 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8119 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
8120 UBaseType_t uxIndexToNotify,
8121 BaseType_t * pxHigherPriorityTaskWoken )
8124 uint8_t ucOriginalNotifyState;
8125 UBaseType_t uxSavedInterruptStatus;
8127 traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
8129 configASSERT( xTaskToNotify );
8130 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8132 /* RTOS ports that support interrupt nesting have the concept of a
8133 * maximum system call (or maximum API call) interrupt priority.
8134 * Interrupts that are above the maximum system call priority are keep
8135 * permanently enabled, even when the RTOS kernel is in a critical section,
8136 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
8137 * is defined in FreeRTOSConfig.h then
8138 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
8139 * failure if a FreeRTOS API function is called from an interrupt that has
8140 * been assigned a priority above the configured maximum system call
8141 * priority. Only FreeRTOS functions that end in FromISR can be called
8142 * from interrupts that have been assigned a priority at or (logically)
8143 * below the maximum system call interrupt priority. FreeRTOS maintains a
8144 * separate interrupt safe API to ensure interrupt entry is as fast and as
8145 * simple as possible. More information (albeit Cortex-M specific) is
8146 * provided on the following link:
8147 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
8148 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
8150 pxTCB = xTaskToNotify;
8152 /* MISRA Ref 4.7.1 [Return value shall be checked] */
8153 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
8154 /* coverity[misra_c_2012_directive_4_7_violation] */
8155 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
8157 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8158 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8160 /* 'Giving' is equivalent to incrementing a count in a counting
8162 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8164 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
8166 /* If the task is in the blocked state specifically to wait for a
8167 * notification then unblock it now. */
8168 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8170 /* The task should not have been on an event list. */
8171 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8173 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8175 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8176 prvAddTaskToReadyList( pxTCB );
8180 /* The delayed and ready lists cannot be accessed, so hold
8181 * this task pending until the scheduler is resumed. */
8182 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8185 #if ( configNUMBER_OF_CORES == 1 )
8187 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8189 /* The notified task has a priority above the currently
8190 * executing task so a yield is required. */
8191 if( pxHigherPriorityTaskWoken != NULL )
8193 *pxHigherPriorityTaskWoken = pdTRUE;
8196 /* Mark that a yield is pending in case the user is not
8197 * using the "xHigherPriorityTaskWoken" parameter in an ISR
8198 * safe FreeRTOS function. */
8199 xYieldPendings[ 0 ] = pdTRUE;
8203 mtCOVERAGE_TEST_MARKER();
8206 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8208 #if ( configUSE_PREEMPTION == 1 )
8210 prvYieldForTask( pxTCB );
8212 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8214 if( pxHigherPriorityTaskWoken != NULL )
8216 *pxHigherPriorityTaskWoken = pdTRUE;
8220 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
8222 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8225 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8227 traceRETURN_vTaskGenericNotifyGiveFromISR();
8230 #endif /* configUSE_TASK_NOTIFICATIONS */
8231 /*-----------------------------------------------------------*/
8233 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8235 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
8236 UBaseType_t uxIndexToClear )
8241 traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
8243 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8245 /* If null is passed in here then it is the calling task that is having
8246 * its notification state cleared. */
8247 pxTCB = prvGetTCBFromHandle( xTask );
8249 taskENTER_CRITICAL();
8251 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
8253 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
8261 taskEXIT_CRITICAL();
8263 traceRETURN_xTaskGenericNotifyStateClear( xReturn );
8268 #endif /* configUSE_TASK_NOTIFICATIONS */
8269 /*-----------------------------------------------------------*/
8271 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8273 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
8274 UBaseType_t uxIndexToClear,
8275 uint32_t ulBitsToClear )
8280 traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
8282 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8284 /* If null is passed in here then it is the calling task that is having
8285 * its notification state cleared. */
8286 pxTCB = prvGetTCBFromHandle( xTask );
8288 taskENTER_CRITICAL();
8290 /* Return the notification as it was before the bits were cleared,
8291 * then clear the bit mask. */
8292 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
8293 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
8295 taskEXIT_CRITICAL();
8297 traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
8302 #endif /* configUSE_TASK_NOTIFICATIONS */
8303 /*-----------------------------------------------------------*/
8305 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8307 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
8311 traceENTER_ulTaskGetRunTimeCounter( xTask );
8313 pxTCB = prvGetTCBFromHandle( xTask );
8315 traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
8317 return pxTCB->ulRunTimeCounter;
8320 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8321 /*-----------------------------------------------------------*/
8323 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8325 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
8328 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8330 traceENTER_ulTaskGetRunTimePercent( xTask );
8332 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
8334 /* For percentage calculations. */
8335 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8337 /* Avoid divide by zero errors. */
8338 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8340 pxTCB = prvGetTCBFromHandle( xTask );
8341 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
8348 traceRETURN_ulTaskGetRunTimePercent( ulReturn );
8353 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8354 /*-----------------------------------------------------------*/
8356 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8358 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
8360 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
8363 traceENTER_ulTaskGetIdleRunTimeCounter();
8365 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8367 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8370 traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
8375 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8376 /*-----------------------------------------------------------*/
8378 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8380 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
8382 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8383 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
8386 traceENTER_ulTaskGetIdleRunTimePercent();
8388 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
8390 /* For percentage calculations. */
8391 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8393 /* Avoid divide by zero errors. */
8394 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8396 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8398 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8401 ulReturn = ulRunTimeCounter / ulTotalTime;
8408 traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
8413 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8414 /*-----------------------------------------------------------*/
8416 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
8417 const BaseType_t xCanBlockIndefinitely )
8419 TickType_t xTimeToWake;
8420 const TickType_t xConstTickCount = xTickCount;
8421 List_t * const pxDelayedList = pxDelayedTaskList;
8422 List_t * const pxOverflowDelayedList = pxOverflowDelayedTaskList;
8424 #if ( INCLUDE_xTaskAbortDelay == 1 )
8426 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
8427 * reset to pdFALSE so it can be detected as having been set to pdTRUE
8428 * when the task leaves the Blocked state. */
8429 pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
8433 /* Remove the task from the ready list before adding it to the blocked list
8434 * as the same list item is used for both lists. */
8435 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
8437 /* The current task must be in a ready list, so there is no need to
8438 * check, and the port reset macro can be called directly. */
8439 portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
8443 mtCOVERAGE_TEST_MARKER();
8446 #if ( INCLUDE_vTaskSuspend == 1 )
8448 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
8450 /* Add the task to the suspended task list instead of a delayed task
8451 * list to ensure it is not woken by a timing event. It will block
8453 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
8457 /* Calculate the time at which the task should be woken if the event
8458 * does not occur. This may overflow but this doesn't matter, the
8459 * kernel will manage it correctly. */
8460 xTimeToWake = xConstTickCount + xTicksToWait;
8462 /* The list item will be inserted in wake time order. */
8463 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8465 if( xTimeToWake < xConstTickCount )
8467 /* Wake time has overflowed. Place this item in the overflow
8469 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8470 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8474 /* The wake time has not overflowed, so the current block list
8476 traceMOVED_TASK_TO_DELAYED_LIST();
8477 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8479 /* If the task entering the blocked state was placed at the
8480 * head of the list of blocked tasks then xNextTaskUnblockTime
8481 * needs to be updated too. */
8482 if( xTimeToWake < xNextTaskUnblockTime )
8484 xNextTaskUnblockTime = xTimeToWake;
8488 mtCOVERAGE_TEST_MARKER();
8493 #else /* INCLUDE_vTaskSuspend */
8495 /* Calculate the time at which the task should be woken if the event
8496 * does not occur. This may overflow but this doesn't matter, the kernel
8497 * will manage it correctly. */
8498 xTimeToWake = xConstTickCount + xTicksToWait;
8500 /* The list item will be inserted in wake time order. */
8501 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8503 if( xTimeToWake < xConstTickCount )
8505 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8506 /* Wake time has overflowed. Place this item in the overflow list. */
8507 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8511 traceMOVED_TASK_TO_DELAYED_LIST();
8512 /* The wake time has not overflowed, so the current block list is used. */
8513 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8515 /* If the task entering the blocked state was placed at the head of the
8516 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
8518 if( xTimeToWake < xNextTaskUnblockTime )
8520 xNextTaskUnblockTime = xTimeToWake;
8524 mtCOVERAGE_TEST_MARKER();
8528 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
8529 ( void ) xCanBlockIndefinitely;
8531 #endif /* INCLUDE_vTaskSuspend */
8533 /*-----------------------------------------------------------*/
8535 #if ( portUSING_MPU_WRAPPERS == 1 )
8537 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
8541 traceENTER_xTaskGetMPUSettings( xTask );
8543 pxTCB = prvGetTCBFromHandle( xTask );
8545 traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
8547 return &( pxTCB->xMPUSettings );
8550 #endif /* portUSING_MPU_WRAPPERS */
8551 /*-----------------------------------------------------------*/
8553 /* Code below here allows additional code to be inserted into this source file,
8554 * especially where access to file scope functions and data is needed (for example
8555 * when performing module tests). */
8557 #ifdef FREERTOS_MODULE_TEST
8558 #include "tasks_test_access_functions.h"
8562 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
8564 #include "freertos_tasks_c_additions.h"
8566 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8567 static void freertos_tasks_c_additions_init( void )
8569 FREERTOS_TASKS_C_ADDITIONS_INIT();
8573 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8574 /*-----------------------------------------------------------*/
8576 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8579 * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8580 * to provide the memory that is used by the Idle task. It is used when
8581 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8582 * it's own implementation of vApplicationGetIdleTaskMemory by setting
8583 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8585 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8586 StackType_t ** ppxIdleTaskStackBuffer,
8587 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize )
8589 static StaticTask_t xIdleTaskTCB;
8590 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8592 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8593 *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8594 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8597 #if ( configNUMBER_OF_CORES > 1 )
8599 void vApplicationGetPassiveIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8600 StackType_t ** ppxIdleTaskStackBuffer,
8601 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize,
8602 BaseType_t xPassiveIdleTaskIndex )
8604 static StaticTask_t xIdleTaskTCBs[ configNUMBER_OF_CORES - 1 ];
8605 static StackType_t uxIdleTaskStacks[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
8607 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCBs[ xPassiveIdleTaskIndex ] );
8608 *ppxIdleTaskStackBuffer = &( uxIdleTaskStacks[ xPassiveIdleTaskIndex ][ 0 ] );
8609 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8612 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
8614 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8615 /*-----------------------------------------------------------*/
8617 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8620 * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8621 * to provide the memory that is used by the Timer service task. It is used when
8622 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8623 * it's own implementation of vApplicationGetTimerTaskMemory by setting
8624 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8626 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8627 StackType_t ** ppxTimerTaskStackBuffer,
8628 configSTACK_DEPTH_TYPE * puxTimerTaskStackSize )
8630 static StaticTask_t xTimerTaskTCB;
8631 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8633 *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8634 *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8635 *puxTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8638 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8639 /*-----------------------------------------------------------*/
8642 * Reset the state in this file. This state is normally initialized at start up.
8643 * This function must be called by the application before restarting the
8646 void vTaskResetState( void )
8650 /* Task control block. */
8651 #if ( configNUMBER_OF_CORES == 1 )
8653 pxCurrentTCB = NULL;
8655 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8657 #if ( INCLUDE_vTaskDelete == 1 )
8659 uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
8661 #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
8663 #if ( configUSE_POSIX_ERRNO == 1 )
8667 #endif /* #if ( configUSE_POSIX_ERRNO == 1 ) */
8669 /* Other file private variables. */
8670 uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
8671 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
8672 uxTopReadyPriority = tskIDLE_PRIORITY;
8673 xSchedulerRunning = pdFALSE;
8674 xPendedTicks = ( TickType_t ) 0U;
8676 for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
8678 xYieldPendings[ xCoreID ] = pdFALSE;
8681 xNumOfOverflows = ( BaseType_t ) 0;
8682 uxTaskNumber = ( UBaseType_t ) 0U;
8683 xNextTaskUnblockTime = ( TickType_t ) 0U;
8685 uxSchedulerSuspended = ( UBaseType_t ) 0U;
8687 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8689 for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
8691 ulTaskSwitchedInTime[ xCoreID ] = 0U;
8692 ulTotalRunTime[ xCoreID ] = 0U;
8695 #endif /* #if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8697 /*-----------------------------------------------------------*/