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 service
849 * the pending interrupt and yield. After servicing the pending interrupt,
850 * the task needs to re-evaluate its run state within this loop, as
851 * other cores may have requested this task to yield, potentially altering
854 portDISABLE_INTERRUPTS();
858 portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
860 if( uxPrevCriticalNesting == 0U )
862 portRELEASE_ISR_LOCK();
866 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
868 /*-----------------------------------------------------------*/
870 #if ( configNUMBER_OF_CORES > 1 )
871 static void prvYieldForTask( const TCB_t * pxTCB )
873 BaseType_t xLowestPriorityToPreempt;
874 BaseType_t xCurrentCoreTaskPriority;
875 BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
878 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
879 BaseType_t xYieldCount = 0;
880 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
882 /* This must be called from a critical section. */
883 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
885 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
887 /* No task should yield for this one if it is a lower priority
888 * than priority level of currently ready tasks. */
889 if( pxTCB->uxPriority >= uxTopReadyPriority )
891 /* Yield is not required for a task which is already running. */
892 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
895 xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
897 /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
898 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
899 --xLowestPriorityToPreempt;
901 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
903 xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
905 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
906 if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
908 xCurrentCoreTaskPriority = ( BaseType_t ) ( xCurrentCoreTaskPriority - 1 );
911 if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
913 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
914 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
917 if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
919 #if ( configUSE_CORE_AFFINITY == 1 )
920 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
923 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
924 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
927 xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
928 xLowestPriorityCore = xCoreID;
934 mtCOVERAGE_TEST_MARKER();
938 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
940 /* Yield all currently running non-idle tasks with a priority lower than
941 * the task that needs to run. */
942 if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
943 ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
945 prvYieldCore( xCoreID );
950 mtCOVERAGE_TEST_MARKER();
953 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
957 mtCOVERAGE_TEST_MARKER();
961 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
962 if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
963 #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
964 if( xLowestPriorityCore >= 0 )
965 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
967 prvYieldCore( xLowestPriorityCore );
970 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
971 /* Verify that the calling core always yields to higher priority tasks. */
972 if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) &&
973 ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
975 configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) ||
976 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) );
981 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
982 /*-----------------------------------------------------------*/
984 #if ( configNUMBER_OF_CORES > 1 )
985 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
987 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
988 BaseType_t xTaskScheduled = pdFALSE;
989 BaseType_t xDecrementTopPriority = pdTRUE;
990 TCB_t * pxTCB = NULL;
992 #if ( configUSE_CORE_AFFINITY == 1 )
993 const TCB_t * pxPreviousTCB = NULL;
995 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
996 BaseType_t xPriorityDropped = pdFALSE;
999 /* This function should be called when scheduler is running. */
1000 configASSERT( xSchedulerRunning == pdTRUE );
1002 /* A new task is created and a running task with the same priority yields
1003 * itself to run the new task. When a running task yields itself, it is still
1004 * in the ready list. This running task will be selected before the new task
1005 * since the new task is always added to the end of the ready list.
1006 * The other problem is that the running task still in the same position of
1007 * the ready list when it yields itself. It is possible that it will be selected
1008 * earlier then other tasks which waits longer than this task.
1010 * To fix these problems, the running task should be put to the end of the
1011 * ready list before searching for the ready task in the ready list. */
1012 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
1013 &pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
1015 ( void ) uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
1016 vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
1017 &pxCurrentTCBs[ xCoreID ]->xStateListItem );
1020 while( xTaskScheduled == pdFALSE )
1022 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1024 if( uxCurrentPriority < uxTopReadyPriority )
1026 /* We can't schedule any tasks, other than idle, that have a
1027 * priority lower than the priority of a task currently running
1028 * on another core. */
1029 uxCurrentPriority = tskIDLE_PRIORITY;
1034 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
1036 const List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
1037 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxReadyList );
1038 ListItem_t * pxIterator;
1040 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
1041 * must not be decremented any further. */
1042 xDecrementTopPriority = pdFALSE;
1044 for( pxIterator = listGET_HEAD_ENTRY( pxReadyList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
1046 /* MISRA Ref 11.5.3 [Void pointer assignment] */
1047 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1048 /* coverity[misra_c_2012_rule_11_5_violation] */
1049 pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
1051 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1053 /* When falling back to the idle priority because only one priority
1054 * level is allowed to run at a time, we should ONLY schedule the true
1055 * idle tasks, not user tasks at the idle priority. */
1056 if( uxCurrentPriority < uxTopReadyPriority )
1058 if( ( pxTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U )
1064 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1066 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
1068 #if ( configUSE_CORE_AFFINITY == 1 )
1069 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1072 /* If the task is not being executed by any core swap it in. */
1073 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
1074 #if ( configUSE_CORE_AFFINITY == 1 )
1075 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
1077 pxTCB->xTaskRunState = xCoreID;
1078 pxCurrentTCBs[ xCoreID ] = pxTCB;
1079 xTaskScheduled = pdTRUE;
1082 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
1084 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) );
1086 #if ( configUSE_CORE_AFFINITY == 1 )
1087 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1090 /* The task is already running on this core, mark it as scheduled. */
1091 pxTCB->xTaskRunState = xCoreID;
1092 xTaskScheduled = pdTRUE;
1097 /* This task is running on the core other than xCoreID. */
1098 mtCOVERAGE_TEST_MARKER();
1101 if( xTaskScheduled != pdFALSE )
1103 /* A task has been selected to run on this core. */
1110 if( xDecrementTopPriority != pdFALSE )
1112 uxTopReadyPriority--;
1113 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1115 xPriorityDropped = pdTRUE;
1121 /* There are configNUMBER_OF_CORES Idle tasks created when scheduler started.
1122 * The scheduler should be able to select a task to run when uxCurrentPriority
1123 * is tskIDLE_PRIORITY. uxCurrentPriority is never decreased to value blow
1124 * tskIDLE_PRIORITY. */
1125 if( uxCurrentPriority > tskIDLE_PRIORITY )
1127 uxCurrentPriority--;
1131 /* This function is called when idle task is not created. Break the
1132 * loop to prevent uxCurrentPriority overrun. */
1137 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1139 if( xTaskScheduled == pdTRUE )
1141 if( xPriorityDropped != pdFALSE )
1143 /* There may be several ready tasks that were being prevented from running because there was
1144 * a higher priority task running. Now that the last of the higher priority tasks is no longer
1145 * running, make sure all the other idle tasks yield. */
1148 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUMBER_OF_CORES; x++ )
1150 if( ( pxCurrentTCBs[ x ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1158 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1160 #if ( configUSE_CORE_AFFINITY == 1 )
1162 if( xTaskScheduled == pdTRUE )
1164 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
1166 /* A ready task was just evicted from this core. See if it can be
1167 * scheduled on any other core. */
1168 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
1169 BaseType_t xLowestPriority = ( BaseType_t ) pxPreviousTCB->uxPriority;
1170 BaseType_t xLowestPriorityCore = -1;
1173 if( ( pxPreviousTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1175 xLowestPriority = xLowestPriority - 1;
1178 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1180 /* pxPreviousTCB was removed from this core and this core is not excluded
1181 * from it's core affinity mask.
1183 * pxPreviousTCB is preempted by the new higher priority task
1184 * pxCurrentTCBs[ xCoreID ]. When searching a new core for pxPreviousTCB,
1185 * we do not need to look at the cores on which pxCurrentTCBs[ xCoreID ]
1186 * is allowed to run. The reason is - when more than one cores are
1187 * eligible for an incoming task, we preempt the core with the minimum
1188 * priority task. Because this core (i.e. xCoreID) was preempted for
1189 * pxCurrentTCBs[ xCoreID ], this means that all the others cores
1190 * where pxCurrentTCBs[ xCoreID ] can run, are running tasks with priority
1191 * no lower than pxPreviousTCB's priority. Therefore, the only cores where
1192 * which can be preempted for pxPreviousTCB are the ones where
1193 * pxCurrentTCBs[ xCoreID ] is not allowed to run (and obviously,
1194 * pxPreviousTCB is allowed to run).
1196 * This is an optimization which reduces the number of cores needed to be
1197 * searched for pxPreviousTCB to run. */
1198 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1202 /* pxPreviousTCB's core affinity mask is changed and it is no longer
1203 * allowed to run on this core. Searching all the cores in pxPreviousTCB's
1204 * new core affinity mask to find a core on which it can run. */
1207 uxCoreMap &= ( ( 1U << configNUMBER_OF_CORES ) - 1U );
1209 for( x = ( ( BaseType_t ) configNUMBER_OF_CORES - 1 ); x >= ( BaseType_t ) 0; x-- )
1211 UBaseType_t uxCore = ( UBaseType_t ) x;
1212 BaseType_t xTaskPriority;
1214 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << uxCore ) ) != 0U )
1216 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority;
1218 if( ( pxCurrentTCBs[ uxCore ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1220 xTaskPriority = xTaskPriority - ( BaseType_t ) 1;
1223 uxCoreMap &= ~( ( UBaseType_t ) 1U << uxCore );
1225 if( ( xTaskPriority < xLowestPriority ) &&
1226 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ] ) != pdFALSE ) &&
1227 ( xYieldPendings[ uxCore ] == pdFALSE ) )
1229 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1230 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1233 xLowestPriority = xTaskPriority;
1234 xLowestPriorityCore = ( BaseType_t ) uxCore;
1240 if( xLowestPriorityCore >= 0 )
1242 prvYieldCore( xLowestPriorityCore );
1247 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) */
1250 #endif /* ( configNUMBER_OF_CORES > 1 ) */
1252 /*-----------------------------------------------------------*/
1254 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1256 static TCB_t * prvCreateStaticTask( TaskFunction_t pxTaskCode,
1257 const char * const pcName,
1258 const configSTACK_DEPTH_TYPE uxStackDepth,
1259 void * const pvParameters,
1260 UBaseType_t uxPriority,
1261 StackType_t * const puxStackBuffer,
1262 StaticTask_t * const pxTaskBuffer,
1263 TaskHandle_t * const pxCreatedTask )
1267 configASSERT( puxStackBuffer != NULL );
1268 configASSERT( pxTaskBuffer != NULL );
1270 #if ( configASSERT_DEFINED == 1 )
1272 /* Sanity check that the size of the structure used to declare a
1273 * variable of type StaticTask_t equals the size of the real task
1275 volatile size_t xSize = sizeof( StaticTask_t );
1276 configASSERT( xSize == sizeof( TCB_t ) );
1277 ( void ) xSize; /* Prevent unused variable warning when configASSERT() is not used. */
1279 #endif /* configASSERT_DEFINED */
1281 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1283 /* The memory used for the task's TCB and stack are passed into this
1284 * function - use them. */
1285 /* MISRA Ref 11.3.1 [Misaligned access] */
1286 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
1287 /* coverity[misra_c_2012_rule_11_3_violation] */
1288 pxNewTCB = ( TCB_t * ) pxTaskBuffer;
1289 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1290 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1292 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1294 /* Tasks can be created statically or dynamically, so note this
1295 * task was created statically in case the task is later deleted. */
1296 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1298 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1300 prvInitialiseNewTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1309 /*-----------------------------------------------------------*/
1311 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1312 const char * const pcName,
1313 const configSTACK_DEPTH_TYPE uxStackDepth,
1314 void * const pvParameters,
1315 UBaseType_t uxPriority,
1316 StackType_t * const puxStackBuffer,
1317 StaticTask_t * const pxTaskBuffer )
1319 TaskHandle_t xReturn = NULL;
1322 traceENTER_xTaskCreateStatic( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer );
1324 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1326 if( pxNewTCB != NULL )
1328 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1330 /* Set the task's affinity before scheduling it. */
1331 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1335 prvAddNewTaskToReadyList( pxNewTCB );
1339 mtCOVERAGE_TEST_MARKER();
1342 traceRETURN_xTaskCreateStatic( xReturn );
1346 /*-----------------------------------------------------------*/
1348 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1349 TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1350 const char * const pcName,
1351 const configSTACK_DEPTH_TYPE uxStackDepth,
1352 void * const pvParameters,
1353 UBaseType_t uxPriority,
1354 StackType_t * const puxStackBuffer,
1355 StaticTask_t * const pxTaskBuffer,
1356 UBaseType_t uxCoreAffinityMask )
1358 TaskHandle_t xReturn = NULL;
1361 traceENTER_xTaskCreateStaticAffinitySet( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, uxCoreAffinityMask );
1363 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1365 if( pxNewTCB != NULL )
1367 /* Set the task's affinity before scheduling it. */
1368 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1370 prvAddNewTaskToReadyList( pxNewTCB );
1374 mtCOVERAGE_TEST_MARKER();
1377 traceRETURN_xTaskCreateStaticAffinitySet( xReturn );
1381 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1383 #endif /* SUPPORT_STATIC_ALLOCATION */
1384 /*-----------------------------------------------------------*/
1386 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1387 static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
1388 TaskHandle_t * const pxCreatedTask )
1392 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1393 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1395 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1397 /* Allocate space for the TCB. Where the memory comes from depends
1398 * on the implementation of the port malloc function and whether or
1399 * not static allocation is being used. */
1400 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1401 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1403 /* Store the stack location in the TCB. */
1404 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1406 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1408 /* Tasks can be created statically or dynamically, so note this
1409 * task was created statically in case the task is later deleted. */
1410 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1412 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1414 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1415 pxTaskDefinition->pcName,
1416 pxTaskDefinition->usStackDepth,
1417 pxTaskDefinition->pvParameters,
1418 pxTaskDefinition->uxPriority,
1419 pxCreatedTask, pxNewTCB,
1420 pxTaskDefinition->xRegions );
1429 /*-----------------------------------------------------------*/
1431 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1432 TaskHandle_t * pxCreatedTask )
1437 traceENTER_xTaskCreateRestrictedStatic( pxTaskDefinition, pxCreatedTask );
1439 configASSERT( pxTaskDefinition != NULL );
1441 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1443 if( pxNewTCB != NULL )
1445 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1447 /* Set the task's affinity before scheduling it. */
1448 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1452 prvAddNewTaskToReadyList( pxNewTCB );
1457 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1460 traceRETURN_xTaskCreateRestrictedStatic( xReturn );
1464 /*-----------------------------------------------------------*/
1466 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1467 BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1468 UBaseType_t uxCoreAffinityMask,
1469 TaskHandle_t * pxCreatedTask )
1474 traceENTER_xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1476 configASSERT( pxTaskDefinition != NULL );
1478 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1480 if( pxNewTCB != NULL )
1482 /* Set the task's affinity before scheduling it. */
1483 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1485 prvAddNewTaskToReadyList( pxNewTCB );
1490 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1493 traceRETURN_xTaskCreateRestrictedStaticAffinitySet( xReturn );
1497 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1499 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1500 /*-----------------------------------------------------------*/
1502 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1503 static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
1504 TaskHandle_t * const pxCreatedTask )
1508 configASSERT( pxTaskDefinition->puxStackBuffer );
1510 if( pxTaskDefinition->puxStackBuffer != NULL )
1512 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1513 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1514 /* coverity[misra_c_2012_rule_11_5_violation] */
1515 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1517 if( pxNewTCB != NULL )
1519 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1521 /* Store the stack location in the TCB. */
1522 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1524 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1526 /* Tasks can be created statically or dynamically, so note
1527 * this task had a statically allocated stack in case it is
1528 * later deleted. The TCB was allocated dynamically. */
1529 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1531 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1533 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1534 pxTaskDefinition->pcName,
1535 pxTaskDefinition->usStackDepth,
1536 pxTaskDefinition->pvParameters,
1537 pxTaskDefinition->uxPriority,
1538 pxCreatedTask, pxNewTCB,
1539 pxTaskDefinition->xRegions );
1549 /*-----------------------------------------------------------*/
1551 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1552 TaskHandle_t * pxCreatedTask )
1557 traceENTER_xTaskCreateRestricted( pxTaskDefinition, pxCreatedTask );
1559 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1561 if( pxNewTCB != NULL )
1563 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1565 /* Set the task's affinity before scheduling it. */
1566 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1568 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1570 prvAddNewTaskToReadyList( pxNewTCB );
1576 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1579 traceRETURN_xTaskCreateRestricted( xReturn );
1583 /*-----------------------------------------------------------*/
1585 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1586 BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1587 UBaseType_t uxCoreAffinityMask,
1588 TaskHandle_t * pxCreatedTask )
1593 traceENTER_xTaskCreateRestrictedAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1595 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1597 if( pxNewTCB != NULL )
1599 /* Set the task's affinity before scheduling it. */
1600 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1602 prvAddNewTaskToReadyList( pxNewTCB );
1608 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1611 traceRETURN_xTaskCreateRestrictedAffinitySet( xReturn );
1615 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1618 #endif /* portUSING_MPU_WRAPPERS */
1619 /*-----------------------------------------------------------*/
1621 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1622 static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
1623 const char * const pcName,
1624 const configSTACK_DEPTH_TYPE uxStackDepth,
1625 void * const pvParameters,
1626 UBaseType_t uxPriority,
1627 TaskHandle_t * const pxCreatedTask )
1631 /* If the stack grows down then allocate the stack then the TCB so the stack
1632 * does not grow into the TCB. Likewise if the stack grows up then allocate
1633 * the TCB then the stack. */
1634 #if ( portSTACK_GROWTH > 0 )
1636 /* Allocate space for the TCB. Where the memory comes from depends on
1637 * the implementation of the port malloc function and whether or not static
1638 * allocation is being used. */
1639 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1640 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1641 /* coverity[misra_c_2012_rule_11_5_violation] */
1642 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1644 if( pxNewTCB != NULL )
1646 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1648 /* Allocate space for the stack used by the task being created.
1649 * The base of the stack memory stored in the TCB so the task can
1650 * be deleted later if required. */
1651 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1652 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1653 /* coverity[misra_c_2012_rule_11_5_violation] */
1654 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) uxStackDepth ) * sizeof( StackType_t ) ) );
1656 if( pxNewTCB->pxStack == NULL )
1658 /* Could not allocate the stack. Delete the allocated TCB. */
1659 vPortFree( pxNewTCB );
1664 #else /* portSTACK_GROWTH */
1666 StackType_t * pxStack;
1668 /* Allocate space for the stack used by the task being created. */
1669 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1670 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1671 /* coverity[misra_c_2012_rule_11_5_violation] */
1672 pxStack = pvPortMallocStack( ( ( ( size_t ) uxStackDepth ) * sizeof( StackType_t ) ) );
1674 if( pxStack != NULL )
1676 /* Allocate space for the TCB. */
1677 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1678 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1679 /* coverity[misra_c_2012_rule_11_5_violation] */
1680 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1682 if( pxNewTCB != NULL )
1684 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1686 /* Store the stack location in the TCB. */
1687 pxNewTCB->pxStack = pxStack;
1691 /* The stack cannot be used as the TCB was not created. Free
1693 vPortFreeStack( pxStack );
1701 #endif /* portSTACK_GROWTH */
1703 if( pxNewTCB != NULL )
1705 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1707 /* Tasks can be created statically or dynamically, so note this
1708 * task was created dynamically in case it is later deleted. */
1709 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1711 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1713 prvInitialiseNewTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1718 /*-----------------------------------------------------------*/
1720 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1721 const char * const pcName,
1722 const configSTACK_DEPTH_TYPE uxStackDepth,
1723 void * const pvParameters,
1724 UBaseType_t uxPriority,
1725 TaskHandle_t * const pxCreatedTask )
1730 traceENTER_xTaskCreate( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
1732 pxNewTCB = prvCreateTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
1734 if( pxNewTCB != NULL )
1736 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1738 /* Set the task's affinity before scheduling it. */
1739 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1743 prvAddNewTaskToReadyList( pxNewTCB );
1748 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1751 traceRETURN_xTaskCreate( xReturn );
1755 /*-----------------------------------------------------------*/
1757 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1758 BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1759 const char * const pcName,
1760 const configSTACK_DEPTH_TYPE uxStackDepth,
1761 void * const pvParameters,
1762 UBaseType_t uxPriority,
1763 UBaseType_t uxCoreAffinityMask,
1764 TaskHandle_t * const pxCreatedTask )
1769 traceENTER_xTaskCreateAffinitySet( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, uxCoreAffinityMask, pxCreatedTask );
1771 pxNewTCB = prvCreateTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
1773 if( pxNewTCB != NULL )
1775 /* Set the task's affinity before scheduling it. */
1776 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1778 prvAddNewTaskToReadyList( pxNewTCB );
1783 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1786 traceRETURN_xTaskCreateAffinitySet( xReturn );
1790 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1792 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1793 /*-----------------------------------------------------------*/
1795 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1796 const char * const pcName,
1797 const configSTACK_DEPTH_TYPE uxStackDepth,
1798 void * const pvParameters,
1799 UBaseType_t uxPriority,
1800 TaskHandle_t * const pxCreatedTask,
1802 const MemoryRegion_t * const xRegions )
1804 StackType_t * pxTopOfStack;
1807 #if ( portUSING_MPU_WRAPPERS == 1 )
1808 /* Should the task be created in privileged mode? */
1809 BaseType_t xRunPrivileged;
1811 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1813 xRunPrivileged = pdTRUE;
1817 xRunPrivileged = pdFALSE;
1819 uxPriority &= ~portPRIVILEGE_BIT;
1820 #endif /* portUSING_MPU_WRAPPERS == 1 */
1822 /* Avoid dependency on memset() if it is not required. */
1823 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1825 /* Fill the stack with a known value to assist debugging. */
1826 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) uxStackDepth * sizeof( StackType_t ) );
1828 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1830 /* Calculate the top of stack address. This depends on whether the stack
1831 * grows from high memory to low (as per the 80x86) or vice versa.
1832 * portSTACK_GROWTH is used to make the result positive or negative as required
1834 #if ( portSTACK_GROWTH < 0 )
1836 pxTopOfStack = &( pxNewTCB->pxStack[ uxStackDepth - ( configSTACK_DEPTH_TYPE ) 1 ] );
1837 pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
1839 /* Check the alignment of the calculated top of stack is correct. */
1840 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0U ) );
1842 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1844 /* Also record the stack's high address, which may assist
1846 pxNewTCB->pxEndOfStack = pxTopOfStack;
1848 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1850 #else /* portSTACK_GROWTH */
1852 pxTopOfStack = pxNewTCB->pxStack;
1853 pxTopOfStack = ( StackType_t * ) ( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) + portBYTE_ALIGNMENT_MASK ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
1855 /* Check the alignment of the calculated top of stack is correct. */
1856 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0U ) );
1858 /* The other extreme of the stack space is required if stack checking is
1860 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( uxStackDepth - ( configSTACK_DEPTH_TYPE ) 1 );
1862 #endif /* portSTACK_GROWTH */
1864 /* Store the task name in the TCB. */
1865 if( pcName != NULL )
1867 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1869 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1871 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1872 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1873 * string is not accessible (extremely unlikely). */
1874 if( pcName[ x ] == ( char ) 0x00 )
1880 mtCOVERAGE_TEST_MARKER();
1884 /* Ensure the name string is terminated in the case that the string length
1885 * was greater or equal to configMAX_TASK_NAME_LEN. */
1886 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1U ] = '\0';
1890 mtCOVERAGE_TEST_MARKER();
1893 /* This is used as an array index so must ensure it's not too large. */
1894 configASSERT( uxPriority < configMAX_PRIORITIES );
1896 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1898 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1902 mtCOVERAGE_TEST_MARKER();
1905 pxNewTCB->uxPriority = uxPriority;
1906 #if ( configUSE_MUTEXES == 1 )
1908 pxNewTCB->uxBasePriority = uxPriority;
1910 #endif /* configUSE_MUTEXES */
1912 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1913 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1915 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1916 * back to the containing TCB from a generic item in a list. */
1917 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1919 /* Event lists are always in priority order. */
1920 listSET_LIST_ITEM_VALUE( &( pxNewTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority );
1921 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1923 #if ( portUSING_MPU_WRAPPERS == 1 )
1925 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, uxStackDepth );
1929 /* Avoid compiler warning about unreferenced parameter. */
1934 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
1936 /* Allocate and initialize memory for the task's TLS Block. */
1937 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
1941 /* Initialize the TCB stack to look as if the task was already running,
1942 * but had been interrupted by the scheduler. The return address is set
1943 * to the start of the task function. Once the stack has been initialised
1944 * the top of stack variable is updated. */
1945 #if ( portUSING_MPU_WRAPPERS == 1 )
1947 /* If the port has capability to detect stack overflow,
1948 * pass the stack end address to the stack initialization
1949 * function as well. */
1950 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1952 #if ( portSTACK_GROWTH < 0 )
1954 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1956 #else /* portSTACK_GROWTH */
1958 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1960 #endif /* portSTACK_GROWTH */
1962 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1964 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1966 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1968 #else /* portUSING_MPU_WRAPPERS */
1970 /* If the port has capability to detect stack overflow,
1971 * pass the stack end address to the stack initialization
1972 * function as well. */
1973 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1975 #if ( portSTACK_GROWTH < 0 )
1977 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1979 #else /* portSTACK_GROWTH */
1981 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1983 #endif /* portSTACK_GROWTH */
1985 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1987 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1989 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1991 #endif /* portUSING_MPU_WRAPPERS */
1993 /* Initialize task state and task attributes. */
1994 #if ( configNUMBER_OF_CORES > 1 )
1996 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1998 /* Is this an idle task? */
1999 if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvPassiveIdleTask ) )
2001 pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
2004 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
2006 if( pxCreatedTask != NULL )
2008 /* Pass the handle out in an anonymous way. The handle can be used to
2009 * change the created task's priority, delete the created task, etc.*/
2010 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
2014 mtCOVERAGE_TEST_MARKER();
2017 /*-----------------------------------------------------------*/
2019 #if ( configNUMBER_OF_CORES == 1 )
2021 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
2023 /* Ensure interrupts don't access the task lists while the lists are being
2025 taskENTER_CRITICAL();
2027 uxCurrentNumberOfTasks = ( UBaseType_t ) ( uxCurrentNumberOfTasks + 1U );
2029 if( pxCurrentTCB == NULL )
2031 /* There are no other tasks, or all the other tasks are in
2032 * the suspended state - make this the current task. */
2033 pxCurrentTCB = pxNewTCB;
2035 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2037 /* This is the first task to be created so do the preliminary
2038 * initialisation required. We will not recover if this call
2039 * fails, but we will report the failure. */
2040 prvInitialiseTaskLists();
2044 mtCOVERAGE_TEST_MARKER();
2049 /* If the scheduler is not already running, make this task the
2050 * current task if it is the highest priority task to be created
2052 if( xSchedulerRunning == pdFALSE )
2054 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
2056 pxCurrentTCB = pxNewTCB;
2060 mtCOVERAGE_TEST_MARKER();
2065 mtCOVERAGE_TEST_MARKER();
2071 #if ( configUSE_TRACE_FACILITY == 1 )
2073 /* Add a counter into the TCB for tracing only. */
2074 pxNewTCB->uxTCBNumber = uxTaskNumber;
2076 #endif /* configUSE_TRACE_FACILITY */
2077 traceTASK_CREATE( pxNewTCB );
2079 prvAddTaskToReadyList( pxNewTCB );
2081 portSETUP_TCB( pxNewTCB );
2083 taskEXIT_CRITICAL();
2085 if( xSchedulerRunning != pdFALSE )
2087 /* If the created task is of a higher priority than the current task
2088 * then it should run now. */
2089 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2093 mtCOVERAGE_TEST_MARKER();
2097 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2099 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
2101 /* Ensure interrupts don't access the task lists while the lists are being
2103 taskENTER_CRITICAL();
2105 uxCurrentNumberOfTasks++;
2107 if( xSchedulerRunning == pdFALSE )
2109 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2111 /* This is the first task to be created so do the preliminary
2112 * initialisation required. We will not recover if this call
2113 * fails, but we will report the failure. */
2114 prvInitialiseTaskLists();
2118 mtCOVERAGE_TEST_MARKER();
2121 /* All the cores start with idle tasks before the SMP scheduler
2122 * is running. Idle tasks are assigned to cores when they are
2123 * created in prvCreateIdleTasks(). */
2128 #if ( configUSE_TRACE_FACILITY == 1 )
2130 /* Add a counter into the TCB for tracing only. */
2131 pxNewTCB->uxTCBNumber = uxTaskNumber;
2133 #endif /* configUSE_TRACE_FACILITY */
2134 traceTASK_CREATE( pxNewTCB );
2136 prvAddTaskToReadyList( pxNewTCB );
2138 portSETUP_TCB( pxNewTCB );
2140 if( xSchedulerRunning != pdFALSE )
2142 /* If the created task is of a higher priority than another
2143 * currently running task and preemption is on then it should
2145 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2149 mtCOVERAGE_TEST_MARKER();
2152 taskEXIT_CRITICAL();
2155 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2156 /*-----------------------------------------------------------*/
2158 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
2160 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
2163 size_t uxCharsWritten;
2165 if( iSnprintfReturnValue < 0 )
2167 /* Encoding error - Return 0 to indicate that nothing
2168 * was written to the buffer. */
2171 else if( iSnprintfReturnValue >= ( int ) n )
2173 /* This is the case when the supplied buffer is not
2174 * large to hold the generated string. Return the
2175 * number of characters actually written without
2176 * counting the terminating NULL character. */
2177 uxCharsWritten = n - 1U;
2181 /* Complete string was written to the buffer. */
2182 uxCharsWritten = ( size_t ) iSnprintfReturnValue;
2185 return uxCharsWritten;
2188 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
2189 /*-----------------------------------------------------------*/
2191 #if ( INCLUDE_vTaskDelete == 1 )
2193 void vTaskDelete( TaskHandle_t xTaskToDelete )
2196 BaseType_t xDeleteTCBInIdleTask = pdFALSE;
2197 BaseType_t xTaskIsRunningOrYielding;
2199 traceENTER_vTaskDelete( xTaskToDelete );
2201 taskENTER_CRITICAL();
2203 /* If null is passed in here then it is the calling task that is
2205 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
2207 /* Remove task from the ready/delayed list. */
2208 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2210 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2214 mtCOVERAGE_TEST_MARKER();
2217 /* Is the task waiting on an event also? */
2218 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2220 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2224 mtCOVERAGE_TEST_MARKER();
2227 /* Increment the uxTaskNumber also so kernel aware debuggers can
2228 * detect that the task lists need re-generating. This is done before
2229 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
2233 /* Use temp variable as distinct sequence points for reading volatile
2234 * variables prior to a logical operator to ensure compliance with
2235 * MISRA C 2012 Rule 13.5. */
2236 xTaskIsRunningOrYielding = taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB );
2238 /* If the task is running (or yielding), we must add it to the
2239 * termination list so that an idle task can delete it when it is
2240 * no longer running. */
2241 if( ( xSchedulerRunning != pdFALSE ) && ( xTaskIsRunningOrYielding != pdFALSE ) )
2243 /* A running task or a task which is scheduled to yield is being
2244 * deleted. This cannot complete when the task is still running
2245 * on a core, as a context switch to another task is required.
2246 * Place the task in the termination list. The idle task will check
2247 * the termination list and free up any memory allocated by the
2248 * scheduler for the TCB and stack of the deleted task. */
2249 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
2251 /* Increment the ucTasksDeleted variable so the idle task knows
2252 * there is a task that has been deleted and that it should therefore
2253 * check the xTasksWaitingTermination list. */
2254 ++uxDeletedTasksWaitingCleanUp;
2256 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
2257 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
2258 traceTASK_DELETE( pxTCB );
2260 /* Delete the task TCB in idle task. */
2261 xDeleteTCBInIdleTask = pdTRUE;
2263 /* The pre-delete hook is primarily for the Windows simulator,
2264 * in which Windows specific clean up operations are performed,
2265 * after which it is not possible to yield away from this task -
2266 * hence xYieldPending is used to latch that a context switch is
2268 #if ( configNUMBER_OF_CORES == 1 )
2269 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
2271 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
2274 /* In the case of SMP, it is possible that the task being deleted
2275 * is running on another core. We must evict the task before
2276 * exiting the critical section to ensure that the task cannot
2277 * take an action which puts it back on ready/state/event list,
2278 * thereby nullifying the delete operation. Once evicted, the
2279 * task won't be scheduled ever as it will no longer be on the
2281 #if ( configNUMBER_OF_CORES > 1 )
2283 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2285 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2287 configASSERT( uxSchedulerSuspended == 0 );
2288 taskYIELD_WITHIN_API();
2292 prvYieldCore( pxTCB->xTaskRunState );
2296 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
2300 --uxCurrentNumberOfTasks;
2301 traceTASK_DELETE( pxTCB );
2303 /* Reset the next expected unblock time in case it referred to
2304 * the task that has just been deleted. */
2305 prvResetNextTaskUnblockTime();
2308 taskEXIT_CRITICAL();
2310 /* If the task is not deleting itself, call prvDeleteTCB from outside of
2311 * critical section. If a task deletes itself, prvDeleteTCB is called
2312 * from prvCheckTasksWaitingTermination which is called from Idle task. */
2313 if( xDeleteTCBInIdleTask != pdTRUE )
2315 prvDeleteTCB( pxTCB );
2318 /* Force a reschedule if it is the currently running task that has just
2320 #if ( configNUMBER_OF_CORES == 1 )
2322 if( xSchedulerRunning != pdFALSE )
2324 if( pxTCB == pxCurrentTCB )
2326 configASSERT( uxSchedulerSuspended == 0 );
2327 taskYIELD_WITHIN_API();
2331 mtCOVERAGE_TEST_MARKER();
2335 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2337 traceRETURN_vTaskDelete();
2340 #endif /* INCLUDE_vTaskDelete */
2341 /*-----------------------------------------------------------*/
2343 #if ( INCLUDE_xTaskDelayUntil == 1 )
2345 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2346 const TickType_t xTimeIncrement )
2348 TickType_t xTimeToWake;
2349 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2351 traceENTER_xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
2353 configASSERT( pxPreviousWakeTime );
2354 configASSERT( ( xTimeIncrement > 0U ) );
2358 /* Minor optimisation. The tick count cannot change in this
2360 const TickType_t xConstTickCount = xTickCount;
2362 configASSERT( uxSchedulerSuspended == 1U );
2364 /* Generate the tick time at which the task wants to wake. */
2365 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2367 if( xConstTickCount < *pxPreviousWakeTime )
2369 /* The tick count has overflowed since this function was
2370 * lasted called. In this case the only time we should ever
2371 * actually delay is if the wake time has also overflowed,
2372 * and the wake time is greater than the tick time. When this
2373 * is the case it is as if neither time had overflowed. */
2374 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2376 xShouldDelay = pdTRUE;
2380 mtCOVERAGE_TEST_MARKER();
2385 /* The tick time has not overflowed. In this case we will
2386 * delay if either the wake time has overflowed, and/or the
2387 * tick time is less than the wake time. */
2388 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2390 xShouldDelay = pdTRUE;
2394 mtCOVERAGE_TEST_MARKER();
2398 /* Update the wake time ready for the next call. */
2399 *pxPreviousWakeTime = xTimeToWake;
2401 if( xShouldDelay != pdFALSE )
2403 traceTASK_DELAY_UNTIL( xTimeToWake );
2405 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2406 * the time to wake, so subtract the current tick count. */
2407 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2411 mtCOVERAGE_TEST_MARKER();
2414 xAlreadyYielded = xTaskResumeAll();
2416 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2417 * have put ourselves to sleep. */
2418 if( xAlreadyYielded == pdFALSE )
2420 taskYIELD_WITHIN_API();
2424 mtCOVERAGE_TEST_MARKER();
2427 traceRETURN_xTaskDelayUntil( xShouldDelay );
2429 return xShouldDelay;
2432 #endif /* INCLUDE_xTaskDelayUntil */
2433 /*-----------------------------------------------------------*/
2435 #if ( INCLUDE_vTaskDelay == 1 )
2437 void vTaskDelay( const TickType_t xTicksToDelay )
2439 BaseType_t xAlreadyYielded = pdFALSE;
2441 traceENTER_vTaskDelay( xTicksToDelay );
2443 /* A delay time of zero just forces a reschedule. */
2444 if( xTicksToDelay > ( TickType_t ) 0U )
2448 configASSERT( uxSchedulerSuspended == 1U );
2452 /* A task that is removed from the event list while the
2453 * scheduler is suspended will not get placed in the ready
2454 * list or removed from the blocked list until the scheduler
2457 * This task cannot be in an event list as it is the currently
2458 * executing task. */
2459 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2461 xAlreadyYielded = xTaskResumeAll();
2465 mtCOVERAGE_TEST_MARKER();
2468 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2469 * have put ourselves to sleep. */
2470 if( xAlreadyYielded == pdFALSE )
2472 taskYIELD_WITHIN_API();
2476 mtCOVERAGE_TEST_MARKER();
2479 traceRETURN_vTaskDelay();
2482 #endif /* INCLUDE_vTaskDelay */
2483 /*-----------------------------------------------------------*/
2485 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2487 eTaskState eTaskGetState( TaskHandle_t xTask )
2490 List_t const * pxStateList;
2491 List_t const * pxEventList;
2492 List_t const * pxDelayedList;
2493 List_t const * pxOverflowedDelayedList;
2494 const TCB_t * const pxTCB = xTask;
2496 traceENTER_eTaskGetState( xTask );
2498 configASSERT( pxTCB );
2500 #if ( configNUMBER_OF_CORES == 1 )
2501 if( pxTCB == pxCurrentTCB )
2503 /* The task calling this function is querying its own state. */
2509 taskENTER_CRITICAL();
2511 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2512 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2513 pxDelayedList = pxDelayedTaskList;
2514 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2516 taskEXIT_CRITICAL();
2518 if( pxEventList == &xPendingReadyList )
2520 /* The task has been placed on the pending ready list, so its
2521 * state is eReady regardless of what list the task's state list
2522 * item is currently placed on. */
2525 else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2527 /* The task being queried is referenced from one of the Blocked
2532 #if ( INCLUDE_vTaskSuspend == 1 )
2533 else if( pxStateList == &xSuspendedTaskList )
2535 /* The task being queried is referenced from the suspended
2536 * list. Is it genuinely suspended or is it blocked
2538 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2540 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2544 /* The task does not appear on the event list item of
2545 * and of the RTOS objects, but could still be in the
2546 * blocked state if it is waiting on its notification
2547 * rather than waiting on an object. If not, is
2549 eReturn = eSuspended;
2551 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2553 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2560 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2562 eReturn = eSuspended;
2564 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2571 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2573 #if ( INCLUDE_vTaskDelete == 1 )
2574 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2576 /* The task being queried is referenced from the deleted
2577 * tasks list, or it is not referenced from any lists at
2585 #if ( configNUMBER_OF_CORES == 1 )
2587 /* If the task is not in any other state, it must be in the
2588 * Ready (including pending ready) state. */
2591 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2593 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2595 /* Is it actively running on a core? */
2600 /* If the task is not in any other state, it must be in the
2601 * Ready (including pending ready) state. */
2605 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2609 traceRETURN_eTaskGetState( eReturn );
2614 #endif /* INCLUDE_eTaskGetState */
2615 /*-----------------------------------------------------------*/
2617 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2619 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2621 TCB_t const * pxTCB;
2622 UBaseType_t uxReturn;
2624 traceENTER_uxTaskPriorityGet( xTask );
2626 taskENTER_CRITICAL();
2628 /* If null is passed in here then it is the priority of the task
2629 * that called uxTaskPriorityGet() that is being queried. */
2630 pxTCB = prvGetTCBFromHandle( xTask );
2631 uxReturn = pxTCB->uxPriority;
2633 taskEXIT_CRITICAL();
2635 traceRETURN_uxTaskPriorityGet( uxReturn );
2640 #endif /* INCLUDE_uxTaskPriorityGet */
2641 /*-----------------------------------------------------------*/
2643 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2645 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2647 TCB_t const * pxTCB;
2648 UBaseType_t uxReturn;
2649 UBaseType_t uxSavedInterruptStatus;
2651 traceENTER_uxTaskPriorityGetFromISR( xTask );
2653 /* RTOS ports that support interrupt nesting have the concept of a
2654 * maximum system call (or maximum API call) interrupt priority.
2655 * Interrupts that are above the maximum system call priority are keep
2656 * permanently enabled, even when the RTOS kernel is in a critical section,
2657 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2658 * is defined in FreeRTOSConfig.h then
2659 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2660 * failure if a FreeRTOS API function is called from an interrupt that has
2661 * been assigned a priority above the configured maximum system call
2662 * priority. Only FreeRTOS functions that end in FromISR can be called
2663 * from interrupts that have been assigned a priority at or (logically)
2664 * below the maximum system call interrupt priority. FreeRTOS maintains a
2665 * separate interrupt safe API to ensure interrupt entry is as fast and as
2666 * simple as possible. More information (albeit Cortex-M specific) is
2667 * provided on the following link:
2668 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2669 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2671 /* MISRA Ref 4.7.1 [Return value shall be checked] */
2672 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
2673 /* coverity[misra_c_2012_directive_4_7_violation] */
2674 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
2676 /* If null is passed in here then it is the priority of the calling
2677 * task that is being queried. */
2678 pxTCB = prvGetTCBFromHandle( xTask );
2679 uxReturn = pxTCB->uxPriority;
2681 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2683 traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
2688 #endif /* INCLUDE_uxTaskPriorityGet */
2689 /*-----------------------------------------------------------*/
2691 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2693 UBaseType_t uxTaskBasePriorityGet( const TaskHandle_t xTask )
2695 TCB_t const * pxTCB;
2696 UBaseType_t uxReturn;
2698 traceENTER_uxTaskBasePriorityGet( xTask );
2700 taskENTER_CRITICAL();
2702 /* If null is passed in here then it is the base priority of the task
2703 * that called uxTaskBasePriorityGet() that is being queried. */
2704 pxTCB = prvGetTCBFromHandle( xTask );
2705 uxReturn = pxTCB->uxBasePriority;
2707 taskEXIT_CRITICAL();
2709 traceRETURN_uxTaskBasePriorityGet( uxReturn );
2714 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2715 /*-----------------------------------------------------------*/
2717 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2719 UBaseType_t uxTaskBasePriorityGetFromISR( const TaskHandle_t xTask )
2721 TCB_t const * pxTCB;
2722 UBaseType_t uxReturn;
2723 UBaseType_t uxSavedInterruptStatus;
2725 traceENTER_uxTaskBasePriorityGetFromISR( xTask );
2727 /* RTOS ports that support interrupt nesting have the concept of a
2728 * maximum system call (or maximum API call) interrupt priority.
2729 * Interrupts that are above the maximum system call priority are keep
2730 * permanently enabled, even when the RTOS kernel is in a critical section,
2731 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2732 * is defined in FreeRTOSConfig.h then
2733 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2734 * failure if a FreeRTOS API function is called from an interrupt that has
2735 * been assigned a priority above the configured maximum system call
2736 * priority. Only FreeRTOS functions that end in FromISR can be called
2737 * from interrupts that have been assigned a priority at or (logically)
2738 * below the maximum system call interrupt priority. FreeRTOS maintains a
2739 * separate interrupt safe API to ensure interrupt entry is as fast and as
2740 * simple as possible. More information (albeit Cortex-M specific) is
2741 * provided on the following link:
2742 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2743 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2745 /* MISRA Ref 4.7.1 [Return value shall be checked] */
2746 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
2747 /* coverity[misra_c_2012_directive_4_7_violation] */
2748 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
2750 /* If null is passed in here then it is the base priority of the calling
2751 * task that is being queried. */
2752 pxTCB = prvGetTCBFromHandle( xTask );
2753 uxReturn = pxTCB->uxBasePriority;
2755 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2757 traceRETURN_uxTaskBasePriorityGetFromISR( uxReturn );
2762 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2763 /*-----------------------------------------------------------*/
2765 #if ( INCLUDE_vTaskPrioritySet == 1 )
2767 void vTaskPrioritySet( TaskHandle_t xTask,
2768 UBaseType_t uxNewPriority )
2771 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2772 BaseType_t xYieldRequired = pdFALSE;
2774 #if ( configNUMBER_OF_CORES > 1 )
2775 BaseType_t xYieldForTask = pdFALSE;
2778 traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
2780 configASSERT( uxNewPriority < configMAX_PRIORITIES );
2782 /* Ensure the new priority is valid. */
2783 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2785 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2789 mtCOVERAGE_TEST_MARKER();
2792 taskENTER_CRITICAL();
2794 /* If null is passed in here then it is the priority of the calling
2795 * task that is being changed. */
2796 pxTCB = prvGetTCBFromHandle( xTask );
2798 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2800 #if ( configUSE_MUTEXES == 1 )
2802 uxCurrentBasePriority = pxTCB->uxBasePriority;
2806 uxCurrentBasePriority = pxTCB->uxPriority;
2810 if( uxCurrentBasePriority != uxNewPriority )
2812 /* The priority change may have readied a task of higher
2813 * priority than a running task. */
2814 if( uxNewPriority > uxCurrentBasePriority )
2816 #if ( configNUMBER_OF_CORES == 1 )
2818 if( pxTCB != pxCurrentTCB )
2820 /* The priority of a task other than the currently
2821 * running task is being raised. Is the priority being
2822 * raised above that of the running task? */
2823 if( uxNewPriority > pxCurrentTCB->uxPriority )
2825 xYieldRequired = pdTRUE;
2829 mtCOVERAGE_TEST_MARKER();
2834 /* The priority of the running task is being raised,
2835 * but the running task must already be the highest
2836 * priority task able to run so no yield is required. */
2839 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2841 /* The priority of a task is being raised so
2842 * perform a yield for this task later. */
2843 xYieldForTask = pdTRUE;
2845 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2847 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2849 /* Setting the priority of a running task down means
2850 * there may now be another task of higher priority that
2851 * is ready to execute. */
2852 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2853 if( pxTCB->xPreemptionDisable == pdFALSE )
2856 xYieldRequired = pdTRUE;
2861 /* Setting the priority of any other task down does not
2862 * require a yield as the running task must be above the
2863 * new priority of the task being modified. */
2866 /* Remember the ready list the task might be referenced from
2867 * before its uxPriority member is changed so the
2868 * taskRESET_READY_PRIORITY() macro can function correctly. */
2869 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2871 #if ( configUSE_MUTEXES == 1 )
2873 /* Only change the priority being used if the task is not
2874 * currently using an inherited priority or the new priority
2875 * is bigger than the inherited priority. */
2876 if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2878 pxTCB->uxPriority = uxNewPriority;
2882 mtCOVERAGE_TEST_MARKER();
2885 /* The base priority gets set whatever. */
2886 pxTCB->uxBasePriority = uxNewPriority;
2888 #else /* if ( configUSE_MUTEXES == 1 ) */
2890 pxTCB->uxPriority = uxNewPriority;
2892 #endif /* if ( configUSE_MUTEXES == 1 ) */
2894 /* Only reset the event list item value if the value is not
2895 * being used for anything else. */
2896 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
2898 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) );
2902 mtCOVERAGE_TEST_MARKER();
2905 /* If the task is in the blocked or suspended list we need do
2906 * nothing more than change its priority variable. However, if
2907 * the task is in a ready list it needs to be removed and placed
2908 * in the list appropriate to its new priority. */
2909 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2911 /* The task is currently in its ready list - remove before
2912 * adding it to its new ready list. As we are in a critical
2913 * section we can do this even if the scheduler is suspended. */
2914 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2916 /* It is known that the task is in its ready list so
2917 * there is no need to check again and the port level
2918 * reset macro can be called directly. */
2919 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2923 mtCOVERAGE_TEST_MARKER();
2926 prvAddTaskToReadyList( pxTCB );
2930 #if ( configNUMBER_OF_CORES == 1 )
2932 mtCOVERAGE_TEST_MARKER();
2936 /* It's possible that xYieldForTask was already set to pdTRUE because
2937 * its priority is being raised. However, since it is not in a ready list
2938 * we don't actually need to yield for it. */
2939 xYieldForTask = pdFALSE;
2944 if( xYieldRequired != pdFALSE )
2946 /* The running task priority is set down. Request the task to yield. */
2947 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2951 #if ( configNUMBER_OF_CORES > 1 )
2952 if( xYieldForTask != pdFALSE )
2954 /* The priority of the task is being raised. If a running
2955 * task has priority lower than this task, it should yield
2957 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2960 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2962 mtCOVERAGE_TEST_MARKER();
2966 /* Remove compiler warning about unused variables when the port
2967 * optimised task selection is not being used. */
2968 ( void ) uxPriorityUsedOnEntry;
2971 taskEXIT_CRITICAL();
2973 traceRETURN_vTaskPrioritySet();
2976 #endif /* INCLUDE_vTaskPrioritySet */
2977 /*-----------------------------------------------------------*/
2979 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2980 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2981 UBaseType_t uxCoreAffinityMask )
2985 UBaseType_t uxPrevCoreAffinityMask;
2987 #if ( configUSE_PREEMPTION == 1 )
2988 UBaseType_t uxPrevNotAllowedCores;
2991 traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
2993 taskENTER_CRITICAL();
2995 pxTCB = prvGetTCBFromHandle( xTask );
2997 uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2998 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
3000 if( xSchedulerRunning != pdFALSE )
3002 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3004 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
3006 /* If the task can no longer run on the core it was running,
3007 * request the core to yield. */
3008 if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
3010 prvYieldCore( xCoreID );
3015 #if ( configUSE_PREEMPTION == 1 )
3017 /* Calculate the cores on which this task was not allowed to
3018 * run previously. */
3019 uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
3021 /* Does the new core mask enables this task to run on any of the
3022 * previously not allowed cores? If yes, check if this task can be
3023 * scheduled on any of those cores. */
3024 if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
3026 prvYieldForTask( pxTCB );
3029 #else /* #if( configUSE_PREEMPTION == 1 ) */
3031 mtCOVERAGE_TEST_MARKER();
3033 #endif /* #if( configUSE_PREEMPTION == 1 ) */
3037 taskEXIT_CRITICAL();
3039 traceRETURN_vTaskCoreAffinitySet();
3041 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3042 /*-----------------------------------------------------------*/
3044 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
3045 UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
3047 const TCB_t * pxTCB;
3048 UBaseType_t uxCoreAffinityMask;
3050 traceENTER_vTaskCoreAffinityGet( xTask );
3052 taskENTER_CRITICAL();
3054 pxTCB = prvGetTCBFromHandle( xTask );
3055 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
3057 taskEXIT_CRITICAL();
3059 traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
3061 return uxCoreAffinityMask;
3063 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3065 /*-----------------------------------------------------------*/
3067 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3069 void vTaskPreemptionDisable( const TaskHandle_t xTask )
3073 traceENTER_vTaskPreemptionDisable( xTask );
3075 taskENTER_CRITICAL();
3077 pxTCB = prvGetTCBFromHandle( xTask );
3079 pxTCB->xPreemptionDisable = pdTRUE;
3081 taskEXIT_CRITICAL();
3083 traceRETURN_vTaskPreemptionDisable();
3086 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3087 /*-----------------------------------------------------------*/
3089 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3091 void vTaskPreemptionEnable( const TaskHandle_t xTask )
3096 traceENTER_vTaskPreemptionEnable( xTask );
3098 taskENTER_CRITICAL();
3100 pxTCB = prvGetTCBFromHandle( xTask );
3102 pxTCB->xPreemptionDisable = pdFALSE;
3104 if( xSchedulerRunning != pdFALSE )
3106 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3108 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
3109 prvYieldCore( xCoreID );
3113 taskEXIT_CRITICAL();
3115 traceRETURN_vTaskPreemptionEnable();
3118 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3119 /*-----------------------------------------------------------*/
3121 #if ( INCLUDE_vTaskSuspend == 1 )
3123 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
3127 traceENTER_vTaskSuspend( xTaskToSuspend );
3129 taskENTER_CRITICAL();
3131 /* If null is passed in here then it is the running task that is
3132 * being suspended. */
3133 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
3135 traceTASK_SUSPEND( pxTCB );
3137 /* Remove task from the ready/delayed list and place in the
3138 * suspended list. */
3139 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
3141 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
3145 mtCOVERAGE_TEST_MARKER();
3148 /* Is the task waiting on an event also? */
3149 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3151 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3155 mtCOVERAGE_TEST_MARKER();
3158 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
3160 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3164 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3166 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3168 /* The task was blocked to wait for a notification, but is
3169 * now suspended, so no notification was received. */
3170 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
3174 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3176 /* In the case of SMP, it is possible that the task being suspended
3177 * is running on another core. We must evict the task before
3178 * exiting the critical section to ensure that the task cannot
3179 * take an action which puts it back on ready/state/event list,
3180 * thereby nullifying the suspend operation. Once evicted, the
3181 * task won't be scheduled before it is resumed as it will no longer
3182 * be on the ready list. */
3183 #if ( configNUMBER_OF_CORES > 1 )
3185 if( xSchedulerRunning != pdFALSE )
3187 /* Reset the next expected unblock time in case it referred to the
3188 * task that is now in the Suspended state. */
3189 prvResetNextTaskUnblockTime();
3191 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3193 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
3195 /* The current task has just been suspended. */
3196 configASSERT( uxSchedulerSuspended == 0 );
3197 vTaskYieldWithinAPI();
3201 prvYieldCore( pxTCB->xTaskRunState );
3206 mtCOVERAGE_TEST_MARKER();
3211 mtCOVERAGE_TEST_MARKER();
3214 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
3216 taskEXIT_CRITICAL();
3218 #if ( configNUMBER_OF_CORES == 1 )
3220 UBaseType_t uxCurrentListLength;
3222 if( xSchedulerRunning != pdFALSE )
3224 /* Reset the next expected unblock time in case it referred to the
3225 * task that is now in the Suspended state. */
3226 taskENTER_CRITICAL();
3228 prvResetNextTaskUnblockTime();
3230 taskEXIT_CRITICAL();
3234 mtCOVERAGE_TEST_MARKER();
3237 if( pxTCB == pxCurrentTCB )
3239 if( xSchedulerRunning != pdFALSE )
3241 /* The current task has just been suspended. */
3242 configASSERT( uxSchedulerSuspended == 0 );
3243 portYIELD_WITHIN_API();
3247 /* The scheduler is not running, but the task that was pointed
3248 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
3249 * must be adjusted to point to a different task. */
3251 /* Use a temp variable as a distinct sequence point for reading
3252 * volatile variables prior to a comparison to ensure compliance
3253 * with MISRA C 2012 Rule 13.2. */
3254 uxCurrentListLength = listCURRENT_LIST_LENGTH( &xSuspendedTaskList );
3256 if( uxCurrentListLength == uxCurrentNumberOfTasks )
3258 /* No other tasks are ready, so set pxCurrentTCB back to
3259 * NULL so when the next task is created pxCurrentTCB will
3260 * be set to point to it no matter what its relative priority
3262 pxCurrentTCB = NULL;
3266 vTaskSwitchContext();
3272 mtCOVERAGE_TEST_MARKER();
3275 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3277 traceRETURN_vTaskSuspend();
3280 #endif /* INCLUDE_vTaskSuspend */
3281 /*-----------------------------------------------------------*/
3283 #if ( INCLUDE_vTaskSuspend == 1 )
3285 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
3287 BaseType_t xReturn = pdFALSE;
3288 const TCB_t * const pxTCB = xTask;
3290 /* Accesses xPendingReadyList so must be called from a critical
3293 /* It does not make sense to check if the calling task is suspended. */
3294 configASSERT( xTask );
3296 /* Is the task being resumed actually in the suspended list? */
3297 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
3299 /* Has the task already been resumed from within an ISR? */
3300 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
3302 /* Is it in the suspended list because it is in the Suspended
3303 * state, or because it is blocked with no timeout? */
3304 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE )
3306 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3310 /* The task does not appear on the event list item of
3311 * and of the RTOS objects, but could still be in the
3312 * blocked state if it is waiting on its notification
3313 * rather than waiting on an object. If not, is
3317 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3319 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3326 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3330 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3334 mtCOVERAGE_TEST_MARKER();
3339 mtCOVERAGE_TEST_MARKER();
3344 mtCOVERAGE_TEST_MARKER();
3350 #endif /* INCLUDE_vTaskSuspend */
3351 /*-----------------------------------------------------------*/
3353 #if ( INCLUDE_vTaskSuspend == 1 )
3355 void vTaskResume( TaskHandle_t xTaskToResume )
3357 TCB_t * const pxTCB = xTaskToResume;
3359 traceENTER_vTaskResume( xTaskToResume );
3361 /* It does not make sense to resume the calling task. */
3362 configASSERT( xTaskToResume );
3364 #if ( configNUMBER_OF_CORES == 1 )
3366 /* The parameter cannot be NULL as it is impossible to resume the
3367 * currently executing task. */
3368 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
3371 /* The parameter cannot be NULL as it is impossible to resume the
3372 * currently executing task. It is also impossible to resume a task
3373 * that is actively running on another core but it is not safe
3374 * to check their run state here. Therefore, we get into a critical
3375 * section and check if the task is actually suspended or not. */
3379 taskENTER_CRITICAL();
3381 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3383 traceTASK_RESUME( pxTCB );
3385 /* The ready list can be accessed even if the scheduler is
3386 * suspended because this is inside a critical section. */
3387 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3388 prvAddTaskToReadyList( pxTCB );
3390 /* This yield may not cause the task just resumed to run,
3391 * but will leave the lists in the correct state for the
3393 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
3397 mtCOVERAGE_TEST_MARKER();
3400 taskEXIT_CRITICAL();
3404 mtCOVERAGE_TEST_MARKER();
3407 traceRETURN_vTaskResume();
3410 #endif /* INCLUDE_vTaskSuspend */
3412 /*-----------------------------------------------------------*/
3414 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
3416 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
3418 BaseType_t xYieldRequired = pdFALSE;
3419 TCB_t * const pxTCB = xTaskToResume;
3420 UBaseType_t uxSavedInterruptStatus;
3422 traceENTER_xTaskResumeFromISR( xTaskToResume );
3424 configASSERT( xTaskToResume );
3426 /* RTOS ports that support interrupt nesting have the concept of a
3427 * maximum system call (or maximum API call) interrupt priority.
3428 * Interrupts that are above the maximum system call priority are keep
3429 * permanently enabled, even when the RTOS kernel is in a critical section,
3430 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
3431 * is defined in FreeRTOSConfig.h then
3432 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3433 * failure if a FreeRTOS API function is called from an interrupt that has
3434 * been assigned a priority above the configured maximum system call
3435 * priority. Only FreeRTOS functions that end in FromISR can be called
3436 * from interrupts that have been assigned a priority at or (logically)
3437 * below the maximum system call interrupt priority. FreeRTOS maintains a
3438 * separate interrupt safe API to ensure interrupt entry is as fast and as
3439 * simple as possible. More information (albeit Cortex-M specific) is
3440 * provided on the following link:
3441 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3442 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3444 /* MISRA Ref 4.7.1 [Return value shall be checked] */
3445 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
3446 /* coverity[misra_c_2012_directive_4_7_violation] */
3447 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3449 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3451 traceTASK_RESUME_FROM_ISR( pxTCB );
3453 /* Check the ready lists can be accessed. */
3454 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3456 #if ( configNUMBER_OF_CORES == 1 )
3458 /* Ready lists can be accessed so move the task from the
3459 * suspended list to the ready list directly. */
3460 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3462 xYieldRequired = pdTRUE;
3464 /* Mark that a yield is pending in case the user is not
3465 * using the return value to initiate a context switch
3466 * from the ISR using the port specific portYIELD_FROM_ISR(). */
3467 xYieldPendings[ 0 ] = pdTRUE;
3471 mtCOVERAGE_TEST_MARKER();
3474 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3476 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3477 prvAddTaskToReadyList( pxTCB );
3481 /* The delayed or ready lists cannot be accessed so the task
3482 * is held in the pending ready list until the scheduler is
3484 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3487 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3489 prvYieldForTask( pxTCB );
3491 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3493 xYieldRequired = pdTRUE;
3496 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3500 mtCOVERAGE_TEST_MARKER();
3503 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3505 traceRETURN_xTaskResumeFromISR( xYieldRequired );
3507 return xYieldRequired;
3510 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3511 /*-----------------------------------------------------------*/
3513 static BaseType_t prvCreateIdleTasks( void )
3515 BaseType_t xReturn = pdPASS;
3517 char cIdleName[ configMAX_TASK_NAME_LEN ];
3518 TaskFunction_t pxIdleTaskFunction = NULL;
3519 BaseType_t xIdleTaskNameIndex;
3521 for( xIdleTaskNameIndex = ( BaseType_t ) 0; xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN; xIdleTaskNameIndex++ )
3523 cIdleName[ xIdleTaskNameIndex ] = configIDLE_TASK_NAME[ xIdleTaskNameIndex ];
3525 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3526 * configMAX_TASK_NAME_LEN characters just in case the memory after the
3527 * string is not accessible (extremely unlikely). */
3528 if( cIdleName[ xIdleTaskNameIndex ] == ( char ) 0x00 )
3534 mtCOVERAGE_TEST_MARKER();
3538 /* Add each idle task at the lowest priority. */
3539 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3541 #if ( configNUMBER_OF_CORES == 1 )
3543 pxIdleTaskFunction = prvIdleTask;
3545 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3547 /* In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks
3548 * are also created to ensure that each core has an idle task to
3549 * run when no other task is available to run. */
3552 pxIdleTaskFunction = prvIdleTask;
3556 pxIdleTaskFunction = prvPassiveIdleTask;
3559 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3561 /* Update the idle task name with suffix to differentiate the idle tasks.
3562 * This function is not required in single core FreeRTOS since there is
3563 * only one idle task. */
3564 #if ( configNUMBER_OF_CORES > 1 )
3566 /* Append the idle task number to the end of the name if there is space. */
3567 if( xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3569 cIdleName[ xIdleTaskNameIndex ] = ( char ) ( xCoreID + '0' );
3571 /* And append a null character if there is space. */
3572 if( ( xIdleTaskNameIndex + 1 ) < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3574 cIdleName[ xIdleTaskNameIndex + 1 ] = '\0';
3578 mtCOVERAGE_TEST_MARKER();
3583 mtCOVERAGE_TEST_MARKER();
3586 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
3588 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3590 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3591 StackType_t * pxIdleTaskStackBuffer = NULL;
3592 configSTACK_DEPTH_TYPE uxIdleTaskStackSize;
3594 /* The Idle task is created using user provided RAM - obtain the
3595 * address of the RAM then create the idle task. */
3596 #if ( configNUMBER_OF_CORES == 1 )
3598 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
3604 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
3608 vApplicationGetPassiveIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize, ( BaseType_t ) ( xCoreID - 1 ) );
3611 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
3612 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( pxIdleTaskFunction,
3614 uxIdleTaskStackSize,
3616 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3617 pxIdleTaskStackBuffer,
3618 pxIdleTaskTCBBuffer );
3620 if( xIdleTaskHandles[ xCoreID ] != NULL )
3629 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3631 /* The Idle task is being created using dynamically allocated RAM. */
3632 xReturn = xTaskCreate( pxIdleTaskFunction,
3634 configMINIMAL_STACK_SIZE,
3636 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3637 &xIdleTaskHandles[ xCoreID ] );
3639 #endif /* configSUPPORT_STATIC_ALLOCATION */
3641 /* Break the loop if any of the idle task is failed to be created. */
3642 if( xReturn == pdFAIL )
3648 #if ( configNUMBER_OF_CORES == 1 )
3650 mtCOVERAGE_TEST_MARKER();
3654 /* Assign idle task to each core before SMP scheduler is running. */
3655 xIdleTaskHandles[ xCoreID ]->xTaskRunState = xCoreID;
3656 pxCurrentTCBs[ xCoreID ] = xIdleTaskHandles[ xCoreID ];
3665 /*-----------------------------------------------------------*/
3667 void vTaskStartScheduler( void )
3671 traceENTER_vTaskStartScheduler();
3673 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3675 /* Sanity check that the UBaseType_t must have greater than or equal to
3676 * the number of bits as confNUMBER_OF_CORES. */
3677 configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3679 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3681 xReturn = prvCreateIdleTasks();
3683 #if ( configUSE_TIMERS == 1 )
3685 if( xReturn == pdPASS )
3687 xReturn = xTimerCreateTimerTask();
3691 mtCOVERAGE_TEST_MARKER();
3694 #endif /* configUSE_TIMERS */
3696 if( xReturn == pdPASS )
3698 /* freertos_tasks_c_additions_init() should only be called if the user
3699 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3700 * the only macro called by the function. */
3701 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3703 freertos_tasks_c_additions_init();
3707 /* Interrupts are turned off here, to ensure a tick does not occur
3708 * before or during the call to xPortStartScheduler(). The stacks of
3709 * the created tasks contain a status word with interrupts switched on
3710 * so interrupts will automatically get re-enabled when the first task
3712 portDISABLE_INTERRUPTS();
3714 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3716 /* Switch C-Runtime's TLS Block to point to the TLS
3717 * block specific to the task that will run first. */
3718 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3722 xNextTaskUnblockTime = portMAX_DELAY;
3723 xSchedulerRunning = pdTRUE;
3724 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3726 /* If configGENERATE_RUN_TIME_STATS is defined then the following
3727 * macro must be defined to configure the timer/counter used to generate
3728 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
3729 * is set to 0 and the following line fails to build then ensure you do not
3730 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3731 * FreeRTOSConfig.h file. */
3732 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3734 traceTASK_SWITCHED_IN();
3736 traceSTARTING_SCHEDULER( xIdleTaskHandles );
3738 /* Setting up the timer tick is hardware specific and thus in the
3739 * portable interface. */
3741 /* The return value for xPortStartScheduler is not required
3742 * hence using a void datatype. */
3743 ( void ) xPortStartScheduler();
3745 /* In most cases, xPortStartScheduler() will not return. If it
3746 * returns pdTRUE then there was not enough heap memory available
3747 * to create either the Idle or the Timer task. If it returned
3748 * pdFALSE, then the application called xTaskEndScheduler().
3749 * Most ports don't implement xTaskEndScheduler() as there is
3750 * nothing to return to. */
3754 /* This line will only be reached if the kernel could not be started,
3755 * because there was not enough FreeRTOS heap to create the idle task
3756 * or the timer task. */
3757 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3760 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3761 * meaning xIdleTaskHandles are not used anywhere else. */
3762 ( void ) xIdleTaskHandles;
3764 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3765 * from getting optimized out as it is no longer used by the kernel. */
3766 ( void ) uxTopUsedPriority;
3768 traceRETURN_vTaskStartScheduler();
3770 /*-----------------------------------------------------------*/
3772 void vTaskEndScheduler( void )
3774 traceENTER_vTaskEndScheduler();
3776 #if ( INCLUDE_vTaskDelete == 1 )
3780 #if ( configUSE_TIMERS == 1 )
3782 /* Delete the timer task created by the kernel. */
3783 vTaskDelete( xTimerGetTimerDaemonTaskHandle() );
3785 #endif /* #if ( configUSE_TIMERS == 1 ) */
3787 /* Delete Idle tasks created by the kernel.*/
3788 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3790 vTaskDelete( xIdleTaskHandles[ xCoreID ] );
3793 /* Idle task is responsible for reclaiming the resources of the tasks in
3794 * xTasksWaitingTermination list. Since the idle task is now deleted and
3795 * no longer going to run, we need to reclaim resources of all the tasks
3796 * in the xTasksWaitingTermination list. */
3797 prvCheckTasksWaitingTermination();
3799 #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
3801 /* Stop the scheduler interrupts and call the portable scheduler end
3802 * routine so the original ISRs can be restored if necessary. The port
3803 * layer must ensure interrupts enable bit is left in the correct state. */
3804 portDISABLE_INTERRUPTS();
3805 xSchedulerRunning = pdFALSE;
3807 /* This function must be called from a task and the application is
3808 * responsible for deleting that task after the scheduler is stopped. */
3809 vPortEndScheduler();
3811 traceRETURN_vTaskEndScheduler();
3813 /*----------------------------------------------------------*/
3815 void vTaskSuspendAll( void )
3817 traceENTER_vTaskSuspendAll();
3819 #if ( configNUMBER_OF_CORES == 1 )
3821 /* A critical section is not required as the variable is of type
3822 * BaseType_t. Please read Richard Barry's reply in the following link to a
3823 * post in the FreeRTOS support forum before reporting this as a bug! -
3824 * https://goo.gl/wu4acr */
3826 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3827 * do not otherwise exhibit real time behaviour. */
3828 portSOFTWARE_BARRIER();
3830 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3831 * is used to allow calls to vTaskSuspendAll() to nest. */
3832 uxSchedulerSuspended = ( UBaseType_t ) ( uxSchedulerSuspended + 1U );
3834 /* Enforces ordering for ports and optimised compilers that may otherwise place
3835 * the above increment elsewhere. */
3836 portMEMORY_BARRIER();
3838 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3840 UBaseType_t ulState;
3842 /* This must only be called from within a task. */
3843 portASSERT_IF_IN_ISR();
3845 if( xSchedulerRunning != pdFALSE )
3847 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3848 * We must disable interrupts before we grab the locks in the event that this task is
3849 * interrupted and switches context before incrementing uxSchedulerSuspended.
3850 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3851 * uxSchedulerSuspended since that will prevent context switches. */
3852 ulState = portSET_INTERRUPT_MASK();
3854 /* This must never be called from inside a critical section. */
3855 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
3857 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3858 * do not otherwise exhibit real time behaviour. */
3859 portSOFTWARE_BARRIER();
3861 portGET_TASK_LOCK();
3863 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3864 * purpose is to prevent altering the variable when fromISR APIs are readying
3866 if( uxSchedulerSuspended == 0U )
3868 prvCheckForRunStateChange();
3872 mtCOVERAGE_TEST_MARKER();
3877 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3878 * is used to allow calls to vTaskSuspendAll() to nest. */
3879 ++uxSchedulerSuspended;
3880 portRELEASE_ISR_LOCK();
3882 portCLEAR_INTERRUPT_MASK( ulState );
3886 mtCOVERAGE_TEST_MARKER();
3889 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3891 traceRETURN_vTaskSuspendAll();
3894 /*----------------------------------------------------------*/
3896 #if ( configUSE_TICKLESS_IDLE != 0 )
3898 static TickType_t prvGetExpectedIdleTime( void )
3901 BaseType_t xHigherPriorityReadyTasks = pdFALSE;
3903 /* xHigherPriorityReadyTasks takes care of the case where
3904 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3905 * task that are in the Ready state, even though the idle task is
3907 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3909 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3911 xHigherPriorityReadyTasks = pdTRUE;
3916 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3918 /* When port optimised task selection is used the uxTopReadyPriority
3919 * variable is used as a bit map. If bits other than the least
3920 * significant bit are set then there are tasks that have a priority
3921 * above the idle priority that are in the Ready state. This takes
3922 * care of the case where the co-operative scheduler is in use. */
3923 if( uxTopReadyPriority > uxLeastSignificantBit )
3925 xHigherPriorityReadyTasks = pdTRUE;
3928 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3930 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3934 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1U )
3936 /* There are other idle priority tasks in the ready state. If
3937 * time slicing is used then the very next tick interrupt must be
3941 else if( xHigherPriorityReadyTasks != pdFALSE )
3943 /* There are tasks in the Ready state that have a priority above the
3944 * idle priority. This path can only be reached if
3945 * configUSE_PREEMPTION is 0. */
3950 xReturn = xNextTaskUnblockTime;
3951 xReturn -= xTickCount;
3957 #endif /* configUSE_TICKLESS_IDLE */
3958 /*----------------------------------------------------------*/
3960 BaseType_t xTaskResumeAll( void )
3962 TCB_t * pxTCB = NULL;
3963 BaseType_t xAlreadyYielded = pdFALSE;
3965 traceENTER_xTaskResumeAll();
3967 #if ( configNUMBER_OF_CORES > 1 )
3968 if( xSchedulerRunning != pdFALSE )
3971 /* It is possible that an ISR caused a task to be removed from an event
3972 * list while the scheduler was suspended. If this was the case then the
3973 * removed task will have been added to the xPendingReadyList. Once the
3974 * scheduler has been resumed it is safe to move all the pending ready
3975 * tasks from this list into their appropriate ready list. */
3976 taskENTER_CRITICAL();
3979 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3981 /* If uxSchedulerSuspended is zero then this function does not match a
3982 * previous call to vTaskSuspendAll(). */
3983 configASSERT( uxSchedulerSuspended != 0U );
3985 uxSchedulerSuspended = ( UBaseType_t ) ( uxSchedulerSuspended - 1U );
3986 portRELEASE_TASK_LOCK();
3988 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3990 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3992 /* Move any readied tasks from the pending list into the
3993 * appropriate ready list. */
3994 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3996 /* MISRA Ref 11.5.3 [Void pointer assignment] */
3997 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
3998 /* coverity[misra_c_2012_rule_11_5_violation] */
3999 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) );
4000 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4001 portMEMORY_BARRIER();
4002 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4003 prvAddTaskToReadyList( pxTCB );
4005 #if ( configNUMBER_OF_CORES == 1 )
4007 /* If the moved task has a priority higher than the current
4008 * task then a yield must be performed. */
4009 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4011 xYieldPendings[ xCoreID ] = pdTRUE;
4015 mtCOVERAGE_TEST_MARKER();
4018 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4020 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
4021 * If the current core yielded then vTaskSwitchContext() has already been called
4022 * which sets xYieldPendings for the current core to pdTRUE. */
4024 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4029 /* A task was unblocked while the scheduler was suspended,
4030 * which may have prevented the next unblock time from being
4031 * re-calculated, in which case re-calculate it now. Mainly
4032 * important for low power tickless implementations, where
4033 * this can prevent an unnecessary exit from low power
4035 prvResetNextTaskUnblockTime();
4038 /* If any ticks occurred while the scheduler was suspended then
4039 * they should be processed now. This ensures the tick count does
4040 * not slip, and that any delayed tasks are resumed at the correct
4043 * It should be safe to call xTaskIncrementTick here from any core
4044 * since we are in a critical section and xTaskIncrementTick itself
4045 * protects itself within a critical section. Suspending the scheduler
4046 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
4048 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
4050 if( xPendedCounts > ( TickType_t ) 0U )
4054 if( xTaskIncrementTick() != pdFALSE )
4056 /* Other cores are interrupted from
4057 * within xTaskIncrementTick(). */
4058 xYieldPendings[ xCoreID ] = pdTRUE;
4062 mtCOVERAGE_TEST_MARKER();
4066 } while( xPendedCounts > ( TickType_t ) 0U );
4072 mtCOVERAGE_TEST_MARKER();
4076 if( xYieldPendings[ xCoreID ] != pdFALSE )
4078 #if ( configUSE_PREEMPTION != 0 )
4080 xAlreadyYielded = pdTRUE;
4082 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
4084 #if ( configNUMBER_OF_CORES == 1 )
4086 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
4088 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4092 mtCOVERAGE_TEST_MARKER();
4098 mtCOVERAGE_TEST_MARKER();
4101 taskEXIT_CRITICAL();
4104 traceRETURN_xTaskResumeAll( xAlreadyYielded );
4106 return xAlreadyYielded;
4108 /*-----------------------------------------------------------*/
4110 TickType_t xTaskGetTickCount( void )
4114 traceENTER_xTaskGetTickCount();
4116 /* Critical section required if running on a 16 bit processor. */
4117 portTICK_TYPE_ENTER_CRITICAL();
4119 xTicks = xTickCount;
4121 portTICK_TYPE_EXIT_CRITICAL();
4123 traceRETURN_xTaskGetTickCount( xTicks );
4127 /*-----------------------------------------------------------*/
4129 TickType_t xTaskGetTickCountFromISR( void )
4132 UBaseType_t uxSavedInterruptStatus;
4134 traceENTER_xTaskGetTickCountFromISR();
4136 /* RTOS ports that support interrupt nesting have the concept of a maximum
4137 * system call (or maximum API call) interrupt priority. Interrupts that are
4138 * above the maximum system call priority are kept permanently enabled, even
4139 * when the RTOS kernel is in a critical section, but cannot make any calls to
4140 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
4141 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
4142 * failure if a FreeRTOS API function is called from an interrupt that has been
4143 * assigned a priority above the configured maximum system call priority.
4144 * Only FreeRTOS functions that end in FromISR can be called from interrupts
4145 * that have been assigned a priority at or (logically) below the maximum
4146 * system call interrupt priority. FreeRTOS maintains a separate interrupt
4147 * safe API to ensure interrupt entry is as fast and as simple as possible.
4148 * More information (albeit Cortex-M specific) is provided on the following
4149 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
4150 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
4152 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
4154 xReturn = xTickCount;
4156 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
4158 traceRETURN_xTaskGetTickCountFromISR( xReturn );
4162 /*-----------------------------------------------------------*/
4164 UBaseType_t uxTaskGetNumberOfTasks( void )
4166 traceENTER_uxTaskGetNumberOfTasks();
4168 /* A critical section is not required because the variables are of type
4170 traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
4172 return uxCurrentNumberOfTasks;
4174 /*-----------------------------------------------------------*/
4176 char * pcTaskGetName( TaskHandle_t xTaskToQuery )
4180 traceENTER_pcTaskGetName( xTaskToQuery );
4182 /* If null is passed in here then the name of the calling task is being
4184 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4185 configASSERT( pxTCB );
4187 traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
4189 return &( pxTCB->pcTaskName[ 0 ] );
4191 /*-----------------------------------------------------------*/
4193 #if ( INCLUDE_xTaskGetHandle == 1 )
4194 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4195 const char pcNameToQuery[] )
4197 TCB_t * pxReturn = NULL;
4198 TCB_t * pxTCB = NULL;
4201 BaseType_t xBreakLoop;
4202 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
4203 ListItem_t * pxIterator;
4205 /* This function is called with the scheduler suspended. */
4207 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4209 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
4211 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4212 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4213 /* coverity[misra_c_2012_rule_11_5_violation] */
4214 pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
4216 /* Check each character in the name looking for a match or
4218 xBreakLoop = pdFALSE;
4220 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4222 cNextChar = pxTCB->pcTaskName[ x ];
4224 if( cNextChar != pcNameToQuery[ x ] )
4226 /* Characters didn't match. */
4227 xBreakLoop = pdTRUE;
4229 else if( cNextChar == ( char ) 0x00 )
4231 /* Both strings terminated, a match must have been
4234 xBreakLoop = pdTRUE;
4238 mtCOVERAGE_TEST_MARKER();
4241 if( xBreakLoop != pdFALSE )
4247 if( pxReturn != NULL )
4249 /* The handle has been found. */
4256 mtCOVERAGE_TEST_MARKER();
4262 #endif /* INCLUDE_xTaskGetHandle */
4263 /*-----------------------------------------------------------*/
4265 #if ( INCLUDE_xTaskGetHandle == 1 )
4267 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery )
4269 UBaseType_t uxQueue = configMAX_PRIORITIES;
4272 traceENTER_xTaskGetHandle( pcNameToQuery );
4274 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
4275 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
4279 /* Search the ready lists. */
4283 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
4287 /* Found the handle. */
4290 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4292 /* Search the delayed lists. */
4295 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
4300 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
4303 #if ( INCLUDE_vTaskSuspend == 1 )
4307 /* Search the suspended list. */
4308 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
4313 #if ( INCLUDE_vTaskDelete == 1 )
4317 /* Search the deleted list. */
4318 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
4323 ( void ) xTaskResumeAll();
4325 traceRETURN_xTaskGetHandle( pxTCB );
4330 #endif /* INCLUDE_xTaskGetHandle */
4331 /*-----------------------------------------------------------*/
4333 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4335 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4336 StackType_t ** ppuxStackBuffer,
4337 StaticTask_t ** ppxTaskBuffer )
4342 traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4344 configASSERT( ppuxStackBuffer != NULL );
4345 configASSERT( ppxTaskBuffer != NULL );
4347 pxTCB = prvGetTCBFromHandle( xTask );
4349 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4351 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4353 *ppuxStackBuffer = pxTCB->pxStack;
4354 /* MISRA Ref 11.3.1 [Misaligned access] */
4355 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
4356 /* coverity[misra_c_2012_rule_11_3_violation] */
4357 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4360 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4362 *ppuxStackBuffer = pxTCB->pxStack;
4363 *ppxTaskBuffer = NULL;
4371 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4373 *ppuxStackBuffer = pxTCB->pxStack;
4374 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4377 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4379 traceRETURN_xTaskGetStaticBuffers( xReturn );
4384 #endif /* configSUPPORT_STATIC_ALLOCATION */
4385 /*-----------------------------------------------------------*/
4387 #if ( configUSE_TRACE_FACILITY == 1 )
4389 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4390 const UBaseType_t uxArraySize,
4391 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4393 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4395 traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4399 /* Is there a space in the array for each task in the system? */
4400 if( uxArraySize >= uxCurrentNumberOfTasks )
4402 /* Fill in an TaskStatus_t structure with information on each
4403 * task in the Ready state. */
4407 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4408 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4410 /* Fill in an TaskStatus_t structure with information on each
4411 * task in the Blocked state. */
4412 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4413 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4415 #if ( INCLUDE_vTaskDelete == 1 )
4417 /* Fill in an TaskStatus_t structure with information on
4418 * each task that has been deleted but not yet cleaned up. */
4419 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4423 #if ( INCLUDE_vTaskSuspend == 1 )
4425 /* Fill in an TaskStatus_t structure with information on
4426 * each task in the Suspended state. */
4427 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4431 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4433 if( pulTotalRunTime != NULL )
4435 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4436 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4438 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4442 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4444 if( pulTotalRunTime != NULL )
4446 *pulTotalRunTime = 0;
4449 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4453 mtCOVERAGE_TEST_MARKER();
4456 ( void ) xTaskResumeAll();
4458 traceRETURN_uxTaskGetSystemState( uxTask );
4463 #endif /* configUSE_TRACE_FACILITY */
4464 /*----------------------------------------------------------*/
4466 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4468 #if ( configNUMBER_OF_CORES == 1 )
4469 TaskHandle_t xTaskGetIdleTaskHandle( void )
4471 traceENTER_xTaskGetIdleTaskHandle();
4473 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4474 * started, then xIdleTaskHandles will be NULL. */
4475 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4477 traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4479 return xIdleTaskHandles[ 0 ];
4481 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
4483 TaskHandle_t xTaskGetIdleTaskHandleForCore( BaseType_t xCoreID )
4485 traceENTER_xTaskGetIdleTaskHandleForCore( xCoreID );
4487 /* Ensure the core ID is valid. */
4488 configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE );
4490 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4491 * started, then xIdleTaskHandles will be NULL. */
4492 configASSERT( ( xIdleTaskHandles[ xCoreID ] != NULL ) );
4494 traceRETURN_xTaskGetIdleTaskHandleForCore( xIdleTaskHandles[ xCoreID ] );
4496 return xIdleTaskHandles[ xCoreID ];
4499 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4500 /*----------------------------------------------------------*/
4502 /* This conditional compilation should use inequality to 0, not equality to 1.
4503 * This is to ensure vTaskStepTick() is available when user defined low power mode
4504 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4506 #if ( configUSE_TICKLESS_IDLE != 0 )
4508 void vTaskStepTick( TickType_t xTicksToJump )
4510 TickType_t xUpdatedTickCount;
4512 traceENTER_vTaskStepTick( xTicksToJump );
4514 /* Correct the tick count value after a period during which the tick
4515 * was suppressed. Note this does *not* call the tick hook function for
4516 * each stepped tick. */
4517 xUpdatedTickCount = xTickCount + xTicksToJump;
4518 configASSERT( xUpdatedTickCount <= xNextTaskUnblockTime );
4520 if( xUpdatedTickCount == xNextTaskUnblockTime )
4522 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4523 * xTaskIncrementTick() when the scheduler resumes. This ensures
4524 * that any delayed tasks are resumed at the correct time. */
4525 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4526 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4528 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4529 taskENTER_CRITICAL();
4533 taskEXIT_CRITICAL();
4538 mtCOVERAGE_TEST_MARKER();
4541 xTickCount += xTicksToJump;
4543 traceINCREASE_TICK_COUNT( xTicksToJump );
4544 traceRETURN_vTaskStepTick();
4547 #endif /* configUSE_TICKLESS_IDLE */
4548 /*----------------------------------------------------------*/
4550 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4552 BaseType_t xYieldOccurred;
4554 traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4556 /* Must not be called with the scheduler suspended as the implementation
4557 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4558 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4560 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4561 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4564 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4565 taskENTER_CRITICAL();
4567 xPendedTicks += xTicksToCatchUp;
4569 taskEXIT_CRITICAL();
4570 xYieldOccurred = xTaskResumeAll();
4572 traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4574 return xYieldOccurred;
4576 /*----------------------------------------------------------*/
4578 #if ( INCLUDE_xTaskAbortDelay == 1 )
4580 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4582 TCB_t * pxTCB = xTask;
4585 traceENTER_xTaskAbortDelay( xTask );
4587 configASSERT( pxTCB );
4591 /* A task can only be prematurely removed from the Blocked state if
4592 * it is actually in the Blocked state. */
4593 if( eTaskGetState( xTask ) == eBlocked )
4597 /* Remove the reference to the task from the blocked list. An
4598 * interrupt won't touch the xStateListItem because the
4599 * scheduler is suspended. */
4600 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4602 /* Is the task waiting on an event also? If so remove it from
4603 * the event list too. Interrupts can touch the event list item,
4604 * even though the scheduler is suspended, so a critical section
4606 taskENTER_CRITICAL();
4608 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4610 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4612 /* This lets the task know it was forcibly removed from the
4613 * blocked state so it should not re-evaluate its block time and
4614 * then block again. */
4615 pxTCB->ucDelayAborted = ( uint8_t ) pdTRUE;
4619 mtCOVERAGE_TEST_MARKER();
4622 taskEXIT_CRITICAL();
4624 /* Place the unblocked task into the appropriate ready list. */
4625 prvAddTaskToReadyList( pxTCB );
4627 /* A task being unblocked cannot cause an immediate context
4628 * switch if preemption is turned off. */
4629 #if ( configUSE_PREEMPTION == 1 )
4631 #if ( configNUMBER_OF_CORES == 1 )
4633 /* Preemption is on, but a context switch should only be
4634 * performed if the unblocked task has a priority that is
4635 * higher than the currently executing task. */
4636 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4638 /* Pend the yield to be performed when the scheduler
4639 * is unsuspended. */
4640 xYieldPendings[ 0 ] = pdTRUE;
4644 mtCOVERAGE_TEST_MARKER();
4647 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4649 taskENTER_CRITICAL();
4651 prvYieldForTask( pxTCB );
4653 taskEXIT_CRITICAL();
4655 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4657 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4664 ( void ) xTaskResumeAll();
4666 traceRETURN_xTaskAbortDelay( xReturn );
4671 #endif /* INCLUDE_xTaskAbortDelay */
4672 /*----------------------------------------------------------*/
4674 BaseType_t xTaskIncrementTick( void )
4677 TickType_t xItemValue;
4678 BaseType_t xSwitchRequired = pdFALSE;
4680 #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4681 BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4682 #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4684 traceENTER_xTaskIncrementTick();
4686 /* Called by the portable layer each time a tick interrupt occurs.
4687 * Increments the tick then checks to see if the new tick value will cause any
4688 * tasks to be unblocked. */
4689 traceTASK_INCREMENT_TICK( xTickCount );
4691 /* Tick increment should occur on every kernel timer event. Core 0 has the
4692 * responsibility to increment the tick, or increment the pended ticks if the
4693 * scheduler is suspended. If pended ticks is greater than zero, the core that
4694 * calls xTaskResumeAll has the responsibility to increment the tick. */
4695 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4697 /* Minor optimisation. The tick count cannot change in this
4699 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4701 /* Increment the RTOS tick, switching the delayed and overflowed
4702 * delayed lists if it wraps to 0. */
4703 xTickCount = xConstTickCount;
4705 if( xConstTickCount == ( TickType_t ) 0U )
4707 taskSWITCH_DELAYED_LISTS();
4711 mtCOVERAGE_TEST_MARKER();
4714 /* See if this tick has made a timeout expire. Tasks are stored in
4715 * the queue in the order of their wake time - meaning once one task
4716 * has been found whose block time has not expired there is no need to
4717 * look any further down the list. */
4718 if( xConstTickCount >= xNextTaskUnblockTime )
4722 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4724 /* The delayed list is empty. Set xNextTaskUnblockTime
4725 * to the maximum possible value so it is extremely
4727 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4728 * next time through. */
4729 xNextTaskUnblockTime = portMAX_DELAY;
4734 /* The delayed list is not empty, get the value of the
4735 * item at the head of the delayed list. This is the time
4736 * at which the task at the head of the delayed list must
4737 * be removed from the Blocked state. */
4738 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4739 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4740 /* coverity[misra_c_2012_rule_11_5_violation] */
4741 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
4742 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4744 if( xConstTickCount < xItemValue )
4746 /* It is not time to unblock this item yet, but the
4747 * item value is the time at which the task at the head
4748 * of the blocked list must be removed from the Blocked
4749 * state - so record the item value in
4750 * xNextTaskUnblockTime. */
4751 xNextTaskUnblockTime = xItemValue;
4756 mtCOVERAGE_TEST_MARKER();
4759 /* It is time to remove the item from the Blocked state. */
4760 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4762 /* Is the task waiting on an event also? If so remove
4763 * it from the event list. */
4764 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4766 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4770 mtCOVERAGE_TEST_MARKER();
4773 /* Place the unblocked task into the appropriate ready
4775 prvAddTaskToReadyList( pxTCB );
4777 /* A task being unblocked cannot cause an immediate
4778 * context switch if preemption is turned off. */
4779 #if ( configUSE_PREEMPTION == 1 )
4781 #if ( configNUMBER_OF_CORES == 1 )
4783 /* Preemption is on, but a context switch should
4784 * only be performed if the unblocked task's
4785 * priority is higher than the currently executing
4787 * The case of equal priority tasks sharing
4788 * processing time (which happens when both
4789 * preemption and time slicing are on) is
4791 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4793 xSwitchRequired = pdTRUE;
4797 mtCOVERAGE_TEST_MARKER();
4800 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4802 prvYieldForTask( pxTCB );
4804 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4806 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4811 /* Tasks of equal priority to the currently running task will share
4812 * processing time (time slice) if preemption is on, and the application
4813 * writer has not explicitly turned time slicing off. */
4814 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4816 #if ( configNUMBER_OF_CORES == 1 )
4818 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > 1U )
4820 xSwitchRequired = pdTRUE;
4824 mtCOVERAGE_TEST_MARKER();
4827 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4831 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4833 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1U )
4835 xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4839 mtCOVERAGE_TEST_MARKER();
4843 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4845 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4847 #if ( configUSE_TICK_HOOK == 1 )
4849 /* Guard against the tick hook being called when the pended tick
4850 * count is being unwound (when the scheduler is being unlocked). */
4851 if( xPendedTicks == ( TickType_t ) 0 )
4853 vApplicationTickHook();
4857 mtCOVERAGE_TEST_MARKER();
4860 #endif /* configUSE_TICK_HOOK */
4862 #if ( configUSE_PREEMPTION == 1 )
4864 #if ( configNUMBER_OF_CORES == 1 )
4866 /* For single core the core ID is always 0. */
4867 if( xYieldPendings[ 0 ] != pdFALSE )
4869 xSwitchRequired = pdTRUE;
4873 mtCOVERAGE_TEST_MARKER();
4876 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4878 BaseType_t xCoreID, xCurrentCoreID;
4879 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4881 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4883 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4884 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4887 if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4889 if( xCoreID == xCurrentCoreID )
4891 xSwitchRequired = pdTRUE;
4895 prvYieldCore( xCoreID );
4900 mtCOVERAGE_TEST_MARKER();
4905 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4907 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4913 /* The tick hook gets called at regular intervals, even if the
4914 * scheduler is locked. */
4915 #if ( configUSE_TICK_HOOK == 1 )
4917 vApplicationTickHook();
4922 traceRETURN_xTaskIncrementTick( xSwitchRequired );
4924 return xSwitchRequired;
4926 /*-----------------------------------------------------------*/
4928 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4930 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4931 TaskHookFunction_t pxHookFunction )
4935 traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4937 /* If xTask is NULL then it is the task hook of the calling task that is
4941 xTCB = ( TCB_t * ) pxCurrentTCB;
4948 /* Save the hook function in the TCB. A critical section is required as
4949 * the value can be accessed from an interrupt. */
4950 taskENTER_CRITICAL();
4952 xTCB->pxTaskTag = pxHookFunction;
4954 taskEXIT_CRITICAL();
4956 traceRETURN_vTaskSetApplicationTaskTag();
4959 #endif /* configUSE_APPLICATION_TASK_TAG */
4960 /*-----------------------------------------------------------*/
4962 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4964 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4967 TaskHookFunction_t xReturn;
4969 traceENTER_xTaskGetApplicationTaskTag( xTask );
4971 /* If xTask is NULL then set the calling task's hook. */
4972 pxTCB = prvGetTCBFromHandle( xTask );
4974 /* Save the hook function in the TCB. A critical section is required as
4975 * the value can be accessed from an interrupt. */
4976 taskENTER_CRITICAL();
4978 xReturn = pxTCB->pxTaskTag;
4980 taskEXIT_CRITICAL();
4982 traceRETURN_xTaskGetApplicationTaskTag( xReturn );
4987 #endif /* configUSE_APPLICATION_TASK_TAG */
4988 /*-----------------------------------------------------------*/
4990 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4992 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
4995 TaskHookFunction_t xReturn;
4996 UBaseType_t uxSavedInterruptStatus;
4998 traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
5000 /* If xTask is NULL then set the calling task's hook. */
5001 pxTCB = prvGetTCBFromHandle( xTask );
5003 /* Save the hook function in the TCB. A critical section is required as
5004 * the value can be accessed from an interrupt. */
5005 /* MISRA Ref 4.7.1 [Return value shall be checked] */
5006 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
5007 /* coverity[misra_c_2012_directive_4_7_violation] */
5008 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
5010 xReturn = pxTCB->pxTaskTag;
5012 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
5014 traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
5019 #endif /* configUSE_APPLICATION_TASK_TAG */
5020 /*-----------------------------------------------------------*/
5022 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5024 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
5025 void * pvParameter )
5030 traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
5032 /* If xTask is NULL then we are calling our own task hook. */
5035 xTCB = pxCurrentTCB;
5042 if( xTCB->pxTaskTag != NULL )
5044 xReturn = xTCB->pxTaskTag( pvParameter );
5051 traceRETURN_xTaskCallApplicationTaskHook( xReturn );
5056 #endif /* configUSE_APPLICATION_TASK_TAG */
5057 /*-----------------------------------------------------------*/
5059 #if ( configNUMBER_OF_CORES == 1 )
5060 void vTaskSwitchContext( void )
5062 traceENTER_vTaskSwitchContext();
5064 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5066 /* The scheduler is currently suspended - do not allow a context
5068 xYieldPendings[ 0 ] = pdTRUE;
5072 xYieldPendings[ 0 ] = pdFALSE;
5073 traceTASK_SWITCHED_OUT();
5075 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5077 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5078 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
5080 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
5083 /* Add the amount of time the task has been running to the
5084 * accumulated time so far. The time the task started running was
5085 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5086 * protection here so count values are only valid until the timer
5087 * overflows. The guard against negative values is to protect
5088 * against suspect run time stat counter implementations - which
5089 * are provided by the application, not the kernel. */
5090 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
5092 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
5096 mtCOVERAGE_TEST_MARKER();
5099 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
5101 #endif /* configGENERATE_RUN_TIME_STATS */
5103 /* Check for stack overflow, if configured. */
5104 taskCHECK_FOR_STACK_OVERFLOW();
5106 /* Before the currently running task is switched out, save its errno. */
5107 #if ( configUSE_POSIX_ERRNO == 1 )
5109 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
5113 /* Select a new task to run using either the generic C or port
5114 * optimised asm code. */
5115 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5116 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5117 /* coverity[misra_c_2012_rule_11_5_violation] */
5118 taskSELECT_HIGHEST_PRIORITY_TASK();
5119 traceTASK_SWITCHED_IN();
5121 /* Macro to inject port specific behaviour immediately after
5122 * switching tasks, such as setting an end of stack watchpoint
5123 * or reconfiguring the MPU. */
5124 portTASK_SWITCH_HOOK( pxCurrentTCB );
5126 /* After the new task is switched in, update the global errno. */
5127 #if ( configUSE_POSIX_ERRNO == 1 )
5129 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
5133 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5135 /* Switch C-Runtime's TLS Block to point to the TLS
5136 * Block specific to this task. */
5137 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
5142 traceRETURN_vTaskSwitchContext();
5144 #else /* if ( configNUMBER_OF_CORES == 1 ) */
5145 void vTaskSwitchContext( BaseType_t xCoreID )
5147 traceENTER_vTaskSwitchContext();
5149 /* Acquire both locks:
5150 * - The ISR lock protects the ready list from simultaneous access by
5151 * both other ISRs and tasks.
5152 * - We also take the task lock to pause here in case another core has
5153 * suspended the scheduler. We don't want to simply set xYieldPending
5154 * and move on if another core suspended the scheduler. We should only
5155 * do that if the current core has suspended the scheduler. */
5157 portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
5160 /* vTaskSwitchContext() must never be called from within a critical section.
5161 * This is not necessarily true for single core FreeRTOS, but it is for this
5163 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
5165 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5167 /* The scheduler is currently suspended - do not allow a context
5169 xYieldPendings[ xCoreID ] = pdTRUE;
5173 xYieldPendings[ xCoreID ] = pdFALSE;
5174 traceTASK_SWITCHED_OUT();
5176 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5178 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5179 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
5181 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
5184 /* Add the amount of time the task has been running to the
5185 * accumulated time so far. The time the task started running was
5186 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5187 * protection here so count values are only valid until the timer
5188 * overflows. The guard against negative values is to protect
5189 * against suspect run time stat counter implementations - which
5190 * are provided by the application, not the kernel. */
5191 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
5193 pxCurrentTCBs[ xCoreID ]->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
5197 mtCOVERAGE_TEST_MARKER();
5200 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
5202 #endif /* configGENERATE_RUN_TIME_STATS */
5204 /* Check for stack overflow, if configured. */
5205 taskCHECK_FOR_STACK_OVERFLOW();
5207 /* Before the currently running task is switched out, save its errno. */
5208 #if ( configUSE_POSIX_ERRNO == 1 )
5210 pxCurrentTCBs[ xCoreID ]->iTaskErrno = FreeRTOS_errno;
5214 /* Select a new task to run. */
5215 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
5216 traceTASK_SWITCHED_IN();
5218 /* Macro to inject port specific behaviour immediately after
5219 * switching tasks, such as setting an end of stack watchpoint
5220 * or reconfiguring the MPU. */
5221 portTASK_SWITCH_HOOK( pxCurrentTCBs[ portGET_CORE_ID() ] );
5223 /* After the new task is switched in, update the global errno. */
5224 #if ( configUSE_POSIX_ERRNO == 1 )
5226 FreeRTOS_errno = pxCurrentTCBs[ xCoreID ]->iTaskErrno;
5230 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5232 /* Switch C-Runtime's TLS Block to point to the TLS
5233 * Block specific to this task. */
5234 configSET_TLS_BLOCK( pxCurrentTCBs[ xCoreID ]->xTLSBlock );
5239 portRELEASE_ISR_LOCK();
5240 portRELEASE_TASK_LOCK();
5242 traceRETURN_vTaskSwitchContext();
5244 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
5245 /*-----------------------------------------------------------*/
5247 void vTaskPlaceOnEventList( List_t * const pxEventList,
5248 const TickType_t xTicksToWait )
5250 traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
5252 configASSERT( pxEventList );
5254 /* THIS FUNCTION MUST BE CALLED WITH THE
5255 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
5257 /* Place the event list item of the TCB in the appropriate event list.
5258 * This is placed in the list in priority order so the highest priority task
5259 * is the first to be woken by the event.
5261 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
5262 * Normally, the xItemValue of a TCB's ListItem_t members is:
5263 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
5264 * Therefore, the event list is sorted in descending priority order.
5266 * The queue that contains the event list is locked, preventing
5267 * simultaneous access from interrupts. */
5268 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5270 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5272 traceRETURN_vTaskPlaceOnEventList();
5274 /*-----------------------------------------------------------*/
5276 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
5277 const TickType_t xItemValue,
5278 const TickType_t xTicksToWait )
5280 traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
5282 configASSERT( pxEventList );
5284 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5285 * the event groups implementation. */
5286 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5288 /* Store the item value in the event list item. It is safe to access the
5289 * event list item here as interrupts won't access the event list item of a
5290 * task that is not in the Blocked state. */
5291 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5293 /* Place the event list item of the TCB at the end of the appropriate event
5294 * list. It is safe to access the event list here because it is part of an
5295 * event group implementation - and interrupts don't access event groups
5296 * directly (instead they access them indirectly by pending function calls to
5297 * the task level). */
5298 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5300 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5302 traceRETURN_vTaskPlaceOnUnorderedEventList();
5304 /*-----------------------------------------------------------*/
5306 #if ( configUSE_TIMERS == 1 )
5308 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
5309 TickType_t xTicksToWait,
5310 const BaseType_t xWaitIndefinitely )
5312 traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
5314 configASSERT( pxEventList );
5316 /* This function should not be called by application code hence the
5317 * 'Restricted' in its name. It is not part of the public API. It is
5318 * designed for use by kernel code, and has special calling requirements -
5319 * it should be called with the scheduler suspended. */
5322 /* Place the event list item of the TCB in the appropriate event list.
5323 * In this case it is assume that this is the only task that is going to
5324 * be waiting on this event list, so the faster vListInsertEnd() function
5325 * can be used in place of vListInsert. */
5326 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5328 /* If the task should block indefinitely then set the block time to a
5329 * value that will be recognised as an indefinite delay inside the
5330 * prvAddCurrentTaskToDelayedList() function. */
5331 if( xWaitIndefinitely != pdFALSE )
5333 xTicksToWait = portMAX_DELAY;
5336 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
5337 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
5339 traceRETURN_vTaskPlaceOnEventListRestricted();
5342 #endif /* configUSE_TIMERS */
5343 /*-----------------------------------------------------------*/
5345 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
5347 TCB_t * pxUnblockedTCB;
5350 traceENTER_xTaskRemoveFromEventList( pxEventList );
5352 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
5353 * called from a critical section within an ISR. */
5355 /* The event list is sorted in priority order, so the first in the list can
5356 * be removed as it is known to be the highest priority. Remove the TCB from
5357 * the delayed list, and add it to the ready list.
5359 * If an event is for a queue that is locked then this function will never
5360 * get called - the lock count on the queue will get modified instead. This
5361 * means exclusive access to the event list is guaranteed here.
5363 * This function assumes that a check has already been made to ensure that
5364 * pxEventList is not empty. */
5365 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5366 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5367 /* coverity[misra_c_2012_rule_11_5_violation] */
5368 pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
5369 configASSERT( pxUnblockedTCB );
5370 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5372 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5374 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5375 prvAddTaskToReadyList( pxUnblockedTCB );
5377 #if ( configUSE_TICKLESS_IDLE != 0 )
5379 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5380 * might be set to the blocked task's time out time. If the task is
5381 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5382 * normally left unchanged, because it is automatically reset to a new
5383 * value when the tick count equals xNextTaskUnblockTime. However if
5384 * tickless idling is used it might be more important to enter sleep mode
5385 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5386 * ensure it is updated at the earliest possible time. */
5387 prvResetNextTaskUnblockTime();
5393 /* The delayed and ready lists cannot be accessed, so hold this task
5394 * pending until the scheduler is resumed. */
5395 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5398 #if ( configNUMBER_OF_CORES == 1 )
5400 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5402 /* Return true if the task removed from the event list has a higher
5403 * priority than the calling task. This allows the calling task to know if
5404 * it should force a context switch now. */
5407 /* Mark that a yield is pending in case the user is not using the
5408 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5409 xYieldPendings[ 0 ] = pdTRUE;
5416 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5420 #if ( configUSE_PREEMPTION == 1 )
5422 prvYieldForTask( pxUnblockedTCB );
5424 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5429 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5431 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5433 traceRETURN_xTaskRemoveFromEventList( xReturn );
5436 /*-----------------------------------------------------------*/
5438 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5439 const TickType_t xItemValue )
5441 TCB_t * pxUnblockedTCB;
5443 traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5445 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5446 * the event flags implementation. */
5447 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5449 /* Store the new item value in the event list. */
5450 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5452 /* Remove the event list form the event flag. Interrupts do not access
5454 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5455 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5456 /* coverity[misra_c_2012_rule_11_5_violation] */
5457 pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem );
5458 configASSERT( pxUnblockedTCB );
5459 listREMOVE_ITEM( pxEventListItem );
5461 #if ( configUSE_TICKLESS_IDLE != 0 )
5463 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5464 * might be set to the blocked task's time out time. If the task is
5465 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5466 * normally left unchanged, because it is automatically reset to a new
5467 * value when the tick count equals xNextTaskUnblockTime. However if
5468 * tickless idling is used it might be more important to enter sleep mode
5469 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5470 * ensure it is updated at the earliest possible time. */
5471 prvResetNextTaskUnblockTime();
5475 /* Remove the task from the delayed list and add it to the ready list. The
5476 * scheduler is suspended so interrupts will not be accessing the ready
5478 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5479 prvAddTaskToReadyList( pxUnblockedTCB );
5481 #if ( configNUMBER_OF_CORES == 1 )
5483 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5485 /* The unblocked task has a priority above that of the calling task, so
5486 * a context switch is required. This function is called with the
5487 * scheduler suspended so xYieldPending is set so the context switch
5488 * occurs immediately that the scheduler is resumed (unsuspended). */
5489 xYieldPendings[ 0 ] = pdTRUE;
5492 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5494 #if ( configUSE_PREEMPTION == 1 )
5496 taskENTER_CRITICAL();
5498 prvYieldForTask( pxUnblockedTCB );
5500 taskEXIT_CRITICAL();
5504 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5506 traceRETURN_vTaskRemoveFromUnorderedEventList();
5508 /*-----------------------------------------------------------*/
5510 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5512 traceENTER_vTaskSetTimeOutState( pxTimeOut );
5514 configASSERT( pxTimeOut );
5515 taskENTER_CRITICAL();
5517 pxTimeOut->xOverflowCount = xNumOfOverflows;
5518 pxTimeOut->xTimeOnEntering = xTickCount;
5520 taskEXIT_CRITICAL();
5522 traceRETURN_vTaskSetTimeOutState();
5524 /*-----------------------------------------------------------*/
5526 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5528 traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5530 /* For internal use only as it does not use a critical section. */
5531 pxTimeOut->xOverflowCount = xNumOfOverflows;
5532 pxTimeOut->xTimeOnEntering = xTickCount;
5534 traceRETURN_vTaskInternalSetTimeOutState();
5536 /*-----------------------------------------------------------*/
5538 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5539 TickType_t * const pxTicksToWait )
5543 traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5545 configASSERT( pxTimeOut );
5546 configASSERT( pxTicksToWait );
5548 taskENTER_CRITICAL();
5550 /* Minor optimisation. The tick count cannot change in this block. */
5551 const TickType_t xConstTickCount = xTickCount;
5552 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5554 #if ( INCLUDE_xTaskAbortDelay == 1 )
5555 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5557 /* The delay was aborted, which is not the same as a time out,
5558 * but has the same result. */
5559 pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
5565 #if ( INCLUDE_vTaskSuspend == 1 )
5566 if( *pxTicksToWait == portMAX_DELAY )
5568 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5569 * specified is the maximum block time then the task should block
5570 * indefinitely, and therefore never time out. */
5576 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) )
5578 /* The tick count is greater than the time at which
5579 * vTaskSetTimeout() was called, but has also overflowed since
5580 * vTaskSetTimeOut() was called. It must have wrapped all the way
5581 * around and gone past again. This passed since vTaskSetTimeout()
5584 *pxTicksToWait = ( TickType_t ) 0;
5586 else if( xElapsedTime < *pxTicksToWait )
5588 /* Not a genuine timeout. Adjust parameters for time remaining. */
5589 *pxTicksToWait -= xElapsedTime;
5590 vTaskInternalSetTimeOutState( pxTimeOut );
5595 *pxTicksToWait = ( TickType_t ) 0;
5599 taskEXIT_CRITICAL();
5601 traceRETURN_xTaskCheckForTimeOut( xReturn );
5605 /*-----------------------------------------------------------*/
5607 void vTaskMissedYield( void )
5609 traceENTER_vTaskMissedYield();
5611 /* Must be called from within a critical section. */
5612 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5614 traceRETURN_vTaskMissedYield();
5616 /*-----------------------------------------------------------*/
5618 #if ( configUSE_TRACE_FACILITY == 1 )
5620 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5622 UBaseType_t uxReturn;
5623 TCB_t const * pxTCB;
5625 traceENTER_uxTaskGetTaskNumber( xTask );
5630 uxReturn = pxTCB->uxTaskNumber;
5637 traceRETURN_uxTaskGetTaskNumber( uxReturn );
5642 #endif /* configUSE_TRACE_FACILITY */
5643 /*-----------------------------------------------------------*/
5645 #if ( configUSE_TRACE_FACILITY == 1 )
5647 void vTaskSetTaskNumber( TaskHandle_t xTask,
5648 const UBaseType_t uxHandle )
5652 traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5657 pxTCB->uxTaskNumber = uxHandle;
5660 traceRETURN_vTaskSetTaskNumber();
5663 #endif /* configUSE_TRACE_FACILITY */
5664 /*-----------------------------------------------------------*/
5667 * -----------------------------------------------------------
5668 * The passive idle task.
5669 * ----------------------------------------------------------
5671 * The passive idle task is used for all the additional cores in a SMP
5672 * system. There must be only 1 active idle task and the rest are passive
5675 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5676 * language extensions. The equivalent prototype for this function is:
5678 * void prvPassiveIdleTask( void *pvParameters );
5681 #if ( configNUMBER_OF_CORES > 1 )
5682 static portTASK_FUNCTION( prvPassiveIdleTask, pvParameters )
5684 ( void ) pvParameters;
5688 for( ; configCONTROL_INFINITE_LOOP(); )
5690 #if ( configUSE_PREEMPTION == 0 )
5692 /* If we are not using preemption we keep forcing a task switch to
5693 * see if any other task has become available. If we are using
5694 * preemption we don't need to do this as any task becoming available
5695 * will automatically get the processor anyway. */
5698 #endif /* configUSE_PREEMPTION */
5700 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5702 /* When using preemption tasks of equal priority will be
5703 * timesliced. If a task that is sharing the idle priority is ready
5704 * to run then the idle task should yield before the end of the
5707 * A critical region is not required here as we are just reading from
5708 * the list, and an occasional incorrect value will not matter. If
5709 * the ready list at the idle priority contains one more task than the
5710 * number of idle tasks, which is equal to the configured numbers of cores
5711 * then a task other than the idle task is ready to execute. */
5712 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5718 mtCOVERAGE_TEST_MARKER();
5721 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5723 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
5725 /* Call the user defined function from within the idle task. This
5726 * allows the application designer to add background functionality
5727 * without the overhead of a separate task.
5729 * This hook is intended to manage core activity such as disabling cores that go idle.
5731 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5732 * CALL A FUNCTION THAT MIGHT BLOCK. */
5733 vApplicationPassiveIdleHook();
5735 #endif /* configUSE_PASSIVE_IDLE_HOOK */
5738 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5741 * -----------------------------------------------------------
5743 * ----------------------------------------------------------
5745 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5746 * language extensions. The equivalent prototype for this function is:
5748 * void prvIdleTask( void *pvParameters );
5752 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5754 /* Stop warnings. */
5755 ( void ) pvParameters;
5757 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5758 * SCHEDULER IS STARTED. **/
5760 /* In case a task that has a secure context deletes itself, in which case
5761 * the idle task is responsible for deleting the task's secure context, if
5763 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5765 #if ( configNUMBER_OF_CORES > 1 )
5767 /* SMP all cores start up in the idle task. This initial yield gets the application
5771 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5773 for( ; configCONTROL_INFINITE_LOOP(); )
5775 /* See if any tasks have deleted themselves - if so then the idle task
5776 * is responsible for freeing the deleted task's TCB and stack. */
5777 prvCheckTasksWaitingTermination();
5779 #if ( configUSE_PREEMPTION == 0 )
5781 /* If we are not using preemption we keep forcing a task switch to
5782 * see if any other task has become available. If we are using
5783 * preemption we don't need to do this as any task becoming available
5784 * will automatically get the processor anyway. */
5787 #endif /* configUSE_PREEMPTION */
5789 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5791 /* When using preemption tasks of equal priority will be
5792 * timesliced. If a task that is sharing the idle priority is ready
5793 * to run then the idle task should yield before the end of the
5796 * A critical region is not required here as we are just reading from
5797 * the list, and an occasional incorrect value will not matter. If
5798 * the ready list at the idle priority contains one more task than the
5799 * number of idle tasks, which is equal to the configured numbers of cores
5800 * then a task other than the idle task is ready to execute. */
5801 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5807 mtCOVERAGE_TEST_MARKER();
5810 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5812 #if ( configUSE_IDLE_HOOK == 1 )
5814 /* Call the user defined function from within the idle task. */
5815 vApplicationIdleHook();
5817 #endif /* configUSE_IDLE_HOOK */
5819 /* This conditional compilation should use inequality to 0, not equality
5820 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5821 * user defined low power mode implementations require
5822 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5823 #if ( configUSE_TICKLESS_IDLE != 0 )
5825 TickType_t xExpectedIdleTime;
5827 /* It is not desirable to suspend then resume the scheduler on
5828 * each iteration of the idle task. Therefore, a preliminary
5829 * test of the expected idle time is performed without the
5830 * scheduler suspended. The result here is not necessarily
5832 xExpectedIdleTime = prvGetExpectedIdleTime();
5834 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5838 /* Now the scheduler is suspended, the expected idle
5839 * time can be sampled again, and this time its value can
5841 configASSERT( xNextTaskUnblockTime >= xTickCount );
5842 xExpectedIdleTime = prvGetExpectedIdleTime();
5844 /* Define the following macro to set xExpectedIdleTime to 0
5845 * if the application does not want
5846 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5847 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5849 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5851 traceLOW_POWER_IDLE_BEGIN();
5852 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5853 traceLOW_POWER_IDLE_END();
5857 mtCOVERAGE_TEST_MARKER();
5860 ( void ) xTaskResumeAll();
5864 mtCOVERAGE_TEST_MARKER();
5867 #endif /* configUSE_TICKLESS_IDLE */
5869 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) )
5871 /* Call the user defined function from within the idle task. This
5872 * allows the application designer to add background functionality
5873 * without the overhead of a separate task.
5875 * This hook is intended to manage core activity such as disabling cores that go idle.
5877 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5878 * CALL A FUNCTION THAT MIGHT BLOCK. */
5879 vApplicationPassiveIdleHook();
5881 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) ) */
5884 /*-----------------------------------------------------------*/
5886 #if ( configUSE_TICKLESS_IDLE != 0 )
5888 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5890 #if ( INCLUDE_vTaskSuspend == 1 )
5891 /* The idle task exists in addition to the application tasks. */
5892 const UBaseType_t uxNonApplicationTasks = configNUMBER_OF_CORES;
5893 #endif /* INCLUDE_vTaskSuspend */
5895 eSleepModeStatus eReturn = eStandardSleep;
5897 traceENTER_eTaskConfirmSleepModeStatus();
5899 /* This function must be called from a critical section. */
5901 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0U )
5903 /* A task was made ready while the scheduler was suspended. */
5904 eReturn = eAbortSleep;
5906 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5908 /* A yield was pended while the scheduler was suspended. */
5909 eReturn = eAbortSleep;
5911 else if( xPendedTicks != 0U )
5913 /* A tick interrupt has already occurred but was held pending
5914 * because the scheduler is suspended. */
5915 eReturn = eAbortSleep;
5918 #if ( INCLUDE_vTaskSuspend == 1 )
5919 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5921 /* If all the tasks are in the suspended list (which might mean they
5922 * have an infinite block time rather than actually being suspended)
5923 * then it is safe to turn all clocks off and just wait for external
5925 eReturn = eNoTasksWaitingTimeout;
5927 #endif /* INCLUDE_vTaskSuspend */
5930 mtCOVERAGE_TEST_MARKER();
5933 traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5938 #endif /* configUSE_TICKLESS_IDLE */
5939 /*-----------------------------------------------------------*/
5941 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5943 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5949 traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
5951 if( ( xIndex >= 0 ) &&
5952 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5954 pxTCB = prvGetTCBFromHandle( xTaskToSet );
5955 configASSERT( pxTCB != NULL );
5956 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5959 traceRETURN_vTaskSetThreadLocalStoragePointer();
5962 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5963 /*-----------------------------------------------------------*/
5965 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5967 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5970 void * pvReturn = NULL;
5973 traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
5975 if( ( xIndex >= 0 ) &&
5976 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5978 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
5979 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
5986 traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
5991 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5992 /*-----------------------------------------------------------*/
5994 #if ( portUSING_MPU_WRAPPERS == 1 )
5996 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
5997 const MemoryRegion_t * const pxRegions )
6001 traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
6003 /* If null is passed in here then we are modifying the MPU settings of
6004 * the calling task. */
6005 pxTCB = prvGetTCBFromHandle( xTaskToModify );
6007 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
6009 traceRETURN_vTaskAllocateMPURegions();
6012 #endif /* portUSING_MPU_WRAPPERS */
6013 /*-----------------------------------------------------------*/
6015 static void prvInitialiseTaskLists( void )
6017 UBaseType_t uxPriority;
6019 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
6021 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
6024 vListInitialise( &xDelayedTaskList1 );
6025 vListInitialise( &xDelayedTaskList2 );
6026 vListInitialise( &xPendingReadyList );
6028 #if ( INCLUDE_vTaskDelete == 1 )
6030 vListInitialise( &xTasksWaitingTermination );
6032 #endif /* INCLUDE_vTaskDelete */
6034 #if ( INCLUDE_vTaskSuspend == 1 )
6036 vListInitialise( &xSuspendedTaskList );
6038 #endif /* INCLUDE_vTaskSuspend */
6040 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
6042 pxDelayedTaskList = &xDelayedTaskList1;
6043 pxOverflowDelayedTaskList = &xDelayedTaskList2;
6045 /*-----------------------------------------------------------*/
6047 static void prvCheckTasksWaitingTermination( void )
6049 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
6051 #if ( INCLUDE_vTaskDelete == 1 )
6055 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
6056 * being called too often in the idle task. */
6057 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6059 #if ( configNUMBER_OF_CORES == 1 )
6061 taskENTER_CRITICAL();
6064 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6065 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6066 /* coverity[misra_c_2012_rule_11_5_violation] */
6067 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6068 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6069 --uxCurrentNumberOfTasks;
6070 --uxDeletedTasksWaitingCleanUp;
6073 taskEXIT_CRITICAL();
6075 prvDeleteTCB( pxTCB );
6077 #else /* #if( configNUMBER_OF_CORES == 1 ) */
6081 taskENTER_CRITICAL();
6083 /* For SMP, multiple idles can be running simultaneously
6084 * and we need to check that other idles did not cleanup while we were
6085 * waiting to enter the critical section. */
6086 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6088 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6089 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6090 /* coverity[misra_c_2012_rule_11_5_violation] */
6091 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6093 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
6095 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6096 --uxCurrentNumberOfTasks;
6097 --uxDeletedTasksWaitingCleanUp;
6101 /* The TCB to be deleted still has not yet been switched out
6102 * by the scheduler, so we will just exit this loop early and
6103 * try again next time. */
6104 taskEXIT_CRITICAL();
6109 taskEXIT_CRITICAL();
6113 prvDeleteTCB( pxTCB );
6116 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
6119 #endif /* INCLUDE_vTaskDelete */
6121 /*-----------------------------------------------------------*/
6123 #if ( configUSE_TRACE_FACILITY == 1 )
6125 void vTaskGetInfo( TaskHandle_t xTask,
6126 TaskStatus_t * pxTaskStatus,
6127 BaseType_t xGetFreeStackSpace,
6132 traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
6134 /* xTask is NULL then get the state of the calling task. */
6135 pxTCB = prvGetTCBFromHandle( xTask );
6137 pxTaskStatus->xHandle = pxTCB;
6138 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
6139 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
6140 pxTaskStatus->pxStackBase = pxTCB->pxStack;
6141 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
6142 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
6143 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
6145 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
6147 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
6149 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
6153 #if ( configUSE_MUTEXES == 1 )
6155 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
6159 pxTaskStatus->uxBasePriority = 0;
6163 #if ( configGENERATE_RUN_TIME_STATS == 1 )
6165 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
6169 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
6173 /* Obtaining the task state is a little fiddly, so is only done if the
6174 * value of eState passed into this function is eInvalid - otherwise the
6175 * state is just set to whatever is passed in. */
6176 if( eState != eInvalid )
6178 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6180 pxTaskStatus->eCurrentState = eRunning;
6184 pxTaskStatus->eCurrentState = eState;
6186 #if ( INCLUDE_vTaskSuspend == 1 )
6188 /* If the task is in the suspended list then there is a
6189 * chance it is actually just blocked indefinitely - so really
6190 * it should be reported as being in the Blocked state. */
6191 if( eState == eSuspended )
6195 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
6197 pxTaskStatus->eCurrentState = eBlocked;
6201 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6205 /* The task does not appear on the event list item of
6206 * and of the RTOS objects, but could still be in the
6207 * blocked state if it is waiting on its notification
6208 * rather than waiting on an object. If not, is
6210 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
6212 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
6214 pxTaskStatus->eCurrentState = eBlocked;
6219 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
6222 ( void ) xTaskResumeAll();
6225 #endif /* INCLUDE_vTaskSuspend */
6227 /* Tasks can be in pending ready list and other state list at the
6228 * same time. These tasks are in ready state no matter what state
6229 * list the task is in. */
6230 taskENTER_CRITICAL();
6232 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
6234 pxTaskStatus->eCurrentState = eReady;
6237 taskEXIT_CRITICAL();
6242 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
6245 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
6246 * parameter is provided to allow it to be skipped. */
6247 if( xGetFreeStackSpace != pdFALSE )
6249 #if ( portSTACK_GROWTH > 0 )
6251 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
6255 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
6261 pxTaskStatus->usStackHighWaterMark = 0;
6264 traceRETURN_vTaskGetInfo();
6267 #endif /* configUSE_TRACE_FACILITY */
6268 /*-----------------------------------------------------------*/
6270 #if ( configUSE_TRACE_FACILITY == 1 )
6272 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
6276 UBaseType_t uxTask = 0;
6277 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
6278 ListItem_t * pxIterator;
6279 TCB_t * pxTCB = NULL;
6281 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
6283 /* Populate an TaskStatus_t structure within the
6284 * pxTaskStatusArray array for each task that is referenced from
6285 * pxList. See the definition of TaskStatus_t in task.h for the
6286 * meaning of each TaskStatus_t structure member. */
6287 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
6289 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6290 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6291 /* coverity[misra_c_2012_rule_11_5_violation] */
6292 pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
6294 vTaskGetInfo( ( TaskHandle_t ) pxTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
6300 mtCOVERAGE_TEST_MARKER();
6306 #endif /* configUSE_TRACE_FACILITY */
6307 /*-----------------------------------------------------------*/
6309 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
6311 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
6313 configSTACK_DEPTH_TYPE uxCount = 0U;
6315 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
6317 pucStackByte -= portSTACK_GROWTH;
6321 uxCount /= ( configSTACK_DEPTH_TYPE ) sizeof( StackType_t );
6326 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
6327 /*-----------------------------------------------------------*/
6329 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
6331 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
6332 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
6333 * user to determine the return type. It gets around the problem of the value
6334 * overflowing on 8-bit types without breaking backward compatibility for
6335 * applications that expect an 8-bit return type. */
6336 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
6339 uint8_t * pucEndOfStack;
6340 configSTACK_DEPTH_TYPE uxReturn;
6342 traceENTER_uxTaskGetStackHighWaterMark2( xTask );
6344 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
6345 * the same except for their return type. Using configSTACK_DEPTH_TYPE
6346 * allows the user to determine the return type. It gets around the
6347 * problem of the value overflowing on 8-bit types without breaking
6348 * backward compatibility for applications that expect an 8-bit return
6351 pxTCB = prvGetTCBFromHandle( xTask );
6353 #if portSTACK_GROWTH < 0
6355 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6359 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6363 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
6365 traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
6370 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
6371 /*-----------------------------------------------------------*/
6373 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
6375 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
6378 uint8_t * pucEndOfStack;
6379 UBaseType_t uxReturn;
6381 traceENTER_uxTaskGetStackHighWaterMark( xTask );
6383 pxTCB = prvGetTCBFromHandle( xTask );
6385 #if portSTACK_GROWTH < 0
6387 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6391 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6395 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
6397 traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6402 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6403 /*-----------------------------------------------------------*/
6405 #if ( INCLUDE_vTaskDelete == 1 )
6407 static void prvDeleteTCB( TCB_t * pxTCB )
6409 /* This call is required specifically for the TriCore port. It must be
6410 * above the vPortFree() calls. The call is also used by ports/demos that
6411 * want to allocate and clean RAM statically. */
6412 portCLEAN_UP_TCB( pxTCB );
6414 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6416 /* Free up the memory allocated for the task's TLS Block. */
6417 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6421 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6423 /* The task can only have been allocated dynamically - free both
6424 * the stack and TCB. */
6425 vPortFreeStack( pxTCB->pxStack );
6428 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
6430 /* The task could have been allocated statically or dynamically, so
6431 * check what was statically allocated before trying to free the
6433 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6435 /* Both the stack and TCB were allocated dynamically, so both
6437 vPortFreeStack( pxTCB->pxStack );
6440 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6442 /* Only the stack was statically allocated, so the TCB is the
6443 * only memory that must be freed. */
6448 /* Neither the stack nor the TCB were allocated dynamically, so
6449 * nothing needs to be freed. */
6450 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6451 mtCOVERAGE_TEST_MARKER();
6454 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6457 #endif /* INCLUDE_vTaskDelete */
6458 /*-----------------------------------------------------------*/
6460 static void prvResetNextTaskUnblockTime( void )
6462 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6464 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
6465 * the maximum possible value so it is extremely unlikely that the
6466 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6467 * there is an item in the delayed list. */
6468 xNextTaskUnblockTime = portMAX_DELAY;
6472 /* The new current delayed list is not empty, get the value of
6473 * the item at the head of the delayed list. This is the time at
6474 * which the task at the head of the delayed list should be removed
6475 * from the Blocked state. */
6476 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6479 /*-----------------------------------------------------------*/
6481 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6483 #if ( configNUMBER_OF_CORES == 1 )
6484 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6486 TaskHandle_t xReturn;
6488 traceENTER_xTaskGetCurrentTaskHandle();
6490 /* A critical section is not required as this is not called from
6491 * an interrupt and the current TCB will always be the same for any
6492 * individual execution thread. */
6493 xReturn = pxCurrentTCB;
6495 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6499 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6500 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6502 TaskHandle_t xReturn;
6503 UBaseType_t uxSavedInterruptStatus;
6505 traceENTER_xTaskGetCurrentTaskHandle();
6507 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6509 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6511 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6513 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6517 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6519 TaskHandle_t xTaskGetCurrentTaskHandleForCore( BaseType_t xCoreID )
6521 TaskHandle_t xReturn = NULL;
6523 traceENTER_xTaskGetCurrentTaskHandleForCore( xCoreID );
6525 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6527 #if ( configNUMBER_OF_CORES == 1 )
6528 xReturn = pxCurrentTCB;
6529 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6530 xReturn = pxCurrentTCBs[ xCoreID ];
6531 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6534 traceRETURN_xTaskGetCurrentTaskHandleForCore( xReturn );
6539 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
6540 /*-----------------------------------------------------------*/
6542 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6544 BaseType_t xTaskGetSchedulerState( void )
6548 traceENTER_xTaskGetSchedulerState();
6550 if( xSchedulerRunning == pdFALSE )
6552 xReturn = taskSCHEDULER_NOT_STARTED;
6556 #if ( configNUMBER_OF_CORES > 1 )
6557 taskENTER_CRITICAL();
6560 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6562 xReturn = taskSCHEDULER_RUNNING;
6566 xReturn = taskSCHEDULER_SUSPENDED;
6569 #if ( configNUMBER_OF_CORES > 1 )
6570 taskEXIT_CRITICAL();
6574 traceRETURN_xTaskGetSchedulerState( xReturn );
6579 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6580 /*-----------------------------------------------------------*/
6582 #if ( configUSE_MUTEXES == 1 )
6584 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6586 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6587 BaseType_t xReturn = pdFALSE;
6589 traceENTER_xTaskPriorityInherit( pxMutexHolder );
6591 /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6592 * inheritance is not applied in this scenario. */
6593 if( pxMutexHolder != NULL )
6595 /* If the holder of the mutex has a priority below the priority of
6596 * the task attempting to obtain the mutex then it will temporarily
6597 * inherit the priority of the task attempting to obtain the mutex. */
6598 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6600 /* Adjust the mutex holder state to account for its new
6601 * priority. Only reset the event list item value if the value is
6602 * not being used for anything else. */
6603 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
6605 listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority );
6609 mtCOVERAGE_TEST_MARKER();
6612 /* If the task being modified is in the ready state it will need
6613 * to be moved into a new list. */
6614 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6616 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6618 /* It is known that the task is in its ready list so
6619 * there is no need to check again and the port level
6620 * reset macro can be called directly. */
6621 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6625 mtCOVERAGE_TEST_MARKER();
6628 /* Inherit the priority before being moved into the new list. */
6629 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6630 prvAddTaskToReadyList( pxMutexHolderTCB );
6631 #if ( configNUMBER_OF_CORES > 1 )
6633 /* The priority of the task is raised. Yield for this task
6634 * if it is not running. */
6635 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6637 prvYieldForTask( pxMutexHolderTCB );
6640 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6644 /* Just inherit the priority. */
6645 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6648 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6650 /* Inheritance occurred. */
6655 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6657 /* The base priority of the mutex holder is lower than the
6658 * priority of the task attempting to take the mutex, but the
6659 * current priority of the mutex holder is not lower than the
6660 * priority of the task attempting to take the mutex.
6661 * Therefore the mutex holder must have already inherited a
6662 * priority, but inheritance would have occurred if that had
6663 * not been the case. */
6668 mtCOVERAGE_TEST_MARKER();
6674 mtCOVERAGE_TEST_MARKER();
6677 traceRETURN_xTaskPriorityInherit( xReturn );
6682 #endif /* configUSE_MUTEXES */
6683 /*-----------------------------------------------------------*/
6685 #if ( configUSE_MUTEXES == 1 )
6687 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6689 TCB_t * const pxTCB = pxMutexHolder;
6690 BaseType_t xReturn = pdFALSE;
6692 traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6694 if( pxMutexHolder != NULL )
6696 /* A task can only have an inherited priority if it holds the mutex.
6697 * If the mutex is held by a task then it cannot be given from an
6698 * interrupt, and if a mutex is given by the holding task then it must
6699 * be the running state task. */
6700 configASSERT( pxTCB == pxCurrentTCB );
6701 configASSERT( pxTCB->uxMutexesHeld );
6702 ( pxTCB->uxMutexesHeld )--;
6704 /* Has the holder of the mutex inherited the priority of another
6706 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6708 /* Only disinherit if no other mutexes are held. */
6709 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6711 /* A task can only have an inherited priority if it holds
6712 * the mutex. If the mutex is held by a task then it cannot be
6713 * given from an interrupt, and if a mutex is given by the
6714 * holding task then it must be the running state task. Remove
6715 * the holding task from the ready list. */
6716 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6718 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6722 mtCOVERAGE_TEST_MARKER();
6725 /* Disinherit the priority before adding the task into the
6726 * new ready list. */
6727 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6728 pxTCB->uxPriority = pxTCB->uxBasePriority;
6730 /* Reset the event list item value. It cannot be in use for
6731 * any other purpose if this task is running, and it must be
6732 * running to give back the mutex. */
6733 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority );
6734 prvAddTaskToReadyList( pxTCB );
6735 #if ( configNUMBER_OF_CORES > 1 )
6737 /* The priority of the task is dropped. Yield the core on
6738 * which the task is running. */
6739 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6741 prvYieldCore( pxTCB->xTaskRunState );
6744 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6746 /* Return true to indicate that a context switch is required.
6747 * This is only actually required in the corner case whereby
6748 * multiple mutexes were held and the mutexes were given back
6749 * in an order different to that in which they were taken.
6750 * If a context switch did not occur when the first mutex was
6751 * returned, even if a task was waiting on it, then a context
6752 * switch should occur when the last mutex is returned whether
6753 * a task is waiting on it or not. */
6758 mtCOVERAGE_TEST_MARKER();
6763 mtCOVERAGE_TEST_MARKER();
6768 mtCOVERAGE_TEST_MARKER();
6771 traceRETURN_xTaskPriorityDisinherit( xReturn );
6776 #endif /* configUSE_MUTEXES */
6777 /*-----------------------------------------------------------*/
6779 #if ( configUSE_MUTEXES == 1 )
6781 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6782 UBaseType_t uxHighestPriorityWaitingTask )
6784 TCB_t * const pxTCB = pxMutexHolder;
6785 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6786 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6788 traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6790 if( pxMutexHolder != NULL )
6792 /* If pxMutexHolder is not NULL then the holder must hold at least
6794 configASSERT( pxTCB->uxMutexesHeld );
6796 /* Determine the priority to which the priority of the task that
6797 * holds the mutex should be set. This will be the greater of the
6798 * holding task's base priority and the priority of the highest
6799 * priority task that is waiting to obtain the mutex. */
6800 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6802 uxPriorityToUse = uxHighestPriorityWaitingTask;
6806 uxPriorityToUse = pxTCB->uxBasePriority;
6809 /* Does the priority need to change? */
6810 if( pxTCB->uxPriority != uxPriorityToUse )
6812 /* Only disinherit if no other mutexes are held. This is a
6813 * simplification in the priority inheritance implementation. If
6814 * the task that holds the mutex is also holding other mutexes then
6815 * the other mutexes may have caused the priority inheritance. */
6816 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6818 /* If a task has timed out because it already holds the
6819 * mutex it was trying to obtain then it cannot of inherited
6820 * its own priority. */
6821 configASSERT( pxTCB != pxCurrentTCB );
6823 /* Disinherit the priority, remembering the previous
6824 * priority to facilitate determining the subject task's
6826 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6827 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6828 pxTCB->uxPriority = uxPriorityToUse;
6830 /* Only reset the event list item value if the value is not
6831 * being used for anything else. */
6832 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
6834 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse );
6838 mtCOVERAGE_TEST_MARKER();
6841 /* If the running task is not the task that holds the mutex
6842 * then the task that holds the mutex could be in either the
6843 * Ready, Blocked or Suspended states. Only remove the task
6844 * from its current state list if it is in the Ready state as
6845 * the task's priority is going to change and there is one
6846 * Ready list per priority. */
6847 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6849 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6851 /* It is known that the task is in its ready list so
6852 * there is no need to check again and the port level
6853 * reset macro can be called directly. */
6854 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6858 mtCOVERAGE_TEST_MARKER();
6861 prvAddTaskToReadyList( pxTCB );
6862 #if ( configNUMBER_OF_CORES > 1 )
6864 /* The priority of the task is dropped. Yield the core on
6865 * which the task is running. */
6866 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6868 prvYieldCore( pxTCB->xTaskRunState );
6871 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6875 mtCOVERAGE_TEST_MARKER();
6880 mtCOVERAGE_TEST_MARKER();
6885 mtCOVERAGE_TEST_MARKER();
6890 mtCOVERAGE_TEST_MARKER();
6893 traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6896 #endif /* configUSE_MUTEXES */
6897 /*-----------------------------------------------------------*/
6899 #if ( configNUMBER_OF_CORES > 1 )
6901 /* If not in a critical section then yield immediately.
6902 * Otherwise set xYieldPendings to true to wait to
6903 * yield until exiting the critical section.
6905 void vTaskYieldWithinAPI( void )
6907 traceENTER_vTaskYieldWithinAPI();
6909 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6915 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6918 traceRETURN_vTaskYieldWithinAPI();
6920 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6922 /*-----------------------------------------------------------*/
6924 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6926 void vTaskEnterCritical( void )
6928 traceENTER_vTaskEnterCritical();
6930 portDISABLE_INTERRUPTS();
6932 if( xSchedulerRunning != pdFALSE )
6934 ( pxCurrentTCB->uxCriticalNesting )++;
6936 /* This is not the interrupt safe version of the enter critical
6937 * function so assert() if it is being called from an interrupt
6938 * context. Only API functions that end in "FromISR" can be used in an
6939 * interrupt. Only assert if the critical nesting count is 1 to
6940 * protect against recursive calls if the assert function also uses a
6941 * critical section. */
6942 if( pxCurrentTCB->uxCriticalNesting == 1U )
6944 portASSERT_IF_IN_ISR();
6949 mtCOVERAGE_TEST_MARKER();
6952 traceRETURN_vTaskEnterCritical();
6955 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6956 /*-----------------------------------------------------------*/
6958 #if ( configNUMBER_OF_CORES > 1 )
6960 void vTaskEnterCritical( void )
6962 traceENTER_vTaskEnterCritical();
6964 portDISABLE_INTERRUPTS();
6966 if( xSchedulerRunning != pdFALSE )
6968 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6970 portGET_TASK_LOCK();
6974 portINCREMENT_CRITICAL_NESTING_COUNT();
6976 /* This is not the interrupt safe version of the enter critical
6977 * function so assert() if it is being called from an interrupt
6978 * context. Only API functions that end in "FromISR" can be used in an
6979 * interrupt. Only assert if the critical nesting count is 1 to
6980 * protect against recursive calls if the assert function also uses a
6981 * critical section. */
6982 if( portGET_CRITICAL_NESTING_COUNT() == 1U )
6984 portASSERT_IF_IN_ISR();
6986 if( uxSchedulerSuspended == 0U )
6988 /* The only time there would be a problem is if this is called
6989 * before a context switch and vTaskExitCritical() is called
6990 * after pxCurrentTCB changes. Therefore this should not be
6991 * used within vTaskSwitchContext(). */
6992 prvCheckForRunStateChange();
6998 mtCOVERAGE_TEST_MARKER();
7001 traceRETURN_vTaskEnterCritical();
7004 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7006 /*-----------------------------------------------------------*/
7008 #if ( configNUMBER_OF_CORES > 1 )
7010 UBaseType_t vTaskEnterCriticalFromISR( void )
7012 UBaseType_t uxSavedInterruptStatus = 0;
7014 traceENTER_vTaskEnterCriticalFromISR();
7016 if( xSchedulerRunning != pdFALSE )
7018 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
7020 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7025 portINCREMENT_CRITICAL_NESTING_COUNT();
7029 mtCOVERAGE_TEST_MARKER();
7032 traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
7034 return uxSavedInterruptStatus;
7037 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7038 /*-----------------------------------------------------------*/
7040 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
7042 void vTaskExitCritical( void )
7044 traceENTER_vTaskExitCritical();
7046 if( xSchedulerRunning != pdFALSE )
7048 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
7049 * does not match a previous call to vTaskEnterCritical(). */
7050 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
7052 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7053 * to exit critical section from ISR. */
7054 portASSERT_IF_IN_ISR();
7056 if( pxCurrentTCB->uxCriticalNesting > 0U )
7058 ( pxCurrentTCB->uxCriticalNesting )--;
7060 if( pxCurrentTCB->uxCriticalNesting == 0U )
7062 portENABLE_INTERRUPTS();
7066 mtCOVERAGE_TEST_MARKER();
7071 mtCOVERAGE_TEST_MARKER();
7076 mtCOVERAGE_TEST_MARKER();
7079 traceRETURN_vTaskExitCritical();
7082 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
7083 /*-----------------------------------------------------------*/
7085 #if ( configNUMBER_OF_CORES > 1 )
7087 void vTaskExitCritical( void )
7089 traceENTER_vTaskExitCritical();
7091 if( xSchedulerRunning != pdFALSE )
7093 /* If critical nesting count is zero then this function
7094 * does not match a previous call to vTaskEnterCritical(). */
7095 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7097 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7098 * to exit critical section from ISR. */
7099 portASSERT_IF_IN_ISR();
7101 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7103 portDECREMENT_CRITICAL_NESTING_COUNT();
7105 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7107 BaseType_t xYieldCurrentTask;
7109 /* Get the xYieldPending stats inside the critical section. */
7110 xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
7112 portRELEASE_ISR_LOCK();
7113 portRELEASE_TASK_LOCK();
7114 portENABLE_INTERRUPTS();
7116 /* When a task yields in a critical section it just sets
7117 * xYieldPending to true. So now that we have exited the
7118 * critical section check if xYieldPending is true, and
7120 if( xYieldCurrentTask != pdFALSE )
7127 mtCOVERAGE_TEST_MARKER();
7132 mtCOVERAGE_TEST_MARKER();
7137 mtCOVERAGE_TEST_MARKER();
7140 traceRETURN_vTaskExitCritical();
7143 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7144 /*-----------------------------------------------------------*/
7146 #if ( configNUMBER_OF_CORES > 1 )
7148 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
7150 traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
7152 if( xSchedulerRunning != pdFALSE )
7154 /* If critical nesting count is zero then this function
7155 * does not match a previous call to vTaskEnterCritical(). */
7156 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7158 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7160 portDECREMENT_CRITICAL_NESTING_COUNT();
7162 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7164 portRELEASE_ISR_LOCK();
7165 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
7169 mtCOVERAGE_TEST_MARKER();
7174 mtCOVERAGE_TEST_MARKER();
7179 mtCOVERAGE_TEST_MARKER();
7182 traceRETURN_vTaskExitCriticalFromISR();
7185 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7186 /*-----------------------------------------------------------*/
7188 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
7190 static char * prvWriteNameToBuffer( char * pcBuffer,
7191 const char * pcTaskName )
7195 /* Start by copying the entire string. */
7196 ( void ) strcpy( pcBuffer, pcTaskName );
7198 /* Pad the end of the string with spaces to ensure columns line up when
7200 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
7202 pcBuffer[ x ] = ' ';
7206 pcBuffer[ x ] = ( char ) 0x00;
7208 /* Return the new end of string. */
7209 return &( pcBuffer[ x ] );
7212 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
7213 /*-----------------------------------------------------------*/
7215 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
7217 void vTaskListTasks( char * pcWriteBuffer,
7218 size_t uxBufferLength )
7220 TaskStatus_t * pxTaskStatusArray;
7221 size_t uxConsumedBufferLength = 0;
7222 size_t uxCharsWrittenBySnprintf;
7223 int iSnprintfReturnValue;
7224 BaseType_t xOutputBufferFull = pdFALSE;
7225 UBaseType_t uxArraySize, x;
7228 traceENTER_vTaskListTasks( pcWriteBuffer, uxBufferLength );
7233 * This function is provided for convenience only, and is used by many
7234 * of the demo applications. Do not consider it to be part of the
7237 * vTaskListTasks() calls uxTaskGetSystemState(), then formats part of the
7238 * uxTaskGetSystemState() output into a human readable table that
7239 * displays task: names, states, priority, stack usage and task number.
7240 * Stack usage specified as the number of unused StackType_t words stack can hold
7241 * on top of stack - not the number of bytes.
7243 * vTaskListTasks() has a dependency on the snprintf() C library function that
7244 * might bloat the code size, use a lot of stack, and provide different
7245 * results on different platforms. An alternative, tiny, third party,
7246 * and limited functionality implementation of snprintf() is provided in
7247 * many of the FreeRTOS/Demo sub-directories in a file called
7248 * printf-stdarg.c (note printf-stdarg.c does not provide a full
7249 * snprintf() implementation!).
7251 * It is recommended that production systems call uxTaskGetSystemState()
7252 * directly to get access to raw stats data, rather than indirectly
7253 * through a call to vTaskListTasks().
7257 /* Make sure the write buffer does not contain a string. */
7258 *pcWriteBuffer = ( char ) 0x00;
7260 /* Take a snapshot of the number of tasks in case it changes while this
7261 * function is executing. */
7262 uxArraySize = uxCurrentNumberOfTasks;
7264 /* Allocate an array index for each task. NOTE! if
7265 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7266 * equate to NULL. */
7267 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7268 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7269 /* coverity[misra_c_2012_rule_11_5_violation] */
7270 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7272 if( pxTaskStatusArray != NULL )
7274 /* Generate the (binary) data. */
7275 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
7277 /* Create a human readable table from the binary data. */
7278 for( x = 0; x < uxArraySize; x++ )
7280 switch( pxTaskStatusArray[ x ].eCurrentState )
7283 cStatus = tskRUNNING_CHAR;
7287 cStatus = tskREADY_CHAR;
7291 cStatus = tskBLOCKED_CHAR;
7295 cStatus = tskSUSPENDED_CHAR;
7299 cStatus = tskDELETED_CHAR;
7302 case eInvalid: /* Fall through. */
7303 default: /* Should not get here, but it is included
7304 * to prevent static checking errors. */
7305 cStatus = ( char ) 0x00;
7309 /* Is there enough space in the buffer to hold task name? */
7310 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7312 /* Write the task name to the string, padding with spaces so it
7313 * can be printed in tabular form more easily. */
7314 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7315 /* Do not count the terminating null character. */
7316 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7318 /* Is there space left in the buffer? -1 is done because snprintf
7319 * writes a terminating null character. So we are essentially
7320 * checking if the buffer has space to write at least one non-null
7322 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7324 /* Write the rest of the string. */
7325 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
7326 /* MISRA Ref 21.6.1 [snprintf for utility] */
7327 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7328 /* coverity[misra_c_2012_rule_21_6_violation] */
7329 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7330 uxBufferLength - uxConsumedBufferLength,
7331 "\t%c\t%u\t%u\t%u\t0x%x\r\n",
7333 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7334 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7335 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber,
7336 ( unsigned int ) pxTaskStatusArray[ x ].uxCoreAffinityMask );
7337 #else /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7338 /* MISRA Ref 21.6.1 [snprintf for utility] */
7339 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7340 /* coverity[misra_c_2012_rule_21_6_violation] */
7341 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7342 uxBufferLength - uxConsumedBufferLength,
7343 "\t%c\t%u\t%u\t%u\r\n",
7345 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7346 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7347 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber );
7348 #endif /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7349 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7351 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7352 pcWriteBuffer += uxCharsWrittenBySnprintf;
7356 xOutputBufferFull = pdTRUE;
7361 xOutputBufferFull = pdTRUE;
7364 if( xOutputBufferFull == pdTRUE )
7370 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7371 * is 0 then vPortFree() will be #defined to nothing. */
7372 vPortFree( pxTaskStatusArray );
7376 mtCOVERAGE_TEST_MARKER();
7379 traceRETURN_vTaskListTasks();
7382 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7383 /*----------------------------------------------------------*/
7385 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
7387 void vTaskGetRunTimeStatistics( char * pcWriteBuffer,
7388 size_t uxBufferLength )
7390 TaskStatus_t * pxTaskStatusArray;
7391 size_t uxConsumedBufferLength = 0;
7392 size_t uxCharsWrittenBySnprintf;
7393 int iSnprintfReturnValue;
7394 BaseType_t xOutputBufferFull = pdFALSE;
7395 UBaseType_t uxArraySize, x;
7396 configRUN_TIME_COUNTER_TYPE ulTotalTime = 0;
7397 configRUN_TIME_COUNTER_TYPE ulStatsAsPercentage;
7399 traceENTER_vTaskGetRunTimeStatistics( pcWriteBuffer, uxBufferLength );
7404 * This function is provided for convenience only, and is used by many
7405 * of the demo applications. Do not consider it to be part of the
7408 * vTaskGetRunTimeStatistics() calls uxTaskGetSystemState(), then formats part
7409 * of the uxTaskGetSystemState() output into a human readable table that
7410 * displays the amount of time each task has spent in the Running state
7411 * in both absolute and percentage terms.
7413 * vTaskGetRunTimeStatistics() has a dependency on the snprintf() C library
7414 * function that might bloat the code size, use a lot of stack, and
7415 * provide different results on different platforms. An alternative,
7416 * tiny, third party, and limited functionality implementation of
7417 * snprintf() is provided in many of the FreeRTOS/Demo sub-directories in
7418 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
7419 * a full snprintf() implementation!).
7421 * It is recommended that production systems call uxTaskGetSystemState()
7422 * directly to get access to raw stats data, rather than indirectly
7423 * through a call to vTaskGetRunTimeStatistics().
7426 /* Make sure the write buffer does not contain a string. */
7427 *pcWriteBuffer = ( char ) 0x00;
7429 /* Take a snapshot of the number of tasks in case it changes while this
7430 * function is executing. */
7431 uxArraySize = uxCurrentNumberOfTasks;
7433 /* Allocate an array index for each task. NOTE! If
7434 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7435 * equate to NULL. */
7436 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7437 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7438 /* coverity[misra_c_2012_rule_11_5_violation] */
7439 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7441 if( pxTaskStatusArray != NULL )
7443 /* Generate the (binary) data. */
7444 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
7446 /* For percentage calculations. */
7447 ulTotalTime /= ( ( configRUN_TIME_COUNTER_TYPE ) 100U );
7449 /* Avoid divide by zero errors. */
7450 if( ulTotalTime > 0U )
7452 /* Create a human readable table from the binary data. */
7453 for( x = 0; x < uxArraySize; x++ )
7455 /* What percentage of the total run time has the task used?
7456 * This will always be rounded down to the nearest integer.
7457 * ulTotalRunTime has already been divided by 100. */
7458 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
7460 /* Is there enough space in the buffer to hold task name? */
7461 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7463 /* Write the task name to the string, padding with
7464 * spaces so it can be printed in tabular form more
7466 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7467 /* Do not count the terminating null character. */
7468 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7470 /* Is there space left in the buffer? -1 is done because snprintf
7471 * writes a terminating null character. So we are essentially
7472 * checking if the buffer has space to write at least one non-null
7474 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7476 if( ulStatsAsPercentage > 0U )
7478 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7480 /* MISRA Ref 21.6.1 [snprintf for utility] */
7481 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7482 /* coverity[misra_c_2012_rule_21_6_violation] */
7483 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7484 uxBufferLength - uxConsumedBufferLength,
7485 "\t%lu\t\t%lu%%\r\n",
7486 pxTaskStatusArray[ x ].ulRunTimeCounter,
7487 ulStatsAsPercentage );
7489 #else /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7491 /* sizeof( int ) == sizeof( long ) so a smaller
7492 * printf() library can be used. */
7493 /* MISRA Ref 21.6.1 [snprintf for utility] */
7494 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7495 /* coverity[misra_c_2012_rule_21_6_violation] */
7496 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7497 uxBufferLength - uxConsumedBufferLength,
7499 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter,
7500 ( unsigned int ) ulStatsAsPercentage );
7502 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7506 /* If the percentage is zero here then the task has
7507 * consumed less than 1% of the total run time. */
7508 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7510 /* MISRA Ref 21.6.1 [snprintf for utility] */
7511 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7512 /* coverity[misra_c_2012_rule_21_6_violation] */
7513 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7514 uxBufferLength - uxConsumedBufferLength,
7515 "\t%lu\t\t<1%%\r\n",
7516 pxTaskStatusArray[ x ].ulRunTimeCounter );
7520 /* sizeof( int ) == sizeof( long ) so a smaller
7521 * printf() library can be used. */
7522 /* MISRA Ref 21.6.1 [snprintf for utility] */
7523 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7524 /* coverity[misra_c_2012_rule_21_6_violation] */
7525 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7526 uxBufferLength - uxConsumedBufferLength,
7528 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter );
7530 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7533 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7534 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7535 pcWriteBuffer += uxCharsWrittenBySnprintf;
7539 xOutputBufferFull = pdTRUE;
7544 xOutputBufferFull = pdTRUE;
7547 if( xOutputBufferFull == pdTRUE )
7555 mtCOVERAGE_TEST_MARKER();
7558 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7559 * is 0 then vPortFree() will be #defined to nothing. */
7560 vPortFree( pxTaskStatusArray );
7564 mtCOVERAGE_TEST_MARKER();
7567 traceRETURN_vTaskGetRunTimeStatistics();
7570 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7571 /*-----------------------------------------------------------*/
7573 TickType_t uxTaskResetEventItemValue( void )
7575 TickType_t uxReturn;
7577 traceENTER_uxTaskResetEventItemValue();
7579 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7581 /* Reset the event list item to its normal value - so it can be used with
7582 * queues and semaphores. */
7583 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) );
7585 traceRETURN_uxTaskResetEventItemValue( uxReturn );
7589 /*-----------------------------------------------------------*/
7591 #if ( configUSE_MUTEXES == 1 )
7593 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7597 traceENTER_pvTaskIncrementMutexHeldCount();
7599 pxTCB = pxCurrentTCB;
7601 /* If xSemaphoreCreateMutex() is called before any tasks have been created
7602 * then pxCurrentTCB will be NULL. */
7605 ( pxTCB->uxMutexesHeld )++;
7608 traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7613 #endif /* configUSE_MUTEXES */
7614 /*-----------------------------------------------------------*/
7616 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7618 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7619 BaseType_t xClearCountOnExit,
7620 TickType_t xTicksToWait )
7623 BaseType_t xAlreadyYielded, xShouldBlock = pdFALSE;
7625 traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7627 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7629 /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
7630 * non-deterministic operation. */
7633 /* We MUST enter a critical section to atomically check if a notification
7634 * has occurred and set the flag to indicate that we are waiting for
7635 * a notification. If we do not do so, a notification sent from an ISR
7637 taskENTER_CRITICAL();
7639 /* Only block if the notification count is not already non-zero. */
7640 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0U )
7642 /* Mark this task as waiting for a notification. */
7643 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7645 if( xTicksToWait > ( TickType_t ) 0 )
7647 xShouldBlock = pdTRUE;
7651 mtCOVERAGE_TEST_MARKER();
7656 mtCOVERAGE_TEST_MARKER();
7659 taskEXIT_CRITICAL();
7661 /* We are now out of the critical section but the scheduler is still
7662 * suspended, so we are safe to do non-deterministic operations such
7663 * as prvAddCurrentTaskToDelayedList. */
7664 if( xShouldBlock == pdTRUE )
7666 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7667 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7671 mtCOVERAGE_TEST_MARKER();
7674 xAlreadyYielded = xTaskResumeAll();
7676 /* Force a reschedule if xTaskResumeAll has not already done so. */
7677 if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
7679 taskYIELD_WITHIN_API();
7683 mtCOVERAGE_TEST_MARKER();
7686 taskENTER_CRITICAL();
7688 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7689 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7691 if( ulReturn != 0U )
7693 if( xClearCountOnExit != pdFALSE )
7695 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ( uint32_t ) 0U;
7699 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7704 mtCOVERAGE_TEST_MARKER();
7707 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7709 taskEXIT_CRITICAL();
7711 traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7716 #endif /* configUSE_TASK_NOTIFICATIONS */
7717 /*-----------------------------------------------------------*/
7719 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7721 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7722 uint32_t ulBitsToClearOnEntry,
7723 uint32_t ulBitsToClearOnExit,
7724 uint32_t * pulNotificationValue,
7725 TickType_t xTicksToWait )
7727 BaseType_t xReturn, xAlreadyYielded, xShouldBlock = pdFALSE;
7729 traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7731 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7733 /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
7734 * non-deterministic operation. */
7737 /* We MUST enter a critical section to atomically check and update the
7738 * task notification value. If we do not do so, a notification from
7739 * an ISR will get lost. */
7740 taskENTER_CRITICAL();
7742 /* Only block if a notification is not already pending. */
7743 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7745 /* Clear bits in the task's notification value as bits may get
7746 * set by the notifying task or interrupt. This can be used
7747 * to clear the value to zero. */
7748 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7750 /* Mark this task as waiting for a notification. */
7751 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7753 if( xTicksToWait > ( TickType_t ) 0 )
7755 xShouldBlock = pdTRUE;
7759 mtCOVERAGE_TEST_MARKER();
7764 mtCOVERAGE_TEST_MARKER();
7767 taskEXIT_CRITICAL();
7769 /* We are now out of the critical section but the scheduler is still
7770 * suspended, so we are safe to do non-deterministic operations such
7771 * as prvAddCurrentTaskToDelayedList. */
7772 if( xShouldBlock == pdTRUE )
7774 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7775 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7779 mtCOVERAGE_TEST_MARKER();
7782 xAlreadyYielded = xTaskResumeAll();
7784 /* Force a reschedule if xTaskResumeAll has not already done so. */
7785 if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
7787 taskYIELD_WITHIN_API();
7791 mtCOVERAGE_TEST_MARKER();
7794 taskENTER_CRITICAL();
7796 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7798 if( pulNotificationValue != NULL )
7800 /* Output the current notification value, which may or may not
7802 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7805 /* If ucNotifyValue is set then either the task never entered the
7806 * blocked state (because a notification was already pending) or the
7807 * task unblocked because of a notification. Otherwise the task
7808 * unblocked because of a timeout. */
7809 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7811 /* A notification was not received. */
7816 /* A notification was already pending or a notification was
7817 * received while the task was waiting. */
7818 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7822 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7824 taskEXIT_CRITICAL();
7826 traceRETURN_xTaskGenericNotifyWait( xReturn );
7831 #endif /* configUSE_TASK_NOTIFICATIONS */
7832 /*-----------------------------------------------------------*/
7834 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7836 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7837 UBaseType_t uxIndexToNotify,
7839 eNotifyAction eAction,
7840 uint32_t * pulPreviousNotificationValue )
7843 BaseType_t xReturn = pdPASS;
7844 uint8_t ucOriginalNotifyState;
7846 traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7848 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7849 configASSERT( xTaskToNotify );
7850 pxTCB = xTaskToNotify;
7852 taskENTER_CRITICAL();
7854 if( pulPreviousNotificationValue != NULL )
7856 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7859 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7861 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7866 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7870 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7873 case eSetValueWithOverwrite:
7874 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7877 case eSetValueWithoutOverwrite:
7879 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7881 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7885 /* The value could not be written to the task. */
7893 /* The task is being notified without its notify value being
7899 /* Should not get here if all enums are handled.
7900 * Artificially force an assert by testing a value the
7901 * compiler can't assume is const. */
7902 configASSERT( xTickCount == ( TickType_t ) 0 );
7907 traceTASK_NOTIFY( uxIndexToNotify );
7909 /* If the task is in the blocked state specifically to wait for a
7910 * notification then unblock it now. */
7911 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7913 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7914 prvAddTaskToReadyList( pxTCB );
7916 /* The task should not have been on an event list. */
7917 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7919 #if ( configUSE_TICKLESS_IDLE != 0 )
7921 /* If a task is blocked waiting for a notification then
7922 * xNextTaskUnblockTime might be set to the blocked task's time
7923 * out time. If the task is unblocked for a reason other than
7924 * a timeout xNextTaskUnblockTime is normally left unchanged,
7925 * because it will automatically get reset to a new value when
7926 * the tick count equals xNextTaskUnblockTime. However if
7927 * tickless idling is used it might be more important to enter
7928 * sleep mode at the earliest possible time - so reset
7929 * xNextTaskUnblockTime here to ensure it is updated at the
7930 * earliest possible time. */
7931 prvResetNextTaskUnblockTime();
7935 /* Check if the notified task has a priority above the currently
7936 * executing task. */
7937 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7941 mtCOVERAGE_TEST_MARKER();
7944 taskEXIT_CRITICAL();
7946 traceRETURN_xTaskGenericNotify( xReturn );
7951 #endif /* configUSE_TASK_NOTIFICATIONS */
7952 /*-----------------------------------------------------------*/
7954 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7956 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
7957 UBaseType_t uxIndexToNotify,
7959 eNotifyAction eAction,
7960 uint32_t * pulPreviousNotificationValue,
7961 BaseType_t * pxHigherPriorityTaskWoken )
7964 uint8_t ucOriginalNotifyState;
7965 BaseType_t xReturn = pdPASS;
7966 UBaseType_t uxSavedInterruptStatus;
7968 traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
7970 configASSERT( xTaskToNotify );
7971 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7973 /* RTOS ports that support interrupt nesting have the concept of a
7974 * maximum system call (or maximum API call) interrupt priority.
7975 * Interrupts that are above the maximum system call priority are keep
7976 * permanently enabled, even when the RTOS kernel is in a critical section,
7977 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7978 * is defined in FreeRTOSConfig.h then
7979 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7980 * failure if a FreeRTOS API function is called from an interrupt that has
7981 * been assigned a priority above the configured maximum system call
7982 * priority. Only FreeRTOS functions that end in FromISR can be called
7983 * from interrupts that have been assigned a priority at or (logically)
7984 * below the maximum system call interrupt priority. FreeRTOS maintains a
7985 * separate interrupt safe API to ensure interrupt entry is as fast and as
7986 * simple as possible. More information (albeit Cortex-M specific) is
7987 * provided on the following link:
7988 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7989 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7991 pxTCB = xTaskToNotify;
7993 /* MISRA Ref 4.7.1 [Return value shall be checked] */
7994 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
7995 /* coverity[misra_c_2012_directive_4_7_violation] */
7996 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
7998 if( pulPreviousNotificationValue != NULL )
8000 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
8003 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8004 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8009 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
8013 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8016 case eSetValueWithOverwrite:
8017 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8020 case eSetValueWithoutOverwrite:
8022 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
8024 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8028 /* The value could not be written to the task. */
8036 /* The task is being notified without its notify value being
8042 /* Should not get here if all enums are handled.
8043 * Artificially force an assert by testing a value the
8044 * compiler can't assume is const. */
8045 configASSERT( xTickCount == ( TickType_t ) 0 );
8049 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
8051 /* If the task is in the blocked state specifically to wait for a
8052 * notification then unblock it now. */
8053 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8055 /* The task should not have been on an event list. */
8056 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8058 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8060 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8061 prvAddTaskToReadyList( pxTCB );
8065 /* The delayed and ready lists cannot be accessed, so hold
8066 * this task pending until the scheduler is resumed. */
8067 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8070 #if ( configNUMBER_OF_CORES == 1 )
8072 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8074 /* The notified task has a priority above the currently
8075 * executing task so a yield is required. */
8076 if( pxHigherPriorityTaskWoken != NULL )
8078 *pxHigherPriorityTaskWoken = pdTRUE;
8081 /* Mark that a yield is pending in case the user is not
8082 * using the "xHigherPriorityTaskWoken" parameter to an ISR
8083 * safe FreeRTOS function. */
8084 xYieldPendings[ 0 ] = pdTRUE;
8088 mtCOVERAGE_TEST_MARKER();
8091 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8093 #if ( configUSE_PREEMPTION == 1 )
8095 prvYieldForTask( pxTCB );
8097 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8099 if( pxHigherPriorityTaskWoken != NULL )
8101 *pxHigherPriorityTaskWoken = pdTRUE;
8105 #endif /* if ( configUSE_PREEMPTION == 1 ) */
8107 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8110 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8112 traceRETURN_xTaskGenericNotifyFromISR( xReturn );
8117 #endif /* configUSE_TASK_NOTIFICATIONS */
8118 /*-----------------------------------------------------------*/
8120 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8122 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
8123 UBaseType_t uxIndexToNotify,
8124 BaseType_t * pxHigherPriorityTaskWoken )
8127 uint8_t ucOriginalNotifyState;
8128 UBaseType_t uxSavedInterruptStatus;
8130 traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
8132 configASSERT( xTaskToNotify );
8133 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8135 /* RTOS ports that support interrupt nesting have the concept of a
8136 * maximum system call (or maximum API call) interrupt priority.
8137 * Interrupts that are above the maximum system call priority are keep
8138 * permanently enabled, even when the RTOS kernel is in a critical section,
8139 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
8140 * is defined in FreeRTOSConfig.h then
8141 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
8142 * failure if a FreeRTOS API function is called from an interrupt that has
8143 * been assigned a priority above the configured maximum system call
8144 * priority. Only FreeRTOS functions that end in FromISR can be called
8145 * from interrupts that have been assigned a priority at or (logically)
8146 * below the maximum system call interrupt priority. FreeRTOS maintains a
8147 * separate interrupt safe API to ensure interrupt entry is as fast and as
8148 * simple as possible. More information (albeit Cortex-M specific) is
8149 * provided on the following link:
8150 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
8151 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
8153 pxTCB = xTaskToNotify;
8155 /* MISRA Ref 4.7.1 [Return value shall be checked] */
8156 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
8157 /* coverity[misra_c_2012_directive_4_7_violation] */
8158 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
8160 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8161 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8163 /* 'Giving' is equivalent to incrementing a count in a counting
8165 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8167 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
8169 /* If the task is in the blocked state specifically to wait for a
8170 * notification then unblock it now. */
8171 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8173 /* The task should not have been on an event list. */
8174 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8176 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8178 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8179 prvAddTaskToReadyList( pxTCB );
8183 /* The delayed and ready lists cannot be accessed, so hold
8184 * this task pending until the scheduler is resumed. */
8185 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8188 #if ( configNUMBER_OF_CORES == 1 )
8190 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8192 /* The notified task has a priority above the currently
8193 * executing task so a yield is required. */
8194 if( pxHigherPriorityTaskWoken != NULL )
8196 *pxHigherPriorityTaskWoken = pdTRUE;
8199 /* Mark that a yield is pending in case the user is not
8200 * using the "xHigherPriorityTaskWoken" parameter in an ISR
8201 * safe FreeRTOS function. */
8202 xYieldPendings[ 0 ] = pdTRUE;
8206 mtCOVERAGE_TEST_MARKER();
8209 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8211 #if ( configUSE_PREEMPTION == 1 )
8213 prvYieldForTask( pxTCB );
8215 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8217 if( pxHigherPriorityTaskWoken != NULL )
8219 *pxHigherPriorityTaskWoken = pdTRUE;
8223 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
8225 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8228 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8230 traceRETURN_vTaskGenericNotifyGiveFromISR();
8233 #endif /* configUSE_TASK_NOTIFICATIONS */
8234 /*-----------------------------------------------------------*/
8236 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8238 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
8239 UBaseType_t uxIndexToClear )
8244 traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
8246 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8248 /* If null is passed in here then it is the calling task that is having
8249 * its notification state cleared. */
8250 pxTCB = prvGetTCBFromHandle( xTask );
8252 taskENTER_CRITICAL();
8254 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
8256 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
8264 taskEXIT_CRITICAL();
8266 traceRETURN_xTaskGenericNotifyStateClear( xReturn );
8271 #endif /* configUSE_TASK_NOTIFICATIONS */
8272 /*-----------------------------------------------------------*/
8274 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8276 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
8277 UBaseType_t uxIndexToClear,
8278 uint32_t ulBitsToClear )
8283 traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
8285 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8287 /* If null is passed in here then it is the calling task that is having
8288 * its notification state cleared. */
8289 pxTCB = prvGetTCBFromHandle( xTask );
8291 taskENTER_CRITICAL();
8293 /* Return the notification as it was before the bits were cleared,
8294 * then clear the bit mask. */
8295 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
8296 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
8298 taskEXIT_CRITICAL();
8300 traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
8305 #endif /* configUSE_TASK_NOTIFICATIONS */
8306 /*-----------------------------------------------------------*/
8308 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8310 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
8314 traceENTER_ulTaskGetRunTimeCounter( xTask );
8316 pxTCB = prvGetTCBFromHandle( xTask );
8318 traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
8320 return pxTCB->ulRunTimeCounter;
8323 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8324 /*-----------------------------------------------------------*/
8326 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8328 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
8331 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8333 traceENTER_ulTaskGetRunTimePercent( xTask );
8335 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
8337 /* For percentage calculations. */
8338 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8340 /* Avoid divide by zero errors. */
8341 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8343 pxTCB = prvGetTCBFromHandle( xTask );
8344 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
8351 traceRETURN_ulTaskGetRunTimePercent( ulReturn );
8356 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8357 /*-----------------------------------------------------------*/
8359 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8361 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
8363 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
8366 traceENTER_ulTaskGetIdleRunTimeCounter();
8368 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8370 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8373 traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
8378 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8379 /*-----------------------------------------------------------*/
8381 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8383 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
8385 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8386 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
8389 traceENTER_ulTaskGetIdleRunTimePercent();
8391 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
8393 /* For percentage calculations. */
8394 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8396 /* Avoid divide by zero errors. */
8397 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8399 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8401 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8404 ulReturn = ulRunTimeCounter / ulTotalTime;
8411 traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
8416 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8417 /*-----------------------------------------------------------*/
8419 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
8420 const BaseType_t xCanBlockIndefinitely )
8422 TickType_t xTimeToWake;
8423 const TickType_t xConstTickCount = xTickCount;
8424 List_t * const pxDelayedList = pxDelayedTaskList;
8425 List_t * const pxOverflowDelayedList = pxOverflowDelayedTaskList;
8427 #if ( INCLUDE_xTaskAbortDelay == 1 )
8429 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
8430 * reset to pdFALSE so it can be detected as having been set to pdTRUE
8431 * when the task leaves the Blocked state. */
8432 pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
8436 /* Remove the task from the ready list before adding it to the blocked list
8437 * as the same list item is used for both lists. */
8438 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
8440 /* The current task must be in a ready list, so there is no need to
8441 * check, and the port reset macro can be called directly. */
8442 portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
8446 mtCOVERAGE_TEST_MARKER();
8449 #if ( INCLUDE_vTaskSuspend == 1 )
8451 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
8453 /* Add the task to the suspended task list instead of a delayed task
8454 * list to ensure it is not woken by a timing event. It will block
8456 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
8460 /* Calculate the time at which the task should be woken if the event
8461 * does not occur. This may overflow but this doesn't matter, the
8462 * kernel will manage it correctly. */
8463 xTimeToWake = xConstTickCount + xTicksToWait;
8465 /* The list item will be inserted in wake time order. */
8466 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8468 if( xTimeToWake < xConstTickCount )
8470 /* Wake time has overflowed. Place this item in the overflow
8472 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8473 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8477 /* The wake time has not overflowed, so the current block list
8479 traceMOVED_TASK_TO_DELAYED_LIST();
8480 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8482 /* If the task entering the blocked state was placed at the
8483 * head of the list of blocked tasks then xNextTaskUnblockTime
8484 * needs to be updated too. */
8485 if( xTimeToWake < xNextTaskUnblockTime )
8487 xNextTaskUnblockTime = xTimeToWake;
8491 mtCOVERAGE_TEST_MARKER();
8496 #else /* INCLUDE_vTaskSuspend */
8498 /* Calculate the time at which the task should be woken if the event
8499 * does not occur. This may overflow but this doesn't matter, the kernel
8500 * will manage it correctly. */
8501 xTimeToWake = xConstTickCount + xTicksToWait;
8503 /* The list item will be inserted in wake time order. */
8504 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8506 if( xTimeToWake < xConstTickCount )
8508 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8509 /* Wake time has overflowed. Place this item in the overflow list. */
8510 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8514 traceMOVED_TASK_TO_DELAYED_LIST();
8515 /* The wake time has not overflowed, so the current block list is used. */
8516 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8518 /* If the task entering the blocked state was placed at the head of the
8519 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
8521 if( xTimeToWake < xNextTaskUnblockTime )
8523 xNextTaskUnblockTime = xTimeToWake;
8527 mtCOVERAGE_TEST_MARKER();
8531 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
8532 ( void ) xCanBlockIndefinitely;
8534 #endif /* INCLUDE_vTaskSuspend */
8536 /*-----------------------------------------------------------*/
8538 #if ( portUSING_MPU_WRAPPERS == 1 )
8540 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
8544 traceENTER_xTaskGetMPUSettings( xTask );
8546 pxTCB = prvGetTCBFromHandle( xTask );
8548 traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
8550 return &( pxTCB->xMPUSettings );
8553 #endif /* portUSING_MPU_WRAPPERS */
8554 /*-----------------------------------------------------------*/
8556 /* Code below here allows additional code to be inserted into this source file,
8557 * especially where access to file scope functions and data is needed (for example
8558 * when performing module tests). */
8560 #ifdef FREERTOS_MODULE_TEST
8561 #include "tasks_test_access_functions.h"
8565 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
8567 #include "freertos_tasks_c_additions.h"
8569 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8570 static void freertos_tasks_c_additions_init( void )
8572 FREERTOS_TASKS_C_ADDITIONS_INIT();
8576 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8577 /*-----------------------------------------------------------*/
8579 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8582 * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8583 * to provide the memory that is used by the Idle task. It is used when
8584 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8585 * it's own implementation of vApplicationGetIdleTaskMemory by setting
8586 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8588 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8589 StackType_t ** ppxIdleTaskStackBuffer,
8590 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize )
8592 static StaticTask_t xIdleTaskTCB;
8593 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8595 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8596 *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8597 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8600 #if ( configNUMBER_OF_CORES > 1 )
8602 void vApplicationGetPassiveIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8603 StackType_t ** ppxIdleTaskStackBuffer,
8604 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize,
8605 BaseType_t xPassiveIdleTaskIndex )
8607 static StaticTask_t xIdleTaskTCBs[ configNUMBER_OF_CORES - 1 ];
8608 static StackType_t uxIdleTaskStacks[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
8610 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCBs[ xPassiveIdleTaskIndex ] );
8611 *ppxIdleTaskStackBuffer = &( uxIdleTaskStacks[ xPassiveIdleTaskIndex ][ 0 ] );
8612 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8615 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
8617 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8618 /*-----------------------------------------------------------*/
8620 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8623 * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8624 * to provide the memory that is used by the Timer service task. It is used when
8625 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8626 * it's own implementation of vApplicationGetTimerTaskMemory by setting
8627 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8629 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8630 StackType_t ** ppxTimerTaskStackBuffer,
8631 configSTACK_DEPTH_TYPE * puxTimerTaskStackSize )
8633 static StaticTask_t xTimerTaskTCB;
8634 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8636 *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8637 *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8638 *puxTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8641 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8642 /*-----------------------------------------------------------*/
8645 * Reset the state in this file. This state is normally initialized at start up.
8646 * This function must be called by the application before restarting the
8649 void vTaskResetState( void )
8653 /* Task control block. */
8654 #if ( configNUMBER_OF_CORES == 1 )
8656 pxCurrentTCB = NULL;
8658 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8660 #if ( INCLUDE_vTaskDelete == 1 )
8662 uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
8664 #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
8666 #if ( configUSE_POSIX_ERRNO == 1 )
8670 #endif /* #if ( configUSE_POSIX_ERRNO == 1 ) */
8672 /* Other file private variables. */
8673 uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
8674 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
8675 uxTopReadyPriority = tskIDLE_PRIORITY;
8676 xSchedulerRunning = pdFALSE;
8677 xPendedTicks = ( TickType_t ) 0U;
8679 for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
8681 xYieldPendings[ xCoreID ] = pdFALSE;
8684 xNumOfOverflows = ( BaseType_t ) 0;
8685 uxTaskNumber = ( UBaseType_t ) 0U;
8686 xNextTaskUnblockTime = ( TickType_t ) 0U;
8688 uxSchedulerSuspended = ( UBaseType_t ) 0U;
8690 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8692 for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
8694 ulTaskSwitchedInTime[ xCoreID ] = 0U;
8695 ulTotalRunTime[ xCoreID ] = 0U;
8698 #endif /* #if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8700 /*-----------------------------------------------------------*/