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 traceSTARTING_SCHEDULER( xIdleTaskHandles );
3737 /* Setting up the timer tick is hardware specific and thus in the
3738 * portable interface. */
3740 /* The return value for xPortStartScheduler is not required
3741 * hence using a void datatype. */
3742 ( void ) xPortStartScheduler();
3744 /* In most cases, xPortStartScheduler() will not return. If it
3745 * returns pdTRUE then there was not enough heap memory available
3746 * to create either the Idle or the Timer task. If it returned
3747 * pdFALSE, then the application called xTaskEndScheduler().
3748 * Most ports don't implement xTaskEndScheduler() as there is
3749 * nothing to return to. */
3753 /* This line will only be reached if the kernel could not be started,
3754 * because there was not enough FreeRTOS heap to create the idle task
3755 * or the timer task. */
3756 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3759 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3760 * meaning xIdleTaskHandles are not used anywhere else. */
3761 ( void ) xIdleTaskHandles;
3763 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3764 * from getting optimized out as it is no longer used by the kernel. */
3765 ( void ) uxTopUsedPriority;
3767 traceRETURN_vTaskStartScheduler();
3769 /*-----------------------------------------------------------*/
3771 void vTaskEndScheduler( void )
3773 traceENTER_vTaskEndScheduler();
3775 #if ( INCLUDE_vTaskDelete == 1 )
3779 #if ( configUSE_TIMERS == 1 )
3781 /* Delete the timer task created by the kernel. */
3782 vTaskDelete( xTimerGetTimerDaemonTaskHandle() );
3784 #endif /* #if ( configUSE_TIMERS == 1 ) */
3786 /* Delete Idle tasks created by the kernel.*/
3787 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3789 vTaskDelete( xIdleTaskHandles[ xCoreID ] );
3792 /* Idle task is responsible for reclaiming the resources of the tasks in
3793 * xTasksWaitingTermination list. Since the idle task is now deleted and
3794 * no longer going to run, we need to reclaim resources of all the tasks
3795 * in the xTasksWaitingTermination list. */
3796 prvCheckTasksWaitingTermination();
3798 #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
3800 /* Stop the scheduler interrupts and call the portable scheduler end
3801 * routine so the original ISRs can be restored if necessary. The port
3802 * layer must ensure interrupts enable bit is left in the correct state. */
3803 portDISABLE_INTERRUPTS();
3804 xSchedulerRunning = pdFALSE;
3806 /* This function must be called from a task and the application is
3807 * responsible for deleting that task after the scheduler is stopped. */
3808 vPortEndScheduler();
3810 traceRETURN_vTaskEndScheduler();
3812 /*----------------------------------------------------------*/
3814 void vTaskSuspendAll( void )
3816 traceENTER_vTaskSuspendAll();
3818 #if ( configNUMBER_OF_CORES == 1 )
3820 /* A critical section is not required as the variable is of type
3821 * BaseType_t. Please read Richard Barry's reply in the following link to a
3822 * post in the FreeRTOS support forum before reporting this as a bug! -
3823 * https://goo.gl/wu4acr */
3825 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3826 * do not otherwise exhibit real time behaviour. */
3827 portSOFTWARE_BARRIER();
3829 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3830 * is used to allow calls to vTaskSuspendAll() to nest. */
3831 uxSchedulerSuspended = ( UBaseType_t ) ( uxSchedulerSuspended + 1U );
3833 /* Enforces ordering for ports and optimised compilers that may otherwise place
3834 * the above increment elsewhere. */
3835 portMEMORY_BARRIER();
3837 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3839 UBaseType_t ulState;
3841 /* This must only be called from within a task. */
3842 portASSERT_IF_IN_ISR();
3844 if( xSchedulerRunning != pdFALSE )
3846 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3847 * We must disable interrupts before we grab the locks in the event that this task is
3848 * interrupted and switches context before incrementing uxSchedulerSuspended.
3849 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3850 * uxSchedulerSuspended since that will prevent context switches. */
3851 ulState = portSET_INTERRUPT_MASK();
3853 /* This must never be called from inside a critical section. */
3854 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
3856 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3857 * do not otherwise exhibit real time behaviour. */
3858 portSOFTWARE_BARRIER();
3860 portGET_TASK_LOCK();
3862 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3863 * purpose is to prevent altering the variable when fromISR APIs are readying
3865 if( uxSchedulerSuspended == 0U )
3867 prvCheckForRunStateChange();
3871 mtCOVERAGE_TEST_MARKER();
3876 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3877 * is used to allow calls to vTaskSuspendAll() to nest. */
3878 ++uxSchedulerSuspended;
3879 portRELEASE_ISR_LOCK();
3881 portCLEAR_INTERRUPT_MASK( ulState );
3885 mtCOVERAGE_TEST_MARKER();
3888 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3890 traceRETURN_vTaskSuspendAll();
3893 /*----------------------------------------------------------*/
3895 #if ( configUSE_TICKLESS_IDLE != 0 )
3897 static TickType_t prvGetExpectedIdleTime( void )
3900 BaseType_t xHigherPriorityReadyTasks = pdFALSE;
3902 /* xHigherPriorityReadyTasks takes care of the case where
3903 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3904 * task that are in the Ready state, even though the idle task is
3906 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3908 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3910 xHigherPriorityReadyTasks = pdTRUE;
3915 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3917 /* When port optimised task selection is used the uxTopReadyPriority
3918 * variable is used as a bit map. If bits other than the least
3919 * significant bit are set then there are tasks that have a priority
3920 * above the idle priority that are in the Ready state. This takes
3921 * care of the case where the co-operative scheduler is in use. */
3922 if( uxTopReadyPriority > uxLeastSignificantBit )
3924 xHigherPriorityReadyTasks = pdTRUE;
3927 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3929 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3933 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1U )
3935 /* There are other idle priority tasks in the ready state. If
3936 * time slicing is used then the very next tick interrupt must be
3940 else if( xHigherPriorityReadyTasks != pdFALSE )
3942 /* There are tasks in the Ready state that have a priority above the
3943 * idle priority. This path can only be reached if
3944 * configUSE_PREEMPTION is 0. */
3949 xReturn = xNextTaskUnblockTime;
3950 xReturn -= xTickCount;
3956 #endif /* configUSE_TICKLESS_IDLE */
3957 /*----------------------------------------------------------*/
3959 BaseType_t xTaskResumeAll( void )
3961 TCB_t * pxTCB = NULL;
3962 BaseType_t xAlreadyYielded = pdFALSE;
3964 traceENTER_xTaskResumeAll();
3966 #if ( configNUMBER_OF_CORES > 1 )
3967 if( xSchedulerRunning != pdFALSE )
3970 /* It is possible that an ISR caused a task to be removed from an event
3971 * list while the scheduler was suspended. If this was the case then the
3972 * removed task will have been added to the xPendingReadyList. Once the
3973 * scheduler has been resumed it is safe to move all the pending ready
3974 * tasks from this list into their appropriate ready list. */
3975 taskENTER_CRITICAL();
3978 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3980 /* If uxSchedulerSuspended is zero then this function does not match a
3981 * previous call to vTaskSuspendAll(). */
3982 configASSERT( uxSchedulerSuspended != 0U );
3984 uxSchedulerSuspended = ( UBaseType_t ) ( uxSchedulerSuspended - 1U );
3985 portRELEASE_TASK_LOCK();
3987 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3989 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3991 /* Move any readied tasks from the pending list into the
3992 * appropriate ready list. */
3993 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3995 /* MISRA Ref 11.5.3 [Void pointer assignment] */
3996 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
3997 /* coverity[misra_c_2012_rule_11_5_violation] */
3998 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) );
3999 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4000 portMEMORY_BARRIER();
4001 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4002 prvAddTaskToReadyList( pxTCB );
4004 #if ( configNUMBER_OF_CORES == 1 )
4006 /* If the moved task has a priority higher than the current
4007 * task then a yield must be performed. */
4008 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4010 xYieldPendings[ xCoreID ] = pdTRUE;
4014 mtCOVERAGE_TEST_MARKER();
4017 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4019 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
4020 * If the current core yielded then vTaskSwitchContext() has already been called
4021 * which sets xYieldPendings for the current core to pdTRUE. */
4023 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4028 /* A task was unblocked while the scheduler was suspended,
4029 * which may have prevented the next unblock time from being
4030 * re-calculated, in which case re-calculate it now. Mainly
4031 * important for low power tickless implementations, where
4032 * this can prevent an unnecessary exit from low power
4034 prvResetNextTaskUnblockTime();
4037 /* If any ticks occurred while the scheduler was suspended then
4038 * they should be processed now. This ensures the tick count does
4039 * not slip, and that any delayed tasks are resumed at the correct
4042 * It should be safe to call xTaskIncrementTick here from any core
4043 * since we are in a critical section and xTaskIncrementTick itself
4044 * protects itself within a critical section. Suspending the scheduler
4045 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
4047 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
4049 if( xPendedCounts > ( TickType_t ) 0U )
4053 if( xTaskIncrementTick() != pdFALSE )
4055 /* Other cores are interrupted from
4056 * within xTaskIncrementTick(). */
4057 xYieldPendings[ xCoreID ] = pdTRUE;
4061 mtCOVERAGE_TEST_MARKER();
4065 } while( xPendedCounts > ( TickType_t ) 0U );
4071 mtCOVERAGE_TEST_MARKER();
4075 if( xYieldPendings[ xCoreID ] != pdFALSE )
4077 #if ( configUSE_PREEMPTION != 0 )
4079 xAlreadyYielded = pdTRUE;
4081 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
4083 #if ( configNUMBER_OF_CORES == 1 )
4085 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
4087 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4091 mtCOVERAGE_TEST_MARKER();
4097 mtCOVERAGE_TEST_MARKER();
4100 taskEXIT_CRITICAL();
4103 traceRETURN_xTaskResumeAll( xAlreadyYielded );
4105 return xAlreadyYielded;
4107 /*-----------------------------------------------------------*/
4109 TickType_t xTaskGetTickCount( void )
4113 traceENTER_xTaskGetTickCount();
4115 /* Critical section required if running on a 16 bit processor. */
4116 portTICK_TYPE_ENTER_CRITICAL();
4118 xTicks = xTickCount;
4120 portTICK_TYPE_EXIT_CRITICAL();
4122 traceRETURN_xTaskGetTickCount( xTicks );
4126 /*-----------------------------------------------------------*/
4128 TickType_t xTaskGetTickCountFromISR( void )
4131 UBaseType_t uxSavedInterruptStatus;
4133 traceENTER_xTaskGetTickCountFromISR();
4135 /* RTOS ports that support interrupt nesting have the concept of a maximum
4136 * system call (or maximum API call) interrupt priority. Interrupts that are
4137 * above the maximum system call priority are kept permanently enabled, even
4138 * when the RTOS kernel is in a critical section, but cannot make any calls to
4139 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
4140 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
4141 * failure if a FreeRTOS API function is called from an interrupt that has been
4142 * assigned a priority above the configured maximum system call priority.
4143 * Only FreeRTOS functions that end in FromISR can be called from interrupts
4144 * that have been assigned a priority at or (logically) below the maximum
4145 * system call interrupt priority. FreeRTOS maintains a separate interrupt
4146 * safe API to ensure interrupt entry is as fast and as simple as possible.
4147 * More information (albeit Cortex-M specific) is provided on the following
4148 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
4149 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
4151 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
4153 xReturn = xTickCount;
4155 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
4157 traceRETURN_xTaskGetTickCountFromISR( xReturn );
4161 /*-----------------------------------------------------------*/
4163 UBaseType_t uxTaskGetNumberOfTasks( void )
4165 traceENTER_uxTaskGetNumberOfTasks();
4167 /* A critical section is not required because the variables are of type
4169 traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
4171 return uxCurrentNumberOfTasks;
4173 /*-----------------------------------------------------------*/
4175 char * pcTaskGetName( TaskHandle_t xTaskToQuery )
4179 traceENTER_pcTaskGetName( xTaskToQuery );
4181 /* If null is passed in here then the name of the calling task is being
4183 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4184 configASSERT( pxTCB );
4186 traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
4188 return &( pxTCB->pcTaskName[ 0 ] );
4190 /*-----------------------------------------------------------*/
4192 #if ( INCLUDE_xTaskGetHandle == 1 )
4193 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4194 const char pcNameToQuery[] )
4196 TCB_t * pxReturn = NULL;
4197 TCB_t * pxTCB = NULL;
4200 BaseType_t xBreakLoop;
4201 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
4202 ListItem_t * pxIterator;
4204 /* This function is called with the scheduler suspended. */
4206 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4208 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
4210 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4211 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4212 /* coverity[misra_c_2012_rule_11_5_violation] */
4213 pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
4215 /* Check each character in the name looking for a match or
4217 xBreakLoop = pdFALSE;
4219 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4221 cNextChar = pxTCB->pcTaskName[ x ];
4223 if( cNextChar != pcNameToQuery[ x ] )
4225 /* Characters didn't match. */
4226 xBreakLoop = pdTRUE;
4228 else if( cNextChar == ( char ) 0x00 )
4230 /* Both strings terminated, a match must have been
4233 xBreakLoop = pdTRUE;
4237 mtCOVERAGE_TEST_MARKER();
4240 if( xBreakLoop != pdFALSE )
4246 if( pxReturn != NULL )
4248 /* The handle has been found. */
4255 mtCOVERAGE_TEST_MARKER();
4261 #endif /* INCLUDE_xTaskGetHandle */
4262 /*-----------------------------------------------------------*/
4264 #if ( INCLUDE_xTaskGetHandle == 1 )
4266 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery )
4268 UBaseType_t uxQueue = configMAX_PRIORITIES;
4271 traceENTER_xTaskGetHandle( pcNameToQuery );
4273 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
4274 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
4278 /* Search the ready lists. */
4282 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
4286 /* Found the handle. */
4289 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4291 /* Search the delayed lists. */
4294 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
4299 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
4302 #if ( INCLUDE_vTaskSuspend == 1 )
4306 /* Search the suspended list. */
4307 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
4312 #if ( INCLUDE_vTaskDelete == 1 )
4316 /* Search the deleted list. */
4317 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
4322 ( void ) xTaskResumeAll();
4324 traceRETURN_xTaskGetHandle( pxTCB );
4329 #endif /* INCLUDE_xTaskGetHandle */
4330 /*-----------------------------------------------------------*/
4332 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4334 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4335 StackType_t ** ppuxStackBuffer,
4336 StaticTask_t ** ppxTaskBuffer )
4341 traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4343 configASSERT( ppuxStackBuffer != NULL );
4344 configASSERT( ppxTaskBuffer != NULL );
4346 pxTCB = prvGetTCBFromHandle( xTask );
4348 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4350 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4352 *ppuxStackBuffer = pxTCB->pxStack;
4353 /* MISRA Ref 11.3.1 [Misaligned access] */
4354 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
4355 /* coverity[misra_c_2012_rule_11_3_violation] */
4356 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4359 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4361 *ppuxStackBuffer = pxTCB->pxStack;
4362 *ppxTaskBuffer = NULL;
4370 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4372 *ppuxStackBuffer = pxTCB->pxStack;
4373 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4376 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4378 traceRETURN_xTaskGetStaticBuffers( xReturn );
4383 #endif /* configSUPPORT_STATIC_ALLOCATION */
4384 /*-----------------------------------------------------------*/
4386 #if ( configUSE_TRACE_FACILITY == 1 )
4388 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4389 const UBaseType_t uxArraySize,
4390 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4392 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4394 traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4398 /* Is there a space in the array for each task in the system? */
4399 if( uxArraySize >= uxCurrentNumberOfTasks )
4401 /* Fill in an TaskStatus_t structure with information on each
4402 * task in the Ready state. */
4406 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4407 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4409 /* Fill in an TaskStatus_t structure with information on each
4410 * task in the Blocked state. */
4411 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4412 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4414 #if ( INCLUDE_vTaskDelete == 1 )
4416 /* Fill in an TaskStatus_t structure with information on
4417 * each task that has been deleted but not yet cleaned up. */
4418 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4422 #if ( INCLUDE_vTaskSuspend == 1 )
4424 /* Fill in an TaskStatus_t structure with information on
4425 * each task in the Suspended state. */
4426 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4430 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4432 if( pulTotalRunTime != NULL )
4434 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4435 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4437 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4441 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4443 if( pulTotalRunTime != NULL )
4445 *pulTotalRunTime = 0;
4448 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4452 mtCOVERAGE_TEST_MARKER();
4455 ( void ) xTaskResumeAll();
4457 traceRETURN_uxTaskGetSystemState( uxTask );
4462 #endif /* configUSE_TRACE_FACILITY */
4463 /*----------------------------------------------------------*/
4465 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4467 #if ( configNUMBER_OF_CORES == 1 )
4468 TaskHandle_t xTaskGetIdleTaskHandle( void )
4470 traceENTER_xTaskGetIdleTaskHandle();
4472 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4473 * started, then xIdleTaskHandles will be NULL. */
4474 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4476 traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4478 return xIdleTaskHandles[ 0 ];
4480 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
4482 TaskHandle_t xTaskGetIdleTaskHandleForCore( BaseType_t xCoreID )
4484 traceENTER_xTaskGetIdleTaskHandleForCore( xCoreID );
4486 /* Ensure the core ID is valid. */
4487 configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE );
4489 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4490 * started, then xIdleTaskHandles will be NULL. */
4491 configASSERT( ( xIdleTaskHandles[ xCoreID ] != NULL ) );
4493 traceRETURN_xTaskGetIdleTaskHandleForCore( xIdleTaskHandles[ xCoreID ] );
4495 return xIdleTaskHandles[ xCoreID ];
4498 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4499 /*----------------------------------------------------------*/
4501 /* This conditional compilation should use inequality to 0, not equality to 1.
4502 * This is to ensure vTaskStepTick() is available when user defined low power mode
4503 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4505 #if ( configUSE_TICKLESS_IDLE != 0 )
4507 void vTaskStepTick( TickType_t xTicksToJump )
4509 TickType_t xUpdatedTickCount;
4511 traceENTER_vTaskStepTick( xTicksToJump );
4513 /* Correct the tick count value after a period during which the tick
4514 * was suppressed. Note this does *not* call the tick hook function for
4515 * each stepped tick. */
4516 xUpdatedTickCount = xTickCount + xTicksToJump;
4517 configASSERT( xUpdatedTickCount <= xNextTaskUnblockTime );
4519 if( xUpdatedTickCount == xNextTaskUnblockTime )
4521 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4522 * xTaskIncrementTick() when the scheduler resumes. This ensures
4523 * that any delayed tasks are resumed at the correct time. */
4524 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4525 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4527 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4528 taskENTER_CRITICAL();
4532 taskEXIT_CRITICAL();
4537 mtCOVERAGE_TEST_MARKER();
4540 xTickCount += xTicksToJump;
4542 traceINCREASE_TICK_COUNT( xTicksToJump );
4543 traceRETURN_vTaskStepTick();
4546 #endif /* configUSE_TICKLESS_IDLE */
4547 /*----------------------------------------------------------*/
4549 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4551 BaseType_t xYieldOccurred;
4553 traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4555 /* Must not be called with the scheduler suspended as the implementation
4556 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4557 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4559 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4560 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4563 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4564 taskENTER_CRITICAL();
4566 xPendedTicks += xTicksToCatchUp;
4568 taskEXIT_CRITICAL();
4569 xYieldOccurred = xTaskResumeAll();
4571 traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4573 return xYieldOccurred;
4575 /*----------------------------------------------------------*/
4577 #if ( INCLUDE_xTaskAbortDelay == 1 )
4579 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4581 TCB_t * pxTCB = xTask;
4584 traceENTER_xTaskAbortDelay( xTask );
4586 configASSERT( pxTCB );
4590 /* A task can only be prematurely removed from the Blocked state if
4591 * it is actually in the Blocked state. */
4592 if( eTaskGetState( xTask ) == eBlocked )
4596 /* Remove the reference to the task from the blocked list. An
4597 * interrupt won't touch the xStateListItem because the
4598 * scheduler is suspended. */
4599 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4601 /* Is the task waiting on an event also? If so remove it from
4602 * the event list too. Interrupts can touch the event list item,
4603 * even though the scheduler is suspended, so a critical section
4605 taskENTER_CRITICAL();
4607 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4609 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4611 /* This lets the task know it was forcibly removed from the
4612 * blocked state so it should not re-evaluate its block time and
4613 * then block again. */
4614 pxTCB->ucDelayAborted = ( uint8_t ) pdTRUE;
4618 mtCOVERAGE_TEST_MARKER();
4621 taskEXIT_CRITICAL();
4623 /* Place the unblocked task into the appropriate ready list. */
4624 prvAddTaskToReadyList( pxTCB );
4626 /* A task being unblocked cannot cause an immediate context
4627 * switch if preemption is turned off. */
4628 #if ( configUSE_PREEMPTION == 1 )
4630 #if ( configNUMBER_OF_CORES == 1 )
4632 /* Preemption is on, but a context switch should only be
4633 * performed if the unblocked task has a priority that is
4634 * higher than the currently executing task. */
4635 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4637 /* Pend the yield to be performed when the scheduler
4638 * is unsuspended. */
4639 xYieldPendings[ 0 ] = pdTRUE;
4643 mtCOVERAGE_TEST_MARKER();
4646 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4648 taskENTER_CRITICAL();
4650 prvYieldForTask( pxTCB );
4652 taskEXIT_CRITICAL();
4654 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4656 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4663 ( void ) xTaskResumeAll();
4665 traceRETURN_xTaskAbortDelay( xReturn );
4670 #endif /* INCLUDE_xTaskAbortDelay */
4671 /*----------------------------------------------------------*/
4673 BaseType_t xTaskIncrementTick( void )
4676 TickType_t xItemValue;
4677 BaseType_t xSwitchRequired = pdFALSE;
4679 #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4680 BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4681 #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4683 traceENTER_xTaskIncrementTick();
4685 /* Called by the portable layer each time a tick interrupt occurs.
4686 * Increments the tick then checks to see if the new tick value will cause any
4687 * tasks to be unblocked. */
4688 traceTASK_INCREMENT_TICK( xTickCount );
4690 /* Tick increment should occur on every kernel timer event. Core 0 has the
4691 * responsibility to increment the tick, or increment the pended ticks if the
4692 * scheduler is suspended. If pended ticks is greater than zero, the core that
4693 * calls xTaskResumeAll has the responsibility to increment the tick. */
4694 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4696 /* Minor optimisation. The tick count cannot change in this
4698 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4700 /* Increment the RTOS tick, switching the delayed and overflowed
4701 * delayed lists if it wraps to 0. */
4702 xTickCount = xConstTickCount;
4704 if( xConstTickCount == ( TickType_t ) 0U )
4706 taskSWITCH_DELAYED_LISTS();
4710 mtCOVERAGE_TEST_MARKER();
4713 /* See if this tick has made a timeout expire. Tasks are stored in
4714 * the queue in the order of their wake time - meaning once one task
4715 * has been found whose block time has not expired there is no need to
4716 * look any further down the list. */
4717 if( xConstTickCount >= xNextTaskUnblockTime )
4721 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4723 /* The delayed list is empty. Set xNextTaskUnblockTime
4724 * to the maximum possible value so it is extremely
4726 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4727 * next time through. */
4728 xNextTaskUnblockTime = portMAX_DELAY;
4733 /* The delayed list is not empty, get the value of the
4734 * item at the head of the delayed list. This is the time
4735 * at which the task at the head of the delayed list must
4736 * be removed from the Blocked state. */
4737 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4738 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4739 /* coverity[misra_c_2012_rule_11_5_violation] */
4740 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
4741 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4743 if( xConstTickCount < xItemValue )
4745 /* It is not time to unblock this item yet, but the
4746 * item value is the time at which the task at the head
4747 * of the blocked list must be removed from the Blocked
4748 * state - so record the item value in
4749 * xNextTaskUnblockTime. */
4750 xNextTaskUnblockTime = xItemValue;
4755 mtCOVERAGE_TEST_MARKER();
4758 /* It is time to remove the item from the Blocked state. */
4759 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4761 /* Is the task waiting on an event also? If so remove
4762 * it from the event list. */
4763 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4765 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4769 mtCOVERAGE_TEST_MARKER();
4772 /* Place the unblocked task into the appropriate ready
4774 prvAddTaskToReadyList( pxTCB );
4776 /* A task being unblocked cannot cause an immediate
4777 * context switch if preemption is turned off. */
4778 #if ( configUSE_PREEMPTION == 1 )
4780 #if ( configNUMBER_OF_CORES == 1 )
4782 /* Preemption is on, but a context switch should
4783 * only be performed if the unblocked task's
4784 * priority is higher than the currently executing
4786 * The case of equal priority tasks sharing
4787 * processing time (which happens when both
4788 * preemption and time slicing are on) is
4790 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4792 xSwitchRequired = pdTRUE;
4796 mtCOVERAGE_TEST_MARKER();
4799 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4801 prvYieldForTask( pxTCB );
4803 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4805 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4810 /* Tasks of equal priority to the currently running task will share
4811 * processing time (time slice) if preemption is on, and the application
4812 * writer has not explicitly turned time slicing off. */
4813 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4815 #if ( configNUMBER_OF_CORES == 1 )
4817 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > 1U )
4819 xSwitchRequired = pdTRUE;
4823 mtCOVERAGE_TEST_MARKER();
4826 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4830 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4832 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1U )
4834 xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4838 mtCOVERAGE_TEST_MARKER();
4842 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4844 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4846 #if ( configUSE_TICK_HOOK == 1 )
4848 /* Guard against the tick hook being called when the pended tick
4849 * count is being unwound (when the scheduler is being unlocked). */
4850 if( xPendedTicks == ( TickType_t ) 0 )
4852 vApplicationTickHook();
4856 mtCOVERAGE_TEST_MARKER();
4859 #endif /* configUSE_TICK_HOOK */
4861 #if ( configUSE_PREEMPTION == 1 )
4863 #if ( configNUMBER_OF_CORES == 1 )
4865 /* For single core the core ID is always 0. */
4866 if( xYieldPendings[ 0 ] != pdFALSE )
4868 xSwitchRequired = pdTRUE;
4872 mtCOVERAGE_TEST_MARKER();
4875 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4877 BaseType_t xCoreID, xCurrentCoreID;
4878 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4880 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4882 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4883 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4886 if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4888 if( xCoreID == xCurrentCoreID )
4890 xSwitchRequired = pdTRUE;
4894 prvYieldCore( xCoreID );
4899 mtCOVERAGE_TEST_MARKER();
4904 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4906 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4912 /* The tick hook gets called at regular intervals, even if the
4913 * scheduler is locked. */
4914 #if ( configUSE_TICK_HOOK == 1 )
4916 vApplicationTickHook();
4921 traceRETURN_xTaskIncrementTick( xSwitchRequired );
4923 return xSwitchRequired;
4925 /*-----------------------------------------------------------*/
4927 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4929 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4930 TaskHookFunction_t pxHookFunction )
4934 traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4936 /* If xTask is NULL then it is the task hook of the calling task that is
4940 xTCB = ( TCB_t * ) pxCurrentTCB;
4947 /* Save the hook function in the TCB. A critical section is required as
4948 * the value can be accessed from an interrupt. */
4949 taskENTER_CRITICAL();
4951 xTCB->pxTaskTag = pxHookFunction;
4953 taskEXIT_CRITICAL();
4955 traceRETURN_vTaskSetApplicationTaskTag();
4958 #endif /* configUSE_APPLICATION_TASK_TAG */
4959 /*-----------------------------------------------------------*/
4961 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4963 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4966 TaskHookFunction_t xReturn;
4968 traceENTER_xTaskGetApplicationTaskTag( xTask );
4970 /* If xTask is NULL then set the calling task's hook. */
4971 pxTCB = prvGetTCBFromHandle( xTask );
4973 /* Save the hook function in the TCB. A critical section is required as
4974 * the value can be accessed from an interrupt. */
4975 taskENTER_CRITICAL();
4977 xReturn = pxTCB->pxTaskTag;
4979 taskEXIT_CRITICAL();
4981 traceRETURN_xTaskGetApplicationTaskTag( xReturn );
4986 #endif /* configUSE_APPLICATION_TASK_TAG */
4987 /*-----------------------------------------------------------*/
4989 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4991 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
4994 TaskHookFunction_t xReturn;
4995 UBaseType_t uxSavedInterruptStatus;
4997 traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
4999 /* If xTask is NULL then set the calling task's hook. */
5000 pxTCB = prvGetTCBFromHandle( xTask );
5002 /* Save the hook function in the TCB. A critical section is required as
5003 * the value can be accessed from an interrupt. */
5004 /* MISRA Ref 4.7.1 [Return value shall be checked] */
5005 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
5006 /* coverity[misra_c_2012_directive_4_7_violation] */
5007 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
5009 xReturn = pxTCB->pxTaskTag;
5011 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
5013 traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
5018 #endif /* configUSE_APPLICATION_TASK_TAG */
5019 /*-----------------------------------------------------------*/
5021 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5023 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
5024 void * pvParameter )
5029 traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
5031 /* If xTask is NULL then we are calling our own task hook. */
5034 xTCB = pxCurrentTCB;
5041 if( xTCB->pxTaskTag != NULL )
5043 xReturn = xTCB->pxTaskTag( pvParameter );
5050 traceRETURN_xTaskCallApplicationTaskHook( xReturn );
5055 #endif /* configUSE_APPLICATION_TASK_TAG */
5056 /*-----------------------------------------------------------*/
5058 #if ( configNUMBER_OF_CORES == 1 )
5059 void vTaskSwitchContext( void )
5061 traceENTER_vTaskSwitchContext();
5063 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5065 /* The scheduler is currently suspended - do not allow a context
5067 xYieldPendings[ 0 ] = pdTRUE;
5071 xYieldPendings[ 0 ] = pdFALSE;
5072 traceTASK_SWITCHED_OUT();
5074 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5076 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5077 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
5079 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
5082 /* Add the amount of time the task has been running to the
5083 * accumulated time so far. The time the task started running was
5084 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5085 * protection here so count values are only valid until the timer
5086 * overflows. The guard against negative values is to protect
5087 * against suspect run time stat counter implementations - which
5088 * are provided by the application, not the kernel. */
5089 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
5091 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
5095 mtCOVERAGE_TEST_MARKER();
5098 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
5100 #endif /* configGENERATE_RUN_TIME_STATS */
5102 /* Check for stack overflow, if configured. */
5103 taskCHECK_FOR_STACK_OVERFLOW();
5105 /* Before the currently running task is switched out, save its errno. */
5106 #if ( configUSE_POSIX_ERRNO == 1 )
5108 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
5112 /* Select a new task to run using either the generic C or port
5113 * optimised asm code. */
5114 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5115 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5116 /* coverity[misra_c_2012_rule_11_5_violation] */
5117 taskSELECT_HIGHEST_PRIORITY_TASK();
5118 traceTASK_SWITCHED_IN();
5120 /* Macro to inject port specific behaviour immediately after
5121 * switching tasks, such as setting an end of stack watchpoint
5122 * or reconfiguring the MPU. */
5123 portTASK_SWITCH_HOOK( pxCurrentTCB );
5125 /* After the new task is switched in, update the global errno. */
5126 #if ( configUSE_POSIX_ERRNO == 1 )
5128 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
5132 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5134 /* Switch C-Runtime's TLS Block to point to the TLS
5135 * Block specific to this task. */
5136 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
5141 traceRETURN_vTaskSwitchContext();
5143 #else /* if ( configNUMBER_OF_CORES == 1 ) */
5144 void vTaskSwitchContext( BaseType_t xCoreID )
5146 traceENTER_vTaskSwitchContext();
5148 /* Acquire both locks:
5149 * - The ISR lock protects the ready list from simultaneous access by
5150 * both other ISRs and tasks.
5151 * - We also take the task lock to pause here in case another core has
5152 * suspended the scheduler. We don't want to simply set xYieldPending
5153 * and move on if another core suspended the scheduler. We should only
5154 * do that if the current core has suspended the scheduler. */
5156 portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
5159 /* vTaskSwitchContext() must never be called from within a critical section.
5160 * This is not necessarily true for single core FreeRTOS, but it is for this
5162 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
5164 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5166 /* The scheduler is currently suspended - do not allow a context
5168 xYieldPendings[ xCoreID ] = pdTRUE;
5172 xYieldPendings[ xCoreID ] = pdFALSE;
5173 traceTASK_SWITCHED_OUT();
5175 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5177 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5178 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
5180 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
5183 /* Add the amount of time the task has been running to the
5184 * accumulated time so far. The time the task started running was
5185 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5186 * protection here so count values are only valid until the timer
5187 * overflows. The guard against negative values is to protect
5188 * against suspect run time stat counter implementations - which
5189 * are provided by the application, not the kernel. */
5190 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
5192 pxCurrentTCBs[ xCoreID ]->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
5196 mtCOVERAGE_TEST_MARKER();
5199 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
5201 #endif /* configGENERATE_RUN_TIME_STATS */
5203 /* Check for stack overflow, if configured. */
5204 taskCHECK_FOR_STACK_OVERFLOW();
5206 /* Before the currently running task is switched out, save its errno. */
5207 #if ( configUSE_POSIX_ERRNO == 1 )
5209 pxCurrentTCBs[ xCoreID ]->iTaskErrno = FreeRTOS_errno;
5213 /* Select a new task to run. */
5214 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
5215 traceTASK_SWITCHED_IN();
5217 /* Macro to inject port specific behaviour immediately after
5218 * switching tasks, such as setting an end of stack watchpoint
5219 * or reconfiguring the MPU. */
5220 portTASK_SWITCH_HOOK( pxCurrentTCBs[ portGET_CORE_ID() ] );
5222 /* After the new task is switched in, update the global errno. */
5223 #if ( configUSE_POSIX_ERRNO == 1 )
5225 FreeRTOS_errno = pxCurrentTCBs[ xCoreID ]->iTaskErrno;
5229 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5231 /* Switch C-Runtime's TLS Block to point to the TLS
5232 * Block specific to this task. */
5233 configSET_TLS_BLOCK( pxCurrentTCBs[ xCoreID ]->xTLSBlock );
5238 portRELEASE_ISR_LOCK();
5239 portRELEASE_TASK_LOCK();
5241 traceRETURN_vTaskSwitchContext();
5243 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
5244 /*-----------------------------------------------------------*/
5246 void vTaskPlaceOnEventList( List_t * const pxEventList,
5247 const TickType_t xTicksToWait )
5249 traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
5251 configASSERT( pxEventList );
5253 /* THIS FUNCTION MUST BE CALLED WITH THE
5254 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
5256 /* Place the event list item of the TCB in the appropriate event list.
5257 * This is placed in the list in priority order so the highest priority task
5258 * is the first to be woken by the event.
5260 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
5261 * Normally, the xItemValue of a TCB's ListItem_t members is:
5262 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
5263 * Therefore, the event list is sorted in descending priority order.
5265 * The queue that contains the event list is locked, preventing
5266 * simultaneous access from interrupts. */
5267 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5269 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5271 traceRETURN_vTaskPlaceOnEventList();
5273 /*-----------------------------------------------------------*/
5275 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
5276 const TickType_t xItemValue,
5277 const TickType_t xTicksToWait )
5279 traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
5281 configASSERT( pxEventList );
5283 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5284 * the event groups implementation. */
5285 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5287 /* Store the item value in the event list item. It is safe to access the
5288 * event list item here as interrupts won't access the event list item of a
5289 * task that is not in the Blocked state. */
5290 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5292 /* Place the event list item of the TCB at the end of the appropriate event
5293 * list. It is safe to access the event list here because it is part of an
5294 * event group implementation - and interrupts don't access event groups
5295 * directly (instead they access them indirectly by pending function calls to
5296 * the task level). */
5297 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5299 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5301 traceRETURN_vTaskPlaceOnUnorderedEventList();
5303 /*-----------------------------------------------------------*/
5305 #if ( configUSE_TIMERS == 1 )
5307 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
5308 TickType_t xTicksToWait,
5309 const BaseType_t xWaitIndefinitely )
5311 traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
5313 configASSERT( pxEventList );
5315 /* This function should not be called by application code hence the
5316 * 'Restricted' in its name. It is not part of the public API. It is
5317 * designed for use by kernel code, and has special calling requirements -
5318 * it should be called with the scheduler suspended. */
5321 /* Place the event list item of the TCB in the appropriate event list.
5322 * In this case it is assume that this is the only task that is going to
5323 * be waiting on this event list, so the faster vListInsertEnd() function
5324 * can be used in place of vListInsert. */
5325 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5327 /* If the task should block indefinitely then set the block time to a
5328 * value that will be recognised as an indefinite delay inside the
5329 * prvAddCurrentTaskToDelayedList() function. */
5330 if( xWaitIndefinitely != pdFALSE )
5332 xTicksToWait = portMAX_DELAY;
5335 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
5336 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
5338 traceRETURN_vTaskPlaceOnEventListRestricted();
5341 #endif /* configUSE_TIMERS */
5342 /*-----------------------------------------------------------*/
5344 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
5346 TCB_t * pxUnblockedTCB;
5349 traceENTER_xTaskRemoveFromEventList( pxEventList );
5351 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
5352 * called from a critical section within an ISR. */
5354 /* The event list is sorted in priority order, so the first in the list can
5355 * be removed as it is known to be the highest priority. Remove the TCB from
5356 * the delayed list, and add it to the ready list.
5358 * If an event is for a queue that is locked then this function will never
5359 * get called - the lock count on the queue will get modified instead. This
5360 * means exclusive access to the event list is guaranteed here.
5362 * This function assumes that a check has already been made to ensure that
5363 * pxEventList is not empty. */
5364 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5365 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5366 /* coverity[misra_c_2012_rule_11_5_violation] */
5367 pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
5368 configASSERT( pxUnblockedTCB );
5369 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5371 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5373 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5374 prvAddTaskToReadyList( pxUnblockedTCB );
5376 #if ( configUSE_TICKLESS_IDLE != 0 )
5378 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5379 * might be set to the blocked task's time out time. If the task is
5380 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5381 * normally left unchanged, because it is automatically reset to a new
5382 * value when the tick count equals xNextTaskUnblockTime. However if
5383 * tickless idling is used it might be more important to enter sleep mode
5384 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5385 * ensure it is updated at the earliest possible time. */
5386 prvResetNextTaskUnblockTime();
5392 /* The delayed and ready lists cannot be accessed, so hold this task
5393 * pending until the scheduler is resumed. */
5394 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5397 #if ( configNUMBER_OF_CORES == 1 )
5399 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5401 /* Return true if the task removed from the event list has a higher
5402 * priority than the calling task. This allows the calling task to know if
5403 * it should force a context switch now. */
5406 /* Mark that a yield is pending in case the user is not using the
5407 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5408 xYieldPendings[ 0 ] = pdTRUE;
5415 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5419 #if ( configUSE_PREEMPTION == 1 )
5421 prvYieldForTask( pxUnblockedTCB );
5423 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5428 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5430 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5432 traceRETURN_xTaskRemoveFromEventList( xReturn );
5435 /*-----------------------------------------------------------*/
5437 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5438 const TickType_t xItemValue )
5440 TCB_t * pxUnblockedTCB;
5442 traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5444 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5445 * the event flags implementation. */
5446 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5448 /* Store the new item value in the event list. */
5449 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5451 /* Remove the event list form the event flag. Interrupts do not access
5453 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5454 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5455 /* coverity[misra_c_2012_rule_11_5_violation] */
5456 pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem );
5457 configASSERT( pxUnblockedTCB );
5458 listREMOVE_ITEM( pxEventListItem );
5460 #if ( configUSE_TICKLESS_IDLE != 0 )
5462 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5463 * might be set to the blocked task's time out time. If the task is
5464 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5465 * normally left unchanged, because it is automatically reset to a new
5466 * value when the tick count equals xNextTaskUnblockTime. However if
5467 * tickless idling is used it might be more important to enter sleep mode
5468 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5469 * ensure it is updated at the earliest possible time. */
5470 prvResetNextTaskUnblockTime();
5474 /* Remove the task from the delayed list and add it to the ready list. The
5475 * scheduler is suspended so interrupts will not be accessing the ready
5477 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5478 prvAddTaskToReadyList( pxUnblockedTCB );
5480 #if ( configNUMBER_OF_CORES == 1 )
5482 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5484 /* The unblocked task has a priority above that of the calling task, so
5485 * a context switch is required. This function is called with the
5486 * scheduler suspended so xYieldPending is set so the context switch
5487 * occurs immediately that the scheduler is resumed (unsuspended). */
5488 xYieldPendings[ 0 ] = pdTRUE;
5491 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5493 #if ( configUSE_PREEMPTION == 1 )
5495 taskENTER_CRITICAL();
5497 prvYieldForTask( pxUnblockedTCB );
5499 taskEXIT_CRITICAL();
5503 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5505 traceRETURN_vTaskRemoveFromUnorderedEventList();
5507 /*-----------------------------------------------------------*/
5509 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5511 traceENTER_vTaskSetTimeOutState( pxTimeOut );
5513 configASSERT( pxTimeOut );
5514 taskENTER_CRITICAL();
5516 pxTimeOut->xOverflowCount = xNumOfOverflows;
5517 pxTimeOut->xTimeOnEntering = xTickCount;
5519 taskEXIT_CRITICAL();
5521 traceRETURN_vTaskSetTimeOutState();
5523 /*-----------------------------------------------------------*/
5525 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5527 traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5529 /* For internal use only as it does not use a critical section. */
5530 pxTimeOut->xOverflowCount = xNumOfOverflows;
5531 pxTimeOut->xTimeOnEntering = xTickCount;
5533 traceRETURN_vTaskInternalSetTimeOutState();
5535 /*-----------------------------------------------------------*/
5537 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5538 TickType_t * const pxTicksToWait )
5542 traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5544 configASSERT( pxTimeOut );
5545 configASSERT( pxTicksToWait );
5547 taskENTER_CRITICAL();
5549 /* Minor optimisation. The tick count cannot change in this block. */
5550 const TickType_t xConstTickCount = xTickCount;
5551 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5553 #if ( INCLUDE_xTaskAbortDelay == 1 )
5554 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5556 /* The delay was aborted, which is not the same as a time out,
5557 * but has the same result. */
5558 pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
5564 #if ( INCLUDE_vTaskSuspend == 1 )
5565 if( *pxTicksToWait == portMAX_DELAY )
5567 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5568 * specified is the maximum block time then the task should block
5569 * indefinitely, and therefore never time out. */
5575 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) )
5577 /* The tick count is greater than the time at which
5578 * vTaskSetTimeout() was called, but has also overflowed since
5579 * vTaskSetTimeOut() was called. It must have wrapped all the way
5580 * around and gone past again. This passed since vTaskSetTimeout()
5583 *pxTicksToWait = ( TickType_t ) 0;
5585 else if( xElapsedTime < *pxTicksToWait )
5587 /* Not a genuine timeout. Adjust parameters for time remaining. */
5588 *pxTicksToWait -= xElapsedTime;
5589 vTaskInternalSetTimeOutState( pxTimeOut );
5594 *pxTicksToWait = ( TickType_t ) 0;
5598 taskEXIT_CRITICAL();
5600 traceRETURN_xTaskCheckForTimeOut( xReturn );
5604 /*-----------------------------------------------------------*/
5606 void vTaskMissedYield( void )
5608 traceENTER_vTaskMissedYield();
5610 /* Must be called from within a critical section. */
5611 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5613 traceRETURN_vTaskMissedYield();
5615 /*-----------------------------------------------------------*/
5617 #if ( configUSE_TRACE_FACILITY == 1 )
5619 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5621 UBaseType_t uxReturn;
5622 TCB_t const * pxTCB;
5624 traceENTER_uxTaskGetTaskNumber( xTask );
5629 uxReturn = pxTCB->uxTaskNumber;
5636 traceRETURN_uxTaskGetTaskNumber( uxReturn );
5641 #endif /* configUSE_TRACE_FACILITY */
5642 /*-----------------------------------------------------------*/
5644 #if ( configUSE_TRACE_FACILITY == 1 )
5646 void vTaskSetTaskNumber( TaskHandle_t xTask,
5647 const UBaseType_t uxHandle )
5651 traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5656 pxTCB->uxTaskNumber = uxHandle;
5659 traceRETURN_vTaskSetTaskNumber();
5662 #endif /* configUSE_TRACE_FACILITY */
5663 /*-----------------------------------------------------------*/
5666 * -----------------------------------------------------------
5667 * The passive idle task.
5668 * ----------------------------------------------------------
5670 * The passive idle task is used for all the additional cores in a SMP
5671 * system. There must be only 1 active idle task and the rest are passive
5674 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5675 * language extensions. The equivalent prototype for this function is:
5677 * void prvPassiveIdleTask( void *pvParameters );
5680 #if ( configNUMBER_OF_CORES > 1 )
5681 static portTASK_FUNCTION( prvPassiveIdleTask, pvParameters )
5683 ( void ) pvParameters;
5687 for( ; configCONTROL_INFINITE_LOOP(); )
5689 #if ( configUSE_PREEMPTION == 0 )
5691 /* If we are not using preemption we keep forcing a task switch to
5692 * see if any other task has become available. If we are using
5693 * preemption we don't need to do this as any task becoming available
5694 * will automatically get the processor anyway. */
5697 #endif /* configUSE_PREEMPTION */
5699 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5701 /* When using preemption tasks of equal priority will be
5702 * timesliced. If a task that is sharing the idle priority is ready
5703 * to run then the idle task should yield before the end of the
5706 * A critical region is not required here as we are just reading from
5707 * the list, and an occasional incorrect value will not matter. If
5708 * the ready list at the idle priority contains one more task than the
5709 * number of idle tasks, which is equal to the configured numbers of cores
5710 * then a task other than the idle task is ready to execute. */
5711 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5717 mtCOVERAGE_TEST_MARKER();
5720 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5722 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
5724 /* Call the user defined function from within the idle task. This
5725 * allows the application designer to add background functionality
5726 * without the overhead of a separate task.
5728 * This hook is intended to manage core activity such as disabling cores that go idle.
5730 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5731 * CALL A FUNCTION THAT MIGHT BLOCK. */
5732 vApplicationPassiveIdleHook();
5734 #endif /* configUSE_PASSIVE_IDLE_HOOK */
5737 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5740 * -----------------------------------------------------------
5742 * ----------------------------------------------------------
5744 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5745 * language extensions. The equivalent prototype for this function is:
5747 * void prvIdleTask( void *pvParameters );
5751 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5753 /* Stop warnings. */
5754 ( void ) pvParameters;
5756 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5757 * SCHEDULER IS STARTED. **/
5759 /* In case a task that has a secure context deletes itself, in which case
5760 * the idle task is responsible for deleting the task's secure context, if
5762 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5764 #if ( configNUMBER_OF_CORES > 1 )
5766 /* SMP all cores start up in the idle task. This initial yield gets the application
5770 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5772 for( ; configCONTROL_INFINITE_LOOP(); )
5774 /* See if any tasks have deleted themselves - if so then the idle task
5775 * is responsible for freeing the deleted task's TCB and stack. */
5776 prvCheckTasksWaitingTermination();
5778 #if ( configUSE_PREEMPTION == 0 )
5780 /* If we are not using preemption we keep forcing a task switch to
5781 * see if any other task has become available. If we are using
5782 * preemption we don't need to do this as any task becoming available
5783 * will automatically get the processor anyway. */
5786 #endif /* configUSE_PREEMPTION */
5788 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5790 /* When using preemption tasks of equal priority will be
5791 * timesliced. If a task that is sharing the idle priority is ready
5792 * to run then the idle task should yield before the end of the
5795 * A critical region is not required here as we are just reading from
5796 * the list, and an occasional incorrect value will not matter. If
5797 * the ready list at the idle priority contains one more task than the
5798 * number of idle tasks, which is equal to the configured numbers of cores
5799 * then a task other than the idle task is ready to execute. */
5800 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5806 mtCOVERAGE_TEST_MARKER();
5809 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5811 #if ( configUSE_IDLE_HOOK == 1 )
5813 /* Call the user defined function from within the idle task. */
5814 vApplicationIdleHook();
5816 #endif /* configUSE_IDLE_HOOK */
5818 /* This conditional compilation should use inequality to 0, not equality
5819 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5820 * user defined low power mode implementations require
5821 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5822 #if ( configUSE_TICKLESS_IDLE != 0 )
5824 TickType_t xExpectedIdleTime;
5826 /* It is not desirable to suspend then resume the scheduler on
5827 * each iteration of the idle task. Therefore, a preliminary
5828 * test of the expected idle time is performed without the
5829 * scheduler suspended. The result here is not necessarily
5831 xExpectedIdleTime = prvGetExpectedIdleTime();
5833 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5837 /* Now the scheduler is suspended, the expected idle
5838 * time can be sampled again, and this time its value can
5840 configASSERT( xNextTaskUnblockTime >= xTickCount );
5841 xExpectedIdleTime = prvGetExpectedIdleTime();
5843 /* Define the following macro to set xExpectedIdleTime to 0
5844 * if the application does not want
5845 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5846 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5848 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5850 traceLOW_POWER_IDLE_BEGIN();
5851 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5852 traceLOW_POWER_IDLE_END();
5856 mtCOVERAGE_TEST_MARKER();
5859 ( void ) xTaskResumeAll();
5863 mtCOVERAGE_TEST_MARKER();
5866 #endif /* configUSE_TICKLESS_IDLE */
5868 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) )
5870 /* Call the user defined function from within the idle task. This
5871 * allows the application designer to add background functionality
5872 * without the overhead of a separate task.
5874 * This hook is intended to manage core activity such as disabling cores that go idle.
5876 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5877 * CALL A FUNCTION THAT MIGHT BLOCK. */
5878 vApplicationPassiveIdleHook();
5880 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) ) */
5883 /*-----------------------------------------------------------*/
5885 #if ( configUSE_TICKLESS_IDLE != 0 )
5887 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5889 #if ( INCLUDE_vTaskSuspend == 1 )
5890 /* The idle task exists in addition to the application tasks. */
5891 const UBaseType_t uxNonApplicationTasks = configNUMBER_OF_CORES;
5892 #endif /* INCLUDE_vTaskSuspend */
5894 eSleepModeStatus eReturn = eStandardSleep;
5896 traceENTER_eTaskConfirmSleepModeStatus();
5898 /* This function must be called from a critical section. */
5900 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0U )
5902 /* A task was made ready while the scheduler was suspended. */
5903 eReturn = eAbortSleep;
5905 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5907 /* A yield was pended while the scheduler was suspended. */
5908 eReturn = eAbortSleep;
5910 else if( xPendedTicks != 0U )
5912 /* A tick interrupt has already occurred but was held pending
5913 * because the scheduler is suspended. */
5914 eReturn = eAbortSleep;
5917 #if ( INCLUDE_vTaskSuspend == 1 )
5918 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5920 /* If all the tasks are in the suspended list (which might mean they
5921 * have an infinite block time rather than actually being suspended)
5922 * then it is safe to turn all clocks off and just wait for external
5924 eReturn = eNoTasksWaitingTimeout;
5926 #endif /* INCLUDE_vTaskSuspend */
5929 mtCOVERAGE_TEST_MARKER();
5932 traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5937 #endif /* configUSE_TICKLESS_IDLE */
5938 /*-----------------------------------------------------------*/
5940 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5942 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5948 traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
5950 if( ( xIndex >= 0 ) &&
5951 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5953 pxTCB = prvGetTCBFromHandle( xTaskToSet );
5954 configASSERT( pxTCB != NULL );
5955 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5958 traceRETURN_vTaskSetThreadLocalStoragePointer();
5961 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5962 /*-----------------------------------------------------------*/
5964 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5966 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5969 void * pvReturn = NULL;
5972 traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
5974 if( ( xIndex >= 0 ) &&
5975 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5977 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
5978 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
5985 traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
5990 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5991 /*-----------------------------------------------------------*/
5993 #if ( portUSING_MPU_WRAPPERS == 1 )
5995 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
5996 const MemoryRegion_t * const pxRegions )
6000 traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
6002 /* If null is passed in here then we are modifying the MPU settings of
6003 * the calling task. */
6004 pxTCB = prvGetTCBFromHandle( xTaskToModify );
6006 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
6008 traceRETURN_vTaskAllocateMPURegions();
6011 #endif /* portUSING_MPU_WRAPPERS */
6012 /*-----------------------------------------------------------*/
6014 static void prvInitialiseTaskLists( void )
6016 UBaseType_t uxPriority;
6018 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
6020 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
6023 vListInitialise( &xDelayedTaskList1 );
6024 vListInitialise( &xDelayedTaskList2 );
6025 vListInitialise( &xPendingReadyList );
6027 #if ( INCLUDE_vTaskDelete == 1 )
6029 vListInitialise( &xTasksWaitingTermination );
6031 #endif /* INCLUDE_vTaskDelete */
6033 #if ( INCLUDE_vTaskSuspend == 1 )
6035 vListInitialise( &xSuspendedTaskList );
6037 #endif /* INCLUDE_vTaskSuspend */
6039 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
6041 pxDelayedTaskList = &xDelayedTaskList1;
6042 pxOverflowDelayedTaskList = &xDelayedTaskList2;
6044 /*-----------------------------------------------------------*/
6046 static void prvCheckTasksWaitingTermination( void )
6048 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
6050 #if ( INCLUDE_vTaskDelete == 1 )
6054 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
6055 * being called too often in the idle task. */
6056 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6058 #if ( configNUMBER_OF_CORES == 1 )
6060 taskENTER_CRITICAL();
6063 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6064 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6065 /* coverity[misra_c_2012_rule_11_5_violation] */
6066 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6067 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6068 --uxCurrentNumberOfTasks;
6069 --uxDeletedTasksWaitingCleanUp;
6072 taskEXIT_CRITICAL();
6074 prvDeleteTCB( pxTCB );
6076 #else /* #if( configNUMBER_OF_CORES == 1 ) */
6080 taskENTER_CRITICAL();
6082 /* For SMP, multiple idles can be running simultaneously
6083 * and we need to check that other idles did not cleanup while we were
6084 * waiting to enter the critical section. */
6085 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6087 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6088 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6089 /* coverity[misra_c_2012_rule_11_5_violation] */
6090 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6092 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
6094 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6095 --uxCurrentNumberOfTasks;
6096 --uxDeletedTasksWaitingCleanUp;
6100 /* The TCB to be deleted still has not yet been switched out
6101 * by the scheduler, so we will just exit this loop early and
6102 * try again next time. */
6103 taskEXIT_CRITICAL();
6108 taskEXIT_CRITICAL();
6112 prvDeleteTCB( pxTCB );
6115 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
6118 #endif /* INCLUDE_vTaskDelete */
6120 /*-----------------------------------------------------------*/
6122 #if ( configUSE_TRACE_FACILITY == 1 )
6124 void vTaskGetInfo( TaskHandle_t xTask,
6125 TaskStatus_t * pxTaskStatus,
6126 BaseType_t xGetFreeStackSpace,
6131 traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
6133 /* xTask is NULL then get the state of the calling task. */
6134 pxTCB = prvGetTCBFromHandle( xTask );
6136 pxTaskStatus->xHandle = pxTCB;
6137 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
6138 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
6139 pxTaskStatus->pxStackBase = pxTCB->pxStack;
6140 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
6141 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
6142 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
6144 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
6146 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
6148 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
6152 #if ( configUSE_MUTEXES == 1 )
6154 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
6158 pxTaskStatus->uxBasePriority = 0;
6162 #if ( configGENERATE_RUN_TIME_STATS == 1 )
6164 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
6168 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
6172 /* Obtaining the task state is a little fiddly, so is only done if the
6173 * value of eState passed into this function is eInvalid - otherwise the
6174 * state is just set to whatever is passed in. */
6175 if( eState != eInvalid )
6177 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6179 pxTaskStatus->eCurrentState = eRunning;
6183 pxTaskStatus->eCurrentState = eState;
6185 #if ( INCLUDE_vTaskSuspend == 1 )
6187 /* If the task is in the suspended list then there is a
6188 * chance it is actually just blocked indefinitely - so really
6189 * it should be reported as being in the Blocked state. */
6190 if( eState == eSuspended )
6194 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
6196 pxTaskStatus->eCurrentState = eBlocked;
6200 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6204 /* The task does not appear on the event list item of
6205 * and of the RTOS objects, but could still be in the
6206 * blocked state if it is waiting on its notification
6207 * rather than waiting on an object. If not, is
6209 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
6211 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
6213 pxTaskStatus->eCurrentState = eBlocked;
6218 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
6221 ( void ) xTaskResumeAll();
6224 #endif /* INCLUDE_vTaskSuspend */
6226 /* Tasks can be in pending ready list and other state list at the
6227 * same time. These tasks are in ready state no matter what state
6228 * list the task is in. */
6229 taskENTER_CRITICAL();
6231 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
6233 pxTaskStatus->eCurrentState = eReady;
6236 taskEXIT_CRITICAL();
6241 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
6244 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
6245 * parameter is provided to allow it to be skipped. */
6246 if( xGetFreeStackSpace != pdFALSE )
6248 #if ( portSTACK_GROWTH > 0 )
6250 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
6254 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
6260 pxTaskStatus->usStackHighWaterMark = 0;
6263 traceRETURN_vTaskGetInfo();
6266 #endif /* configUSE_TRACE_FACILITY */
6267 /*-----------------------------------------------------------*/
6269 #if ( configUSE_TRACE_FACILITY == 1 )
6271 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
6275 UBaseType_t uxTask = 0;
6276 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
6277 ListItem_t * pxIterator;
6278 TCB_t * pxTCB = NULL;
6280 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
6282 /* Populate an TaskStatus_t structure within the
6283 * pxTaskStatusArray array for each task that is referenced from
6284 * pxList. See the definition of TaskStatus_t in task.h for the
6285 * meaning of each TaskStatus_t structure member. */
6286 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
6288 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6289 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6290 /* coverity[misra_c_2012_rule_11_5_violation] */
6291 pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
6293 vTaskGetInfo( ( TaskHandle_t ) pxTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
6299 mtCOVERAGE_TEST_MARKER();
6305 #endif /* configUSE_TRACE_FACILITY */
6306 /*-----------------------------------------------------------*/
6308 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
6310 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
6312 configSTACK_DEPTH_TYPE uxCount = 0U;
6314 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
6316 pucStackByte -= portSTACK_GROWTH;
6320 uxCount /= ( configSTACK_DEPTH_TYPE ) sizeof( StackType_t );
6325 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
6326 /*-----------------------------------------------------------*/
6328 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
6330 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
6331 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
6332 * user to determine the return type. It gets around the problem of the value
6333 * overflowing on 8-bit types without breaking backward compatibility for
6334 * applications that expect an 8-bit return type. */
6335 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
6338 uint8_t * pucEndOfStack;
6339 configSTACK_DEPTH_TYPE uxReturn;
6341 traceENTER_uxTaskGetStackHighWaterMark2( xTask );
6343 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
6344 * the same except for their return type. Using configSTACK_DEPTH_TYPE
6345 * allows the user to determine the return type. It gets around the
6346 * problem of the value overflowing on 8-bit types without breaking
6347 * backward compatibility for applications that expect an 8-bit return
6350 pxTCB = prvGetTCBFromHandle( xTask );
6352 #if portSTACK_GROWTH < 0
6354 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6358 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6362 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
6364 traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
6369 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
6370 /*-----------------------------------------------------------*/
6372 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
6374 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
6377 uint8_t * pucEndOfStack;
6378 UBaseType_t uxReturn;
6380 traceENTER_uxTaskGetStackHighWaterMark( xTask );
6382 pxTCB = prvGetTCBFromHandle( xTask );
6384 #if portSTACK_GROWTH < 0
6386 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6390 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6394 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
6396 traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6401 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6402 /*-----------------------------------------------------------*/
6404 #if ( INCLUDE_vTaskDelete == 1 )
6406 static void prvDeleteTCB( TCB_t * pxTCB )
6408 /* This call is required specifically for the TriCore port. It must be
6409 * above the vPortFree() calls. The call is also used by ports/demos that
6410 * want to allocate and clean RAM statically. */
6411 portCLEAN_UP_TCB( pxTCB );
6413 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6415 /* Free up the memory allocated for the task's TLS Block. */
6416 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6420 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6422 /* The task can only have been allocated dynamically - free both
6423 * the stack and TCB. */
6424 vPortFreeStack( pxTCB->pxStack );
6427 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
6429 /* The task could have been allocated statically or dynamically, so
6430 * check what was statically allocated before trying to free the
6432 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6434 /* Both the stack and TCB were allocated dynamically, so both
6436 vPortFreeStack( pxTCB->pxStack );
6439 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6441 /* Only the stack was statically allocated, so the TCB is the
6442 * only memory that must be freed. */
6447 /* Neither the stack nor the TCB were allocated dynamically, so
6448 * nothing needs to be freed. */
6449 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6450 mtCOVERAGE_TEST_MARKER();
6453 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6456 #endif /* INCLUDE_vTaskDelete */
6457 /*-----------------------------------------------------------*/
6459 static void prvResetNextTaskUnblockTime( void )
6461 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6463 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
6464 * the maximum possible value so it is extremely unlikely that the
6465 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6466 * there is an item in the delayed list. */
6467 xNextTaskUnblockTime = portMAX_DELAY;
6471 /* The new current delayed list is not empty, get the value of
6472 * the item at the head of the delayed list. This is the time at
6473 * which the task at the head of the delayed list should be removed
6474 * from the Blocked state. */
6475 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6478 /*-----------------------------------------------------------*/
6480 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6482 #if ( configNUMBER_OF_CORES == 1 )
6483 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6485 TaskHandle_t xReturn;
6487 traceENTER_xTaskGetCurrentTaskHandle();
6489 /* A critical section is not required as this is not called from
6490 * an interrupt and the current TCB will always be the same for any
6491 * individual execution thread. */
6492 xReturn = pxCurrentTCB;
6494 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6498 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6499 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6501 TaskHandle_t xReturn;
6502 UBaseType_t uxSavedInterruptStatus;
6504 traceENTER_xTaskGetCurrentTaskHandle();
6506 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6508 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6510 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6512 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6516 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6518 TaskHandle_t xTaskGetCurrentTaskHandleForCore( BaseType_t xCoreID )
6520 TaskHandle_t xReturn = NULL;
6522 traceENTER_xTaskGetCurrentTaskHandleForCore( xCoreID );
6524 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6526 #if ( configNUMBER_OF_CORES == 1 )
6527 xReturn = pxCurrentTCB;
6528 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6529 xReturn = pxCurrentTCBs[ xCoreID ];
6530 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6533 traceRETURN_xTaskGetCurrentTaskHandleForCore( xReturn );
6538 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
6539 /*-----------------------------------------------------------*/
6541 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6543 BaseType_t xTaskGetSchedulerState( void )
6547 traceENTER_xTaskGetSchedulerState();
6549 if( xSchedulerRunning == pdFALSE )
6551 xReturn = taskSCHEDULER_NOT_STARTED;
6555 #if ( configNUMBER_OF_CORES > 1 )
6556 taskENTER_CRITICAL();
6559 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6561 xReturn = taskSCHEDULER_RUNNING;
6565 xReturn = taskSCHEDULER_SUSPENDED;
6568 #if ( configNUMBER_OF_CORES > 1 )
6569 taskEXIT_CRITICAL();
6573 traceRETURN_xTaskGetSchedulerState( xReturn );
6578 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6579 /*-----------------------------------------------------------*/
6581 #if ( configUSE_MUTEXES == 1 )
6583 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6585 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6586 BaseType_t xReturn = pdFALSE;
6588 traceENTER_xTaskPriorityInherit( pxMutexHolder );
6590 /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6591 * inheritance is not applied in this scenario. */
6592 if( pxMutexHolder != NULL )
6594 /* If the holder of the mutex has a priority below the priority of
6595 * the task attempting to obtain the mutex then it will temporarily
6596 * inherit the priority of the task attempting to obtain the mutex. */
6597 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6599 /* Adjust the mutex holder state to account for its new
6600 * priority. Only reset the event list item value if the value is
6601 * not being used for anything else. */
6602 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
6604 listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority );
6608 mtCOVERAGE_TEST_MARKER();
6611 /* If the task being modified is in the ready state it will need
6612 * to be moved into a new list. */
6613 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6615 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6617 /* It is known that the task is in its ready list so
6618 * there is no need to check again and the port level
6619 * reset macro can be called directly. */
6620 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6624 mtCOVERAGE_TEST_MARKER();
6627 /* Inherit the priority before being moved into the new list. */
6628 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6629 prvAddTaskToReadyList( pxMutexHolderTCB );
6630 #if ( configNUMBER_OF_CORES > 1 )
6632 /* The priority of the task is raised. Yield for this task
6633 * if it is not running. */
6634 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6636 prvYieldForTask( pxMutexHolderTCB );
6639 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6643 /* Just inherit the priority. */
6644 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6647 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6649 /* Inheritance occurred. */
6654 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6656 /* The base priority of the mutex holder is lower than the
6657 * priority of the task attempting to take the mutex, but the
6658 * current priority of the mutex holder is not lower than the
6659 * priority of the task attempting to take the mutex.
6660 * Therefore the mutex holder must have already inherited a
6661 * priority, but inheritance would have occurred if that had
6662 * not been the case. */
6667 mtCOVERAGE_TEST_MARKER();
6673 mtCOVERAGE_TEST_MARKER();
6676 traceRETURN_xTaskPriorityInherit( xReturn );
6681 #endif /* configUSE_MUTEXES */
6682 /*-----------------------------------------------------------*/
6684 #if ( configUSE_MUTEXES == 1 )
6686 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6688 TCB_t * const pxTCB = pxMutexHolder;
6689 BaseType_t xReturn = pdFALSE;
6691 traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6693 if( pxMutexHolder != NULL )
6695 /* A task can only have an inherited priority if it holds the mutex.
6696 * If the mutex is held by a task then it cannot be given from an
6697 * interrupt, and if a mutex is given by the holding task then it must
6698 * be the running state task. */
6699 configASSERT( pxTCB == pxCurrentTCB );
6700 configASSERT( pxTCB->uxMutexesHeld );
6701 ( pxTCB->uxMutexesHeld )--;
6703 /* Has the holder of the mutex inherited the priority of another
6705 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6707 /* Only disinherit if no other mutexes are held. */
6708 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6710 /* A task can only have an inherited priority if it holds
6711 * the mutex. If the mutex is held by a task then it cannot be
6712 * given from an interrupt, and if a mutex is given by the
6713 * holding task then it must be the running state task. Remove
6714 * the holding task from the ready list. */
6715 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6717 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6721 mtCOVERAGE_TEST_MARKER();
6724 /* Disinherit the priority before adding the task into the
6725 * new ready list. */
6726 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6727 pxTCB->uxPriority = pxTCB->uxBasePriority;
6729 /* Reset the event list item value. It cannot be in use for
6730 * any other purpose if this task is running, and it must be
6731 * running to give back the mutex. */
6732 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority );
6733 prvAddTaskToReadyList( pxTCB );
6734 #if ( configNUMBER_OF_CORES > 1 )
6736 /* The priority of the task is dropped. Yield the core on
6737 * which the task is running. */
6738 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6740 prvYieldCore( pxTCB->xTaskRunState );
6743 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6745 /* Return true to indicate that a context switch is required.
6746 * This is only actually required in the corner case whereby
6747 * multiple mutexes were held and the mutexes were given back
6748 * in an order different to that in which they were taken.
6749 * If a context switch did not occur when the first mutex was
6750 * returned, even if a task was waiting on it, then a context
6751 * switch should occur when the last mutex is returned whether
6752 * a task is waiting on it or not. */
6757 mtCOVERAGE_TEST_MARKER();
6762 mtCOVERAGE_TEST_MARKER();
6767 mtCOVERAGE_TEST_MARKER();
6770 traceRETURN_xTaskPriorityDisinherit( xReturn );
6775 #endif /* configUSE_MUTEXES */
6776 /*-----------------------------------------------------------*/
6778 #if ( configUSE_MUTEXES == 1 )
6780 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6781 UBaseType_t uxHighestPriorityWaitingTask )
6783 TCB_t * const pxTCB = pxMutexHolder;
6784 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6785 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6787 traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6789 if( pxMutexHolder != NULL )
6791 /* If pxMutexHolder is not NULL then the holder must hold at least
6793 configASSERT( pxTCB->uxMutexesHeld );
6795 /* Determine the priority to which the priority of the task that
6796 * holds the mutex should be set. This will be the greater of the
6797 * holding task's base priority and the priority of the highest
6798 * priority task that is waiting to obtain the mutex. */
6799 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6801 uxPriorityToUse = uxHighestPriorityWaitingTask;
6805 uxPriorityToUse = pxTCB->uxBasePriority;
6808 /* Does the priority need to change? */
6809 if( pxTCB->uxPriority != uxPriorityToUse )
6811 /* Only disinherit if no other mutexes are held. This is a
6812 * simplification in the priority inheritance implementation. If
6813 * the task that holds the mutex is also holding other mutexes then
6814 * the other mutexes may have caused the priority inheritance. */
6815 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6817 /* If a task has timed out because it already holds the
6818 * mutex it was trying to obtain then it cannot of inherited
6819 * its own priority. */
6820 configASSERT( pxTCB != pxCurrentTCB );
6822 /* Disinherit the priority, remembering the previous
6823 * priority to facilitate determining the subject task's
6825 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6826 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6827 pxTCB->uxPriority = uxPriorityToUse;
6829 /* Only reset the event list item value if the value is not
6830 * being used for anything else. */
6831 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
6833 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse );
6837 mtCOVERAGE_TEST_MARKER();
6840 /* If the running task is not the task that holds the mutex
6841 * then the task that holds the mutex could be in either the
6842 * Ready, Blocked or Suspended states. Only remove the task
6843 * from its current state list if it is in the Ready state as
6844 * the task's priority is going to change and there is one
6845 * Ready list per priority. */
6846 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6848 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6850 /* It is known that the task is in its ready list so
6851 * there is no need to check again and the port level
6852 * reset macro can be called directly. */
6853 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6857 mtCOVERAGE_TEST_MARKER();
6860 prvAddTaskToReadyList( pxTCB );
6861 #if ( configNUMBER_OF_CORES > 1 )
6863 /* The priority of the task is dropped. Yield the core on
6864 * which the task is running. */
6865 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6867 prvYieldCore( pxTCB->xTaskRunState );
6870 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6874 mtCOVERAGE_TEST_MARKER();
6879 mtCOVERAGE_TEST_MARKER();
6884 mtCOVERAGE_TEST_MARKER();
6889 mtCOVERAGE_TEST_MARKER();
6892 traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6895 #endif /* configUSE_MUTEXES */
6896 /*-----------------------------------------------------------*/
6898 #if ( configNUMBER_OF_CORES > 1 )
6900 /* If not in a critical section then yield immediately.
6901 * Otherwise set xYieldPendings to true to wait to
6902 * yield until exiting the critical section.
6904 void vTaskYieldWithinAPI( void )
6906 traceENTER_vTaskYieldWithinAPI();
6908 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6914 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6917 traceRETURN_vTaskYieldWithinAPI();
6919 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6921 /*-----------------------------------------------------------*/
6923 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6925 void vTaskEnterCritical( void )
6927 traceENTER_vTaskEnterCritical();
6929 portDISABLE_INTERRUPTS();
6931 if( xSchedulerRunning != pdFALSE )
6933 ( pxCurrentTCB->uxCriticalNesting )++;
6935 /* This is not the interrupt safe version of the enter critical
6936 * function so assert() if it is being called from an interrupt
6937 * context. Only API functions that end in "FromISR" can be used in an
6938 * interrupt. Only assert if the critical nesting count is 1 to
6939 * protect against recursive calls if the assert function also uses a
6940 * critical section. */
6941 if( pxCurrentTCB->uxCriticalNesting == 1U )
6943 portASSERT_IF_IN_ISR();
6948 mtCOVERAGE_TEST_MARKER();
6951 traceRETURN_vTaskEnterCritical();
6954 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6955 /*-----------------------------------------------------------*/
6957 #if ( configNUMBER_OF_CORES > 1 )
6959 void vTaskEnterCritical( void )
6961 traceENTER_vTaskEnterCritical();
6963 portDISABLE_INTERRUPTS();
6965 if( xSchedulerRunning != pdFALSE )
6967 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6969 portGET_TASK_LOCK();
6973 portINCREMENT_CRITICAL_NESTING_COUNT();
6975 /* This is not the interrupt safe version of the enter critical
6976 * function so assert() if it is being called from an interrupt
6977 * context. Only API functions that end in "FromISR" can be used in an
6978 * interrupt. Only assert if the critical nesting count is 1 to
6979 * protect against recursive calls if the assert function also uses a
6980 * critical section. */
6981 if( portGET_CRITICAL_NESTING_COUNT() == 1U )
6983 portASSERT_IF_IN_ISR();
6985 if( uxSchedulerSuspended == 0U )
6987 /* The only time there would be a problem is if this is called
6988 * before a context switch and vTaskExitCritical() is called
6989 * after pxCurrentTCB changes. Therefore this should not be
6990 * used within vTaskSwitchContext(). */
6991 prvCheckForRunStateChange();
6997 mtCOVERAGE_TEST_MARKER();
7000 traceRETURN_vTaskEnterCritical();
7003 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7005 /*-----------------------------------------------------------*/
7007 #if ( configNUMBER_OF_CORES > 1 )
7009 UBaseType_t vTaskEnterCriticalFromISR( void )
7011 UBaseType_t uxSavedInterruptStatus = 0;
7013 traceENTER_vTaskEnterCriticalFromISR();
7015 if( xSchedulerRunning != pdFALSE )
7017 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
7019 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7024 portINCREMENT_CRITICAL_NESTING_COUNT();
7028 mtCOVERAGE_TEST_MARKER();
7031 traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
7033 return uxSavedInterruptStatus;
7036 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7037 /*-----------------------------------------------------------*/
7039 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
7041 void vTaskExitCritical( void )
7043 traceENTER_vTaskExitCritical();
7045 if( xSchedulerRunning != pdFALSE )
7047 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
7048 * does not match a previous call to vTaskEnterCritical(). */
7049 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
7051 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7052 * to exit critical section from ISR. */
7053 portASSERT_IF_IN_ISR();
7055 if( pxCurrentTCB->uxCriticalNesting > 0U )
7057 ( pxCurrentTCB->uxCriticalNesting )--;
7059 if( pxCurrentTCB->uxCriticalNesting == 0U )
7061 portENABLE_INTERRUPTS();
7065 mtCOVERAGE_TEST_MARKER();
7070 mtCOVERAGE_TEST_MARKER();
7075 mtCOVERAGE_TEST_MARKER();
7078 traceRETURN_vTaskExitCritical();
7081 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
7082 /*-----------------------------------------------------------*/
7084 #if ( configNUMBER_OF_CORES > 1 )
7086 void vTaskExitCritical( void )
7088 traceENTER_vTaskExitCritical();
7090 if( xSchedulerRunning != pdFALSE )
7092 /* If critical nesting count is zero then this function
7093 * does not match a previous call to vTaskEnterCritical(). */
7094 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7096 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7097 * to exit critical section from ISR. */
7098 portASSERT_IF_IN_ISR();
7100 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7102 portDECREMENT_CRITICAL_NESTING_COUNT();
7104 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7106 BaseType_t xYieldCurrentTask;
7108 /* Get the xYieldPending stats inside the critical section. */
7109 xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
7111 portRELEASE_ISR_LOCK();
7112 portRELEASE_TASK_LOCK();
7113 portENABLE_INTERRUPTS();
7115 /* When a task yields in a critical section it just sets
7116 * xYieldPending to true. So now that we have exited the
7117 * critical section check if xYieldPending is true, and
7119 if( xYieldCurrentTask != pdFALSE )
7126 mtCOVERAGE_TEST_MARKER();
7131 mtCOVERAGE_TEST_MARKER();
7136 mtCOVERAGE_TEST_MARKER();
7139 traceRETURN_vTaskExitCritical();
7142 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7143 /*-----------------------------------------------------------*/
7145 #if ( configNUMBER_OF_CORES > 1 )
7147 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
7149 traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
7151 if( xSchedulerRunning != pdFALSE )
7153 /* If critical nesting count is zero then this function
7154 * does not match a previous call to vTaskEnterCritical(). */
7155 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7157 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7159 portDECREMENT_CRITICAL_NESTING_COUNT();
7161 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7163 portRELEASE_ISR_LOCK();
7164 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
7168 mtCOVERAGE_TEST_MARKER();
7173 mtCOVERAGE_TEST_MARKER();
7178 mtCOVERAGE_TEST_MARKER();
7181 traceRETURN_vTaskExitCriticalFromISR();
7184 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7185 /*-----------------------------------------------------------*/
7187 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
7189 static char * prvWriteNameToBuffer( char * pcBuffer,
7190 const char * pcTaskName )
7194 /* Start by copying the entire string. */
7195 ( void ) strcpy( pcBuffer, pcTaskName );
7197 /* Pad the end of the string with spaces to ensure columns line up when
7199 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
7201 pcBuffer[ x ] = ' ';
7205 pcBuffer[ x ] = ( char ) 0x00;
7207 /* Return the new end of string. */
7208 return &( pcBuffer[ x ] );
7211 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
7212 /*-----------------------------------------------------------*/
7214 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
7216 void vTaskListTasks( char * pcWriteBuffer,
7217 size_t uxBufferLength )
7219 TaskStatus_t * pxTaskStatusArray;
7220 size_t uxConsumedBufferLength = 0;
7221 size_t uxCharsWrittenBySnprintf;
7222 int iSnprintfReturnValue;
7223 BaseType_t xOutputBufferFull = pdFALSE;
7224 UBaseType_t uxArraySize, x;
7227 traceENTER_vTaskListTasks( pcWriteBuffer, uxBufferLength );
7232 * This function is provided for convenience only, and is used by many
7233 * of the demo applications. Do not consider it to be part of the
7236 * vTaskListTasks() calls uxTaskGetSystemState(), then formats part of the
7237 * uxTaskGetSystemState() output into a human readable table that
7238 * displays task: names, states, priority, stack usage and task number.
7239 * Stack usage specified as the number of unused StackType_t words stack can hold
7240 * on top of stack - not the number of bytes.
7242 * vTaskListTasks() has a dependency on the snprintf() C library function that
7243 * might bloat the code size, use a lot of stack, and provide different
7244 * results on different platforms. An alternative, tiny, third party,
7245 * and limited functionality implementation of snprintf() is provided in
7246 * many of the FreeRTOS/Demo sub-directories in a file called
7247 * printf-stdarg.c (note printf-stdarg.c does not provide a full
7248 * snprintf() implementation!).
7250 * It is recommended that production systems call uxTaskGetSystemState()
7251 * directly to get access to raw stats data, rather than indirectly
7252 * through a call to vTaskListTasks().
7256 /* Make sure the write buffer does not contain a string. */
7257 *pcWriteBuffer = ( char ) 0x00;
7259 /* Take a snapshot of the number of tasks in case it changes while this
7260 * function is executing. */
7261 uxArraySize = uxCurrentNumberOfTasks;
7263 /* Allocate an array index for each task. NOTE! if
7264 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7265 * equate to NULL. */
7266 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7267 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7268 /* coverity[misra_c_2012_rule_11_5_violation] */
7269 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7271 if( pxTaskStatusArray != NULL )
7273 /* Generate the (binary) data. */
7274 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
7276 /* Create a human readable table from the binary data. */
7277 for( x = 0; x < uxArraySize; x++ )
7279 switch( pxTaskStatusArray[ x ].eCurrentState )
7282 cStatus = tskRUNNING_CHAR;
7286 cStatus = tskREADY_CHAR;
7290 cStatus = tskBLOCKED_CHAR;
7294 cStatus = tskSUSPENDED_CHAR;
7298 cStatus = tskDELETED_CHAR;
7301 case eInvalid: /* Fall through. */
7302 default: /* Should not get here, but it is included
7303 * to prevent static checking errors. */
7304 cStatus = ( char ) 0x00;
7308 /* Is there enough space in the buffer to hold task name? */
7309 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7311 /* Write the task name to the string, padding with spaces so it
7312 * can be printed in tabular form more easily. */
7313 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7314 /* Do not count the terminating null character. */
7315 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7317 /* Is there space left in the buffer? -1 is done because snprintf
7318 * writes a terminating null character. So we are essentially
7319 * checking if the buffer has space to write at least one non-null
7321 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7323 /* Write the rest of the string. */
7324 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
7325 /* MISRA Ref 21.6.1 [snprintf for utility] */
7326 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7327 /* coverity[misra_c_2012_rule_21_6_violation] */
7328 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7329 uxBufferLength - uxConsumedBufferLength,
7330 "\t%c\t%u\t%u\t%u\t0x%x\r\n",
7332 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7333 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7334 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber,
7335 ( unsigned int ) pxTaskStatusArray[ x ].uxCoreAffinityMask );
7336 #else /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7337 /* MISRA Ref 21.6.1 [snprintf for utility] */
7338 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7339 /* coverity[misra_c_2012_rule_21_6_violation] */
7340 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7341 uxBufferLength - uxConsumedBufferLength,
7342 "\t%c\t%u\t%u\t%u\r\n",
7344 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7345 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7346 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber );
7347 #endif /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7348 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7350 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7351 pcWriteBuffer += uxCharsWrittenBySnprintf;
7355 xOutputBufferFull = pdTRUE;
7360 xOutputBufferFull = pdTRUE;
7363 if( xOutputBufferFull == pdTRUE )
7369 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7370 * is 0 then vPortFree() will be #defined to nothing. */
7371 vPortFree( pxTaskStatusArray );
7375 mtCOVERAGE_TEST_MARKER();
7378 traceRETURN_vTaskListTasks();
7381 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7382 /*----------------------------------------------------------*/
7384 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
7386 void vTaskGetRunTimeStatistics( char * pcWriteBuffer,
7387 size_t uxBufferLength )
7389 TaskStatus_t * pxTaskStatusArray;
7390 size_t uxConsumedBufferLength = 0;
7391 size_t uxCharsWrittenBySnprintf;
7392 int iSnprintfReturnValue;
7393 BaseType_t xOutputBufferFull = pdFALSE;
7394 UBaseType_t uxArraySize, x;
7395 configRUN_TIME_COUNTER_TYPE ulTotalTime = 0;
7396 configRUN_TIME_COUNTER_TYPE ulStatsAsPercentage;
7398 traceENTER_vTaskGetRunTimeStatistics( pcWriteBuffer, uxBufferLength );
7403 * This function is provided for convenience only, and is used by many
7404 * of the demo applications. Do not consider it to be part of the
7407 * vTaskGetRunTimeStatistics() calls uxTaskGetSystemState(), then formats part
7408 * of the uxTaskGetSystemState() output into a human readable table that
7409 * displays the amount of time each task has spent in the Running state
7410 * in both absolute and percentage terms.
7412 * vTaskGetRunTimeStatistics() has a dependency on the snprintf() C library
7413 * function that might bloat the code size, use a lot of stack, and
7414 * provide different results on different platforms. An alternative,
7415 * tiny, third party, and limited functionality implementation of
7416 * snprintf() is provided in many of the FreeRTOS/Demo sub-directories in
7417 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
7418 * a full snprintf() implementation!).
7420 * It is recommended that production systems call uxTaskGetSystemState()
7421 * directly to get access to raw stats data, rather than indirectly
7422 * through a call to vTaskGetRunTimeStatistics().
7425 /* Make sure the write buffer does not contain a string. */
7426 *pcWriteBuffer = ( char ) 0x00;
7428 /* Take a snapshot of the number of tasks in case it changes while this
7429 * function is executing. */
7430 uxArraySize = uxCurrentNumberOfTasks;
7432 /* Allocate an array index for each task. NOTE! If
7433 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7434 * equate to NULL. */
7435 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7436 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7437 /* coverity[misra_c_2012_rule_11_5_violation] */
7438 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7440 if( pxTaskStatusArray != NULL )
7442 /* Generate the (binary) data. */
7443 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
7445 /* For percentage calculations. */
7446 ulTotalTime /= ( ( configRUN_TIME_COUNTER_TYPE ) 100U );
7448 /* Avoid divide by zero errors. */
7449 if( ulTotalTime > 0U )
7451 /* Create a human readable table from the binary data. */
7452 for( x = 0; x < uxArraySize; x++ )
7454 /* What percentage of the total run time has the task used?
7455 * This will always be rounded down to the nearest integer.
7456 * ulTotalRunTime has already been divided by 100. */
7457 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
7459 /* Is there enough space in the buffer to hold task name? */
7460 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7462 /* Write the task name to the string, padding with
7463 * spaces so it can be printed in tabular form more
7465 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7466 /* Do not count the terminating null character. */
7467 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7469 /* Is there space left in the buffer? -1 is done because snprintf
7470 * writes a terminating null character. So we are essentially
7471 * checking if the buffer has space to write at least one non-null
7473 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7475 if( ulStatsAsPercentage > 0U )
7477 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7479 /* MISRA Ref 21.6.1 [snprintf for utility] */
7480 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7481 /* coverity[misra_c_2012_rule_21_6_violation] */
7482 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7483 uxBufferLength - uxConsumedBufferLength,
7484 "\t%lu\t\t%lu%%\r\n",
7485 pxTaskStatusArray[ x ].ulRunTimeCounter,
7486 ulStatsAsPercentage );
7488 #else /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7490 /* sizeof( int ) == sizeof( long ) so a smaller
7491 * printf() library can be used. */
7492 /* MISRA Ref 21.6.1 [snprintf for utility] */
7493 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7494 /* coverity[misra_c_2012_rule_21_6_violation] */
7495 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7496 uxBufferLength - uxConsumedBufferLength,
7498 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter,
7499 ( unsigned int ) ulStatsAsPercentage );
7501 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7505 /* If the percentage is zero here then the task has
7506 * consumed less than 1% of the total run time. */
7507 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7509 /* MISRA Ref 21.6.1 [snprintf for utility] */
7510 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7511 /* coverity[misra_c_2012_rule_21_6_violation] */
7512 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7513 uxBufferLength - uxConsumedBufferLength,
7514 "\t%lu\t\t<1%%\r\n",
7515 pxTaskStatusArray[ x ].ulRunTimeCounter );
7519 /* sizeof( int ) == sizeof( long ) so a smaller
7520 * printf() library can be used. */
7521 /* MISRA Ref 21.6.1 [snprintf for utility] */
7522 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7523 /* coverity[misra_c_2012_rule_21_6_violation] */
7524 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7525 uxBufferLength - uxConsumedBufferLength,
7527 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter );
7529 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7532 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7533 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7534 pcWriteBuffer += uxCharsWrittenBySnprintf;
7538 xOutputBufferFull = pdTRUE;
7543 xOutputBufferFull = pdTRUE;
7546 if( xOutputBufferFull == pdTRUE )
7554 mtCOVERAGE_TEST_MARKER();
7557 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7558 * is 0 then vPortFree() will be #defined to nothing. */
7559 vPortFree( pxTaskStatusArray );
7563 mtCOVERAGE_TEST_MARKER();
7566 traceRETURN_vTaskGetRunTimeStatistics();
7569 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7570 /*-----------------------------------------------------------*/
7572 TickType_t uxTaskResetEventItemValue( void )
7574 TickType_t uxReturn;
7576 traceENTER_uxTaskResetEventItemValue();
7578 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7580 /* Reset the event list item to its normal value - so it can be used with
7581 * queues and semaphores. */
7582 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) );
7584 traceRETURN_uxTaskResetEventItemValue( uxReturn );
7588 /*-----------------------------------------------------------*/
7590 #if ( configUSE_MUTEXES == 1 )
7592 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7596 traceENTER_pvTaskIncrementMutexHeldCount();
7598 pxTCB = pxCurrentTCB;
7600 /* If xSemaphoreCreateMutex() is called before any tasks have been created
7601 * then pxCurrentTCB will be NULL. */
7604 ( pxTCB->uxMutexesHeld )++;
7607 traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7612 #endif /* configUSE_MUTEXES */
7613 /*-----------------------------------------------------------*/
7615 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7617 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7618 BaseType_t xClearCountOnExit,
7619 TickType_t xTicksToWait )
7622 BaseType_t xAlreadyYielded, xShouldBlock = pdFALSE;
7624 traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7626 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7628 /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
7629 * non-deterministic operation. */
7632 /* We MUST enter a critical section to atomically check if a notification
7633 * has occurred and set the flag to indicate that we are waiting for
7634 * a notification. If we do not do so, a notification sent from an ISR
7636 taskENTER_CRITICAL();
7638 /* Only block if the notification count is not already non-zero. */
7639 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0U )
7641 /* Mark this task as waiting for a notification. */
7642 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7644 if( xTicksToWait > ( TickType_t ) 0 )
7646 xShouldBlock = pdTRUE;
7650 mtCOVERAGE_TEST_MARKER();
7655 mtCOVERAGE_TEST_MARKER();
7658 taskEXIT_CRITICAL();
7660 /* We are now out of the critical section but the scheduler is still
7661 * suspended, so we are safe to do non-deterministic operations such
7662 * as prvAddCurrentTaskToDelayedList. */
7663 if( xShouldBlock == pdTRUE )
7665 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7666 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7670 mtCOVERAGE_TEST_MARKER();
7673 xAlreadyYielded = xTaskResumeAll();
7675 /* Force a reschedule if xTaskResumeAll has not already done so. */
7676 if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
7678 taskYIELD_WITHIN_API();
7682 mtCOVERAGE_TEST_MARKER();
7685 taskENTER_CRITICAL();
7687 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7688 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7690 if( ulReturn != 0U )
7692 if( xClearCountOnExit != pdFALSE )
7694 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ( uint32_t ) 0U;
7698 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7703 mtCOVERAGE_TEST_MARKER();
7706 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7708 taskEXIT_CRITICAL();
7710 traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7715 #endif /* configUSE_TASK_NOTIFICATIONS */
7716 /*-----------------------------------------------------------*/
7718 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7720 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7721 uint32_t ulBitsToClearOnEntry,
7722 uint32_t ulBitsToClearOnExit,
7723 uint32_t * pulNotificationValue,
7724 TickType_t xTicksToWait )
7726 BaseType_t xReturn, xAlreadyYielded, xShouldBlock = pdFALSE;
7728 traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7730 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7732 /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
7733 * non-deterministic operation. */
7736 /* We MUST enter a critical section to atomically check and update the
7737 * task notification value. If we do not do so, a notification from
7738 * an ISR will get lost. */
7739 taskENTER_CRITICAL();
7741 /* Only block if a notification is not already pending. */
7742 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7744 /* Clear bits in the task's notification value as bits may get
7745 * set by the notifying task or interrupt. This can be used
7746 * to clear the value to zero. */
7747 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7749 /* Mark this task as waiting for a notification. */
7750 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7752 if( xTicksToWait > ( TickType_t ) 0 )
7754 xShouldBlock = pdTRUE;
7758 mtCOVERAGE_TEST_MARKER();
7763 mtCOVERAGE_TEST_MARKER();
7766 taskEXIT_CRITICAL();
7768 /* We are now out of the critical section but the scheduler is still
7769 * suspended, so we are safe to do non-deterministic operations such
7770 * as prvAddCurrentTaskToDelayedList. */
7771 if( xShouldBlock == pdTRUE )
7773 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7774 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7778 mtCOVERAGE_TEST_MARKER();
7781 xAlreadyYielded = xTaskResumeAll();
7783 /* Force a reschedule if xTaskResumeAll has not already done so. */
7784 if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
7786 taskYIELD_WITHIN_API();
7790 mtCOVERAGE_TEST_MARKER();
7793 taskENTER_CRITICAL();
7795 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7797 if( pulNotificationValue != NULL )
7799 /* Output the current notification value, which may or may not
7801 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7804 /* If ucNotifyValue is set then either the task never entered the
7805 * blocked state (because a notification was already pending) or the
7806 * task unblocked because of a notification. Otherwise the task
7807 * unblocked because of a timeout. */
7808 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7810 /* A notification was not received. */
7815 /* A notification was already pending or a notification was
7816 * received while the task was waiting. */
7817 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7821 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7823 taskEXIT_CRITICAL();
7825 traceRETURN_xTaskGenericNotifyWait( xReturn );
7830 #endif /* configUSE_TASK_NOTIFICATIONS */
7831 /*-----------------------------------------------------------*/
7833 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7835 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7836 UBaseType_t uxIndexToNotify,
7838 eNotifyAction eAction,
7839 uint32_t * pulPreviousNotificationValue )
7842 BaseType_t xReturn = pdPASS;
7843 uint8_t ucOriginalNotifyState;
7845 traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7847 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7848 configASSERT( xTaskToNotify );
7849 pxTCB = xTaskToNotify;
7851 taskENTER_CRITICAL();
7853 if( pulPreviousNotificationValue != NULL )
7855 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7858 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7860 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7865 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7869 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7872 case eSetValueWithOverwrite:
7873 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7876 case eSetValueWithoutOverwrite:
7878 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7880 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7884 /* The value could not be written to the task. */
7892 /* The task is being notified without its notify value being
7898 /* Should not get here if all enums are handled.
7899 * Artificially force an assert by testing a value the
7900 * compiler can't assume is const. */
7901 configASSERT( xTickCount == ( TickType_t ) 0 );
7906 traceTASK_NOTIFY( uxIndexToNotify );
7908 /* If the task is in the blocked state specifically to wait for a
7909 * notification then unblock it now. */
7910 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7912 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7913 prvAddTaskToReadyList( pxTCB );
7915 /* The task should not have been on an event list. */
7916 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7918 #if ( configUSE_TICKLESS_IDLE != 0 )
7920 /* If a task is blocked waiting for a notification then
7921 * xNextTaskUnblockTime might be set to the blocked task's time
7922 * out time. If the task is unblocked for a reason other than
7923 * a timeout xNextTaskUnblockTime is normally left unchanged,
7924 * because it will automatically get reset to a new value when
7925 * the tick count equals xNextTaskUnblockTime. However if
7926 * tickless idling is used it might be more important to enter
7927 * sleep mode at the earliest possible time - so reset
7928 * xNextTaskUnblockTime here to ensure it is updated at the
7929 * earliest possible time. */
7930 prvResetNextTaskUnblockTime();
7934 /* Check if the notified task has a priority above the currently
7935 * executing task. */
7936 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7940 mtCOVERAGE_TEST_MARKER();
7943 taskEXIT_CRITICAL();
7945 traceRETURN_xTaskGenericNotify( xReturn );
7950 #endif /* configUSE_TASK_NOTIFICATIONS */
7951 /*-----------------------------------------------------------*/
7953 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7955 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
7956 UBaseType_t uxIndexToNotify,
7958 eNotifyAction eAction,
7959 uint32_t * pulPreviousNotificationValue,
7960 BaseType_t * pxHigherPriorityTaskWoken )
7963 uint8_t ucOriginalNotifyState;
7964 BaseType_t xReturn = pdPASS;
7965 UBaseType_t uxSavedInterruptStatus;
7967 traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
7969 configASSERT( xTaskToNotify );
7970 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7972 /* RTOS ports that support interrupt nesting have the concept of a
7973 * maximum system call (or maximum API call) interrupt priority.
7974 * Interrupts that are above the maximum system call priority are keep
7975 * permanently enabled, even when the RTOS kernel is in a critical section,
7976 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7977 * is defined in FreeRTOSConfig.h then
7978 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7979 * failure if a FreeRTOS API function is called from an interrupt that has
7980 * been assigned a priority above the configured maximum system call
7981 * priority. Only FreeRTOS functions that end in FromISR can be called
7982 * from interrupts that have been assigned a priority at or (logically)
7983 * below the maximum system call interrupt priority. FreeRTOS maintains a
7984 * separate interrupt safe API to ensure interrupt entry is as fast and as
7985 * simple as possible. More information (albeit Cortex-M specific) is
7986 * provided on the following link:
7987 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7988 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7990 pxTCB = xTaskToNotify;
7992 /* MISRA Ref 4.7.1 [Return value shall be checked] */
7993 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
7994 /* coverity[misra_c_2012_directive_4_7_violation] */
7995 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
7997 if( pulPreviousNotificationValue != NULL )
7999 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
8002 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8003 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8008 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
8012 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8015 case eSetValueWithOverwrite:
8016 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8019 case eSetValueWithoutOverwrite:
8021 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
8023 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8027 /* The value could not be written to the task. */
8035 /* The task is being notified without its notify value being
8041 /* Should not get here if all enums are handled.
8042 * Artificially force an assert by testing a value the
8043 * compiler can't assume is const. */
8044 configASSERT( xTickCount == ( TickType_t ) 0 );
8048 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
8050 /* If the task is in the blocked state specifically to wait for a
8051 * notification then unblock it now. */
8052 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8054 /* The task should not have been on an event list. */
8055 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8057 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8059 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8060 prvAddTaskToReadyList( pxTCB );
8064 /* The delayed and ready lists cannot be accessed, so hold
8065 * this task pending until the scheduler is resumed. */
8066 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8069 #if ( configNUMBER_OF_CORES == 1 )
8071 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8073 /* The notified task has a priority above the currently
8074 * executing task so a yield is required. */
8075 if( pxHigherPriorityTaskWoken != NULL )
8077 *pxHigherPriorityTaskWoken = pdTRUE;
8080 /* Mark that a yield is pending in case the user is not
8081 * using the "xHigherPriorityTaskWoken" parameter to an ISR
8082 * safe FreeRTOS function. */
8083 xYieldPendings[ 0 ] = pdTRUE;
8087 mtCOVERAGE_TEST_MARKER();
8090 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8092 #if ( configUSE_PREEMPTION == 1 )
8094 prvYieldForTask( pxTCB );
8096 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8098 if( pxHigherPriorityTaskWoken != NULL )
8100 *pxHigherPriorityTaskWoken = pdTRUE;
8104 #endif /* if ( configUSE_PREEMPTION == 1 ) */
8106 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8109 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8111 traceRETURN_xTaskGenericNotifyFromISR( xReturn );
8116 #endif /* configUSE_TASK_NOTIFICATIONS */
8117 /*-----------------------------------------------------------*/
8119 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8121 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
8122 UBaseType_t uxIndexToNotify,
8123 BaseType_t * pxHigherPriorityTaskWoken )
8126 uint8_t ucOriginalNotifyState;
8127 UBaseType_t uxSavedInterruptStatus;
8129 traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
8131 configASSERT( xTaskToNotify );
8132 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8134 /* RTOS ports that support interrupt nesting have the concept of a
8135 * maximum system call (or maximum API call) interrupt priority.
8136 * Interrupts that are above the maximum system call priority are keep
8137 * permanently enabled, even when the RTOS kernel is in a critical section,
8138 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
8139 * is defined in FreeRTOSConfig.h then
8140 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
8141 * failure if a FreeRTOS API function is called from an interrupt that has
8142 * been assigned a priority above the configured maximum system call
8143 * priority. Only FreeRTOS functions that end in FromISR can be called
8144 * from interrupts that have been assigned a priority at or (logically)
8145 * below the maximum system call interrupt priority. FreeRTOS maintains a
8146 * separate interrupt safe API to ensure interrupt entry is as fast and as
8147 * simple as possible. More information (albeit Cortex-M specific) is
8148 * provided on the following link:
8149 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
8150 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
8152 pxTCB = xTaskToNotify;
8154 /* MISRA Ref 4.7.1 [Return value shall be checked] */
8155 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
8156 /* coverity[misra_c_2012_directive_4_7_violation] */
8157 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
8159 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8160 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8162 /* 'Giving' is equivalent to incrementing a count in a counting
8164 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8166 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
8168 /* If the task is in the blocked state specifically to wait for a
8169 * notification then unblock it now. */
8170 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8172 /* The task should not have been on an event list. */
8173 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8175 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8177 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8178 prvAddTaskToReadyList( pxTCB );
8182 /* The delayed and ready lists cannot be accessed, so hold
8183 * this task pending until the scheduler is resumed. */
8184 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8187 #if ( configNUMBER_OF_CORES == 1 )
8189 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8191 /* The notified task has a priority above the currently
8192 * executing task so a yield is required. */
8193 if( pxHigherPriorityTaskWoken != NULL )
8195 *pxHigherPriorityTaskWoken = pdTRUE;
8198 /* Mark that a yield is pending in case the user is not
8199 * using the "xHigherPriorityTaskWoken" parameter in an ISR
8200 * safe FreeRTOS function. */
8201 xYieldPendings[ 0 ] = pdTRUE;
8205 mtCOVERAGE_TEST_MARKER();
8208 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8210 #if ( configUSE_PREEMPTION == 1 )
8212 prvYieldForTask( pxTCB );
8214 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8216 if( pxHigherPriorityTaskWoken != NULL )
8218 *pxHigherPriorityTaskWoken = pdTRUE;
8222 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
8224 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8227 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8229 traceRETURN_vTaskGenericNotifyGiveFromISR();
8232 #endif /* configUSE_TASK_NOTIFICATIONS */
8233 /*-----------------------------------------------------------*/
8235 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8237 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
8238 UBaseType_t uxIndexToClear )
8243 traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
8245 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8247 /* If null is passed in here then it is the calling task that is having
8248 * its notification state cleared. */
8249 pxTCB = prvGetTCBFromHandle( xTask );
8251 taskENTER_CRITICAL();
8253 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
8255 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
8263 taskEXIT_CRITICAL();
8265 traceRETURN_xTaskGenericNotifyStateClear( xReturn );
8270 #endif /* configUSE_TASK_NOTIFICATIONS */
8271 /*-----------------------------------------------------------*/
8273 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8275 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
8276 UBaseType_t uxIndexToClear,
8277 uint32_t ulBitsToClear )
8282 traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
8284 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8286 /* If null is passed in here then it is the calling task that is having
8287 * its notification state cleared. */
8288 pxTCB = prvGetTCBFromHandle( xTask );
8290 taskENTER_CRITICAL();
8292 /* Return the notification as it was before the bits were cleared,
8293 * then clear the bit mask. */
8294 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
8295 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
8297 taskEXIT_CRITICAL();
8299 traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
8304 #endif /* configUSE_TASK_NOTIFICATIONS */
8305 /*-----------------------------------------------------------*/
8307 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8309 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
8313 traceENTER_ulTaskGetRunTimeCounter( xTask );
8315 pxTCB = prvGetTCBFromHandle( xTask );
8317 traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
8319 return pxTCB->ulRunTimeCounter;
8322 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8323 /*-----------------------------------------------------------*/
8325 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8327 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
8330 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8332 traceENTER_ulTaskGetRunTimePercent( xTask );
8334 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
8336 /* For percentage calculations. */
8337 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8339 /* Avoid divide by zero errors. */
8340 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8342 pxTCB = prvGetTCBFromHandle( xTask );
8343 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
8350 traceRETURN_ulTaskGetRunTimePercent( ulReturn );
8355 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8356 /*-----------------------------------------------------------*/
8358 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8360 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
8362 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
8365 traceENTER_ulTaskGetIdleRunTimeCounter();
8367 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8369 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8372 traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
8377 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8378 /*-----------------------------------------------------------*/
8380 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8382 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
8384 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8385 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
8388 traceENTER_ulTaskGetIdleRunTimePercent();
8390 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
8392 /* For percentage calculations. */
8393 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8395 /* Avoid divide by zero errors. */
8396 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8398 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8400 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8403 ulReturn = ulRunTimeCounter / ulTotalTime;
8410 traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
8415 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8416 /*-----------------------------------------------------------*/
8418 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
8419 const BaseType_t xCanBlockIndefinitely )
8421 TickType_t xTimeToWake;
8422 const TickType_t xConstTickCount = xTickCount;
8423 List_t * const pxDelayedList = pxDelayedTaskList;
8424 List_t * const pxOverflowDelayedList = pxOverflowDelayedTaskList;
8426 #if ( INCLUDE_xTaskAbortDelay == 1 )
8428 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
8429 * reset to pdFALSE so it can be detected as having been set to pdTRUE
8430 * when the task leaves the Blocked state. */
8431 pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
8435 /* Remove the task from the ready list before adding it to the blocked list
8436 * as the same list item is used for both lists. */
8437 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
8439 /* The current task must be in a ready list, so there is no need to
8440 * check, and the port reset macro can be called directly. */
8441 portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
8445 mtCOVERAGE_TEST_MARKER();
8448 #if ( INCLUDE_vTaskSuspend == 1 )
8450 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
8452 /* Add the task to the suspended task list instead of a delayed task
8453 * list to ensure it is not woken by a timing event. It will block
8455 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
8459 /* Calculate the time at which the task should be woken if the event
8460 * does not occur. This may overflow but this doesn't matter, the
8461 * kernel will manage it correctly. */
8462 xTimeToWake = xConstTickCount + xTicksToWait;
8464 /* The list item will be inserted in wake time order. */
8465 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8467 if( xTimeToWake < xConstTickCount )
8469 /* Wake time has overflowed. Place this item in the overflow
8471 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8472 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8476 /* The wake time has not overflowed, so the current block list
8478 traceMOVED_TASK_TO_DELAYED_LIST();
8479 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8481 /* If the task entering the blocked state was placed at the
8482 * head of the list of blocked tasks then xNextTaskUnblockTime
8483 * needs to be updated too. */
8484 if( xTimeToWake < xNextTaskUnblockTime )
8486 xNextTaskUnblockTime = xTimeToWake;
8490 mtCOVERAGE_TEST_MARKER();
8495 #else /* INCLUDE_vTaskSuspend */
8497 /* Calculate the time at which the task should be woken if the event
8498 * does not occur. This may overflow but this doesn't matter, the kernel
8499 * will manage it correctly. */
8500 xTimeToWake = xConstTickCount + xTicksToWait;
8502 /* The list item will be inserted in wake time order. */
8503 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8505 if( xTimeToWake < xConstTickCount )
8507 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8508 /* Wake time has overflowed. Place this item in the overflow list. */
8509 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8513 traceMOVED_TASK_TO_DELAYED_LIST();
8514 /* The wake time has not overflowed, so the current block list is used. */
8515 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8517 /* If the task entering the blocked state was placed at the head of the
8518 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
8520 if( xTimeToWake < xNextTaskUnblockTime )
8522 xNextTaskUnblockTime = xTimeToWake;
8526 mtCOVERAGE_TEST_MARKER();
8530 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
8531 ( void ) xCanBlockIndefinitely;
8533 #endif /* INCLUDE_vTaskSuspend */
8535 /*-----------------------------------------------------------*/
8537 #if ( portUSING_MPU_WRAPPERS == 1 )
8539 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
8543 traceENTER_xTaskGetMPUSettings( xTask );
8545 pxTCB = prvGetTCBFromHandle( xTask );
8547 traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
8549 return &( pxTCB->xMPUSettings );
8552 #endif /* portUSING_MPU_WRAPPERS */
8553 /*-----------------------------------------------------------*/
8555 /* Code below here allows additional code to be inserted into this source file,
8556 * especially where access to file scope functions and data is needed (for example
8557 * when performing module tests). */
8559 #ifdef FREERTOS_MODULE_TEST
8560 #include "tasks_test_access_functions.h"
8564 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
8566 #include "freertos_tasks_c_additions.h"
8568 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8569 static void freertos_tasks_c_additions_init( void )
8571 FREERTOS_TASKS_C_ADDITIONS_INIT();
8575 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8576 /*-----------------------------------------------------------*/
8578 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8581 * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8582 * to provide the memory that is used by the Idle task. It is used when
8583 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8584 * it's own implementation of vApplicationGetIdleTaskMemory by setting
8585 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8587 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8588 StackType_t ** ppxIdleTaskStackBuffer,
8589 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize )
8591 static StaticTask_t xIdleTaskTCB;
8592 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8594 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8595 *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8596 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8599 #if ( configNUMBER_OF_CORES > 1 )
8601 void vApplicationGetPassiveIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8602 StackType_t ** ppxIdleTaskStackBuffer,
8603 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize,
8604 BaseType_t xPassiveIdleTaskIndex )
8606 static StaticTask_t xIdleTaskTCBs[ configNUMBER_OF_CORES - 1 ];
8607 static StackType_t uxIdleTaskStacks[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
8609 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCBs[ xPassiveIdleTaskIndex ] );
8610 *ppxIdleTaskStackBuffer = &( uxIdleTaskStacks[ xPassiveIdleTaskIndex ][ 0 ] );
8611 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8614 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
8616 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8617 /*-----------------------------------------------------------*/
8619 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8622 * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8623 * to provide the memory that is used by the Timer service task. It is used when
8624 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8625 * it's own implementation of vApplicationGetTimerTaskMemory by setting
8626 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8628 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8629 StackType_t ** ppxTimerTaskStackBuffer,
8630 configSTACK_DEPTH_TYPE * puxTimerTaskStackSize )
8632 static StaticTask_t xTimerTaskTCB;
8633 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8635 *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8636 *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8637 *puxTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8640 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8641 /*-----------------------------------------------------------*/
8644 * Reset the state in this file. This state is normally initialized at start up.
8645 * This function must be called by the application before restarting the
8648 void vTaskResetState( void )
8652 /* Task control block. */
8653 #if ( configNUMBER_OF_CORES == 1 )
8655 pxCurrentTCB = NULL;
8657 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8659 #if ( INCLUDE_vTaskDelete == 1 )
8661 uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
8663 #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
8665 #if ( configUSE_POSIX_ERRNO == 1 )
8669 #endif /* #if ( configUSE_POSIX_ERRNO == 1 ) */
8671 /* Other file private variables. */
8672 uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
8673 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
8674 uxTopReadyPriority = tskIDLE_PRIORITY;
8675 xSchedulerRunning = pdFALSE;
8676 xPendedTicks = ( TickType_t ) 0U;
8678 for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
8680 xYieldPendings[ xCoreID ] = pdFALSE;
8683 xNumOfOverflows = ( BaseType_t ) 0;
8684 uxTaskNumber = ( UBaseType_t ) 0U;
8685 xNextTaskUnblockTime = ( TickType_t ) 0U;
8687 uxSchedulerSuspended = ( UBaseType_t ) 0U;
8689 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8691 for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
8693 ulTaskSwitchedInTime[ xCoreID ] = 0U;
8694 ulTotalRunTime[ xCoreID ] = 0U;
8697 #endif /* #if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8699 /*-----------------------------------------------------------*/