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 ) 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 ) 0x80000000UL )
297 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
298 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint64_t ) 0x8000000000000000ULL )
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 ) ( 1UL << 0UL )
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.
667 * This conditional compilation should use inequality to 0, not equality to 1.
668 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
669 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
670 * set to a value other than 1.
672 #if ( configUSE_TICKLESS_IDLE != 0 )
674 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
679 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
680 * will exit the Blocked state.
682 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
684 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
687 * Helper function used to pad task names with spaces when printing out
688 * human readable tables of task information.
690 static char * prvWriteNameToBuffer( char * pcBuffer,
691 const char * pcTaskName ) PRIVILEGED_FUNCTION;
696 * Called after a Task_t structure has been allocated either statically or
697 * dynamically to fill in the structure's members.
699 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
700 const char * const pcName,
701 const configSTACK_DEPTH_TYPE uxStackDepth,
702 void * const pvParameters,
703 UBaseType_t uxPriority,
704 TaskHandle_t * const pxCreatedTask,
706 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
709 * Called after a new task has been created and initialised to place the task
710 * under the control of the scheduler.
712 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
715 * Create a task with static buffer for both TCB and stack. Returns a handle to
716 * the task if it is created successfully. Otherwise, returns NULL.
718 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
719 static TCB_t * prvCreateStaticTask( TaskFunction_t pxTaskCode,
720 const char * const pcName,
721 const configSTACK_DEPTH_TYPE uxStackDepth,
722 void * const pvParameters,
723 UBaseType_t uxPriority,
724 StackType_t * const puxStackBuffer,
725 StaticTask_t * const pxTaskBuffer,
726 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
727 #endif /* #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
730 * Create a restricted task with static buffer for both TCB and stack. Returns
731 * a handle to the task if it is created successfully. Otherwise, returns NULL.
733 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
734 static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
735 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
736 #endif /* #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
739 * Create a restricted task with static buffer for task stack and allocated buffer
740 * for TCB. Returns a handle to the task if it is created successfully. Otherwise,
743 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
744 static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
745 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
746 #endif /* #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
749 * Create a task with allocated buffer for both TCB and stack. Returns a handle to
750 * the task if it is created successfully. Otherwise, returns NULL.
752 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
753 static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
754 const char * const pcName,
755 const configSTACK_DEPTH_TYPE uxStackDepth,
756 void * const pvParameters,
757 UBaseType_t uxPriority,
758 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
759 #endif /* #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) */
762 * freertos_tasks_c_additions_init() should only be called if the user definable
763 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
764 * called by the function.
766 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
768 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
772 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
773 extern void vApplicationPassiveIdleHook( void );
774 #endif /* #if ( configUSE_PASSIVE_IDLE_HOOK == 1 ) */
776 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
779 * Convert the snprintf return value to the number of characters
780 * written. The following are the possible cases:
782 * 1. The buffer supplied to snprintf is large enough to hold the
783 * generated string. The return value in this case is the number
784 * of characters actually written, not counting the terminating
786 * 2. The buffer supplied to snprintf is NOT large enough to hold
787 * the generated string. The return value in this case is the
788 * number of characters that would have been written if the
789 * buffer had been sufficiently large, not counting the
790 * terminating null character.
791 * 3. Encoding error. The return value in this case is a negative
794 * From 1 and 2 above ==> Only when the return value is non-negative
795 * and less than the supplied buffer length, the string has been
796 * completely written.
798 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
801 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
802 /*-----------------------------------------------------------*/
804 #if ( configNUMBER_OF_CORES > 1 )
805 static void prvCheckForRunStateChange( void )
807 UBaseType_t uxPrevCriticalNesting;
808 const TCB_t * pxThisTCB;
810 /* This must only be called from within a task. */
811 portASSERT_IF_IN_ISR();
813 /* This function is always called with interrupts disabled
814 * so this is safe. */
815 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
817 while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD )
819 /* We are only here if we just entered a critical section
820 * or if we just suspended the scheduler, and another task
821 * has requested that we yield.
823 * This is slightly complicated since we need to save and restore
824 * the suspension and critical nesting counts, as well as release
825 * and reacquire the correct locks. And then, do it all over again
826 * if our state changed again during the reacquisition. */
827 uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
829 if( uxPrevCriticalNesting > 0U )
831 portSET_CRITICAL_NESTING_COUNT( 0U );
832 portRELEASE_ISR_LOCK();
836 /* The scheduler is suspended. uxSchedulerSuspended is updated
837 * only when the task is not requested to yield. */
838 mtCOVERAGE_TEST_MARKER();
841 portRELEASE_TASK_LOCK();
842 portMEMORY_BARRIER();
843 configASSERT( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD );
845 portENABLE_INTERRUPTS();
847 /* Enabling interrupts should cause this core to immediately
848 * service the pending interrupt and yield. If the run state is still
849 * yielding here then that is a problem. */
850 configASSERT( pxThisTCB->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD );
852 portDISABLE_INTERRUPTS();
856 portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
858 if( uxPrevCriticalNesting == 0U )
860 portRELEASE_ISR_LOCK();
864 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
866 /*-----------------------------------------------------------*/
868 #if ( configNUMBER_OF_CORES > 1 )
869 static void prvYieldForTask( const TCB_t * pxTCB )
871 BaseType_t xLowestPriorityToPreempt;
872 BaseType_t xCurrentCoreTaskPriority;
873 BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
876 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
877 BaseType_t xYieldCount = 0;
878 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
880 /* This must be called from a critical section. */
881 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
883 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
885 /* No task should yield for this one if it is a lower priority
886 * than priority level of currently ready tasks. */
887 if( pxTCB->uxPriority >= uxTopReadyPriority )
889 /* Yield is not required for a task which is already running. */
890 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
893 xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
895 /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
896 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
897 --xLowestPriorityToPreempt;
899 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
901 xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
903 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
904 if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
906 xCurrentCoreTaskPriority = xCurrentCoreTaskPriority - 1;
909 if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
911 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
912 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
915 if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
917 #if ( configUSE_CORE_AFFINITY == 1 )
918 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
921 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
922 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
925 xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
926 xLowestPriorityCore = xCoreID;
932 mtCOVERAGE_TEST_MARKER();
936 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
938 /* Yield all currently running non-idle tasks with a priority lower than
939 * the task that needs to run. */
940 if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
941 ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
943 prvYieldCore( xCoreID );
948 mtCOVERAGE_TEST_MARKER();
951 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
955 mtCOVERAGE_TEST_MARKER();
959 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
960 if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
961 #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
962 if( xLowestPriorityCore >= 0 )
963 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
965 prvYieldCore( xLowestPriorityCore );
968 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
969 /* Verify that the calling core always yields to higher priority tasks. */
970 if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) &&
971 ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
973 configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) ||
974 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) );
979 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
980 /*-----------------------------------------------------------*/
982 #if ( configNUMBER_OF_CORES > 1 )
983 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
985 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
986 BaseType_t xTaskScheduled = pdFALSE;
987 BaseType_t xDecrementTopPriority = pdTRUE;
988 TCB_t * pxTCB = NULL;
990 #if ( configUSE_CORE_AFFINITY == 1 )
991 const TCB_t * pxPreviousTCB = NULL;
993 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
994 BaseType_t xPriorityDropped = pdFALSE;
997 /* This function should be called when scheduler is running. */
998 configASSERT( xSchedulerRunning == pdTRUE );
1000 /* A new task is created and a running task with the same priority yields
1001 * itself to run the new task. When a running task yields itself, it is still
1002 * in the ready list. This running task will be selected before the new task
1003 * since the new task is always added to the end of the ready list.
1004 * The other problem is that the running task still in the same position of
1005 * the ready list when it yields itself. It is possible that it will be selected
1006 * earlier then other tasks which waits longer than this task.
1008 * To fix these problems, the running task should be put to the end of the
1009 * ready list before searching for the ready task in the ready list. */
1010 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
1011 &pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
1013 ( void ) uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
1014 vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
1015 &pxCurrentTCBs[ xCoreID ]->xStateListItem );
1018 while( xTaskScheduled == pdFALSE )
1020 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1022 if( uxCurrentPriority < uxTopReadyPriority )
1024 /* We can't schedule any tasks, other than idle, that have a
1025 * priority lower than the priority of a task currently running
1026 * on another core. */
1027 uxCurrentPriority = tskIDLE_PRIORITY;
1032 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
1034 const List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
1035 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxReadyList );
1036 ListItem_t * pxIterator;
1038 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
1039 * must not be decremented any further. */
1040 xDecrementTopPriority = pdFALSE;
1042 for( pxIterator = listGET_HEAD_ENTRY( pxReadyList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
1044 /* MISRA Ref 11.5.3 [Void pointer assignment] */
1045 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1046 /* coverity[misra_c_2012_rule_11_5_violation] */
1047 pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
1049 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1051 /* When falling back to the idle priority because only one priority
1052 * level is allowed to run at a time, we should ONLY schedule the true
1053 * idle tasks, not user tasks at the idle priority. */
1054 if( uxCurrentPriority < uxTopReadyPriority )
1056 if( ( pxTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U )
1062 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1064 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
1066 #if ( configUSE_CORE_AFFINITY == 1 )
1067 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1070 /* If the task is not being executed by any core swap it in. */
1071 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
1072 #if ( configUSE_CORE_AFFINITY == 1 )
1073 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
1075 pxTCB->xTaskRunState = xCoreID;
1076 pxCurrentTCBs[ xCoreID ] = pxTCB;
1077 xTaskScheduled = pdTRUE;
1080 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
1082 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) );
1084 #if ( configUSE_CORE_AFFINITY == 1 )
1085 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1088 /* The task is already running on this core, mark it as scheduled. */
1089 pxTCB->xTaskRunState = xCoreID;
1090 xTaskScheduled = pdTRUE;
1095 /* This task is running on the core other than xCoreID. */
1096 mtCOVERAGE_TEST_MARKER();
1099 if( xTaskScheduled != pdFALSE )
1101 /* A task has been selected to run on this core. */
1108 if( xDecrementTopPriority != pdFALSE )
1110 uxTopReadyPriority--;
1111 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1113 xPriorityDropped = pdTRUE;
1119 /* There are configNUMBER_OF_CORES Idle tasks created when scheduler started.
1120 * The scheduler should be able to select a task to run when uxCurrentPriority
1121 * is tskIDLE_PRIORITY. uxCurrentPriority is never decreased to value blow
1122 * tskIDLE_PRIORITY. */
1123 if( uxCurrentPriority > tskIDLE_PRIORITY )
1125 uxCurrentPriority--;
1129 /* This function is called when idle task is not created. Break the
1130 * loop to prevent uxCurrentPriority overrun. */
1135 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1137 if( xTaskScheduled == pdTRUE )
1139 if( xPriorityDropped != pdFALSE )
1141 /* There may be several ready tasks that were being prevented from running because there was
1142 * a higher priority task running. Now that the last of the higher priority tasks is no longer
1143 * running, make sure all the other idle tasks yield. */
1146 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUMBER_OF_CORES; x++ )
1148 if( ( pxCurrentTCBs[ x ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1156 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1158 #if ( configUSE_CORE_AFFINITY == 1 )
1160 if( xTaskScheduled == pdTRUE )
1162 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
1164 /* A ready task was just evicted from this core. See if it can be
1165 * scheduled on any other core. */
1166 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
1167 BaseType_t xLowestPriority = ( BaseType_t ) pxPreviousTCB->uxPriority;
1168 BaseType_t xLowestPriorityCore = -1;
1171 if( ( pxPreviousTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1173 xLowestPriority = xLowestPriority - 1;
1176 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1178 /* pxPreviousTCB was removed from this core and this core is not excluded
1179 * from it's core affinity mask.
1181 * pxPreviousTCB is preempted by the new higher priority task
1182 * pxCurrentTCBs[ xCoreID ]. When searching a new core for pxPreviousTCB,
1183 * we do not need to look at the cores on which pxCurrentTCBs[ xCoreID ]
1184 * is allowed to run. The reason is - when more than one cores are
1185 * eligible for an incoming task, we preempt the core with the minimum
1186 * priority task. Because this core (i.e. xCoreID) was preempted for
1187 * pxCurrentTCBs[ xCoreID ], this means that all the others cores
1188 * where pxCurrentTCBs[ xCoreID ] can run, are running tasks with priority
1189 * no lower than pxPreviousTCB's priority. Therefore, the only cores where
1190 * which can be preempted for pxPreviousTCB are the ones where
1191 * pxCurrentTCBs[ xCoreID ] is not allowed to run (and obviously,
1192 * pxPreviousTCB is allowed to run).
1194 * This is an optimization which reduces the number of cores needed to be
1195 * searched for pxPreviousTCB to run. */
1196 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1200 /* pxPreviousTCB's core affinity mask is changed and it is no longer
1201 * allowed to run on this core. Searching all the cores in pxPreviousTCB's
1202 * new core affinity mask to find a core on which it can run. */
1205 uxCoreMap &= ( ( 1U << configNUMBER_OF_CORES ) - 1U );
1207 for( x = ( ( BaseType_t ) configNUMBER_OF_CORES - 1 ); x >= ( BaseType_t ) 0; x-- )
1209 UBaseType_t uxCore = ( UBaseType_t ) x;
1210 BaseType_t xTaskPriority;
1212 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << uxCore ) ) != 0U )
1214 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority;
1216 if( ( pxCurrentTCBs[ uxCore ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1218 xTaskPriority = xTaskPriority - ( BaseType_t ) 1;
1221 uxCoreMap &= ~( ( UBaseType_t ) 1U << uxCore );
1223 if( ( xTaskPriority < xLowestPriority ) &&
1224 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ] ) != pdFALSE ) &&
1225 ( xYieldPendings[ uxCore ] == pdFALSE ) )
1227 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1228 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1231 xLowestPriority = xTaskPriority;
1232 xLowestPriorityCore = ( BaseType_t ) uxCore;
1238 if( xLowestPriorityCore >= 0 )
1240 prvYieldCore( xLowestPriorityCore );
1245 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) */
1248 #endif /* ( configNUMBER_OF_CORES > 1 ) */
1250 /*-----------------------------------------------------------*/
1252 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1254 static TCB_t * prvCreateStaticTask( TaskFunction_t pxTaskCode,
1255 const char * const pcName,
1256 const configSTACK_DEPTH_TYPE uxStackDepth,
1257 void * const pvParameters,
1258 UBaseType_t uxPriority,
1259 StackType_t * const puxStackBuffer,
1260 StaticTask_t * const pxTaskBuffer,
1261 TaskHandle_t * const pxCreatedTask )
1265 configASSERT( puxStackBuffer != NULL );
1266 configASSERT( pxTaskBuffer != NULL );
1268 #if ( configASSERT_DEFINED == 1 )
1270 /* Sanity check that the size of the structure used to declare a
1271 * variable of type StaticTask_t equals the size of the real task
1273 volatile size_t xSize = sizeof( StaticTask_t );
1274 configASSERT( xSize == sizeof( TCB_t ) );
1275 ( void ) xSize; /* Prevent unused variable warning when configASSERT() is not used. */
1277 #endif /* configASSERT_DEFINED */
1279 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1281 /* The memory used for the task's TCB and stack are passed into this
1282 * function - use them. */
1283 /* MISRA Ref 11.3.1 [Misaligned access] */
1284 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
1285 /* coverity[misra_c_2012_rule_11_3_violation] */
1286 pxNewTCB = ( TCB_t * ) pxTaskBuffer;
1287 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1288 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1290 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1292 /* Tasks can be created statically or dynamically, so note this
1293 * task was created statically in case the task is later deleted. */
1294 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1296 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1298 prvInitialiseNewTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1307 /*-----------------------------------------------------------*/
1309 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1310 const char * const pcName,
1311 const configSTACK_DEPTH_TYPE uxStackDepth,
1312 void * const pvParameters,
1313 UBaseType_t uxPriority,
1314 StackType_t * const puxStackBuffer,
1315 StaticTask_t * const pxTaskBuffer )
1317 TaskHandle_t xReturn = NULL;
1320 traceENTER_xTaskCreateStatic( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer );
1322 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1324 if( pxNewTCB != NULL )
1326 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1328 /* Set the task's affinity before scheduling it. */
1329 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1333 prvAddNewTaskToReadyList( pxNewTCB );
1337 mtCOVERAGE_TEST_MARKER();
1340 traceRETURN_xTaskCreateStatic( xReturn );
1344 /*-----------------------------------------------------------*/
1346 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1347 TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1348 const char * const pcName,
1349 const configSTACK_DEPTH_TYPE uxStackDepth,
1350 void * const pvParameters,
1351 UBaseType_t uxPriority,
1352 StackType_t * const puxStackBuffer,
1353 StaticTask_t * const pxTaskBuffer,
1354 UBaseType_t uxCoreAffinityMask )
1356 TaskHandle_t xReturn = NULL;
1359 traceENTER_xTaskCreateStaticAffinitySet( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, uxCoreAffinityMask );
1361 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1363 if( pxNewTCB != NULL )
1365 /* Set the task's affinity before scheduling it. */
1366 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1368 prvAddNewTaskToReadyList( pxNewTCB );
1372 mtCOVERAGE_TEST_MARKER();
1375 traceRETURN_xTaskCreateStaticAffinitySet( xReturn );
1379 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1381 #endif /* SUPPORT_STATIC_ALLOCATION */
1382 /*-----------------------------------------------------------*/
1384 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1385 static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
1386 TaskHandle_t * const pxCreatedTask )
1390 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1391 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1393 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1395 /* Allocate space for the TCB. Where the memory comes from depends
1396 * on the implementation of the port malloc function and whether or
1397 * not static allocation is being used. */
1398 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1399 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1401 /* Store the stack location in the TCB. */
1402 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1404 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1406 /* Tasks can be created statically or dynamically, so note this
1407 * task was created statically in case the task is later deleted. */
1408 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1410 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1412 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1413 pxTaskDefinition->pcName,
1414 pxTaskDefinition->usStackDepth,
1415 pxTaskDefinition->pvParameters,
1416 pxTaskDefinition->uxPriority,
1417 pxCreatedTask, pxNewTCB,
1418 pxTaskDefinition->xRegions );
1427 /*-----------------------------------------------------------*/
1429 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1430 TaskHandle_t * pxCreatedTask )
1435 traceENTER_xTaskCreateRestrictedStatic( pxTaskDefinition, pxCreatedTask );
1437 configASSERT( pxTaskDefinition != NULL );
1439 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1441 if( pxNewTCB != NULL )
1443 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1445 /* Set the task's affinity before scheduling it. */
1446 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1450 prvAddNewTaskToReadyList( pxNewTCB );
1455 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1458 traceRETURN_xTaskCreateRestrictedStatic( xReturn );
1462 /*-----------------------------------------------------------*/
1464 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1465 BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1466 UBaseType_t uxCoreAffinityMask,
1467 TaskHandle_t * pxCreatedTask )
1472 traceENTER_xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1474 configASSERT( pxTaskDefinition != NULL );
1476 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1478 if( pxNewTCB != NULL )
1480 /* Set the task's affinity before scheduling it. */
1481 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1483 prvAddNewTaskToReadyList( pxNewTCB );
1488 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1491 traceRETURN_xTaskCreateRestrictedStaticAffinitySet( xReturn );
1495 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1497 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1498 /*-----------------------------------------------------------*/
1500 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1501 static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
1502 TaskHandle_t * const pxCreatedTask )
1506 configASSERT( pxTaskDefinition->puxStackBuffer );
1508 if( pxTaskDefinition->puxStackBuffer != NULL )
1510 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1511 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1512 /* coverity[misra_c_2012_rule_11_5_violation] */
1513 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1515 if( pxNewTCB != NULL )
1517 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1519 /* Store the stack location in the TCB. */
1520 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1522 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1524 /* Tasks can be created statically or dynamically, so note
1525 * this task had a statically allocated stack in case it is
1526 * later deleted. The TCB was allocated dynamically. */
1527 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1529 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1531 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1532 pxTaskDefinition->pcName,
1533 pxTaskDefinition->usStackDepth,
1534 pxTaskDefinition->pvParameters,
1535 pxTaskDefinition->uxPriority,
1536 pxCreatedTask, pxNewTCB,
1537 pxTaskDefinition->xRegions );
1547 /*-----------------------------------------------------------*/
1549 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1550 TaskHandle_t * pxCreatedTask )
1555 traceENTER_xTaskCreateRestricted( pxTaskDefinition, pxCreatedTask );
1557 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1559 if( pxNewTCB != NULL )
1561 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1563 /* Set the task's affinity before scheduling it. */
1564 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1566 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1568 prvAddNewTaskToReadyList( pxNewTCB );
1574 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1577 traceRETURN_xTaskCreateRestricted( xReturn );
1581 /*-----------------------------------------------------------*/
1583 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1584 BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1585 UBaseType_t uxCoreAffinityMask,
1586 TaskHandle_t * pxCreatedTask )
1591 traceENTER_xTaskCreateRestrictedAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1593 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1595 if( pxNewTCB != NULL )
1597 /* Set the task's affinity before scheduling it. */
1598 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1600 prvAddNewTaskToReadyList( pxNewTCB );
1606 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1609 traceRETURN_xTaskCreateRestrictedAffinitySet( xReturn );
1613 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1616 #endif /* portUSING_MPU_WRAPPERS */
1617 /*-----------------------------------------------------------*/
1619 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1620 static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
1621 const char * const pcName,
1622 const configSTACK_DEPTH_TYPE uxStackDepth,
1623 void * const pvParameters,
1624 UBaseType_t uxPriority,
1625 TaskHandle_t * const pxCreatedTask )
1629 /* If the stack grows down then allocate the stack then the TCB so the stack
1630 * does not grow into the TCB. Likewise if the stack grows up then allocate
1631 * the TCB then the stack. */
1632 #if ( portSTACK_GROWTH > 0 )
1634 /* Allocate space for the TCB. Where the memory comes from depends on
1635 * the implementation of the port malloc function and whether or not static
1636 * allocation is being used. */
1637 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1638 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1639 /* coverity[misra_c_2012_rule_11_5_violation] */
1640 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1642 if( pxNewTCB != NULL )
1644 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1646 /* Allocate space for the stack used by the task being created.
1647 * The base of the stack memory stored in the TCB so the task can
1648 * be deleted later if required. */
1649 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1650 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1651 /* coverity[misra_c_2012_rule_11_5_violation] */
1652 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) uxStackDepth ) * sizeof( StackType_t ) ) );
1654 if( pxNewTCB->pxStack == NULL )
1656 /* Could not allocate the stack. Delete the allocated TCB. */
1657 vPortFree( pxNewTCB );
1662 #else /* portSTACK_GROWTH */
1664 StackType_t * pxStack;
1666 /* Allocate space for the stack used by the task being created. */
1667 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1668 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1669 /* coverity[misra_c_2012_rule_11_5_violation] */
1670 pxStack = pvPortMallocStack( ( ( ( size_t ) uxStackDepth ) * sizeof( StackType_t ) ) );
1672 if( pxStack != NULL )
1674 /* Allocate space for the TCB. */
1675 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1676 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1677 /* coverity[misra_c_2012_rule_11_5_violation] */
1678 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1680 if( pxNewTCB != NULL )
1682 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1684 /* Store the stack location in the TCB. */
1685 pxNewTCB->pxStack = pxStack;
1689 /* The stack cannot be used as the TCB was not created. Free
1691 vPortFreeStack( pxStack );
1699 #endif /* portSTACK_GROWTH */
1701 if( pxNewTCB != NULL )
1703 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1705 /* Tasks can be created statically or dynamically, so note this
1706 * task was created dynamically in case it is later deleted. */
1707 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1709 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1711 prvInitialiseNewTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1716 /*-----------------------------------------------------------*/
1718 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1719 const char * const pcName,
1720 const configSTACK_DEPTH_TYPE uxStackDepth,
1721 void * const pvParameters,
1722 UBaseType_t uxPriority,
1723 TaskHandle_t * const pxCreatedTask )
1728 traceENTER_xTaskCreate( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
1730 pxNewTCB = prvCreateTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
1732 if( pxNewTCB != NULL )
1734 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1736 /* Set the task's affinity before scheduling it. */
1737 pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
1741 prvAddNewTaskToReadyList( pxNewTCB );
1746 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1749 traceRETURN_xTaskCreate( xReturn );
1753 /*-----------------------------------------------------------*/
1755 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1756 BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1757 const char * const pcName,
1758 const configSTACK_DEPTH_TYPE uxStackDepth,
1759 void * const pvParameters,
1760 UBaseType_t uxPriority,
1761 UBaseType_t uxCoreAffinityMask,
1762 TaskHandle_t * const pxCreatedTask )
1767 traceENTER_xTaskCreateAffinitySet( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, uxCoreAffinityMask, pxCreatedTask );
1769 pxNewTCB = prvCreateTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
1771 if( pxNewTCB != NULL )
1773 /* Set the task's affinity before scheduling it. */
1774 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1776 prvAddNewTaskToReadyList( pxNewTCB );
1781 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1784 traceRETURN_xTaskCreateAffinitySet( xReturn );
1788 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1790 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1791 /*-----------------------------------------------------------*/
1793 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1794 const char * const pcName,
1795 const configSTACK_DEPTH_TYPE uxStackDepth,
1796 void * const pvParameters,
1797 UBaseType_t uxPriority,
1798 TaskHandle_t * const pxCreatedTask,
1800 const MemoryRegion_t * const xRegions )
1802 StackType_t * pxTopOfStack;
1805 #if ( portUSING_MPU_WRAPPERS == 1 )
1806 /* Should the task be created in privileged mode? */
1807 BaseType_t xRunPrivileged;
1809 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1811 xRunPrivileged = pdTRUE;
1815 xRunPrivileged = pdFALSE;
1817 uxPriority &= ~portPRIVILEGE_BIT;
1818 #endif /* portUSING_MPU_WRAPPERS == 1 */
1820 /* Avoid dependency on memset() if it is not required. */
1821 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1823 /* Fill the stack with a known value to assist debugging. */
1824 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) uxStackDepth * sizeof( StackType_t ) );
1826 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1828 /* Calculate the top of stack address. This depends on whether the stack
1829 * grows from high memory to low (as per the 80x86) or vice versa.
1830 * portSTACK_GROWTH is used to make the result positive or negative as required
1832 #if ( portSTACK_GROWTH < 0 )
1834 pxTopOfStack = &( pxNewTCB->pxStack[ uxStackDepth - ( configSTACK_DEPTH_TYPE ) 1 ] );
1835 pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
1837 /* Check the alignment of the calculated top of stack is correct. */
1838 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1840 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1842 /* Also record the stack's high address, which may assist
1844 pxNewTCB->pxEndOfStack = pxTopOfStack;
1846 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1848 #else /* portSTACK_GROWTH */
1850 pxTopOfStack = pxNewTCB->pxStack;
1851 pxTopOfStack = ( StackType_t * ) ( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) + portBYTE_ALIGNMENT_MASK ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
1853 /* Check the alignment of the calculated top of stack is correct. */
1854 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1856 /* The other extreme of the stack space is required if stack checking is
1858 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( uxStackDepth - ( configSTACK_DEPTH_TYPE ) 1 );
1860 #endif /* portSTACK_GROWTH */
1862 /* Store the task name in the TCB. */
1863 if( pcName != NULL )
1865 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1867 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1869 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1870 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1871 * string is not accessible (extremely unlikely). */
1872 if( pcName[ x ] == ( char ) 0x00 )
1878 mtCOVERAGE_TEST_MARKER();
1882 /* Ensure the name string is terminated in the case that the string length
1883 * was greater or equal to configMAX_TASK_NAME_LEN. */
1884 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1U ] = '\0';
1888 mtCOVERAGE_TEST_MARKER();
1891 /* This is used as an array index so must ensure it's not too large. */
1892 configASSERT( uxPriority < configMAX_PRIORITIES );
1894 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1896 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1900 mtCOVERAGE_TEST_MARKER();
1903 pxNewTCB->uxPriority = uxPriority;
1904 #if ( configUSE_MUTEXES == 1 )
1906 pxNewTCB->uxBasePriority = uxPriority;
1908 #endif /* configUSE_MUTEXES */
1910 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1911 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1913 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1914 * back to the containing TCB from a generic item in a list. */
1915 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1917 /* Event lists are always in priority order. */
1918 listSET_LIST_ITEM_VALUE( &( pxNewTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority );
1919 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1921 #if ( portUSING_MPU_WRAPPERS == 1 )
1923 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, uxStackDepth );
1927 /* Avoid compiler warning about unreferenced parameter. */
1932 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
1934 /* Allocate and initialize memory for the task's TLS Block. */
1935 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
1939 /* Initialize the TCB stack to look as if the task was already running,
1940 * but had been interrupted by the scheduler. The return address is set
1941 * to the start of the task function. Once the stack has been initialised
1942 * the top of stack variable is updated. */
1943 #if ( portUSING_MPU_WRAPPERS == 1 )
1945 /* If the port has capability to detect stack overflow,
1946 * pass the stack end address to the stack initialization
1947 * function as well. */
1948 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1950 #if ( portSTACK_GROWTH < 0 )
1952 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1954 #else /* portSTACK_GROWTH */
1956 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1958 #endif /* portSTACK_GROWTH */
1960 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1962 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1964 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1966 #else /* portUSING_MPU_WRAPPERS */
1968 /* If the port has capability to detect stack overflow,
1969 * pass the stack end address to the stack initialization
1970 * function as well. */
1971 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1973 #if ( portSTACK_GROWTH < 0 )
1975 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1977 #else /* portSTACK_GROWTH */
1979 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1981 #endif /* portSTACK_GROWTH */
1983 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1985 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1987 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1989 #endif /* portUSING_MPU_WRAPPERS */
1991 /* Initialize task state and task attributes. */
1992 #if ( configNUMBER_OF_CORES > 1 )
1994 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1996 /* Is this an idle task? */
1997 if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvPassiveIdleTask ) )
1999 pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
2002 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
2004 if( pxCreatedTask != NULL )
2006 /* Pass the handle out in an anonymous way. The handle can be used to
2007 * change the created task's priority, delete the created task, etc.*/
2008 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
2012 mtCOVERAGE_TEST_MARKER();
2015 /*-----------------------------------------------------------*/
2017 #if ( configNUMBER_OF_CORES == 1 )
2019 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
2021 /* Ensure interrupts don't access the task lists while the lists are being
2023 taskENTER_CRITICAL();
2025 uxCurrentNumberOfTasks += ( UBaseType_t ) 1U;
2027 if( pxCurrentTCB == NULL )
2029 /* There are no other tasks, or all the other tasks are in
2030 * the suspended state - make this the current task. */
2031 pxCurrentTCB = pxNewTCB;
2033 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2035 /* This is the first task to be created so do the preliminary
2036 * initialisation required. We will not recover if this call
2037 * fails, but we will report the failure. */
2038 prvInitialiseTaskLists();
2042 mtCOVERAGE_TEST_MARKER();
2047 /* If the scheduler is not already running, make this task the
2048 * current task if it is the highest priority task to be created
2050 if( xSchedulerRunning == pdFALSE )
2052 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
2054 pxCurrentTCB = pxNewTCB;
2058 mtCOVERAGE_TEST_MARKER();
2063 mtCOVERAGE_TEST_MARKER();
2069 #if ( configUSE_TRACE_FACILITY == 1 )
2071 /* Add a counter into the TCB for tracing only. */
2072 pxNewTCB->uxTCBNumber = uxTaskNumber;
2074 #endif /* configUSE_TRACE_FACILITY */
2075 traceTASK_CREATE( pxNewTCB );
2077 prvAddTaskToReadyList( pxNewTCB );
2079 portSETUP_TCB( pxNewTCB );
2081 taskEXIT_CRITICAL();
2083 if( xSchedulerRunning != pdFALSE )
2085 /* If the created task is of a higher priority than the current task
2086 * then it should run now. */
2087 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2091 mtCOVERAGE_TEST_MARKER();
2095 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2097 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
2099 /* Ensure interrupts don't access the task lists while the lists are being
2101 taskENTER_CRITICAL();
2103 uxCurrentNumberOfTasks++;
2105 if( xSchedulerRunning == pdFALSE )
2107 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2109 /* This is the first task to be created so do the preliminary
2110 * initialisation required. We will not recover if this call
2111 * fails, but we will report the failure. */
2112 prvInitialiseTaskLists();
2116 mtCOVERAGE_TEST_MARKER();
2119 /* All the cores start with idle tasks before the SMP scheduler
2120 * is running. Idle tasks are assigned to cores when they are
2121 * created in prvCreateIdleTasks(). */
2126 #if ( configUSE_TRACE_FACILITY == 1 )
2128 /* Add a counter into the TCB for tracing only. */
2129 pxNewTCB->uxTCBNumber = uxTaskNumber;
2131 #endif /* configUSE_TRACE_FACILITY */
2132 traceTASK_CREATE( pxNewTCB );
2134 prvAddTaskToReadyList( pxNewTCB );
2136 portSETUP_TCB( pxNewTCB );
2138 if( xSchedulerRunning != pdFALSE )
2140 /* If the created task is of a higher priority than another
2141 * currently running task and preemption is on then it should
2143 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2147 mtCOVERAGE_TEST_MARKER();
2150 taskEXIT_CRITICAL();
2153 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2154 /*-----------------------------------------------------------*/
2156 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
2158 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
2161 size_t uxCharsWritten;
2163 if( iSnprintfReturnValue < 0 )
2165 /* Encoding error - Return 0 to indicate that nothing
2166 * was written to the buffer. */
2169 else if( iSnprintfReturnValue >= ( int ) n )
2171 /* This is the case when the supplied buffer is not
2172 * large to hold the generated string. Return the
2173 * number of characters actually written without
2174 * counting the terminating NULL character. */
2175 uxCharsWritten = n - 1U;
2179 /* Complete string was written to the buffer. */
2180 uxCharsWritten = ( size_t ) iSnprintfReturnValue;
2183 return uxCharsWritten;
2186 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
2187 /*-----------------------------------------------------------*/
2189 #if ( INCLUDE_vTaskDelete == 1 )
2191 void vTaskDelete( TaskHandle_t xTaskToDelete )
2194 BaseType_t xDeleteTCBInIdleTask = pdFALSE;
2195 BaseType_t xTaskIsRunningOrYielding;
2197 traceENTER_vTaskDelete( xTaskToDelete );
2199 taskENTER_CRITICAL();
2201 /* If null is passed in here then it is the calling task that is
2203 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
2205 /* Remove task from the ready/delayed list. */
2206 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2208 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2212 mtCOVERAGE_TEST_MARKER();
2215 /* Is the task waiting on an event also? */
2216 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2218 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2222 mtCOVERAGE_TEST_MARKER();
2225 /* Increment the uxTaskNumber also so kernel aware debuggers can
2226 * detect that the task lists need re-generating. This is done before
2227 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
2231 /* Use temp variable as distinct sequence points for reading volatile
2232 * variables prior to a logical operator to ensure compliance with
2233 * MISRA C 2012 Rule 13.5. */
2234 xTaskIsRunningOrYielding = taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB );
2236 /* If the task is running (or yielding), we must add it to the
2237 * termination list so that an idle task can delete it when it is
2238 * no longer running. */
2239 if( ( xSchedulerRunning != pdFALSE ) && ( xTaskIsRunningOrYielding != pdFALSE ) )
2241 /* A running task or a task which is scheduled to yield is being
2242 * deleted. This cannot complete when the task is still running
2243 * on a core, as a context switch to another task is required.
2244 * Place the task in the termination list. The idle task will check
2245 * the termination list and free up any memory allocated by the
2246 * scheduler for the TCB and stack of the deleted task. */
2247 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
2249 /* Increment the ucTasksDeleted variable so the idle task knows
2250 * there is a task that has been deleted and that it should therefore
2251 * check the xTasksWaitingTermination list. */
2252 ++uxDeletedTasksWaitingCleanUp;
2254 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
2255 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
2256 traceTASK_DELETE( pxTCB );
2258 /* Delete the task TCB in idle task. */
2259 xDeleteTCBInIdleTask = pdTRUE;
2261 /* The pre-delete hook is primarily for the Windows simulator,
2262 * in which Windows specific clean up operations are performed,
2263 * after which it is not possible to yield away from this task -
2264 * hence xYieldPending is used to latch that a context switch is
2266 #if ( configNUMBER_OF_CORES == 1 )
2267 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
2269 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
2272 /* In the case of SMP, it is possible that the task being deleted
2273 * is running on another core. We must evict the task before
2274 * exiting the critical section to ensure that the task cannot
2275 * take an action which puts it back on ready/state/event list,
2276 * thereby nullifying the delete operation. Once evicted, the
2277 * task won't be scheduled ever as it will no longer be on the
2279 #if ( configNUMBER_OF_CORES > 1 )
2281 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2283 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2285 configASSERT( uxSchedulerSuspended == 0 );
2286 taskYIELD_WITHIN_API();
2290 prvYieldCore( pxTCB->xTaskRunState );
2294 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
2298 --uxCurrentNumberOfTasks;
2299 traceTASK_DELETE( pxTCB );
2301 /* Reset the next expected unblock time in case it referred to
2302 * the task that has just been deleted. */
2303 prvResetNextTaskUnblockTime();
2306 taskEXIT_CRITICAL();
2308 /* If the task is not deleting itself, call prvDeleteTCB from outside of
2309 * critical section. If a task deletes itself, prvDeleteTCB is called
2310 * from prvCheckTasksWaitingTermination which is called from Idle task. */
2311 if( xDeleteTCBInIdleTask != pdTRUE )
2313 prvDeleteTCB( pxTCB );
2316 /* Force a reschedule if it is the currently running task that has just
2318 #if ( configNUMBER_OF_CORES == 1 )
2320 if( xSchedulerRunning != pdFALSE )
2322 if( pxTCB == pxCurrentTCB )
2324 configASSERT( uxSchedulerSuspended == 0 );
2325 taskYIELD_WITHIN_API();
2329 mtCOVERAGE_TEST_MARKER();
2333 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2335 traceRETURN_vTaskDelete();
2338 #endif /* INCLUDE_vTaskDelete */
2339 /*-----------------------------------------------------------*/
2341 #if ( INCLUDE_xTaskDelayUntil == 1 )
2343 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2344 const TickType_t xTimeIncrement )
2346 TickType_t xTimeToWake;
2347 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2349 traceENTER_xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
2351 configASSERT( pxPreviousWakeTime );
2352 configASSERT( ( xTimeIncrement > 0U ) );
2356 /* Minor optimisation. The tick count cannot change in this
2358 const TickType_t xConstTickCount = xTickCount;
2360 configASSERT( uxSchedulerSuspended == 1U );
2362 /* Generate the tick time at which the task wants to wake. */
2363 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2365 if( xConstTickCount < *pxPreviousWakeTime )
2367 /* The tick count has overflowed since this function was
2368 * lasted called. In this case the only time we should ever
2369 * actually delay is if the wake time has also overflowed,
2370 * and the wake time is greater than the tick time. When this
2371 * is the case it is as if neither time had overflowed. */
2372 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2374 xShouldDelay = pdTRUE;
2378 mtCOVERAGE_TEST_MARKER();
2383 /* The tick time has not overflowed. In this case we will
2384 * delay if either the wake time has overflowed, and/or the
2385 * tick time is less than the wake time. */
2386 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2388 xShouldDelay = pdTRUE;
2392 mtCOVERAGE_TEST_MARKER();
2396 /* Update the wake time ready for the next call. */
2397 *pxPreviousWakeTime = xTimeToWake;
2399 if( xShouldDelay != pdFALSE )
2401 traceTASK_DELAY_UNTIL( xTimeToWake );
2403 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2404 * the time to wake, so subtract the current tick count. */
2405 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2409 mtCOVERAGE_TEST_MARKER();
2412 xAlreadyYielded = xTaskResumeAll();
2414 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2415 * have put ourselves to sleep. */
2416 if( xAlreadyYielded == pdFALSE )
2418 taskYIELD_WITHIN_API();
2422 mtCOVERAGE_TEST_MARKER();
2425 traceRETURN_xTaskDelayUntil( xShouldDelay );
2427 return xShouldDelay;
2430 #endif /* INCLUDE_xTaskDelayUntil */
2431 /*-----------------------------------------------------------*/
2433 #if ( INCLUDE_vTaskDelay == 1 )
2435 void vTaskDelay( const TickType_t xTicksToDelay )
2437 BaseType_t xAlreadyYielded = pdFALSE;
2439 traceENTER_vTaskDelay( xTicksToDelay );
2441 /* A delay time of zero just forces a reschedule. */
2442 if( xTicksToDelay > ( TickType_t ) 0U )
2446 configASSERT( uxSchedulerSuspended == 1U );
2450 /* A task that is removed from the event list while the
2451 * scheduler is suspended will not get placed in the ready
2452 * list or removed from the blocked list until the scheduler
2455 * This task cannot be in an event list as it is the currently
2456 * executing task. */
2457 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2459 xAlreadyYielded = xTaskResumeAll();
2463 mtCOVERAGE_TEST_MARKER();
2466 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2467 * have put ourselves to sleep. */
2468 if( xAlreadyYielded == pdFALSE )
2470 taskYIELD_WITHIN_API();
2474 mtCOVERAGE_TEST_MARKER();
2477 traceRETURN_vTaskDelay();
2480 #endif /* INCLUDE_vTaskDelay */
2481 /*-----------------------------------------------------------*/
2483 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2485 eTaskState eTaskGetState( TaskHandle_t xTask )
2488 List_t const * pxStateList;
2489 List_t const * pxEventList;
2490 List_t const * pxDelayedList;
2491 List_t const * pxOverflowedDelayedList;
2492 const TCB_t * const pxTCB = xTask;
2494 traceENTER_eTaskGetState( xTask );
2496 configASSERT( pxTCB );
2498 #if ( configNUMBER_OF_CORES == 1 )
2499 if( pxTCB == pxCurrentTCB )
2501 /* The task calling this function is querying its own state. */
2507 taskENTER_CRITICAL();
2509 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2510 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2511 pxDelayedList = pxDelayedTaskList;
2512 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2514 taskEXIT_CRITICAL();
2516 if( pxEventList == &xPendingReadyList )
2518 /* The task has been placed on the pending ready list, so its
2519 * state is eReady regardless of what list the task's state list
2520 * item is currently placed on. */
2523 else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2525 /* The task being queried is referenced from one of the Blocked
2530 #if ( INCLUDE_vTaskSuspend == 1 )
2531 else if( pxStateList == &xSuspendedTaskList )
2533 /* The task being queried is referenced from the suspended
2534 * list. Is it genuinely suspended or is it blocked
2536 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2538 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2542 /* The task does not appear on the event list item of
2543 * and of the RTOS objects, but could still be in the
2544 * blocked state if it is waiting on its notification
2545 * rather than waiting on an object. If not, is
2547 eReturn = eSuspended;
2549 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2551 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2558 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2560 eReturn = eSuspended;
2562 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2569 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2571 #if ( INCLUDE_vTaskDelete == 1 )
2572 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2574 /* The task being queried is referenced from the deleted
2575 * tasks list, or it is not referenced from any lists at
2583 #if ( configNUMBER_OF_CORES == 1 )
2585 /* If the task is not in any other state, it must be in the
2586 * Ready (including pending ready) state. */
2589 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2591 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2593 /* Is it actively running on a core? */
2598 /* If the task is not in any other state, it must be in the
2599 * Ready (including pending ready) state. */
2603 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2607 traceRETURN_eTaskGetState( eReturn );
2612 #endif /* INCLUDE_eTaskGetState */
2613 /*-----------------------------------------------------------*/
2615 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2617 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2619 TCB_t const * pxTCB;
2620 UBaseType_t uxReturn;
2622 traceENTER_uxTaskPriorityGet( xTask );
2624 taskENTER_CRITICAL();
2626 /* If null is passed in here then it is the priority of the task
2627 * that called uxTaskPriorityGet() that is being queried. */
2628 pxTCB = prvGetTCBFromHandle( xTask );
2629 uxReturn = pxTCB->uxPriority;
2631 taskEXIT_CRITICAL();
2633 traceRETURN_uxTaskPriorityGet( uxReturn );
2638 #endif /* INCLUDE_uxTaskPriorityGet */
2639 /*-----------------------------------------------------------*/
2641 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2643 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2645 TCB_t const * pxTCB;
2646 UBaseType_t uxReturn;
2647 UBaseType_t uxSavedInterruptStatus;
2649 traceENTER_uxTaskPriorityGetFromISR( xTask );
2651 /* RTOS ports that support interrupt nesting have the concept of a
2652 * maximum system call (or maximum API call) interrupt priority.
2653 * Interrupts that are above the maximum system call priority are keep
2654 * permanently enabled, even when the RTOS kernel is in a critical section,
2655 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2656 * is defined in FreeRTOSConfig.h then
2657 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2658 * failure if a FreeRTOS API function is called from an interrupt that has
2659 * been assigned a priority above the configured maximum system call
2660 * priority. Only FreeRTOS functions that end in FromISR can be called
2661 * from interrupts that have been assigned a priority at or (logically)
2662 * below the maximum system call interrupt priority. FreeRTOS maintains a
2663 * separate interrupt safe API to ensure interrupt entry is as fast and as
2664 * simple as possible. More information (albeit Cortex-M specific) is
2665 * provided on the following link:
2666 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2667 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2669 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
2671 /* If null is passed in here then it is the priority of the calling
2672 * task that is being queried. */
2673 pxTCB = prvGetTCBFromHandle( xTask );
2674 uxReturn = pxTCB->uxPriority;
2676 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2678 traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
2683 #endif /* INCLUDE_uxTaskPriorityGet */
2684 /*-----------------------------------------------------------*/
2686 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2688 UBaseType_t uxTaskBasePriorityGet( const TaskHandle_t xTask )
2690 TCB_t const * pxTCB;
2691 UBaseType_t uxReturn;
2693 traceENTER_uxTaskBasePriorityGet( xTask );
2695 taskENTER_CRITICAL();
2697 /* If null is passed in here then it is the base priority of the task
2698 * that called uxTaskBasePriorityGet() that is being queried. */
2699 pxTCB = prvGetTCBFromHandle( xTask );
2700 uxReturn = pxTCB->uxBasePriority;
2702 taskEXIT_CRITICAL();
2704 traceRETURN_uxTaskBasePriorityGet( uxReturn );
2709 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2710 /*-----------------------------------------------------------*/
2712 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2714 UBaseType_t uxTaskBasePriorityGetFromISR( const TaskHandle_t xTask )
2716 TCB_t const * pxTCB;
2717 UBaseType_t uxReturn;
2718 UBaseType_t uxSavedInterruptStatus;
2720 traceENTER_uxTaskBasePriorityGetFromISR( xTask );
2722 /* RTOS ports that support interrupt nesting have the concept of a
2723 * maximum system call (or maximum API call) interrupt priority.
2724 * Interrupts that are above the maximum system call priority are keep
2725 * permanently enabled, even when the RTOS kernel is in a critical section,
2726 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2727 * is defined in FreeRTOSConfig.h then
2728 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2729 * failure if a FreeRTOS API function is called from an interrupt that has
2730 * been assigned a priority above the configured maximum system call
2731 * priority. Only FreeRTOS functions that end in FromISR can be called
2732 * from interrupts that have been assigned a priority at or (logically)
2733 * below the maximum system call interrupt priority. FreeRTOS maintains a
2734 * separate interrupt safe API to ensure interrupt entry is as fast and as
2735 * simple as possible. More information (albeit Cortex-M specific) is
2736 * provided on the following link:
2737 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2738 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2740 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
2742 /* If null is passed in here then it is the base priority of the calling
2743 * task that is being queried. */
2744 pxTCB = prvGetTCBFromHandle( xTask );
2745 uxReturn = pxTCB->uxBasePriority;
2747 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2749 traceRETURN_uxTaskBasePriorityGetFromISR( uxReturn );
2754 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2755 /*-----------------------------------------------------------*/
2757 #if ( INCLUDE_vTaskPrioritySet == 1 )
2759 void vTaskPrioritySet( TaskHandle_t xTask,
2760 UBaseType_t uxNewPriority )
2763 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2764 BaseType_t xYieldRequired = pdFALSE;
2766 #if ( configNUMBER_OF_CORES > 1 )
2767 BaseType_t xYieldForTask = pdFALSE;
2770 traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
2772 configASSERT( uxNewPriority < configMAX_PRIORITIES );
2774 /* Ensure the new priority is valid. */
2775 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2777 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2781 mtCOVERAGE_TEST_MARKER();
2784 taskENTER_CRITICAL();
2786 /* If null is passed in here then it is the priority of the calling
2787 * task that is being changed. */
2788 pxTCB = prvGetTCBFromHandle( xTask );
2790 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2792 #if ( configUSE_MUTEXES == 1 )
2794 uxCurrentBasePriority = pxTCB->uxBasePriority;
2798 uxCurrentBasePriority = pxTCB->uxPriority;
2802 if( uxCurrentBasePriority != uxNewPriority )
2804 /* The priority change may have readied a task of higher
2805 * priority than a running task. */
2806 if( uxNewPriority > uxCurrentBasePriority )
2808 #if ( configNUMBER_OF_CORES == 1 )
2810 if( pxTCB != pxCurrentTCB )
2812 /* The priority of a task other than the currently
2813 * running task is being raised. Is the priority being
2814 * raised above that of the running task? */
2815 if( uxNewPriority > pxCurrentTCB->uxPriority )
2817 xYieldRequired = pdTRUE;
2821 mtCOVERAGE_TEST_MARKER();
2826 /* The priority of the running task is being raised,
2827 * but the running task must already be the highest
2828 * priority task able to run so no yield is required. */
2831 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2833 /* The priority of a task is being raised so
2834 * perform a yield for this task later. */
2835 xYieldForTask = pdTRUE;
2837 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2839 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2841 /* Setting the priority of a running task down means
2842 * there may now be another task of higher priority that
2843 * is ready to execute. */
2844 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2845 if( pxTCB->xPreemptionDisable == pdFALSE )
2848 xYieldRequired = pdTRUE;
2853 /* Setting the priority of any other task down does not
2854 * require a yield as the running task must be above the
2855 * new priority of the task being modified. */
2858 /* Remember the ready list the task might be referenced from
2859 * before its uxPriority member is changed so the
2860 * taskRESET_READY_PRIORITY() macro can function correctly. */
2861 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2863 #if ( configUSE_MUTEXES == 1 )
2865 /* Only change the priority being used if the task is not
2866 * currently using an inherited priority or the new priority
2867 * is bigger than the inherited priority. */
2868 if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2870 pxTCB->uxPriority = uxNewPriority;
2874 mtCOVERAGE_TEST_MARKER();
2877 /* The base priority gets set whatever. */
2878 pxTCB->uxBasePriority = uxNewPriority;
2880 #else /* if ( configUSE_MUTEXES == 1 ) */
2882 pxTCB->uxPriority = uxNewPriority;
2884 #endif /* if ( configUSE_MUTEXES == 1 ) */
2886 /* Only reset the event list item value if the value is not
2887 * being used for anything else. */
2888 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0UL ) )
2890 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) );
2894 mtCOVERAGE_TEST_MARKER();
2897 /* If the task is in the blocked or suspended list we need do
2898 * nothing more than change its priority variable. However, if
2899 * the task is in a ready list it needs to be removed and placed
2900 * in the list appropriate to its new priority. */
2901 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2903 /* The task is currently in its ready list - remove before
2904 * adding it to its new ready list. As we are in a critical
2905 * section we can do this even if the scheduler is suspended. */
2906 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2908 /* It is known that the task is in its ready list so
2909 * there is no need to check again and the port level
2910 * reset macro can be called directly. */
2911 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2915 mtCOVERAGE_TEST_MARKER();
2918 prvAddTaskToReadyList( pxTCB );
2922 #if ( configNUMBER_OF_CORES == 1 )
2924 mtCOVERAGE_TEST_MARKER();
2928 /* It's possible that xYieldForTask was already set to pdTRUE because
2929 * its priority is being raised. However, since it is not in a ready list
2930 * we don't actually need to yield for it. */
2931 xYieldForTask = pdFALSE;
2936 if( xYieldRequired != pdFALSE )
2938 /* The running task priority is set down. Request the task to yield. */
2939 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2943 #if ( configNUMBER_OF_CORES > 1 )
2944 if( xYieldForTask != pdFALSE )
2946 /* The priority of the task is being raised. If a running
2947 * task has priority lower than this task, it should yield
2949 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2952 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2954 mtCOVERAGE_TEST_MARKER();
2958 /* Remove compiler warning about unused variables when the port
2959 * optimised task selection is not being used. */
2960 ( void ) uxPriorityUsedOnEntry;
2963 taskEXIT_CRITICAL();
2965 traceRETURN_vTaskPrioritySet();
2968 #endif /* INCLUDE_vTaskPrioritySet */
2969 /*-----------------------------------------------------------*/
2971 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2972 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2973 UBaseType_t uxCoreAffinityMask )
2977 UBaseType_t uxPrevCoreAffinityMask;
2979 #if ( configUSE_PREEMPTION == 1 )
2980 UBaseType_t uxPrevNotAllowedCores;
2983 traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
2985 taskENTER_CRITICAL();
2987 pxTCB = prvGetTCBFromHandle( xTask );
2989 uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2990 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2992 if( xSchedulerRunning != pdFALSE )
2994 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2996 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2998 /* If the task can no longer run on the core it was running,
2999 * request the core to yield. */
3000 if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
3002 prvYieldCore( xCoreID );
3007 #if ( configUSE_PREEMPTION == 1 )
3009 /* Calculate the cores on which this task was not allowed to
3010 * run previously. */
3011 uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
3013 /* Does the new core mask enables this task to run on any of the
3014 * previously not allowed cores? If yes, check if this task can be
3015 * scheduled on any of those cores. */
3016 if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
3018 prvYieldForTask( pxTCB );
3021 #else /* #if( configUSE_PREEMPTION == 1 ) */
3023 mtCOVERAGE_TEST_MARKER();
3025 #endif /* #if( configUSE_PREEMPTION == 1 ) */
3029 taskEXIT_CRITICAL();
3031 traceRETURN_vTaskCoreAffinitySet();
3033 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3034 /*-----------------------------------------------------------*/
3036 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
3037 UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
3039 const TCB_t * pxTCB;
3040 UBaseType_t uxCoreAffinityMask;
3042 traceENTER_vTaskCoreAffinityGet( xTask );
3044 taskENTER_CRITICAL();
3046 pxTCB = prvGetTCBFromHandle( xTask );
3047 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
3049 taskEXIT_CRITICAL();
3051 traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
3053 return uxCoreAffinityMask;
3055 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3057 /*-----------------------------------------------------------*/
3059 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3061 void vTaskPreemptionDisable( const TaskHandle_t xTask )
3065 traceENTER_vTaskPreemptionDisable( xTask );
3067 taskENTER_CRITICAL();
3069 pxTCB = prvGetTCBFromHandle( xTask );
3071 pxTCB->xPreemptionDisable = pdTRUE;
3073 taskEXIT_CRITICAL();
3075 traceRETURN_vTaskPreemptionDisable();
3078 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3079 /*-----------------------------------------------------------*/
3081 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3083 void vTaskPreemptionEnable( const TaskHandle_t xTask )
3088 traceENTER_vTaskPreemptionEnable( xTask );
3090 taskENTER_CRITICAL();
3092 pxTCB = prvGetTCBFromHandle( xTask );
3094 pxTCB->xPreemptionDisable = pdFALSE;
3096 if( xSchedulerRunning != pdFALSE )
3098 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3100 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
3101 prvYieldCore( xCoreID );
3105 taskEXIT_CRITICAL();
3107 traceRETURN_vTaskPreemptionEnable();
3110 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3111 /*-----------------------------------------------------------*/
3113 #if ( INCLUDE_vTaskSuspend == 1 )
3115 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
3119 traceENTER_vTaskSuspend( xTaskToSuspend );
3121 taskENTER_CRITICAL();
3123 /* If null is passed in here then it is the running task that is
3124 * being suspended. */
3125 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
3127 traceTASK_SUSPEND( pxTCB );
3129 /* Remove task from the ready/delayed list and place in the
3130 * suspended list. */
3131 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
3133 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
3137 mtCOVERAGE_TEST_MARKER();
3140 /* Is the task waiting on an event also? */
3141 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3143 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3147 mtCOVERAGE_TEST_MARKER();
3150 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
3152 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3156 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3158 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3160 /* The task was blocked to wait for a notification, but is
3161 * now suspended, so no notification was received. */
3162 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
3166 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3168 /* In the case of SMP, it is possible that the task being suspended
3169 * is running on another core. We must evict the task before
3170 * exiting the critical section to ensure that the task cannot
3171 * take an action which puts it back on ready/state/event list,
3172 * thereby nullifying the suspend operation. Once evicted, the
3173 * task won't be scheduled before it is resumed as it will no longer
3174 * be on the ready list. */
3175 #if ( configNUMBER_OF_CORES > 1 )
3177 if( xSchedulerRunning != pdFALSE )
3179 /* Reset the next expected unblock time in case it referred to the
3180 * task that is now in the Suspended state. */
3181 prvResetNextTaskUnblockTime();
3183 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3185 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
3187 /* The current task has just been suspended. */
3188 configASSERT( uxSchedulerSuspended == 0 );
3189 vTaskYieldWithinAPI();
3193 prvYieldCore( pxTCB->xTaskRunState );
3198 mtCOVERAGE_TEST_MARKER();
3203 mtCOVERAGE_TEST_MARKER();
3206 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
3208 taskEXIT_CRITICAL();
3210 #if ( configNUMBER_OF_CORES == 1 )
3212 UBaseType_t uxCurrentListLength;
3214 if( xSchedulerRunning != pdFALSE )
3216 /* Reset the next expected unblock time in case it referred to the
3217 * task that is now in the Suspended state. */
3218 taskENTER_CRITICAL();
3220 prvResetNextTaskUnblockTime();
3222 taskEXIT_CRITICAL();
3226 mtCOVERAGE_TEST_MARKER();
3229 if( pxTCB == pxCurrentTCB )
3231 if( xSchedulerRunning != pdFALSE )
3233 /* The current task has just been suspended. */
3234 configASSERT( uxSchedulerSuspended == 0 );
3235 portYIELD_WITHIN_API();
3239 /* The scheduler is not running, but the task that was pointed
3240 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
3241 * must be adjusted to point to a different task. */
3243 /* Use a temp variable as a distinct sequence point for reading
3244 * volatile variables prior to a comparison to ensure compliance
3245 * with MISRA C 2012 Rule 13.2. */
3246 uxCurrentListLength = listCURRENT_LIST_LENGTH( &xSuspendedTaskList );
3248 if( uxCurrentListLength == uxCurrentNumberOfTasks )
3250 /* No other tasks are ready, so set pxCurrentTCB back to
3251 * NULL so when the next task is created pxCurrentTCB will
3252 * be set to point to it no matter what its relative priority
3254 pxCurrentTCB = NULL;
3258 vTaskSwitchContext();
3264 mtCOVERAGE_TEST_MARKER();
3267 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3269 traceRETURN_vTaskSuspend();
3272 #endif /* INCLUDE_vTaskSuspend */
3273 /*-----------------------------------------------------------*/
3275 #if ( INCLUDE_vTaskSuspend == 1 )
3277 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
3279 BaseType_t xReturn = pdFALSE;
3280 const TCB_t * const pxTCB = xTask;
3282 /* Accesses xPendingReadyList so must be called from a critical
3285 /* It does not make sense to check if the calling task is suspended. */
3286 configASSERT( xTask );
3288 /* Is the task being resumed actually in the suspended list? */
3289 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
3291 /* Has the task already been resumed from within an ISR? */
3292 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
3294 /* Is it in the suspended list because it is in the Suspended
3295 * state, or because it is blocked with no timeout? */
3296 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE )
3298 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3302 /* The task does not appear on the event list item of
3303 * and of the RTOS objects, but could still be in the
3304 * blocked state if it is waiting on its notification
3305 * rather than waiting on an object. If not, is
3309 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3311 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3318 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3322 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3326 mtCOVERAGE_TEST_MARKER();
3331 mtCOVERAGE_TEST_MARKER();
3336 mtCOVERAGE_TEST_MARKER();
3342 #endif /* INCLUDE_vTaskSuspend */
3343 /*-----------------------------------------------------------*/
3345 #if ( INCLUDE_vTaskSuspend == 1 )
3347 void vTaskResume( TaskHandle_t xTaskToResume )
3349 TCB_t * const pxTCB = xTaskToResume;
3351 traceENTER_vTaskResume( xTaskToResume );
3353 /* It does not make sense to resume the calling task. */
3354 configASSERT( xTaskToResume );
3356 #if ( configNUMBER_OF_CORES == 1 )
3358 /* The parameter cannot be NULL as it is impossible to resume the
3359 * currently executing task. */
3360 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
3363 /* The parameter cannot be NULL as it is impossible to resume the
3364 * currently executing task. It is also impossible to resume a task
3365 * that is actively running on another core but it is not safe
3366 * to check their run state here. Therefore, we get into a critical
3367 * section and check if the task is actually suspended or not. */
3371 taskENTER_CRITICAL();
3373 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3375 traceTASK_RESUME( pxTCB );
3377 /* The ready list can be accessed even if the scheduler is
3378 * suspended because this is inside a critical section. */
3379 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3380 prvAddTaskToReadyList( pxTCB );
3382 /* This yield may not cause the task just resumed to run,
3383 * but will leave the lists in the correct state for the
3385 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
3389 mtCOVERAGE_TEST_MARKER();
3392 taskEXIT_CRITICAL();
3396 mtCOVERAGE_TEST_MARKER();
3399 traceRETURN_vTaskResume();
3402 #endif /* INCLUDE_vTaskSuspend */
3404 /*-----------------------------------------------------------*/
3406 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
3408 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
3410 BaseType_t xYieldRequired = pdFALSE;
3411 TCB_t * const pxTCB = xTaskToResume;
3412 UBaseType_t uxSavedInterruptStatus;
3414 traceENTER_xTaskResumeFromISR( xTaskToResume );
3416 configASSERT( xTaskToResume );
3418 /* RTOS ports that support interrupt nesting have the concept of a
3419 * maximum system call (or maximum API call) interrupt priority.
3420 * Interrupts that are above the maximum system call priority are keep
3421 * permanently enabled, even when the RTOS kernel is in a critical section,
3422 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
3423 * is defined in FreeRTOSConfig.h then
3424 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3425 * failure if a FreeRTOS API function is called from an interrupt that has
3426 * been assigned a priority above the configured maximum system call
3427 * priority. Only FreeRTOS functions that end in FromISR can be called
3428 * from interrupts that have been assigned a priority at or (logically)
3429 * below the maximum system call interrupt priority. FreeRTOS maintains a
3430 * separate interrupt safe API to ensure interrupt entry is as fast and as
3431 * simple as possible. More information (albeit Cortex-M specific) is
3432 * provided on the following link:
3433 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3434 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3436 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3438 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3440 traceTASK_RESUME_FROM_ISR( pxTCB );
3442 /* Check the ready lists can be accessed. */
3443 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3445 #if ( configNUMBER_OF_CORES == 1 )
3447 /* Ready lists can be accessed so move the task from the
3448 * suspended list to the ready list directly. */
3449 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3451 xYieldRequired = pdTRUE;
3453 /* Mark that a yield is pending in case the user is not
3454 * using the return value to initiate a context switch
3455 * from the ISR using the port specific portYIELD_FROM_ISR(). */
3456 xYieldPendings[ 0 ] = pdTRUE;
3460 mtCOVERAGE_TEST_MARKER();
3463 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3465 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3466 prvAddTaskToReadyList( pxTCB );
3470 /* The delayed or ready lists cannot be accessed so the task
3471 * is held in the pending ready list until the scheduler is
3473 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3476 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3478 prvYieldForTask( pxTCB );
3480 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3482 xYieldRequired = pdTRUE;
3485 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3489 mtCOVERAGE_TEST_MARKER();
3492 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3494 traceRETURN_xTaskResumeFromISR( xYieldRequired );
3496 return xYieldRequired;
3499 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3500 /*-----------------------------------------------------------*/
3502 static BaseType_t prvCreateIdleTasks( void )
3504 BaseType_t xReturn = pdPASS;
3506 char cIdleName[ configMAX_TASK_NAME_LEN ];
3507 TaskFunction_t pxIdleTaskFunction = NULL;
3508 BaseType_t xIdleTaskNameIndex;
3510 for( xIdleTaskNameIndex = ( BaseType_t ) 0; xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN; xIdleTaskNameIndex++ )
3512 cIdleName[ xIdleTaskNameIndex ] = configIDLE_TASK_NAME[ xIdleTaskNameIndex ];
3514 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3515 * configMAX_TASK_NAME_LEN characters just in case the memory after the
3516 * string is not accessible (extremely unlikely). */
3517 if( cIdleName[ xIdleTaskNameIndex ] == ( char ) 0x00 )
3523 mtCOVERAGE_TEST_MARKER();
3527 /* Add each idle task at the lowest priority. */
3528 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3530 #if ( configNUMBER_OF_CORES == 1 )
3532 pxIdleTaskFunction = prvIdleTask;
3534 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3536 /* In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks
3537 * are also created to ensure that each core has an idle task to
3538 * run when no other task is available to run. */
3541 pxIdleTaskFunction = prvIdleTask;
3545 pxIdleTaskFunction = prvPassiveIdleTask;
3548 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3550 /* Update the idle task name with suffix to differentiate the idle tasks.
3551 * This function is not required in single core FreeRTOS since there is
3552 * only one idle task. */
3553 #if ( configNUMBER_OF_CORES > 1 )
3555 /* Append the idle task number to the end of the name if there is space. */
3556 if( xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3558 cIdleName[ xIdleTaskNameIndex ] = ( char ) ( xCoreID + '0' );
3560 /* And append a null character if there is space. */
3561 if( ( xIdleTaskNameIndex + 1 ) < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3563 cIdleName[ xIdleTaskNameIndex + 1 ] = '\0';
3567 mtCOVERAGE_TEST_MARKER();
3572 mtCOVERAGE_TEST_MARKER();
3575 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
3577 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3579 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3580 StackType_t * pxIdleTaskStackBuffer = NULL;
3581 configSTACK_DEPTH_TYPE uxIdleTaskStackSize;
3583 /* The Idle task is created using user provided RAM - obtain the
3584 * address of the RAM then create the idle task. */
3585 #if ( configNUMBER_OF_CORES == 1 )
3587 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
3593 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
3597 vApplicationGetPassiveIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize, xCoreID - 1 );
3600 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
3601 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( pxIdleTaskFunction,
3603 uxIdleTaskStackSize,
3605 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3606 pxIdleTaskStackBuffer,
3607 pxIdleTaskTCBBuffer );
3609 if( xIdleTaskHandles[ xCoreID ] != NULL )
3618 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3620 /* The Idle task is being created using dynamically allocated RAM. */
3621 xReturn = xTaskCreate( pxIdleTaskFunction,
3623 configMINIMAL_STACK_SIZE,
3625 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3626 &xIdleTaskHandles[ xCoreID ] );
3628 #endif /* configSUPPORT_STATIC_ALLOCATION */
3630 /* Break the loop if any of the idle task is failed to be created. */
3631 if( xReturn == pdFAIL )
3637 #if ( configNUMBER_OF_CORES == 1 )
3639 mtCOVERAGE_TEST_MARKER();
3643 /* Assign idle task to each core before SMP scheduler is running. */
3644 xIdleTaskHandles[ xCoreID ]->xTaskRunState = xCoreID;
3645 pxCurrentTCBs[ xCoreID ] = xIdleTaskHandles[ xCoreID ];
3654 /*-----------------------------------------------------------*/
3656 void vTaskStartScheduler( void )
3660 traceENTER_vTaskStartScheduler();
3662 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3664 /* Sanity check that the UBaseType_t must have greater than or equal to
3665 * the number of bits as confNUMBER_OF_CORES. */
3666 configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3668 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3670 xReturn = prvCreateIdleTasks();
3672 #if ( configUSE_TIMERS == 1 )
3674 if( xReturn == pdPASS )
3676 xReturn = xTimerCreateTimerTask();
3680 mtCOVERAGE_TEST_MARKER();
3683 #endif /* configUSE_TIMERS */
3685 if( xReturn == pdPASS )
3687 /* freertos_tasks_c_additions_init() should only be called if the user
3688 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3689 * the only macro called by the function. */
3690 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3692 freertos_tasks_c_additions_init();
3696 /* Interrupts are turned off here, to ensure a tick does not occur
3697 * before or during the call to xPortStartScheduler(). The stacks of
3698 * the created tasks contain a status word with interrupts switched on
3699 * so interrupts will automatically get re-enabled when the first task
3701 portDISABLE_INTERRUPTS();
3703 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3705 /* Switch C-Runtime's TLS Block to point to the TLS
3706 * block specific to the task that will run first. */
3707 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3711 xNextTaskUnblockTime = portMAX_DELAY;
3712 xSchedulerRunning = pdTRUE;
3713 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3715 /* If configGENERATE_RUN_TIME_STATS is defined then the following
3716 * macro must be defined to configure the timer/counter used to generate
3717 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
3718 * is set to 0 and the following line fails to build then ensure you do not
3719 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3720 * FreeRTOSConfig.h file. */
3721 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3723 traceTASK_SWITCHED_IN();
3725 /* Setting up the timer tick is hardware specific and thus in the
3726 * portable interface. */
3728 /* The return value for xPortStartScheduler is not required
3729 * hence using a void datatype. */
3730 ( void ) xPortStartScheduler();
3732 /* In most cases, xPortStartScheduler() will not return. If it
3733 * returns pdTRUE then there was not enough heap memory available
3734 * to create either the Idle or the Timer task. If it returned
3735 * pdFALSE, then the application called xTaskEndScheduler().
3736 * Most ports don't implement xTaskEndScheduler() as there is
3737 * nothing to return to. */
3741 /* This line will only be reached if the kernel could not be started,
3742 * because there was not enough FreeRTOS heap to create the idle task
3743 * or the timer task. */
3744 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3747 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3748 * meaning xIdleTaskHandles are not used anywhere else. */
3749 ( void ) xIdleTaskHandles;
3751 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3752 * from getting optimized out as it is no longer used by the kernel. */
3753 ( void ) uxTopUsedPriority;
3755 traceRETURN_vTaskStartScheduler();
3757 /*-----------------------------------------------------------*/
3759 void vTaskEndScheduler( void )
3761 traceENTER_vTaskEndScheduler();
3763 #if ( INCLUDE_vTaskDelete == 1 )
3767 #if ( configUSE_TIMERS == 1 )
3769 /* Delete the timer task created by the kernel. */
3770 vTaskDelete( xTimerGetTimerDaemonTaskHandle() );
3772 #endif /* #if ( configUSE_TIMERS == 1 ) */
3774 /* Delete Idle tasks created by the kernel.*/
3775 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3777 vTaskDelete( xIdleTaskHandles[ xCoreID ] );
3780 /* Idle task is responsible for reclaiming the resources of the tasks in
3781 * xTasksWaitingTermination list. Since the idle task is now deleted and
3782 * no longer going to run, we need to reclaim resources of all the tasks
3783 * in the xTasksWaitingTermination list. */
3784 prvCheckTasksWaitingTermination();
3786 #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
3788 /* Stop the scheduler interrupts and call the portable scheduler end
3789 * routine so the original ISRs can be restored if necessary. The port
3790 * layer must ensure interrupts enable bit is left in the correct state. */
3791 portDISABLE_INTERRUPTS();
3792 xSchedulerRunning = pdFALSE;
3794 /* This function must be called from a task and the application is
3795 * responsible for deleting that task after the scheduler is stopped. */
3796 vPortEndScheduler();
3798 traceRETURN_vTaskEndScheduler();
3800 /*----------------------------------------------------------*/
3802 void vTaskSuspendAll( void )
3804 traceENTER_vTaskSuspendAll();
3806 #if ( configNUMBER_OF_CORES == 1 )
3808 /* A critical section is not required as the variable is of type
3809 * BaseType_t. Please read Richard Barry's reply in the following link to a
3810 * post in the FreeRTOS support forum before reporting this as a bug! -
3811 * https://goo.gl/wu4acr */
3813 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3814 * do not otherwise exhibit real time behaviour. */
3815 portSOFTWARE_BARRIER();
3817 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3818 * is used to allow calls to vTaskSuspendAll() to nest. */
3819 uxSchedulerSuspended += ( UBaseType_t ) 1U;
3821 /* Enforces ordering for ports and optimised compilers that may otherwise place
3822 * the above increment elsewhere. */
3823 portMEMORY_BARRIER();
3825 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3827 UBaseType_t ulState;
3829 /* This must only be called from within a task. */
3830 portASSERT_IF_IN_ISR();
3832 if( xSchedulerRunning != pdFALSE )
3834 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3835 * We must disable interrupts before we grab the locks in the event that this task is
3836 * interrupted and switches context before incrementing uxSchedulerSuspended.
3837 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3838 * uxSchedulerSuspended since that will prevent context switches. */
3839 ulState = portSET_INTERRUPT_MASK();
3841 /* This must never be called from inside a critical section. */
3842 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
3844 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3845 * do not otherwise exhibit real time behaviour. */
3846 portSOFTWARE_BARRIER();
3848 portGET_TASK_LOCK();
3850 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3851 * purpose is to prevent altering the variable when fromISR APIs are readying
3853 if( uxSchedulerSuspended == 0U )
3855 prvCheckForRunStateChange();
3859 mtCOVERAGE_TEST_MARKER();
3864 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3865 * is used to allow calls to vTaskSuspendAll() to nest. */
3866 ++uxSchedulerSuspended;
3867 portRELEASE_ISR_LOCK();
3869 portCLEAR_INTERRUPT_MASK( ulState );
3873 mtCOVERAGE_TEST_MARKER();
3876 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3878 traceRETURN_vTaskSuspendAll();
3881 /*----------------------------------------------------------*/
3883 #if ( configUSE_TICKLESS_IDLE != 0 )
3885 static TickType_t prvGetExpectedIdleTime( void )
3888 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
3890 /* uxHigherPriorityReadyTasks takes care of the case where
3891 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3892 * task that are in the Ready state, even though the idle task is
3894 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3896 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3898 uxHigherPriorityReadyTasks = pdTRUE;
3903 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3905 /* When port optimised task selection is used the uxTopReadyPriority
3906 * variable is used as a bit map. If bits other than the least
3907 * significant bit are set then there are tasks that have a priority
3908 * above the idle priority that are in the Ready state. This takes
3909 * care of the case where the co-operative scheduler is in use. */
3910 if( uxTopReadyPriority > uxLeastSignificantBit )
3912 uxHigherPriorityReadyTasks = pdTRUE;
3915 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3917 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3921 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1U )
3923 /* There are other idle priority tasks in the ready state. If
3924 * time slicing is used then the very next tick interrupt must be
3928 else if( uxHigherPriorityReadyTasks != pdFALSE )
3930 /* There are tasks in the Ready state that have a priority above the
3931 * idle priority. This path can only be reached if
3932 * configUSE_PREEMPTION is 0. */
3937 xReturn = xNextTaskUnblockTime;
3938 xReturn -= xTickCount;
3944 #endif /* configUSE_TICKLESS_IDLE */
3945 /*----------------------------------------------------------*/
3947 BaseType_t xTaskResumeAll( void )
3949 TCB_t * pxTCB = NULL;
3950 BaseType_t xAlreadyYielded = pdFALSE;
3952 traceENTER_xTaskResumeAll();
3954 #if ( configNUMBER_OF_CORES > 1 )
3955 if( xSchedulerRunning != pdFALSE )
3958 /* It is possible that an ISR caused a task to be removed from an event
3959 * list while the scheduler was suspended. If this was the case then the
3960 * removed task will have been added to the xPendingReadyList. Once the
3961 * scheduler has been resumed it is safe to move all the pending ready
3962 * tasks from this list into their appropriate ready list. */
3963 taskENTER_CRITICAL();
3966 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3968 /* If uxSchedulerSuspended is zero then this function does not match a
3969 * previous call to vTaskSuspendAll(). */
3970 configASSERT( uxSchedulerSuspended != 0U );
3972 uxSchedulerSuspended -= ( UBaseType_t ) 1U;
3973 portRELEASE_TASK_LOCK();
3975 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3977 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3979 /* Move any readied tasks from the pending list into the
3980 * appropriate ready list. */
3981 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3983 /* MISRA Ref 11.5.3 [Void pointer assignment] */
3984 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
3985 /* coverity[misra_c_2012_rule_11_5_violation] */
3986 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) );
3987 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
3988 portMEMORY_BARRIER();
3989 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
3990 prvAddTaskToReadyList( pxTCB );
3992 #if ( configNUMBER_OF_CORES == 1 )
3994 /* If the moved task has a priority higher than the current
3995 * task then a yield must be performed. */
3996 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3998 xYieldPendings[ xCoreID ] = pdTRUE;
4002 mtCOVERAGE_TEST_MARKER();
4005 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4007 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
4008 * If the current core yielded then vTaskSwitchContext() has already been called
4009 * which sets xYieldPendings for the current core to pdTRUE. */
4011 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4016 /* A task was unblocked while the scheduler was suspended,
4017 * which may have prevented the next unblock time from being
4018 * re-calculated, in which case re-calculate it now. Mainly
4019 * important for low power tickless implementations, where
4020 * this can prevent an unnecessary exit from low power
4022 prvResetNextTaskUnblockTime();
4025 /* If any ticks occurred while the scheduler was suspended then
4026 * they should be processed now. This ensures the tick count does
4027 * not slip, and that any delayed tasks are resumed at the correct
4030 * It should be safe to call xTaskIncrementTick here from any core
4031 * since we are in a critical section and xTaskIncrementTick itself
4032 * protects itself within a critical section. Suspending the scheduler
4033 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
4035 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
4037 if( xPendedCounts > ( TickType_t ) 0U )
4041 if( xTaskIncrementTick() != pdFALSE )
4043 /* Other cores are interrupted from
4044 * within xTaskIncrementTick(). */
4045 xYieldPendings[ xCoreID ] = pdTRUE;
4049 mtCOVERAGE_TEST_MARKER();
4053 } while( xPendedCounts > ( TickType_t ) 0U );
4059 mtCOVERAGE_TEST_MARKER();
4063 if( xYieldPendings[ xCoreID ] != pdFALSE )
4065 #if ( configUSE_PREEMPTION != 0 )
4067 xAlreadyYielded = pdTRUE;
4069 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
4071 #if ( configNUMBER_OF_CORES == 1 )
4073 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
4075 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4079 mtCOVERAGE_TEST_MARKER();
4085 mtCOVERAGE_TEST_MARKER();
4088 taskEXIT_CRITICAL();
4091 traceRETURN_xTaskResumeAll( xAlreadyYielded );
4093 return xAlreadyYielded;
4095 /*-----------------------------------------------------------*/
4097 TickType_t xTaskGetTickCount( void )
4101 traceENTER_xTaskGetTickCount();
4103 /* Critical section required if running on a 16 bit processor. */
4104 portTICK_TYPE_ENTER_CRITICAL();
4106 xTicks = xTickCount;
4108 portTICK_TYPE_EXIT_CRITICAL();
4110 traceRETURN_xTaskGetTickCount( xTicks );
4114 /*-----------------------------------------------------------*/
4116 TickType_t xTaskGetTickCountFromISR( void )
4119 UBaseType_t uxSavedInterruptStatus;
4121 traceENTER_xTaskGetTickCountFromISR();
4123 /* RTOS ports that support interrupt nesting have the concept of a maximum
4124 * system call (or maximum API call) interrupt priority. Interrupts that are
4125 * above the maximum system call priority are kept permanently enabled, even
4126 * when the RTOS kernel is in a critical section, but cannot make any calls to
4127 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
4128 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
4129 * failure if a FreeRTOS API function is called from an interrupt that has been
4130 * assigned a priority above the configured maximum system call priority.
4131 * Only FreeRTOS functions that end in FromISR can be called from interrupts
4132 * that have been assigned a priority at or (logically) below the maximum
4133 * system call interrupt priority. FreeRTOS maintains a separate interrupt
4134 * safe API to ensure interrupt entry is as fast and as simple as possible.
4135 * More information (albeit Cortex-M specific) is provided on the following
4136 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
4137 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
4139 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
4141 xReturn = xTickCount;
4143 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
4145 traceRETURN_xTaskGetTickCountFromISR( xReturn );
4149 /*-----------------------------------------------------------*/
4151 UBaseType_t uxTaskGetNumberOfTasks( void )
4153 traceENTER_uxTaskGetNumberOfTasks();
4155 /* A critical section is not required because the variables are of type
4157 traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
4159 return uxCurrentNumberOfTasks;
4161 /*-----------------------------------------------------------*/
4163 char * pcTaskGetName( TaskHandle_t xTaskToQuery )
4167 traceENTER_pcTaskGetName( xTaskToQuery );
4169 /* If null is passed in here then the name of the calling task is being
4171 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4172 configASSERT( pxTCB );
4174 traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
4176 return &( pxTCB->pcTaskName[ 0 ] );
4178 /*-----------------------------------------------------------*/
4180 #if ( INCLUDE_xTaskGetHandle == 1 )
4181 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4182 const char pcNameToQuery[] )
4184 TCB_t * pxReturn = NULL;
4185 TCB_t * pxTCB = NULL;
4188 BaseType_t xBreakLoop;
4189 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
4190 ListItem_t * pxIterator;
4192 /* This function is called with the scheduler suspended. */
4194 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4196 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
4198 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4199 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4200 /* coverity[misra_c_2012_rule_11_5_violation] */
4201 pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
4203 /* Check each character in the name looking for a match or
4205 xBreakLoop = pdFALSE;
4207 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4209 cNextChar = pxTCB->pcTaskName[ x ];
4211 if( cNextChar != pcNameToQuery[ x ] )
4213 /* Characters didn't match. */
4214 xBreakLoop = pdTRUE;
4216 else if( cNextChar == ( char ) 0x00 )
4218 /* Both strings terminated, a match must have been
4221 xBreakLoop = pdTRUE;
4225 mtCOVERAGE_TEST_MARKER();
4228 if( xBreakLoop != pdFALSE )
4234 if( pxReturn != NULL )
4236 /* The handle has been found. */
4243 mtCOVERAGE_TEST_MARKER();
4249 #endif /* INCLUDE_xTaskGetHandle */
4250 /*-----------------------------------------------------------*/
4252 #if ( INCLUDE_xTaskGetHandle == 1 )
4254 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery )
4256 UBaseType_t uxQueue = configMAX_PRIORITIES;
4259 traceENTER_xTaskGetHandle( pcNameToQuery );
4261 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
4262 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
4266 /* Search the ready lists. */
4270 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
4274 /* Found the handle. */
4277 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4279 /* Search the delayed lists. */
4282 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
4287 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
4290 #if ( INCLUDE_vTaskSuspend == 1 )
4294 /* Search the suspended list. */
4295 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
4300 #if ( INCLUDE_vTaskDelete == 1 )
4304 /* Search the deleted list. */
4305 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
4310 ( void ) xTaskResumeAll();
4312 traceRETURN_xTaskGetHandle( pxTCB );
4317 #endif /* INCLUDE_xTaskGetHandle */
4318 /*-----------------------------------------------------------*/
4320 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4322 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4323 StackType_t ** ppuxStackBuffer,
4324 StaticTask_t ** ppxTaskBuffer )
4329 traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4331 configASSERT( ppuxStackBuffer != NULL );
4332 configASSERT( ppxTaskBuffer != NULL );
4334 pxTCB = prvGetTCBFromHandle( xTask );
4336 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4338 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4340 *ppuxStackBuffer = pxTCB->pxStack;
4341 /* MISRA Ref 11.3.1 [Misaligned access] */
4342 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
4343 /* coverity[misra_c_2012_rule_11_3_violation] */
4344 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4347 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4349 *ppuxStackBuffer = pxTCB->pxStack;
4350 *ppxTaskBuffer = NULL;
4358 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4360 *ppuxStackBuffer = pxTCB->pxStack;
4361 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4364 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4366 traceRETURN_xTaskGetStaticBuffers( xReturn );
4371 #endif /* configSUPPORT_STATIC_ALLOCATION */
4372 /*-----------------------------------------------------------*/
4374 #if ( configUSE_TRACE_FACILITY == 1 )
4376 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4377 const UBaseType_t uxArraySize,
4378 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4380 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4382 traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4386 /* Is there a space in the array for each task in the system? */
4387 if( uxArraySize >= uxCurrentNumberOfTasks )
4389 /* Fill in an TaskStatus_t structure with information on each
4390 * task in the Ready state. */
4394 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4395 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4397 /* Fill in an TaskStatus_t structure with information on each
4398 * task in the Blocked state. */
4399 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4400 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4402 #if ( INCLUDE_vTaskDelete == 1 )
4404 /* Fill in an TaskStatus_t structure with information on
4405 * each task that has been deleted but not yet cleaned up. */
4406 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4410 #if ( INCLUDE_vTaskSuspend == 1 )
4412 /* Fill in an TaskStatus_t structure with information on
4413 * each task in the Suspended state. */
4414 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4418 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4420 if( pulTotalRunTime != NULL )
4422 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4423 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4425 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4429 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4431 if( pulTotalRunTime != NULL )
4433 *pulTotalRunTime = 0;
4436 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4440 mtCOVERAGE_TEST_MARKER();
4443 ( void ) xTaskResumeAll();
4445 traceRETURN_uxTaskGetSystemState( uxTask );
4450 #endif /* configUSE_TRACE_FACILITY */
4451 /*----------------------------------------------------------*/
4453 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4455 #if ( configNUMBER_OF_CORES == 1 )
4456 TaskHandle_t xTaskGetIdleTaskHandle( void )
4458 traceENTER_xTaskGetIdleTaskHandle();
4460 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4461 * started, then xIdleTaskHandles will be NULL. */
4462 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4464 traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4466 return xIdleTaskHandles[ 0 ];
4468 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
4470 TaskHandle_t xTaskGetIdleTaskHandleForCore( BaseType_t xCoreID )
4472 traceENTER_xTaskGetIdleTaskHandleForCore( xCoreID );
4474 /* Ensure the core ID is valid. */
4475 configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE );
4477 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4478 * started, then xIdleTaskHandles will be NULL. */
4479 configASSERT( ( xIdleTaskHandles[ xCoreID ] != NULL ) );
4481 traceRETURN_xTaskGetIdleTaskHandleForCore( xIdleTaskHandles[ xCoreID ] );
4483 return xIdleTaskHandles[ xCoreID ];
4486 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4487 /*----------------------------------------------------------*/
4489 /* This conditional compilation should use inequality to 0, not equality to 1.
4490 * This is to ensure vTaskStepTick() is available when user defined low power mode
4491 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4493 #if ( configUSE_TICKLESS_IDLE != 0 )
4495 void vTaskStepTick( TickType_t xTicksToJump )
4497 TickType_t xUpdatedTickCount;
4499 traceENTER_vTaskStepTick( xTicksToJump );
4501 /* Correct the tick count value after a period during which the tick
4502 * was suppressed. Note this does *not* call the tick hook function for
4503 * each stepped tick. */
4504 xUpdatedTickCount = xTickCount + xTicksToJump;
4505 configASSERT( xUpdatedTickCount <= xNextTaskUnblockTime );
4507 if( xUpdatedTickCount == xNextTaskUnblockTime )
4509 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4510 * xTaskIncrementTick() when the scheduler resumes. This ensures
4511 * that any delayed tasks are resumed at the correct time. */
4512 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4513 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4515 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4516 taskENTER_CRITICAL();
4520 taskEXIT_CRITICAL();
4525 mtCOVERAGE_TEST_MARKER();
4528 xTickCount += xTicksToJump;
4530 traceINCREASE_TICK_COUNT( xTicksToJump );
4531 traceRETURN_vTaskStepTick();
4534 #endif /* configUSE_TICKLESS_IDLE */
4535 /*----------------------------------------------------------*/
4537 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4539 BaseType_t xYieldOccurred;
4541 traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4543 /* Must not be called with the scheduler suspended as the implementation
4544 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4545 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4547 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4548 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4551 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4552 taskENTER_CRITICAL();
4554 xPendedTicks += xTicksToCatchUp;
4556 taskEXIT_CRITICAL();
4557 xYieldOccurred = xTaskResumeAll();
4559 traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4561 return xYieldOccurred;
4563 /*----------------------------------------------------------*/
4565 #if ( INCLUDE_xTaskAbortDelay == 1 )
4567 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4569 TCB_t * pxTCB = xTask;
4572 traceENTER_xTaskAbortDelay( xTask );
4574 configASSERT( pxTCB );
4578 /* A task can only be prematurely removed from the Blocked state if
4579 * it is actually in the Blocked state. */
4580 if( eTaskGetState( xTask ) == eBlocked )
4584 /* Remove the reference to the task from the blocked list. An
4585 * interrupt won't touch the xStateListItem because the
4586 * scheduler is suspended. */
4587 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4589 /* Is the task waiting on an event also? If so remove it from
4590 * the event list too. Interrupts can touch the event list item,
4591 * even though the scheduler is suspended, so a critical section
4593 taskENTER_CRITICAL();
4595 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4597 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4599 /* This lets the task know it was forcibly removed from the
4600 * blocked state so it should not re-evaluate its block time and
4601 * then block again. */
4602 pxTCB->ucDelayAborted = ( uint8_t ) pdTRUE;
4606 mtCOVERAGE_TEST_MARKER();
4609 taskEXIT_CRITICAL();
4611 /* Place the unblocked task into the appropriate ready list. */
4612 prvAddTaskToReadyList( pxTCB );
4614 /* A task being unblocked cannot cause an immediate context
4615 * switch if preemption is turned off. */
4616 #if ( configUSE_PREEMPTION == 1 )
4618 #if ( configNUMBER_OF_CORES == 1 )
4620 /* Preemption is on, but a context switch should only be
4621 * performed if the unblocked task has a priority that is
4622 * higher than the currently executing task. */
4623 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4625 /* Pend the yield to be performed when the scheduler
4626 * is unsuspended. */
4627 xYieldPendings[ 0 ] = pdTRUE;
4631 mtCOVERAGE_TEST_MARKER();
4634 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4636 taskENTER_CRITICAL();
4638 prvYieldForTask( pxTCB );
4640 taskEXIT_CRITICAL();
4642 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4644 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4651 ( void ) xTaskResumeAll();
4653 traceRETURN_xTaskAbortDelay( xReturn );
4658 #endif /* INCLUDE_xTaskAbortDelay */
4659 /*----------------------------------------------------------*/
4661 BaseType_t xTaskIncrementTick( void )
4664 TickType_t xItemValue;
4665 BaseType_t xSwitchRequired = pdFALSE;
4667 #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4668 BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4669 #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4671 traceENTER_xTaskIncrementTick();
4673 /* Called by the portable layer each time a tick interrupt occurs.
4674 * Increments the tick then checks to see if the new tick value will cause any
4675 * tasks to be unblocked. */
4676 traceTASK_INCREMENT_TICK( xTickCount );
4678 /* Tick increment should occur on every kernel timer event. Core 0 has the
4679 * responsibility to increment the tick, or increment the pended ticks if the
4680 * scheduler is suspended. If pended ticks is greater than zero, the core that
4681 * calls xTaskResumeAll has the responsibility to increment the tick. */
4682 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4684 /* Minor optimisation. The tick count cannot change in this
4686 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4688 /* Increment the RTOS tick, switching the delayed and overflowed
4689 * delayed lists if it wraps to 0. */
4690 xTickCount = xConstTickCount;
4692 if( xConstTickCount == ( TickType_t ) 0U )
4694 taskSWITCH_DELAYED_LISTS();
4698 mtCOVERAGE_TEST_MARKER();
4701 /* See if this tick has made a timeout expire. Tasks are stored in
4702 * the queue in the order of their wake time - meaning once one task
4703 * has been found whose block time has not expired there is no need to
4704 * look any further down the list. */
4705 if( xConstTickCount >= xNextTaskUnblockTime )
4709 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4711 /* The delayed list is empty. Set xNextTaskUnblockTime
4712 * to the maximum possible value so it is extremely
4714 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4715 * next time through. */
4716 xNextTaskUnblockTime = portMAX_DELAY;
4721 /* The delayed list is not empty, get the value of the
4722 * item at the head of the delayed list. This is the time
4723 * at which the task at the head of the delayed list must
4724 * be removed from the Blocked state. */
4725 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4726 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4727 /* coverity[misra_c_2012_rule_11_5_violation] */
4728 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
4729 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4731 if( xConstTickCount < xItemValue )
4733 /* It is not time to unblock this item yet, but the
4734 * item value is the time at which the task at the head
4735 * of the blocked list must be removed from the Blocked
4736 * state - so record the item value in
4737 * xNextTaskUnblockTime. */
4738 xNextTaskUnblockTime = xItemValue;
4743 mtCOVERAGE_TEST_MARKER();
4746 /* It is time to remove the item from the Blocked state. */
4747 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4749 /* Is the task waiting on an event also? If so remove
4750 * it from the event list. */
4751 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4753 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4757 mtCOVERAGE_TEST_MARKER();
4760 /* Place the unblocked task into the appropriate ready
4762 prvAddTaskToReadyList( pxTCB );
4764 /* A task being unblocked cannot cause an immediate
4765 * context switch if preemption is turned off. */
4766 #if ( configUSE_PREEMPTION == 1 )
4768 #if ( configNUMBER_OF_CORES == 1 )
4770 /* Preemption is on, but a context switch should
4771 * only be performed if the unblocked task's
4772 * priority is higher than the currently executing
4774 * The case of equal priority tasks sharing
4775 * processing time (which happens when both
4776 * preemption and time slicing are on) is
4778 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4780 xSwitchRequired = pdTRUE;
4784 mtCOVERAGE_TEST_MARKER();
4787 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4789 prvYieldForTask( pxTCB );
4791 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4793 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4798 /* Tasks of equal priority to the currently running task will share
4799 * processing time (time slice) if preemption is on, and the application
4800 * writer has not explicitly turned time slicing off. */
4801 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4803 #if ( configNUMBER_OF_CORES == 1 )
4805 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > 1U )
4807 xSwitchRequired = pdTRUE;
4811 mtCOVERAGE_TEST_MARKER();
4814 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4818 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4820 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1U )
4822 xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4826 mtCOVERAGE_TEST_MARKER();
4830 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4832 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4834 #if ( configUSE_TICK_HOOK == 1 )
4836 /* Guard against the tick hook being called when the pended tick
4837 * count is being unwound (when the scheduler is being unlocked). */
4838 if( xPendedTicks == ( TickType_t ) 0 )
4840 vApplicationTickHook();
4844 mtCOVERAGE_TEST_MARKER();
4847 #endif /* configUSE_TICK_HOOK */
4849 #if ( configUSE_PREEMPTION == 1 )
4851 #if ( configNUMBER_OF_CORES == 1 )
4853 /* For single core the core ID is always 0. */
4854 if( xYieldPendings[ 0 ] != pdFALSE )
4856 xSwitchRequired = pdTRUE;
4860 mtCOVERAGE_TEST_MARKER();
4863 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4865 BaseType_t xCoreID, xCurrentCoreID;
4866 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4868 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4870 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4871 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4874 if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4876 if( xCoreID == xCurrentCoreID )
4878 xSwitchRequired = pdTRUE;
4882 prvYieldCore( xCoreID );
4887 mtCOVERAGE_TEST_MARKER();
4892 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4894 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4900 /* The tick hook gets called at regular intervals, even if the
4901 * scheduler is locked. */
4902 #if ( configUSE_TICK_HOOK == 1 )
4904 vApplicationTickHook();
4909 traceRETURN_xTaskIncrementTick( xSwitchRequired );
4911 return xSwitchRequired;
4913 /*-----------------------------------------------------------*/
4915 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4917 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4918 TaskHookFunction_t pxHookFunction )
4922 traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4924 /* If xTask is NULL then it is the task hook of the calling task that is
4928 xTCB = ( TCB_t * ) pxCurrentTCB;
4935 /* Save the hook function in the TCB. A critical section is required as
4936 * the value can be accessed from an interrupt. */
4937 taskENTER_CRITICAL();
4939 xTCB->pxTaskTag = pxHookFunction;
4941 taskEXIT_CRITICAL();
4943 traceRETURN_vTaskSetApplicationTaskTag();
4946 #endif /* configUSE_APPLICATION_TASK_TAG */
4947 /*-----------------------------------------------------------*/
4949 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4951 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4954 TaskHookFunction_t xReturn;
4956 traceENTER_xTaskGetApplicationTaskTag( xTask );
4958 /* If xTask is NULL then set the calling task's hook. */
4959 pxTCB = prvGetTCBFromHandle( xTask );
4961 /* Save the hook function in the TCB. A critical section is required as
4962 * the value can be accessed from an interrupt. */
4963 taskENTER_CRITICAL();
4965 xReturn = pxTCB->pxTaskTag;
4967 taskEXIT_CRITICAL();
4969 traceRETURN_xTaskGetApplicationTaskTag( xReturn );
4974 #endif /* configUSE_APPLICATION_TASK_TAG */
4975 /*-----------------------------------------------------------*/
4977 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4979 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
4982 TaskHookFunction_t xReturn;
4983 UBaseType_t uxSavedInterruptStatus;
4985 traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
4987 /* If xTask is NULL then set the calling task's hook. */
4988 pxTCB = prvGetTCBFromHandle( xTask );
4990 /* Save the hook function in the TCB. A critical section is required as
4991 * the value can be accessed from an interrupt. */
4992 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
4994 xReturn = pxTCB->pxTaskTag;
4996 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
4998 traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
5003 #endif /* configUSE_APPLICATION_TASK_TAG */
5004 /*-----------------------------------------------------------*/
5006 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5008 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
5009 void * pvParameter )
5014 traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
5016 /* If xTask is NULL then we are calling our own task hook. */
5019 xTCB = pxCurrentTCB;
5026 if( xTCB->pxTaskTag != NULL )
5028 xReturn = xTCB->pxTaskTag( pvParameter );
5035 traceRETURN_xTaskCallApplicationTaskHook( xReturn );
5040 #endif /* configUSE_APPLICATION_TASK_TAG */
5041 /*-----------------------------------------------------------*/
5043 #if ( configNUMBER_OF_CORES == 1 )
5044 void vTaskSwitchContext( void )
5046 traceENTER_vTaskSwitchContext();
5048 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5050 /* The scheduler is currently suspended - do not allow a context
5052 xYieldPendings[ 0 ] = pdTRUE;
5056 xYieldPendings[ 0 ] = pdFALSE;
5057 traceTASK_SWITCHED_OUT();
5059 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5061 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5062 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
5064 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
5067 /* Add the amount of time the task has been running to the
5068 * accumulated time so far. The time the task started running was
5069 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5070 * protection here so count values are only valid until the timer
5071 * overflows. The guard against negative values is to protect
5072 * against suspect run time stat counter implementations - which
5073 * are provided by the application, not the kernel. */
5074 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
5076 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
5080 mtCOVERAGE_TEST_MARKER();
5083 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
5085 #endif /* configGENERATE_RUN_TIME_STATS */
5087 /* Check for stack overflow, if configured. */
5088 taskCHECK_FOR_STACK_OVERFLOW();
5090 /* Before the currently running task is switched out, save its errno. */
5091 #if ( configUSE_POSIX_ERRNO == 1 )
5093 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
5097 /* Select a new task to run using either the generic C or port
5098 * optimised asm code. */
5099 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5100 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5101 /* coverity[misra_c_2012_rule_11_5_violation] */
5102 taskSELECT_HIGHEST_PRIORITY_TASK();
5103 traceTASK_SWITCHED_IN();
5105 /* Macro to inject port specific behaviour immediately after
5106 * switching tasks, such as setting an end of stack watchpoint
5107 * or reconfiguring the MPU. */
5108 portTASK_SWITCH_HOOK( pxCurrentTCB );
5110 /* After the new task is switched in, update the global errno. */
5111 #if ( configUSE_POSIX_ERRNO == 1 )
5113 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
5117 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5119 /* Switch C-Runtime's TLS Block to point to the TLS
5120 * Block specific to this task. */
5121 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
5126 traceRETURN_vTaskSwitchContext();
5128 #else /* if ( configNUMBER_OF_CORES == 1 ) */
5129 void vTaskSwitchContext( BaseType_t xCoreID )
5131 traceENTER_vTaskSwitchContext();
5133 /* Acquire both locks:
5134 * - The ISR lock protects the ready list from simultaneous access by
5135 * both other ISRs and tasks.
5136 * - We also take the task lock to pause here in case another core has
5137 * suspended the scheduler. We don't want to simply set xYieldPending
5138 * and move on if another core suspended the scheduler. We should only
5139 * do that if the current core has suspended the scheduler. */
5141 portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
5144 /* vTaskSwitchContext() must never be called from within a critical section.
5145 * This is not necessarily true for single core FreeRTOS, but it is for this
5147 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
5149 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5151 /* The scheduler is currently suspended - do not allow a context
5153 xYieldPendings[ xCoreID ] = pdTRUE;
5157 xYieldPendings[ xCoreID ] = pdFALSE;
5158 traceTASK_SWITCHED_OUT();
5160 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5162 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5163 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
5165 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
5168 /* Add the amount of time the task has been running to the
5169 * accumulated time so far. The time the task started running was
5170 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5171 * protection here so count values are only valid until the timer
5172 * overflows. The guard against negative values is to protect
5173 * against suspect run time stat counter implementations - which
5174 * are provided by the application, not the kernel. */
5175 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
5177 pxCurrentTCBs[ xCoreID ]->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
5181 mtCOVERAGE_TEST_MARKER();
5184 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
5186 #endif /* configGENERATE_RUN_TIME_STATS */
5188 /* Check for stack overflow, if configured. */
5189 taskCHECK_FOR_STACK_OVERFLOW();
5191 /* Before the currently running task is switched out, save its errno. */
5192 #if ( configUSE_POSIX_ERRNO == 1 )
5194 pxCurrentTCBs[ xCoreID ]->iTaskErrno = FreeRTOS_errno;
5198 /* Select a new task to run. */
5199 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
5200 traceTASK_SWITCHED_IN();
5202 /* Macro to inject port specific behaviour immediately after
5203 * switching tasks, such as setting an end of stack watchpoint
5204 * or reconfiguring the MPU. */
5205 portTASK_SWITCH_HOOK( pxCurrentTCBs[ portGET_CORE_ID() ] );
5207 /* After the new task is switched in, update the global errno. */
5208 #if ( configUSE_POSIX_ERRNO == 1 )
5210 FreeRTOS_errno = pxCurrentTCBs[ xCoreID ]->iTaskErrno;
5214 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5216 /* Switch C-Runtime's TLS Block to point to the TLS
5217 * Block specific to this task. */
5218 configSET_TLS_BLOCK( pxCurrentTCBs[ xCoreID ]->xTLSBlock );
5223 portRELEASE_ISR_LOCK();
5224 portRELEASE_TASK_LOCK();
5226 traceRETURN_vTaskSwitchContext();
5228 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
5229 /*-----------------------------------------------------------*/
5231 void vTaskPlaceOnEventList( List_t * const pxEventList,
5232 const TickType_t xTicksToWait )
5234 traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
5236 configASSERT( pxEventList );
5238 /* THIS FUNCTION MUST BE CALLED WITH THE
5239 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
5241 /* Place the event list item of the TCB in the appropriate event list.
5242 * This is placed in the list in priority order so the highest priority task
5243 * is the first to be woken by the event.
5245 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
5246 * Normally, the xItemValue of a TCB's ListItem_t members is:
5247 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
5248 * Therefore, the event list is sorted in descending priority order.
5250 * The queue that contains the event list is locked, preventing
5251 * simultaneous access from interrupts. */
5252 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5254 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5256 traceRETURN_vTaskPlaceOnEventList();
5258 /*-----------------------------------------------------------*/
5260 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
5261 const TickType_t xItemValue,
5262 const TickType_t xTicksToWait )
5264 traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
5266 configASSERT( pxEventList );
5268 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5269 * the event groups implementation. */
5270 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5272 /* Store the item value in the event list item. It is safe to access the
5273 * event list item here as interrupts won't access the event list item of a
5274 * task that is not in the Blocked state. */
5275 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5277 /* Place the event list item of the TCB at the end of the appropriate event
5278 * list. It is safe to access the event list here because it is part of an
5279 * event group implementation - and interrupts don't access event groups
5280 * directly (instead they access them indirectly by pending function calls to
5281 * the task level). */
5282 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5284 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5286 traceRETURN_vTaskPlaceOnUnorderedEventList();
5288 /*-----------------------------------------------------------*/
5290 #if ( configUSE_TIMERS == 1 )
5292 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
5293 TickType_t xTicksToWait,
5294 const BaseType_t xWaitIndefinitely )
5296 traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
5298 configASSERT( pxEventList );
5300 /* This function should not be called by application code hence the
5301 * 'Restricted' in its name. It is not part of the public API. It is
5302 * designed for use by kernel code, and has special calling requirements -
5303 * it should be called with the scheduler suspended. */
5306 /* Place the event list item of the TCB in the appropriate event list.
5307 * In this case it is assume that this is the only task that is going to
5308 * be waiting on this event list, so the faster vListInsertEnd() function
5309 * can be used in place of vListInsert. */
5310 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5312 /* If the task should block indefinitely then set the block time to a
5313 * value that will be recognised as an indefinite delay inside the
5314 * prvAddCurrentTaskToDelayedList() function. */
5315 if( xWaitIndefinitely != pdFALSE )
5317 xTicksToWait = portMAX_DELAY;
5320 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
5321 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
5323 traceRETURN_vTaskPlaceOnEventListRestricted();
5326 #endif /* configUSE_TIMERS */
5327 /*-----------------------------------------------------------*/
5329 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
5331 TCB_t * pxUnblockedTCB;
5334 traceENTER_xTaskRemoveFromEventList( pxEventList );
5336 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
5337 * called from a critical section within an ISR. */
5339 /* The event list is sorted in priority order, so the first in the list can
5340 * be removed as it is known to be the highest priority. Remove the TCB from
5341 * the delayed list, and add it to the ready list.
5343 * If an event is for a queue that is locked then this function will never
5344 * get called - the lock count on the queue will get modified instead. This
5345 * means exclusive access to the event list is guaranteed here.
5347 * This function assumes that a check has already been made to ensure that
5348 * pxEventList is not empty. */
5349 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5350 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5351 /* coverity[misra_c_2012_rule_11_5_violation] */
5352 pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
5353 configASSERT( pxUnblockedTCB );
5354 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5356 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5358 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5359 prvAddTaskToReadyList( pxUnblockedTCB );
5361 #if ( configUSE_TICKLESS_IDLE != 0 )
5363 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5364 * might be set to the blocked task's time out time. If the task is
5365 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5366 * normally left unchanged, because it is automatically reset to a new
5367 * value when the tick count equals xNextTaskUnblockTime. However if
5368 * tickless idling is used it might be more important to enter sleep mode
5369 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5370 * ensure it is updated at the earliest possible time. */
5371 prvResetNextTaskUnblockTime();
5377 /* The delayed and ready lists cannot be accessed, so hold this task
5378 * pending until the scheduler is resumed. */
5379 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5382 #if ( configNUMBER_OF_CORES == 1 )
5384 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5386 /* Return true if the task removed from the event list has a higher
5387 * priority than the calling task. This allows the calling task to know if
5388 * it should force a context switch now. */
5391 /* Mark that a yield is pending in case the user is not using the
5392 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5393 xYieldPendings[ 0 ] = pdTRUE;
5400 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5404 #if ( configUSE_PREEMPTION == 1 )
5406 prvYieldForTask( pxUnblockedTCB );
5408 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5413 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5415 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5417 traceRETURN_xTaskRemoveFromEventList( xReturn );
5420 /*-----------------------------------------------------------*/
5422 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5423 const TickType_t xItemValue )
5425 TCB_t * pxUnblockedTCB;
5427 traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5429 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5430 * the event flags implementation. */
5431 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5433 /* Store the new item value in the event list. */
5434 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5436 /* Remove the event list form the event flag. Interrupts do not access
5438 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5439 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5440 /* coverity[misra_c_2012_rule_11_5_violation] */
5441 pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem );
5442 configASSERT( pxUnblockedTCB );
5443 listREMOVE_ITEM( pxEventListItem );
5445 #if ( configUSE_TICKLESS_IDLE != 0 )
5447 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5448 * might be set to the blocked task's time out time. If the task is
5449 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5450 * normally left unchanged, because it is automatically reset to a new
5451 * value when the tick count equals xNextTaskUnblockTime. However if
5452 * tickless idling is used it might be more important to enter sleep mode
5453 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5454 * ensure it is updated at the earliest possible time. */
5455 prvResetNextTaskUnblockTime();
5459 /* Remove the task from the delayed list and add it to the ready list. The
5460 * scheduler is suspended so interrupts will not be accessing the ready
5462 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5463 prvAddTaskToReadyList( pxUnblockedTCB );
5465 #if ( configNUMBER_OF_CORES == 1 )
5467 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5469 /* The unblocked task has a priority above that of the calling task, so
5470 * a context switch is required. This function is called with the
5471 * scheduler suspended so xYieldPending is set so the context switch
5472 * occurs immediately that the scheduler is resumed (unsuspended). */
5473 xYieldPendings[ 0 ] = pdTRUE;
5476 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5478 #if ( configUSE_PREEMPTION == 1 )
5480 taskENTER_CRITICAL();
5482 prvYieldForTask( pxUnblockedTCB );
5484 taskEXIT_CRITICAL();
5488 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5490 traceRETURN_vTaskRemoveFromUnorderedEventList();
5492 /*-----------------------------------------------------------*/
5494 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5496 traceENTER_vTaskSetTimeOutState( pxTimeOut );
5498 configASSERT( pxTimeOut );
5499 taskENTER_CRITICAL();
5501 pxTimeOut->xOverflowCount = xNumOfOverflows;
5502 pxTimeOut->xTimeOnEntering = xTickCount;
5504 taskEXIT_CRITICAL();
5506 traceRETURN_vTaskSetTimeOutState();
5508 /*-----------------------------------------------------------*/
5510 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5512 traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5514 /* For internal use only as it does not use a critical section. */
5515 pxTimeOut->xOverflowCount = xNumOfOverflows;
5516 pxTimeOut->xTimeOnEntering = xTickCount;
5518 traceRETURN_vTaskInternalSetTimeOutState();
5520 /*-----------------------------------------------------------*/
5522 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5523 TickType_t * const pxTicksToWait )
5527 traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5529 configASSERT( pxTimeOut );
5530 configASSERT( pxTicksToWait );
5532 taskENTER_CRITICAL();
5534 /* Minor optimisation. The tick count cannot change in this block. */
5535 const TickType_t xConstTickCount = xTickCount;
5536 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5538 #if ( INCLUDE_xTaskAbortDelay == 1 )
5539 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5541 /* The delay was aborted, which is not the same as a time out,
5542 * but has the same result. */
5543 pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
5549 #if ( INCLUDE_vTaskSuspend == 1 )
5550 if( *pxTicksToWait == portMAX_DELAY )
5552 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5553 * specified is the maximum block time then the task should block
5554 * indefinitely, and therefore never time out. */
5560 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) )
5562 /* The tick count is greater than the time at which
5563 * vTaskSetTimeout() was called, but has also overflowed since
5564 * vTaskSetTimeOut() was called. It must have wrapped all the way
5565 * around and gone past again. This passed since vTaskSetTimeout()
5568 *pxTicksToWait = ( TickType_t ) 0;
5570 else if( xElapsedTime < *pxTicksToWait )
5572 /* Not a genuine timeout. Adjust parameters for time remaining. */
5573 *pxTicksToWait -= xElapsedTime;
5574 vTaskInternalSetTimeOutState( pxTimeOut );
5579 *pxTicksToWait = ( TickType_t ) 0;
5583 taskEXIT_CRITICAL();
5585 traceRETURN_xTaskCheckForTimeOut( xReturn );
5589 /*-----------------------------------------------------------*/
5591 void vTaskMissedYield( void )
5593 traceENTER_vTaskMissedYield();
5595 /* Must be called from within a critical section. */
5596 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5598 traceRETURN_vTaskMissedYield();
5600 /*-----------------------------------------------------------*/
5602 #if ( configUSE_TRACE_FACILITY == 1 )
5604 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5606 UBaseType_t uxReturn;
5607 TCB_t const * pxTCB;
5609 traceENTER_uxTaskGetTaskNumber( xTask );
5614 uxReturn = pxTCB->uxTaskNumber;
5621 traceRETURN_uxTaskGetTaskNumber( uxReturn );
5626 #endif /* configUSE_TRACE_FACILITY */
5627 /*-----------------------------------------------------------*/
5629 #if ( configUSE_TRACE_FACILITY == 1 )
5631 void vTaskSetTaskNumber( TaskHandle_t xTask,
5632 const UBaseType_t uxHandle )
5636 traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5641 pxTCB->uxTaskNumber = uxHandle;
5644 traceRETURN_vTaskSetTaskNumber();
5647 #endif /* configUSE_TRACE_FACILITY */
5648 /*-----------------------------------------------------------*/
5651 * -----------------------------------------------------------
5652 * The passive idle task.
5653 * ----------------------------------------------------------
5655 * The passive idle task is used for all the additional cores in a SMP
5656 * system. There must be only 1 active idle task and the rest are passive
5659 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5660 * language extensions. The equivalent prototype for this function is:
5662 * void prvPassiveIdleTask( void *pvParameters );
5665 #if ( configNUMBER_OF_CORES > 1 )
5666 static portTASK_FUNCTION( prvPassiveIdleTask, pvParameters )
5668 ( void ) pvParameters;
5672 for( ; configCONTROL_INFINITE_LOOP(); )
5674 #if ( configUSE_PREEMPTION == 0 )
5676 /* If we are not using preemption we keep forcing a task switch to
5677 * see if any other task has become available. If we are using
5678 * preemption we don't need to do this as any task becoming available
5679 * will automatically get the processor anyway. */
5682 #endif /* configUSE_PREEMPTION */
5684 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5686 /* When using preemption tasks of equal priority will be
5687 * timesliced. If a task that is sharing the idle priority is ready
5688 * to run then the idle task should yield before the end of the
5691 * A critical region is not required here as we are just reading from
5692 * the list, and an occasional incorrect value will not matter. If
5693 * the ready list at the idle priority contains one more task than the
5694 * number of idle tasks, which is equal to the configured numbers of cores
5695 * then a task other than the idle task is ready to execute. */
5696 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5702 mtCOVERAGE_TEST_MARKER();
5705 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5707 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
5709 /* Call the user defined function from within the idle task. This
5710 * allows the application designer to add background functionality
5711 * without the overhead of a separate task.
5713 * This hook is intended to manage core activity such as disabling cores that go idle.
5715 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5716 * CALL A FUNCTION THAT MIGHT BLOCK. */
5717 vApplicationPassiveIdleHook();
5719 #endif /* configUSE_PASSIVE_IDLE_HOOK */
5722 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5725 * -----------------------------------------------------------
5727 * ----------------------------------------------------------
5729 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5730 * language extensions. The equivalent prototype for this function is:
5732 * void prvIdleTask( void *pvParameters );
5736 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5738 /* Stop warnings. */
5739 ( void ) pvParameters;
5741 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5742 * SCHEDULER IS STARTED. **/
5744 /* In case a task that has a secure context deletes itself, in which case
5745 * the idle task is responsible for deleting the task's secure context, if
5747 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5749 #if ( configNUMBER_OF_CORES > 1 )
5751 /* SMP all cores start up in the idle task. This initial yield gets the application
5755 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5757 for( ; configCONTROL_INFINITE_LOOP(); )
5759 /* See if any tasks have deleted themselves - if so then the idle task
5760 * is responsible for freeing the deleted task's TCB and stack. */
5761 prvCheckTasksWaitingTermination();
5763 #if ( configUSE_PREEMPTION == 0 )
5765 /* If we are not using preemption we keep forcing a task switch to
5766 * see if any other task has become available. If we are using
5767 * preemption we don't need to do this as any task becoming available
5768 * will automatically get the processor anyway. */
5771 #endif /* configUSE_PREEMPTION */
5773 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5775 /* When using preemption tasks of equal priority will be
5776 * timesliced. If a task that is sharing the idle priority is ready
5777 * to run then the idle task should yield before the end of the
5780 * A critical region is not required here as we are just reading from
5781 * the list, and an occasional incorrect value will not matter. If
5782 * the ready list at the idle priority contains one more task than the
5783 * number of idle tasks, which is equal to the configured numbers of cores
5784 * then a task other than the idle task is ready to execute. */
5785 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5791 mtCOVERAGE_TEST_MARKER();
5794 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5796 #if ( configUSE_IDLE_HOOK == 1 )
5798 /* Call the user defined function from within the idle task. */
5799 vApplicationIdleHook();
5801 #endif /* configUSE_IDLE_HOOK */
5803 /* This conditional compilation should use inequality to 0, not equality
5804 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5805 * user defined low power mode implementations require
5806 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5807 #if ( configUSE_TICKLESS_IDLE != 0 )
5809 TickType_t xExpectedIdleTime;
5811 /* It is not desirable to suspend then resume the scheduler on
5812 * each iteration of the idle task. Therefore, a preliminary
5813 * test of the expected idle time is performed without the
5814 * scheduler suspended. The result here is not necessarily
5816 xExpectedIdleTime = prvGetExpectedIdleTime();
5818 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5822 /* Now the scheduler is suspended, the expected idle
5823 * time can be sampled again, and this time its value can
5825 configASSERT( xNextTaskUnblockTime >= xTickCount );
5826 xExpectedIdleTime = prvGetExpectedIdleTime();
5828 /* Define the following macro to set xExpectedIdleTime to 0
5829 * if the application does not want
5830 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5831 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5833 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5835 traceLOW_POWER_IDLE_BEGIN();
5836 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5837 traceLOW_POWER_IDLE_END();
5841 mtCOVERAGE_TEST_MARKER();
5844 ( void ) xTaskResumeAll();
5848 mtCOVERAGE_TEST_MARKER();
5851 #endif /* configUSE_TICKLESS_IDLE */
5853 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) )
5855 /* Call the user defined function from within the idle task. This
5856 * allows the application designer to add background functionality
5857 * without the overhead of a separate task.
5859 * This hook is intended to manage core activity such as disabling cores that go idle.
5861 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5862 * CALL A FUNCTION THAT MIGHT BLOCK. */
5863 vApplicationPassiveIdleHook();
5865 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) ) */
5868 /*-----------------------------------------------------------*/
5870 #if ( configUSE_TICKLESS_IDLE != 0 )
5872 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5874 #if ( INCLUDE_vTaskSuspend == 1 )
5875 /* The idle task exists in addition to the application tasks. */
5876 const UBaseType_t uxNonApplicationTasks = configNUMBER_OF_CORES;
5877 #endif /* INCLUDE_vTaskSuspend */
5879 eSleepModeStatus eReturn = eStandardSleep;
5881 traceENTER_eTaskConfirmSleepModeStatus();
5883 /* This function must be called from a critical section. */
5885 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0U )
5887 /* A task was made ready while the scheduler was suspended. */
5888 eReturn = eAbortSleep;
5890 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5892 /* A yield was pended while the scheduler was suspended. */
5893 eReturn = eAbortSleep;
5895 else if( xPendedTicks != 0U )
5897 /* A tick interrupt has already occurred but was held pending
5898 * because the scheduler is suspended. */
5899 eReturn = eAbortSleep;
5902 #if ( INCLUDE_vTaskSuspend == 1 )
5903 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5905 /* If all the tasks are in the suspended list (which might mean they
5906 * have an infinite block time rather than actually being suspended)
5907 * then it is safe to turn all clocks off and just wait for external
5909 eReturn = eNoTasksWaitingTimeout;
5911 #endif /* INCLUDE_vTaskSuspend */
5914 mtCOVERAGE_TEST_MARKER();
5917 traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5922 #endif /* configUSE_TICKLESS_IDLE */
5923 /*-----------------------------------------------------------*/
5925 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5927 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5933 traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
5935 if( ( xIndex >= 0 ) &&
5936 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5938 pxTCB = prvGetTCBFromHandle( xTaskToSet );
5939 configASSERT( pxTCB != NULL );
5940 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5943 traceRETURN_vTaskSetThreadLocalStoragePointer();
5946 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5947 /*-----------------------------------------------------------*/
5949 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5951 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5954 void * pvReturn = NULL;
5957 traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
5959 if( ( xIndex >= 0 ) &&
5960 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5962 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
5963 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
5970 traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
5975 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5976 /*-----------------------------------------------------------*/
5978 #if ( portUSING_MPU_WRAPPERS == 1 )
5980 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
5981 const MemoryRegion_t * const pxRegions )
5985 traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
5987 /* If null is passed in here then we are modifying the MPU settings of
5988 * the calling task. */
5989 pxTCB = prvGetTCBFromHandle( xTaskToModify );
5991 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
5993 traceRETURN_vTaskAllocateMPURegions();
5996 #endif /* portUSING_MPU_WRAPPERS */
5997 /*-----------------------------------------------------------*/
5999 static void prvInitialiseTaskLists( void )
6001 UBaseType_t uxPriority;
6003 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
6005 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
6008 vListInitialise( &xDelayedTaskList1 );
6009 vListInitialise( &xDelayedTaskList2 );
6010 vListInitialise( &xPendingReadyList );
6012 #if ( INCLUDE_vTaskDelete == 1 )
6014 vListInitialise( &xTasksWaitingTermination );
6016 #endif /* INCLUDE_vTaskDelete */
6018 #if ( INCLUDE_vTaskSuspend == 1 )
6020 vListInitialise( &xSuspendedTaskList );
6022 #endif /* INCLUDE_vTaskSuspend */
6024 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
6026 pxDelayedTaskList = &xDelayedTaskList1;
6027 pxOverflowDelayedTaskList = &xDelayedTaskList2;
6029 /*-----------------------------------------------------------*/
6031 static void prvCheckTasksWaitingTermination( void )
6033 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
6035 #if ( INCLUDE_vTaskDelete == 1 )
6039 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
6040 * being called too often in the idle task. */
6041 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6043 #if ( configNUMBER_OF_CORES == 1 )
6045 taskENTER_CRITICAL();
6048 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6049 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6050 /* coverity[misra_c_2012_rule_11_5_violation] */
6051 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6052 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6053 --uxCurrentNumberOfTasks;
6054 --uxDeletedTasksWaitingCleanUp;
6057 taskEXIT_CRITICAL();
6059 prvDeleteTCB( pxTCB );
6061 #else /* #if( configNUMBER_OF_CORES == 1 ) */
6065 taskENTER_CRITICAL();
6067 /* For SMP, multiple idles can be running simultaneously
6068 * and we need to check that other idles did not cleanup while we were
6069 * waiting to enter the critical section. */
6070 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6072 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6073 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6074 /* coverity[misra_c_2012_rule_11_5_violation] */
6075 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6077 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
6079 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6080 --uxCurrentNumberOfTasks;
6081 --uxDeletedTasksWaitingCleanUp;
6085 /* The TCB to be deleted still has not yet been switched out
6086 * by the scheduler, so we will just exit this loop early and
6087 * try again next time. */
6088 taskEXIT_CRITICAL();
6093 taskEXIT_CRITICAL();
6097 prvDeleteTCB( pxTCB );
6100 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
6103 #endif /* INCLUDE_vTaskDelete */
6105 /*-----------------------------------------------------------*/
6107 #if ( configUSE_TRACE_FACILITY == 1 )
6109 void vTaskGetInfo( TaskHandle_t xTask,
6110 TaskStatus_t * pxTaskStatus,
6111 BaseType_t xGetFreeStackSpace,
6116 traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
6118 /* xTask is NULL then get the state of the calling task. */
6119 pxTCB = prvGetTCBFromHandle( xTask );
6121 pxTaskStatus->xHandle = pxTCB;
6122 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
6123 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
6124 pxTaskStatus->pxStackBase = pxTCB->pxStack;
6125 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
6126 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
6127 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
6129 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
6131 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
6133 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
6137 #if ( configUSE_MUTEXES == 1 )
6139 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
6143 pxTaskStatus->uxBasePriority = 0;
6147 #if ( configGENERATE_RUN_TIME_STATS == 1 )
6149 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
6153 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
6157 /* Obtaining the task state is a little fiddly, so is only done if the
6158 * value of eState passed into this function is eInvalid - otherwise the
6159 * state is just set to whatever is passed in. */
6160 if( eState != eInvalid )
6162 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6164 pxTaskStatus->eCurrentState = eRunning;
6168 pxTaskStatus->eCurrentState = eState;
6170 #if ( INCLUDE_vTaskSuspend == 1 )
6172 /* If the task is in the suspended list then there is a
6173 * chance it is actually just blocked indefinitely - so really
6174 * it should be reported as being in the Blocked state. */
6175 if( eState == eSuspended )
6179 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
6181 pxTaskStatus->eCurrentState = eBlocked;
6185 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6189 /* The task does not appear on the event list item of
6190 * and of the RTOS objects, but could still be in the
6191 * blocked state if it is waiting on its notification
6192 * rather than waiting on an object. If not, is
6194 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
6196 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
6198 pxTaskStatus->eCurrentState = eBlocked;
6203 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
6206 ( void ) xTaskResumeAll();
6209 #endif /* INCLUDE_vTaskSuspend */
6211 /* Tasks can be in pending ready list and other state list at the
6212 * same time. These tasks are in ready state no matter what state
6213 * list the task is in. */
6214 taskENTER_CRITICAL();
6216 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
6218 pxTaskStatus->eCurrentState = eReady;
6221 taskEXIT_CRITICAL();
6226 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
6229 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
6230 * parameter is provided to allow it to be skipped. */
6231 if( xGetFreeStackSpace != pdFALSE )
6233 #if ( portSTACK_GROWTH > 0 )
6235 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
6239 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
6245 pxTaskStatus->usStackHighWaterMark = 0;
6248 traceRETURN_vTaskGetInfo();
6251 #endif /* configUSE_TRACE_FACILITY */
6252 /*-----------------------------------------------------------*/
6254 #if ( configUSE_TRACE_FACILITY == 1 )
6256 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
6260 UBaseType_t uxTask = 0;
6261 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
6262 ListItem_t * pxIterator;
6263 TCB_t * pxTCB = NULL;
6265 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
6267 /* Populate an TaskStatus_t structure within the
6268 * pxTaskStatusArray array for each task that is referenced from
6269 * pxList. See the definition of TaskStatus_t in task.h for the
6270 * meaning of each TaskStatus_t structure member. */
6271 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
6273 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6274 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6275 /* coverity[misra_c_2012_rule_11_5_violation] */
6276 pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
6278 vTaskGetInfo( ( TaskHandle_t ) pxTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
6284 mtCOVERAGE_TEST_MARKER();
6290 #endif /* configUSE_TRACE_FACILITY */
6291 /*-----------------------------------------------------------*/
6293 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
6295 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
6297 configSTACK_DEPTH_TYPE uxCount = 0U;
6299 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
6301 pucStackByte -= portSTACK_GROWTH;
6305 uxCount /= ( configSTACK_DEPTH_TYPE ) sizeof( StackType_t );
6310 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
6311 /*-----------------------------------------------------------*/
6313 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
6315 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
6316 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
6317 * user to determine the return type. It gets around the problem of the value
6318 * overflowing on 8-bit types without breaking backward compatibility for
6319 * applications that expect an 8-bit return type. */
6320 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
6323 uint8_t * pucEndOfStack;
6324 configSTACK_DEPTH_TYPE uxReturn;
6326 traceENTER_uxTaskGetStackHighWaterMark2( xTask );
6328 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
6329 * the same except for their return type. Using configSTACK_DEPTH_TYPE
6330 * allows the user to determine the return type. It gets around the
6331 * problem of the value overflowing on 8-bit types without breaking
6332 * backward compatibility for applications that expect an 8-bit return
6335 pxTCB = prvGetTCBFromHandle( xTask );
6337 #if portSTACK_GROWTH < 0
6339 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6343 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6347 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
6349 traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
6354 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
6355 /*-----------------------------------------------------------*/
6357 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
6359 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
6362 uint8_t * pucEndOfStack;
6363 UBaseType_t uxReturn;
6365 traceENTER_uxTaskGetStackHighWaterMark( xTask );
6367 pxTCB = prvGetTCBFromHandle( xTask );
6369 #if portSTACK_GROWTH < 0
6371 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6375 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6379 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
6381 traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6386 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6387 /*-----------------------------------------------------------*/
6389 #if ( INCLUDE_vTaskDelete == 1 )
6391 static void prvDeleteTCB( TCB_t * pxTCB )
6393 /* This call is required specifically for the TriCore port. It must be
6394 * above the vPortFree() calls. The call is also used by ports/demos that
6395 * want to allocate and clean RAM statically. */
6396 portCLEAN_UP_TCB( pxTCB );
6398 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6400 /* Free up the memory allocated for the task's TLS Block. */
6401 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6405 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6407 /* The task can only have been allocated dynamically - free both
6408 * the stack and TCB. */
6409 vPortFreeStack( pxTCB->pxStack );
6412 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
6414 /* The task could have been allocated statically or dynamically, so
6415 * check what was statically allocated before trying to free the
6417 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6419 /* Both the stack and TCB were allocated dynamically, so both
6421 vPortFreeStack( pxTCB->pxStack );
6424 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6426 /* Only the stack was statically allocated, so the TCB is the
6427 * only memory that must be freed. */
6432 /* Neither the stack nor the TCB were allocated dynamically, so
6433 * nothing needs to be freed. */
6434 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6435 mtCOVERAGE_TEST_MARKER();
6438 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6441 #endif /* INCLUDE_vTaskDelete */
6442 /*-----------------------------------------------------------*/
6444 static void prvResetNextTaskUnblockTime( void )
6446 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6448 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
6449 * the maximum possible value so it is extremely unlikely that the
6450 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6451 * there is an item in the delayed list. */
6452 xNextTaskUnblockTime = portMAX_DELAY;
6456 /* The new current delayed list is not empty, get the value of
6457 * the item at the head of the delayed list. This is the time at
6458 * which the task at the head of the delayed list should be removed
6459 * from the Blocked state. */
6460 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6463 /*-----------------------------------------------------------*/
6465 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6467 #if ( configNUMBER_OF_CORES == 1 )
6468 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6470 TaskHandle_t xReturn;
6472 traceENTER_xTaskGetCurrentTaskHandle();
6474 /* A critical section is not required as this is not called from
6475 * an interrupt and the current TCB will always be the same for any
6476 * individual execution thread. */
6477 xReturn = pxCurrentTCB;
6479 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6483 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6484 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6486 TaskHandle_t xReturn;
6487 UBaseType_t uxSavedInterruptStatus;
6489 traceENTER_xTaskGetCurrentTaskHandle();
6491 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6493 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6495 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6497 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6501 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6503 TaskHandle_t xTaskGetCurrentTaskHandleForCore( BaseType_t xCoreID )
6505 TaskHandle_t xReturn = NULL;
6507 traceENTER_xTaskGetCurrentTaskHandleForCore( xCoreID );
6509 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6511 #if ( configNUMBER_OF_CORES == 1 )
6512 xReturn = pxCurrentTCB;
6513 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6514 xReturn = pxCurrentTCBs[ xCoreID ];
6515 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6518 traceRETURN_xTaskGetCurrentTaskHandleForCore( xReturn );
6523 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
6524 /*-----------------------------------------------------------*/
6526 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6528 BaseType_t xTaskGetSchedulerState( void )
6532 traceENTER_xTaskGetSchedulerState();
6534 if( xSchedulerRunning == pdFALSE )
6536 xReturn = taskSCHEDULER_NOT_STARTED;
6540 #if ( configNUMBER_OF_CORES > 1 )
6541 taskENTER_CRITICAL();
6544 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6546 xReturn = taskSCHEDULER_RUNNING;
6550 xReturn = taskSCHEDULER_SUSPENDED;
6553 #if ( configNUMBER_OF_CORES > 1 )
6554 taskEXIT_CRITICAL();
6558 traceRETURN_xTaskGetSchedulerState( xReturn );
6563 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6564 /*-----------------------------------------------------------*/
6566 #if ( configUSE_MUTEXES == 1 )
6568 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6570 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6571 BaseType_t xReturn = pdFALSE;
6573 traceENTER_xTaskPriorityInherit( pxMutexHolder );
6575 /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6576 * inheritance is not applied in this scenario. */
6577 if( pxMutexHolder != NULL )
6579 /* If the holder of the mutex has a priority below the priority of
6580 * the task attempting to obtain the mutex then it will temporarily
6581 * inherit the priority of the task attempting to obtain the mutex. */
6582 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6584 /* Adjust the mutex holder state to account for its new
6585 * priority. Only reset the event list item value if the value is
6586 * not being used for anything else. */
6587 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0UL ) )
6589 listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority );
6593 mtCOVERAGE_TEST_MARKER();
6596 /* If the task being modified is in the ready state it will need
6597 * to be moved into a new list. */
6598 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6600 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6602 /* It is known that the task is in its ready list so
6603 * there is no need to check again and the port level
6604 * reset macro can be called directly. */
6605 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6609 mtCOVERAGE_TEST_MARKER();
6612 /* Inherit the priority before being moved into the new list. */
6613 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6614 prvAddTaskToReadyList( pxMutexHolderTCB );
6615 #if ( configNUMBER_OF_CORES > 1 )
6617 /* The priority of the task is raised. Yield for this task
6618 * if it is not running. */
6619 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6621 prvYieldForTask( pxMutexHolderTCB );
6624 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6628 /* Just inherit the priority. */
6629 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6632 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6634 /* Inheritance occurred. */
6639 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6641 /* The base priority of the mutex holder is lower than the
6642 * priority of the task attempting to take the mutex, but the
6643 * current priority of the mutex holder is not lower than the
6644 * priority of the task attempting to take the mutex.
6645 * Therefore the mutex holder must have already inherited a
6646 * priority, but inheritance would have occurred if that had
6647 * not been the case. */
6652 mtCOVERAGE_TEST_MARKER();
6658 mtCOVERAGE_TEST_MARKER();
6661 traceRETURN_xTaskPriorityInherit( xReturn );
6666 #endif /* configUSE_MUTEXES */
6667 /*-----------------------------------------------------------*/
6669 #if ( configUSE_MUTEXES == 1 )
6671 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6673 TCB_t * const pxTCB = pxMutexHolder;
6674 BaseType_t xReturn = pdFALSE;
6676 traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6678 if( pxMutexHolder != NULL )
6680 /* A task can only have an inherited priority if it holds the mutex.
6681 * If the mutex is held by a task then it cannot be given from an
6682 * interrupt, and if a mutex is given by the holding task then it must
6683 * be the running state task. */
6684 configASSERT( pxTCB == pxCurrentTCB );
6685 configASSERT( pxTCB->uxMutexesHeld );
6686 ( pxTCB->uxMutexesHeld )--;
6688 /* Has the holder of the mutex inherited the priority of another
6690 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6692 /* Only disinherit if no other mutexes are held. */
6693 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6695 /* A task can only have an inherited priority if it holds
6696 * the mutex. If the mutex is held by a task then it cannot be
6697 * given from an interrupt, and if a mutex is given by the
6698 * holding task then it must be the running state task. Remove
6699 * the holding task from the ready list. */
6700 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6702 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6706 mtCOVERAGE_TEST_MARKER();
6709 /* Disinherit the priority before adding the task into the
6710 * new ready list. */
6711 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6712 pxTCB->uxPriority = pxTCB->uxBasePriority;
6714 /* Reset the event list item value. It cannot be in use for
6715 * any other purpose if this task is running, and it must be
6716 * running to give back the mutex. */
6717 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority );
6718 prvAddTaskToReadyList( pxTCB );
6719 #if ( configNUMBER_OF_CORES > 1 )
6721 /* The priority of the task is dropped. Yield the core on
6722 * which the task is running. */
6723 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6725 prvYieldCore( pxTCB->xTaskRunState );
6728 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6730 /* Return true to indicate that a context switch is required.
6731 * This is only actually required in the corner case whereby
6732 * multiple mutexes were held and the mutexes were given back
6733 * in an order different to that in which they were taken.
6734 * If a context switch did not occur when the first mutex was
6735 * returned, even if a task was waiting on it, then a context
6736 * switch should occur when the last mutex is returned whether
6737 * a task is waiting on it or not. */
6742 mtCOVERAGE_TEST_MARKER();
6747 mtCOVERAGE_TEST_MARKER();
6752 mtCOVERAGE_TEST_MARKER();
6755 traceRETURN_xTaskPriorityDisinherit( xReturn );
6760 #endif /* configUSE_MUTEXES */
6761 /*-----------------------------------------------------------*/
6763 #if ( configUSE_MUTEXES == 1 )
6765 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6766 UBaseType_t uxHighestPriorityWaitingTask )
6768 TCB_t * const pxTCB = pxMutexHolder;
6769 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6770 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6772 traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6774 if( pxMutexHolder != NULL )
6776 /* If pxMutexHolder is not NULL then the holder must hold at least
6778 configASSERT( pxTCB->uxMutexesHeld );
6780 /* Determine the priority to which the priority of the task that
6781 * holds the mutex should be set. This will be the greater of the
6782 * holding task's base priority and the priority of the highest
6783 * priority task that is waiting to obtain the mutex. */
6784 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6786 uxPriorityToUse = uxHighestPriorityWaitingTask;
6790 uxPriorityToUse = pxTCB->uxBasePriority;
6793 /* Does the priority need to change? */
6794 if( pxTCB->uxPriority != uxPriorityToUse )
6796 /* Only disinherit if no other mutexes are held. This is a
6797 * simplification in the priority inheritance implementation. If
6798 * the task that holds the mutex is also holding other mutexes then
6799 * the other mutexes may have caused the priority inheritance. */
6800 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6802 /* If a task has timed out because it already holds the
6803 * mutex it was trying to obtain then it cannot of inherited
6804 * its own priority. */
6805 configASSERT( pxTCB != pxCurrentTCB );
6807 /* Disinherit the priority, remembering the previous
6808 * priority to facilitate determining the subject task's
6810 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6811 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6812 pxTCB->uxPriority = uxPriorityToUse;
6814 /* Only reset the event list item value if the value is not
6815 * being used for anything else. */
6816 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0UL ) )
6818 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse );
6822 mtCOVERAGE_TEST_MARKER();
6825 /* If the running task is not the task that holds the mutex
6826 * then the task that holds the mutex could be in either the
6827 * Ready, Blocked or Suspended states. Only remove the task
6828 * from its current state list if it is in the Ready state as
6829 * the task's priority is going to change and there is one
6830 * Ready list per priority. */
6831 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6833 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6835 /* It is known that the task is in its ready list so
6836 * there is no need to check again and the port level
6837 * reset macro can be called directly. */
6838 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6842 mtCOVERAGE_TEST_MARKER();
6845 prvAddTaskToReadyList( pxTCB );
6846 #if ( configNUMBER_OF_CORES > 1 )
6848 /* The priority of the task is dropped. Yield the core on
6849 * which the task is running. */
6850 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6852 prvYieldCore( pxTCB->xTaskRunState );
6855 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6859 mtCOVERAGE_TEST_MARKER();
6864 mtCOVERAGE_TEST_MARKER();
6869 mtCOVERAGE_TEST_MARKER();
6874 mtCOVERAGE_TEST_MARKER();
6877 traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6880 #endif /* configUSE_MUTEXES */
6881 /*-----------------------------------------------------------*/
6883 #if ( configNUMBER_OF_CORES > 1 )
6885 /* If not in a critical section then yield immediately.
6886 * Otherwise set xYieldPendings to true to wait to
6887 * yield until exiting the critical section.
6889 void vTaskYieldWithinAPI( void )
6891 traceENTER_vTaskYieldWithinAPI();
6893 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6899 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6902 traceRETURN_vTaskYieldWithinAPI();
6904 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6906 /*-----------------------------------------------------------*/
6908 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6910 void vTaskEnterCritical( void )
6912 traceENTER_vTaskEnterCritical();
6914 portDISABLE_INTERRUPTS();
6916 if( xSchedulerRunning != pdFALSE )
6918 ( pxCurrentTCB->uxCriticalNesting )++;
6920 /* This is not the interrupt safe version of the enter critical
6921 * function so assert() if it is being called from an interrupt
6922 * context. Only API functions that end in "FromISR" can be used in an
6923 * interrupt. Only assert if the critical nesting count is 1 to
6924 * protect against recursive calls if the assert function also uses a
6925 * critical section. */
6926 if( pxCurrentTCB->uxCriticalNesting == 1U )
6928 portASSERT_IF_IN_ISR();
6933 mtCOVERAGE_TEST_MARKER();
6936 traceRETURN_vTaskEnterCritical();
6939 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6940 /*-----------------------------------------------------------*/
6942 #if ( configNUMBER_OF_CORES > 1 )
6944 void vTaskEnterCritical( void )
6946 traceENTER_vTaskEnterCritical();
6948 portDISABLE_INTERRUPTS();
6950 if( xSchedulerRunning != pdFALSE )
6952 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6954 portGET_TASK_LOCK();
6958 portINCREMENT_CRITICAL_NESTING_COUNT();
6960 /* This is not the interrupt safe version of the enter critical
6961 * function so assert() if it is being called from an interrupt
6962 * context. Only API functions that end in "FromISR" can be used in an
6963 * interrupt. Only assert if the critical nesting count is 1 to
6964 * protect against recursive calls if the assert function also uses a
6965 * critical section. */
6966 if( portGET_CRITICAL_NESTING_COUNT() == 1U )
6968 portASSERT_IF_IN_ISR();
6970 if( uxSchedulerSuspended == 0U )
6972 /* The only time there would be a problem is if this is called
6973 * before a context switch and vTaskExitCritical() is called
6974 * after pxCurrentTCB changes. Therefore this should not be
6975 * used within vTaskSwitchContext(). */
6976 prvCheckForRunStateChange();
6982 mtCOVERAGE_TEST_MARKER();
6985 traceRETURN_vTaskEnterCritical();
6988 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6990 /*-----------------------------------------------------------*/
6992 #if ( configNUMBER_OF_CORES > 1 )
6994 UBaseType_t vTaskEnterCriticalFromISR( void )
6996 UBaseType_t uxSavedInterruptStatus = 0;
6998 traceENTER_vTaskEnterCriticalFromISR();
7000 if( xSchedulerRunning != pdFALSE )
7002 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
7004 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7009 portINCREMENT_CRITICAL_NESTING_COUNT();
7013 mtCOVERAGE_TEST_MARKER();
7016 traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
7018 return uxSavedInterruptStatus;
7021 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7022 /*-----------------------------------------------------------*/
7024 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
7026 void vTaskExitCritical( void )
7028 traceENTER_vTaskExitCritical();
7030 if( xSchedulerRunning != pdFALSE )
7032 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
7033 * does not match a previous call to vTaskEnterCritical(). */
7034 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
7036 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7037 * to exit critical section from ISR. */
7038 portASSERT_IF_IN_ISR();
7040 if( pxCurrentTCB->uxCriticalNesting > 0U )
7042 ( pxCurrentTCB->uxCriticalNesting )--;
7044 if( pxCurrentTCB->uxCriticalNesting == 0U )
7046 portENABLE_INTERRUPTS();
7050 mtCOVERAGE_TEST_MARKER();
7055 mtCOVERAGE_TEST_MARKER();
7060 mtCOVERAGE_TEST_MARKER();
7063 traceRETURN_vTaskExitCritical();
7066 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
7067 /*-----------------------------------------------------------*/
7069 #if ( configNUMBER_OF_CORES > 1 )
7071 void vTaskExitCritical( void )
7073 traceENTER_vTaskExitCritical();
7075 if( xSchedulerRunning != pdFALSE )
7077 /* If critical nesting count is zero then this function
7078 * does not match a previous call to vTaskEnterCritical(). */
7079 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7081 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7082 * to exit critical section from ISR. */
7083 portASSERT_IF_IN_ISR();
7085 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7087 portDECREMENT_CRITICAL_NESTING_COUNT();
7089 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7091 BaseType_t xYieldCurrentTask;
7093 /* Get the xYieldPending stats inside the critical section. */
7094 xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
7096 portRELEASE_ISR_LOCK();
7097 portRELEASE_TASK_LOCK();
7098 portENABLE_INTERRUPTS();
7100 /* When a task yields in a critical section it just sets
7101 * xYieldPending to true. So now that we have exited the
7102 * critical section check if xYieldPending is true, and
7104 if( xYieldCurrentTask != pdFALSE )
7111 mtCOVERAGE_TEST_MARKER();
7116 mtCOVERAGE_TEST_MARKER();
7121 mtCOVERAGE_TEST_MARKER();
7124 traceRETURN_vTaskExitCritical();
7127 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7128 /*-----------------------------------------------------------*/
7130 #if ( configNUMBER_OF_CORES > 1 )
7132 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
7134 traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
7136 if( xSchedulerRunning != pdFALSE )
7138 /* If critical nesting count is zero then this function
7139 * does not match a previous call to vTaskEnterCritical(). */
7140 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7142 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7144 portDECREMENT_CRITICAL_NESTING_COUNT();
7146 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7148 portRELEASE_ISR_LOCK();
7149 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
7153 mtCOVERAGE_TEST_MARKER();
7158 mtCOVERAGE_TEST_MARKER();
7163 mtCOVERAGE_TEST_MARKER();
7166 traceRETURN_vTaskExitCriticalFromISR();
7169 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7170 /*-----------------------------------------------------------*/
7172 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
7174 static char * prvWriteNameToBuffer( char * pcBuffer,
7175 const char * pcTaskName )
7179 /* Start by copying the entire string. */
7180 ( void ) strcpy( pcBuffer, pcTaskName );
7182 /* Pad the end of the string with spaces to ensure columns line up when
7184 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
7186 pcBuffer[ x ] = ' ';
7190 pcBuffer[ x ] = ( char ) 0x00;
7192 /* Return the new end of string. */
7193 return &( pcBuffer[ x ] );
7196 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
7197 /*-----------------------------------------------------------*/
7199 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
7201 void vTaskListTasks( char * pcWriteBuffer,
7202 size_t uxBufferLength )
7204 TaskStatus_t * pxTaskStatusArray;
7205 size_t uxConsumedBufferLength = 0;
7206 size_t uxCharsWrittenBySnprintf;
7207 int iSnprintfReturnValue;
7208 BaseType_t xOutputBufferFull = pdFALSE;
7209 UBaseType_t uxArraySize, x;
7212 traceENTER_vTaskListTasks( pcWriteBuffer, uxBufferLength );
7217 * This function is provided for convenience only, and is used by many
7218 * of the demo applications. Do not consider it to be part of the
7221 * vTaskListTasks() calls uxTaskGetSystemState(), then formats part of the
7222 * uxTaskGetSystemState() output into a human readable table that
7223 * displays task: names, states, priority, stack usage and task number.
7224 * Stack usage specified as the number of unused StackType_t words stack can hold
7225 * on top of stack - not the number of bytes.
7227 * vTaskListTasks() has a dependency on the snprintf() C library function that
7228 * might bloat the code size, use a lot of stack, and provide different
7229 * results on different platforms. An alternative, tiny, third party,
7230 * and limited functionality implementation of snprintf() is provided in
7231 * many of the FreeRTOS/Demo sub-directories in a file called
7232 * printf-stdarg.c (note printf-stdarg.c does not provide a full
7233 * snprintf() implementation!).
7235 * It is recommended that production systems call uxTaskGetSystemState()
7236 * directly to get access to raw stats data, rather than indirectly
7237 * through a call to vTaskListTasks().
7241 /* Make sure the write buffer does not contain a string. */
7242 *pcWriteBuffer = ( char ) 0x00;
7244 /* Take a snapshot of the number of tasks in case it changes while this
7245 * function is executing. */
7246 uxArraySize = uxCurrentNumberOfTasks;
7248 /* Allocate an array index for each task. NOTE! if
7249 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7250 * equate to NULL. */
7251 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7252 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7253 /* coverity[misra_c_2012_rule_11_5_violation] */
7254 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7256 if( pxTaskStatusArray != NULL )
7258 /* Generate the (binary) data. */
7259 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
7261 /* Create a human readable table from the binary data. */
7262 for( x = 0; x < uxArraySize; x++ )
7264 switch( pxTaskStatusArray[ x ].eCurrentState )
7267 cStatus = tskRUNNING_CHAR;
7271 cStatus = tskREADY_CHAR;
7275 cStatus = tskBLOCKED_CHAR;
7279 cStatus = tskSUSPENDED_CHAR;
7283 cStatus = tskDELETED_CHAR;
7286 case eInvalid: /* Fall through. */
7287 default: /* Should not get here, but it is included
7288 * to prevent static checking errors. */
7289 cStatus = ( char ) 0x00;
7293 /* Is there enough space in the buffer to hold task name? */
7294 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7296 /* Write the task name to the string, padding with spaces so it
7297 * can be printed in tabular form more easily. */
7298 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7299 /* Do not count the terminating null character. */
7300 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7302 /* Is there space left in the buffer? -1 is done because snprintf
7303 * writes a terminating null character. So we are essentially
7304 * checking if the buffer has space to write at least one non-null
7306 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7308 /* Write the rest of the string. */
7309 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
7310 /* MISRA Ref 21.6.1 [snprintf for utility] */
7311 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7312 /* coverity[misra_c_2012_rule_21_6_violation] */
7313 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7314 uxBufferLength - uxConsumedBufferLength,
7315 "\t%c\t%u\t%u\t%u\t0x%x\r\n",
7317 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7318 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7319 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber,
7320 ( unsigned int ) pxTaskStatusArray[ x ].uxCoreAffinityMask );
7321 #else /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7322 /* MISRA Ref 21.6.1 [snprintf for utility] */
7323 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7324 /* coverity[misra_c_2012_rule_21_6_violation] */
7325 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7326 uxBufferLength - uxConsumedBufferLength,
7327 "\t%c\t%u\t%u\t%u\r\n",
7329 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7330 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7331 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber );
7332 #endif /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7333 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7335 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7336 pcWriteBuffer += uxCharsWrittenBySnprintf;
7340 xOutputBufferFull = pdTRUE;
7345 xOutputBufferFull = pdTRUE;
7348 if( xOutputBufferFull == pdTRUE )
7354 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7355 * is 0 then vPortFree() will be #defined to nothing. */
7356 vPortFree( pxTaskStatusArray );
7360 mtCOVERAGE_TEST_MARKER();
7363 traceRETURN_vTaskListTasks();
7366 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7367 /*----------------------------------------------------------*/
7369 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
7371 void vTaskGetRunTimeStatistics( char * pcWriteBuffer,
7372 size_t uxBufferLength )
7374 TaskStatus_t * pxTaskStatusArray;
7375 size_t uxConsumedBufferLength = 0;
7376 size_t uxCharsWrittenBySnprintf;
7377 int iSnprintfReturnValue;
7378 BaseType_t xOutputBufferFull = pdFALSE;
7379 UBaseType_t uxArraySize, x;
7380 configRUN_TIME_COUNTER_TYPE ulTotalTime = 0;
7381 configRUN_TIME_COUNTER_TYPE ulStatsAsPercentage;
7383 traceENTER_vTaskGetRunTimeStatistics( pcWriteBuffer, uxBufferLength );
7388 * This function is provided for convenience only, and is used by many
7389 * of the demo applications. Do not consider it to be part of the
7392 * vTaskGetRunTimeStatistics() calls uxTaskGetSystemState(), then formats part
7393 * of the uxTaskGetSystemState() output into a human readable table that
7394 * displays the amount of time each task has spent in the Running state
7395 * in both absolute and percentage terms.
7397 * vTaskGetRunTimeStatistics() has a dependency on the snprintf() C library
7398 * function that might bloat the code size, use a lot of stack, and
7399 * provide different results on different platforms. An alternative,
7400 * tiny, third party, and limited functionality implementation of
7401 * snprintf() is provided in many of the FreeRTOS/Demo sub-directories in
7402 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
7403 * a full snprintf() implementation!).
7405 * It is recommended that production systems call uxTaskGetSystemState()
7406 * directly to get access to raw stats data, rather than indirectly
7407 * through a call to vTaskGetRunTimeStatistics().
7410 /* Make sure the write buffer does not contain a string. */
7411 *pcWriteBuffer = ( char ) 0x00;
7413 /* Take a snapshot of the number of tasks in case it changes while this
7414 * function is executing. */
7415 uxArraySize = uxCurrentNumberOfTasks;
7417 /* Allocate an array index for each task. NOTE! If
7418 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7419 * equate to NULL. */
7420 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7421 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7422 /* coverity[misra_c_2012_rule_11_5_violation] */
7423 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7425 if( pxTaskStatusArray != NULL )
7427 /* Generate the (binary) data. */
7428 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
7430 /* For percentage calculations. */
7431 ulTotalTime /= ( ( configRUN_TIME_COUNTER_TYPE ) 100UL );
7433 /* Avoid divide by zero errors. */
7434 if( ulTotalTime > 0UL )
7436 /* Create a human readable table from the binary data. */
7437 for( x = 0; x < uxArraySize; x++ )
7439 /* What percentage of the total run time has the task used?
7440 * This will always be rounded down to the nearest integer.
7441 * ulTotalRunTime has already been divided by 100. */
7442 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
7444 /* Is there enough space in the buffer to hold task name? */
7445 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7447 /* Write the task name to the string, padding with
7448 * spaces so it can be printed in tabular form more
7450 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7451 /* Do not count the terminating null character. */
7452 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7454 /* Is there space left in the buffer? -1 is done because snprintf
7455 * writes a terminating null character. So we are essentially
7456 * checking if the buffer has space to write at least one non-null
7458 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7460 if( ulStatsAsPercentage > 0UL )
7462 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7464 /* MISRA Ref 21.6.1 [snprintf for utility] */
7465 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7466 /* coverity[misra_c_2012_rule_21_6_violation] */
7467 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7468 uxBufferLength - uxConsumedBufferLength,
7469 "\t%lu\t\t%lu%%\r\n",
7470 pxTaskStatusArray[ x ].ulRunTimeCounter,
7471 ulStatsAsPercentage );
7473 #else /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7475 /* sizeof( int ) == sizeof( long ) so a smaller
7476 * printf() library can be used. */
7477 /* MISRA Ref 21.6.1 [snprintf for utility] */
7478 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7479 /* coverity[misra_c_2012_rule_21_6_violation] */
7480 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7481 uxBufferLength - uxConsumedBufferLength,
7483 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter,
7484 ( unsigned int ) ulStatsAsPercentage );
7486 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7490 /* If the percentage is zero here then the task has
7491 * consumed less than 1% of the total run time. */
7492 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7494 /* MISRA Ref 21.6.1 [snprintf for utility] */
7495 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7496 /* coverity[misra_c_2012_rule_21_6_violation] */
7497 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7498 uxBufferLength - uxConsumedBufferLength,
7499 "\t%lu\t\t<1%%\r\n",
7500 pxTaskStatusArray[ x ].ulRunTimeCounter );
7504 /* sizeof( int ) == sizeof( long ) so a smaller
7505 * printf() library can be used. */
7506 /* MISRA Ref 21.6.1 [snprintf for utility] */
7507 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7508 /* coverity[misra_c_2012_rule_21_6_violation] */
7509 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7510 uxBufferLength - uxConsumedBufferLength,
7512 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter );
7514 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7517 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7518 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7519 pcWriteBuffer += uxCharsWrittenBySnprintf;
7523 xOutputBufferFull = pdTRUE;
7528 xOutputBufferFull = pdTRUE;
7531 if( xOutputBufferFull == pdTRUE )
7539 mtCOVERAGE_TEST_MARKER();
7542 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7543 * is 0 then vPortFree() will be #defined to nothing. */
7544 vPortFree( pxTaskStatusArray );
7548 mtCOVERAGE_TEST_MARKER();
7551 traceRETURN_vTaskGetRunTimeStatistics();
7554 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7555 /*-----------------------------------------------------------*/
7557 TickType_t uxTaskResetEventItemValue( void )
7559 TickType_t uxReturn;
7561 traceENTER_uxTaskResetEventItemValue();
7563 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7565 /* Reset the event list item to its normal value - so it can be used with
7566 * queues and semaphores. */
7567 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) );
7569 traceRETURN_uxTaskResetEventItemValue( uxReturn );
7573 /*-----------------------------------------------------------*/
7575 #if ( configUSE_MUTEXES == 1 )
7577 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7581 traceENTER_pvTaskIncrementMutexHeldCount();
7583 pxTCB = pxCurrentTCB;
7585 /* If xSemaphoreCreateMutex() is called before any tasks have been created
7586 * then pxCurrentTCB will be NULL. */
7589 ( pxTCB->uxMutexesHeld )++;
7592 traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7597 #endif /* configUSE_MUTEXES */
7598 /*-----------------------------------------------------------*/
7600 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7602 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7603 BaseType_t xClearCountOnExit,
7604 TickType_t xTicksToWait )
7607 BaseType_t xAlreadyYielded, xShouldBlock = pdFALSE;
7609 traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7611 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7613 /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
7614 * non-deterministic operation. */
7617 /* We MUST enter a critical section to atomically check if a notification
7618 * has occurred and set the flag to indicate that we are waiting for
7619 * a notification. If we do not do so, a notification sent from an ISR
7621 taskENTER_CRITICAL();
7623 /* Only block if the notification count is not already non-zero. */
7624 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0UL )
7626 /* Mark this task as waiting for a notification. */
7627 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7629 if( xTicksToWait > ( TickType_t ) 0 )
7631 xShouldBlock = pdTRUE;
7635 mtCOVERAGE_TEST_MARKER();
7640 mtCOVERAGE_TEST_MARKER();
7643 taskEXIT_CRITICAL();
7645 /* We are now out of the critical section but the scheduler is still
7646 * suspended, so we are safe to do non-deterministic operations such
7647 * as prvAddCurrentTaskToDelayedList. */
7648 if( xShouldBlock == pdTRUE )
7650 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7651 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7655 mtCOVERAGE_TEST_MARKER();
7658 xAlreadyYielded = xTaskResumeAll();
7660 /* Force a reschedule if xTaskResumeAll has not already done so. */
7661 if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
7663 taskYIELD_WITHIN_API();
7667 mtCOVERAGE_TEST_MARKER();
7670 taskENTER_CRITICAL();
7672 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7673 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7675 if( ulReturn != 0UL )
7677 if( xClearCountOnExit != pdFALSE )
7679 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ( uint32_t ) 0UL;
7683 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7688 mtCOVERAGE_TEST_MARKER();
7691 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7693 taskEXIT_CRITICAL();
7695 traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7700 #endif /* configUSE_TASK_NOTIFICATIONS */
7701 /*-----------------------------------------------------------*/
7703 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7705 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7706 uint32_t ulBitsToClearOnEntry,
7707 uint32_t ulBitsToClearOnExit,
7708 uint32_t * pulNotificationValue,
7709 TickType_t xTicksToWait )
7711 BaseType_t xReturn, xAlreadyYielded, xShouldBlock = pdFALSE;
7713 traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7715 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7717 /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
7718 * non-deterministic operation. */
7721 /* We MUST enter a critical section to atomically check and update the
7722 * task notification value. If we do not do so, a notification from
7723 * an ISR will get lost. */
7724 taskENTER_CRITICAL();
7726 /* Only block if a notification is not already pending. */
7727 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7729 /* Clear bits in the task's notification value as bits may get
7730 * set by the notifying task or interrupt. This can be used
7731 * to clear the value to zero. */
7732 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7734 /* Mark this task as waiting for a notification. */
7735 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7737 if( xTicksToWait > ( TickType_t ) 0 )
7739 xShouldBlock = pdTRUE;
7743 mtCOVERAGE_TEST_MARKER();
7748 mtCOVERAGE_TEST_MARKER();
7751 taskEXIT_CRITICAL();
7753 /* We are now out of the critical section but the scheduler is still
7754 * suspended, so we are safe to do non-deterministic operations such
7755 * as prvAddCurrentTaskToDelayedList. */
7756 if( xShouldBlock == pdTRUE )
7758 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7759 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7763 mtCOVERAGE_TEST_MARKER();
7766 xAlreadyYielded = xTaskResumeAll();
7768 /* Force a reschedule if xTaskResumeAll has not already done so. */
7769 if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
7771 taskYIELD_WITHIN_API();
7775 mtCOVERAGE_TEST_MARKER();
7778 taskENTER_CRITICAL();
7780 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7782 if( pulNotificationValue != NULL )
7784 /* Output the current notification value, which may or may not
7786 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7789 /* If ucNotifyValue is set then either the task never entered the
7790 * blocked state (because a notification was already pending) or the
7791 * task unblocked because of a notification. Otherwise the task
7792 * unblocked because of a timeout. */
7793 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7795 /* A notification was not received. */
7800 /* A notification was already pending or a notification was
7801 * received while the task was waiting. */
7802 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7806 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7808 taskEXIT_CRITICAL();
7810 traceRETURN_xTaskGenericNotifyWait( xReturn );
7815 #endif /* configUSE_TASK_NOTIFICATIONS */
7816 /*-----------------------------------------------------------*/
7818 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7820 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7821 UBaseType_t uxIndexToNotify,
7823 eNotifyAction eAction,
7824 uint32_t * pulPreviousNotificationValue )
7827 BaseType_t xReturn = pdPASS;
7828 uint8_t ucOriginalNotifyState;
7830 traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7832 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7833 configASSERT( xTaskToNotify );
7834 pxTCB = xTaskToNotify;
7836 taskENTER_CRITICAL();
7838 if( pulPreviousNotificationValue != NULL )
7840 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7843 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7845 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7850 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7854 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7857 case eSetValueWithOverwrite:
7858 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7861 case eSetValueWithoutOverwrite:
7863 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7865 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7869 /* The value could not be written to the task. */
7877 /* The task is being notified without its notify value being
7883 /* Should not get here if all enums are handled.
7884 * Artificially force an assert by testing a value the
7885 * compiler can't assume is const. */
7886 configASSERT( xTickCount == ( TickType_t ) 0 );
7891 traceTASK_NOTIFY( uxIndexToNotify );
7893 /* If the task is in the blocked state specifically to wait for a
7894 * notification then unblock it now. */
7895 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7897 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7898 prvAddTaskToReadyList( pxTCB );
7900 /* The task should not have been on an event list. */
7901 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7903 #if ( configUSE_TICKLESS_IDLE != 0 )
7905 /* If a task is blocked waiting for a notification then
7906 * xNextTaskUnblockTime might be set to the blocked task's time
7907 * out time. If the task is unblocked for a reason other than
7908 * a timeout xNextTaskUnblockTime is normally left unchanged,
7909 * because it will automatically get reset to a new value when
7910 * the tick count equals xNextTaskUnblockTime. However if
7911 * tickless idling is used it might be more important to enter
7912 * sleep mode at the earliest possible time - so reset
7913 * xNextTaskUnblockTime here to ensure it is updated at the
7914 * earliest possible time. */
7915 prvResetNextTaskUnblockTime();
7919 /* Check if the notified task has a priority above the currently
7920 * executing task. */
7921 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7925 mtCOVERAGE_TEST_MARKER();
7928 taskEXIT_CRITICAL();
7930 traceRETURN_xTaskGenericNotify( xReturn );
7935 #endif /* configUSE_TASK_NOTIFICATIONS */
7936 /*-----------------------------------------------------------*/
7938 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7940 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
7941 UBaseType_t uxIndexToNotify,
7943 eNotifyAction eAction,
7944 uint32_t * pulPreviousNotificationValue,
7945 BaseType_t * pxHigherPriorityTaskWoken )
7948 uint8_t ucOriginalNotifyState;
7949 BaseType_t xReturn = pdPASS;
7950 UBaseType_t uxSavedInterruptStatus;
7952 traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
7954 configASSERT( xTaskToNotify );
7955 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7957 /* RTOS ports that support interrupt nesting have the concept of a
7958 * maximum system call (or maximum API call) interrupt priority.
7959 * Interrupts that are above the maximum system call priority are keep
7960 * permanently enabled, even when the RTOS kernel is in a critical section,
7961 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7962 * is defined in FreeRTOSConfig.h then
7963 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7964 * failure if a FreeRTOS API function is called from an interrupt that has
7965 * been assigned a priority above the configured maximum system call
7966 * priority. Only FreeRTOS functions that end in FromISR can be called
7967 * from interrupts that have been assigned a priority at or (logically)
7968 * below the maximum system call interrupt priority. FreeRTOS maintains a
7969 * separate interrupt safe API to ensure interrupt entry is as fast and as
7970 * simple as possible. More information (albeit Cortex-M specific) is
7971 * provided on the following link:
7972 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7973 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7975 pxTCB = xTaskToNotify;
7977 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
7979 if( pulPreviousNotificationValue != NULL )
7981 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7984 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7985 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7990 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7994 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7997 case eSetValueWithOverwrite:
7998 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8001 case eSetValueWithoutOverwrite:
8003 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
8005 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8009 /* The value could not be written to the task. */
8017 /* The task is being notified without its notify value being
8023 /* Should not get here if all enums are handled.
8024 * Artificially force an assert by testing a value the
8025 * compiler can't assume is const. */
8026 configASSERT( xTickCount == ( TickType_t ) 0 );
8030 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
8032 /* If the task is in the blocked state specifically to wait for a
8033 * notification then unblock it now. */
8034 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8036 /* The task should not have been on an event list. */
8037 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8039 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8041 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8042 prvAddTaskToReadyList( pxTCB );
8046 /* The delayed and ready lists cannot be accessed, so hold
8047 * this task pending until the scheduler is resumed. */
8048 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8051 #if ( configNUMBER_OF_CORES == 1 )
8053 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8055 /* The notified task has a priority above the currently
8056 * executing task so a yield is required. */
8057 if( pxHigherPriorityTaskWoken != NULL )
8059 *pxHigherPriorityTaskWoken = pdTRUE;
8062 /* Mark that a yield is pending in case the user is not
8063 * using the "xHigherPriorityTaskWoken" parameter to an ISR
8064 * safe FreeRTOS function. */
8065 xYieldPendings[ 0 ] = pdTRUE;
8069 mtCOVERAGE_TEST_MARKER();
8072 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8074 #if ( configUSE_PREEMPTION == 1 )
8076 prvYieldForTask( pxTCB );
8078 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8080 if( pxHigherPriorityTaskWoken != NULL )
8082 *pxHigherPriorityTaskWoken = pdTRUE;
8086 #endif /* if ( configUSE_PREEMPTION == 1 ) */
8088 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8091 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8093 traceRETURN_xTaskGenericNotifyFromISR( xReturn );
8098 #endif /* configUSE_TASK_NOTIFICATIONS */
8099 /*-----------------------------------------------------------*/
8101 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8103 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
8104 UBaseType_t uxIndexToNotify,
8105 BaseType_t * pxHigherPriorityTaskWoken )
8108 uint8_t ucOriginalNotifyState;
8109 UBaseType_t uxSavedInterruptStatus;
8111 traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
8113 configASSERT( xTaskToNotify );
8114 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8116 /* RTOS ports that support interrupt nesting have the concept of a
8117 * maximum system call (or maximum API call) interrupt priority.
8118 * Interrupts that are above the maximum system call priority are keep
8119 * permanently enabled, even when the RTOS kernel is in a critical section,
8120 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
8121 * is defined in FreeRTOSConfig.h then
8122 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
8123 * failure if a FreeRTOS API function is called from an interrupt that has
8124 * been assigned a priority above the configured maximum system call
8125 * priority. Only FreeRTOS functions that end in FromISR can be called
8126 * from interrupts that have been assigned a priority at or (logically)
8127 * below the maximum system call interrupt priority. FreeRTOS maintains a
8128 * separate interrupt safe API to ensure interrupt entry is as fast and as
8129 * simple as possible. More information (albeit Cortex-M specific) is
8130 * provided on the following link:
8131 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
8132 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
8134 pxTCB = xTaskToNotify;
8136 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
8138 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8139 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8141 /* 'Giving' is equivalent to incrementing a count in a counting
8143 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8145 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
8147 /* If the task is in the blocked state specifically to wait for a
8148 * notification then unblock it now. */
8149 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8151 /* The task should not have been on an event list. */
8152 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8154 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8156 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8157 prvAddTaskToReadyList( pxTCB );
8161 /* The delayed and ready lists cannot be accessed, so hold
8162 * this task pending until the scheduler is resumed. */
8163 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8166 #if ( configNUMBER_OF_CORES == 1 )
8168 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8170 /* The notified task has a priority above the currently
8171 * executing task so a yield is required. */
8172 if( pxHigherPriorityTaskWoken != NULL )
8174 *pxHigherPriorityTaskWoken = pdTRUE;
8177 /* Mark that a yield is pending in case the user is not
8178 * using the "xHigherPriorityTaskWoken" parameter in an ISR
8179 * safe FreeRTOS function. */
8180 xYieldPendings[ 0 ] = pdTRUE;
8184 mtCOVERAGE_TEST_MARKER();
8187 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8189 #if ( configUSE_PREEMPTION == 1 )
8191 prvYieldForTask( pxTCB );
8193 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8195 if( pxHigherPriorityTaskWoken != NULL )
8197 *pxHigherPriorityTaskWoken = pdTRUE;
8201 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
8203 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8206 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8208 traceRETURN_vTaskGenericNotifyGiveFromISR();
8211 #endif /* configUSE_TASK_NOTIFICATIONS */
8212 /*-----------------------------------------------------------*/
8214 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8216 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
8217 UBaseType_t uxIndexToClear )
8222 traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
8224 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8226 /* If null is passed in here then it is the calling task that is having
8227 * its notification state cleared. */
8228 pxTCB = prvGetTCBFromHandle( xTask );
8230 taskENTER_CRITICAL();
8232 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
8234 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
8242 taskEXIT_CRITICAL();
8244 traceRETURN_xTaskGenericNotifyStateClear( xReturn );
8249 #endif /* configUSE_TASK_NOTIFICATIONS */
8250 /*-----------------------------------------------------------*/
8252 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8254 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
8255 UBaseType_t uxIndexToClear,
8256 uint32_t ulBitsToClear )
8261 traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
8263 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8265 /* If null is passed in here then it is the calling task that is having
8266 * its notification state cleared. */
8267 pxTCB = prvGetTCBFromHandle( xTask );
8269 taskENTER_CRITICAL();
8271 /* Return the notification as it was before the bits were cleared,
8272 * then clear the bit mask. */
8273 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
8274 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
8276 taskEXIT_CRITICAL();
8278 traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
8283 #endif /* configUSE_TASK_NOTIFICATIONS */
8284 /*-----------------------------------------------------------*/
8286 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8288 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
8292 traceENTER_ulTaskGetRunTimeCounter( xTask );
8294 pxTCB = prvGetTCBFromHandle( xTask );
8296 traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
8298 return pxTCB->ulRunTimeCounter;
8301 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8302 /*-----------------------------------------------------------*/
8304 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8306 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
8309 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8311 traceENTER_ulTaskGetRunTimePercent( xTask );
8313 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
8315 /* For percentage calculations. */
8316 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8318 /* Avoid divide by zero errors. */
8319 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8321 pxTCB = prvGetTCBFromHandle( xTask );
8322 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
8329 traceRETURN_ulTaskGetRunTimePercent( ulReturn );
8334 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8335 /*-----------------------------------------------------------*/
8337 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8339 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
8341 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
8344 traceENTER_ulTaskGetIdleRunTimeCounter();
8346 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8348 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8351 traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
8356 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8357 /*-----------------------------------------------------------*/
8359 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8361 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
8363 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8364 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
8367 traceENTER_ulTaskGetIdleRunTimePercent();
8369 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
8371 /* For percentage calculations. */
8372 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8374 /* Avoid divide by zero errors. */
8375 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8377 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8379 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8382 ulReturn = ulRunTimeCounter / ulTotalTime;
8389 traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
8394 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8395 /*-----------------------------------------------------------*/
8397 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
8398 const BaseType_t xCanBlockIndefinitely )
8400 TickType_t xTimeToWake;
8401 const TickType_t xConstTickCount = xTickCount;
8402 List_t * const pxDelayedList = pxDelayedTaskList;
8403 List_t * const pxOverflowDelayedList = pxOverflowDelayedTaskList;
8405 #if ( INCLUDE_xTaskAbortDelay == 1 )
8407 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
8408 * reset to pdFALSE so it can be detected as having been set to pdTRUE
8409 * when the task leaves the Blocked state. */
8410 pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
8414 /* Remove the task from the ready list before adding it to the blocked list
8415 * as the same list item is used for both lists. */
8416 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
8418 /* The current task must be in a ready list, so there is no need to
8419 * check, and the port reset macro can be called directly. */
8420 portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
8424 mtCOVERAGE_TEST_MARKER();
8427 #if ( INCLUDE_vTaskSuspend == 1 )
8429 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
8431 /* Add the task to the suspended task list instead of a delayed task
8432 * list to ensure it is not woken by a timing event. It will block
8434 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
8438 /* Calculate the time at which the task should be woken if the event
8439 * does not occur. This may overflow but this doesn't matter, the
8440 * kernel will manage it correctly. */
8441 xTimeToWake = xConstTickCount + xTicksToWait;
8443 /* The list item will be inserted in wake time order. */
8444 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8446 if( xTimeToWake < xConstTickCount )
8448 /* Wake time has overflowed. Place this item in the overflow
8450 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8451 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8455 /* The wake time has not overflowed, so the current block list
8457 traceMOVED_TASK_TO_DELAYED_LIST();
8458 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8460 /* If the task entering the blocked state was placed at the
8461 * head of the list of blocked tasks then xNextTaskUnblockTime
8462 * needs to be updated too. */
8463 if( xTimeToWake < xNextTaskUnblockTime )
8465 xNextTaskUnblockTime = xTimeToWake;
8469 mtCOVERAGE_TEST_MARKER();
8474 #else /* INCLUDE_vTaskSuspend */
8476 /* Calculate the time at which the task should be woken if the event
8477 * does not occur. This may overflow but this doesn't matter, the kernel
8478 * will manage it correctly. */
8479 xTimeToWake = xConstTickCount + xTicksToWait;
8481 /* The list item will be inserted in wake time order. */
8482 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8484 if( xTimeToWake < xConstTickCount )
8486 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8487 /* Wake time has overflowed. Place this item in the overflow list. */
8488 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8492 traceMOVED_TASK_TO_DELAYED_LIST();
8493 /* The wake time has not overflowed, so the current block list is used. */
8494 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8496 /* If the task entering the blocked state was placed at the head of the
8497 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
8499 if( xTimeToWake < xNextTaskUnblockTime )
8501 xNextTaskUnblockTime = xTimeToWake;
8505 mtCOVERAGE_TEST_MARKER();
8509 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
8510 ( void ) xCanBlockIndefinitely;
8512 #endif /* INCLUDE_vTaskSuspend */
8514 /*-----------------------------------------------------------*/
8516 #if ( portUSING_MPU_WRAPPERS == 1 )
8518 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
8522 traceENTER_xTaskGetMPUSettings( xTask );
8524 pxTCB = prvGetTCBFromHandle( xTask );
8526 traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
8528 return &( pxTCB->xMPUSettings );
8531 #endif /* portUSING_MPU_WRAPPERS */
8532 /*-----------------------------------------------------------*/
8534 /* Code below here allows additional code to be inserted into this source file,
8535 * especially where access to file scope functions and data is needed (for example
8536 * when performing module tests). */
8538 #ifdef FREERTOS_MODULE_TEST
8539 #include "tasks_test_access_functions.h"
8543 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
8545 #include "freertos_tasks_c_additions.h"
8547 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8548 static void freertos_tasks_c_additions_init( void )
8550 FREERTOS_TASKS_C_ADDITIONS_INIT();
8554 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8555 /*-----------------------------------------------------------*/
8557 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8560 * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8561 * to provide the memory that is used by the Idle task. It is used when
8562 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8563 * it's own implementation of vApplicationGetIdleTaskMemory by setting
8564 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8566 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8567 StackType_t ** ppxIdleTaskStackBuffer,
8568 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize )
8570 static StaticTask_t xIdleTaskTCB;
8571 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8573 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8574 *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8575 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8578 #if ( configNUMBER_OF_CORES > 1 )
8580 void vApplicationGetPassiveIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8581 StackType_t ** ppxIdleTaskStackBuffer,
8582 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize,
8583 BaseType_t xPassiveIdleTaskIndex )
8585 static StaticTask_t xIdleTaskTCBs[ configNUMBER_OF_CORES - 1 ];
8586 static StackType_t uxIdleTaskStacks[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
8588 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCBs[ xPassiveIdleTaskIndex ] );
8589 *ppxIdleTaskStackBuffer = &( uxIdleTaskStacks[ xPassiveIdleTaskIndex ][ 0 ] );
8590 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8593 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
8595 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8596 /*-----------------------------------------------------------*/
8598 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8601 * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8602 * to provide the memory that is used by the Timer service task. It is used when
8603 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8604 * it's own implementation of vApplicationGetTimerTaskMemory by setting
8605 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8607 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8608 StackType_t ** ppxTimerTaskStackBuffer,
8609 configSTACK_DEPTH_TYPE * puxTimerTaskStackSize )
8611 static StaticTask_t xTimerTaskTCB;
8612 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8614 *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8615 *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8616 *puxTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8619 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8620 /*-----------------------------------------------------------*/
8623 * Reset the state in this file. This state is normally initialized at start up.
8624 * This function must be called by the application before restarting the
8627 void vTaskResetState( void )
8631 /* Task control block. */
8632 #if ( configNUMBER_OF_CORES == 1 )
8634 pxCurrentTCB = NULL;
8636 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8638 #if ( INCLUDE_vTaskDelete == 1 )
8640 uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
8642 #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
8644 #if ( configUSE_POSIX_ERRNO == 1 )
8648 #endif /* #if ( configUSE_POSIX_ERRNO == 1 ) */
8650 /* Other file private variables. */
8651 uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
8652 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
8653 uxTopReadyPriority = tskIDLE_PRIORITY;
8654 xSchedulerRunning = pdFALSE;
8655 xPendedTicks = ( TickType_t ) 0U;
8657 for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
8659 xYieldPendings[ xCoreID ] = pdFALSE;
8662 xNumOfOverflows = ( BaseType_t ) 0;
8663 uxTaskNumber = ( UBaseType_t ) 0U;
8664 xNextTaskUnblockTime = ( TickType_t ) 0U;
8666 uxSchedulerSuspended = ( UBaseType_t ) 0U;
8668 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8670 for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
8672 ulTaskSwitchedInTime[ xCoreID ] = 0U;
8673 ulTotalRunTime[ xCoreID ] = 0U;
8676 #endif /* #if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8678 /*-----------------------------------------------------------*/