2 * FreeRTOS Kernel V11.1.0
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /* Standard includes. */
33 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
34 * all the API functions to use the MPU wrappers. That should only be done when
35 * task.h is included from an application file. */
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
38 /* FreeRTOS includes. */
42 #include "stack_macros.h"
44 /* The default definitions are only available for non-MPU ports. The
45 * reason is that the stack alignment requirements vary for different
47 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS != 0 ) )
48 #error configKERNEL_PROVIDED_STATIC_MEMORY cannot be set to 1 when using an MPU port. The vApplicationGet*TaskMemory() functions must be provided manually.
51 /* The MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
52 * for the header files above, but not in this file, in order to generate the
53 * correct privileged Vs unprivileged linkage and placement. */
54 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
56 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
57 * functions but without including stdio.h here. */
58 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
60 /* At the bottom of this file are two optional functions that can be used
61 * to generate human readable text from the raw data generated by the
62 * uxTaskGetSystemState() function. Note the formatting functions are provided
63 * for convenience only, and are NOT considered part of the kernel. */
65 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
67 #if ( configUSE_PREEMPTION == 0 )
69 /* If the cooperative scheduler is being used then a yield should not be
70 * performed just because a higher priority task has been woken. */
71 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB )
72 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB )
75 #if ( configNUMBER_OF_CORES == 1 )
77 /* This macro requests the running task pxTCB to yield. In single core
78 * scheduler, a running task always runs on core 0 and portYIELD_WITHIN_API()
79 * can be used to request the task running on core 0 to yield. Therefore, pxTCB
80 * is not used in this macro. */
81 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) \
84 portYIELD_WITHIN_API(); \
87 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \
89 if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \
91 portYIELD_WITHIN_API(); \
95 mtCOVERAGE_TEST_MARKER(); \
99 #else /* if ( configNUMBER_OF_CORES == 1 ) */
101 /* Yield the core on which this task is running. */
102 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldCore( ( pxTCB )->xTaskRunState )
104 /* Yield for the task if a running task has priority lower than this task. */
105 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldForTask( pxTCB )
107 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
109 #endif /* if ( configUSE_PREEMPTION == 0 ) */
111 /* Values that can be assigned to the ucNotifyState member of the TCB. */
112 #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
113 #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
114 #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
117 * The value used to fill the stack of a task when the task is created. This
118 * is used purely for checking the high water mark for tasks.
120 #define tskSTACK_FILL_BYTE ( 0xa5U )
122 /* Bits used to record how a task's stack and TCB were allocated. */
123 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
124 #define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
125 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
127 /* If any of the following are set then task stacks are filled with a known
128 * value so the high water mark can be determined. If none of the following are
129 * set then don't fill the stack so there is no unnecessary dependency on memset. */
130 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
131 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
133 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
137 * Macros used by vListTask to indicate which state a task is in.
139 #define tskRUNNING_CHAR ( 'X' )
140 #define tskBLOCKED_CHAR ( 'B' )
141 #define tskREADY_CHAR ( 'R' )
142 #define tskDELETED_CHAR ( 'D' )
143 #define tskSUSPENDED_CHAR ( 'S' )
146 * Some kernel aware debuggers require the data the debugger needs access to be
147 * global, rather than file scope.
149 #ifdef portREMOVE_STATIC_QUALIFIER
153 /* The name allocated to the Idle task. This can be overridden by defining
154 * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
155 #ifndef configIDLE_TASK_NAME
156 #define configIDLE_TASK_NAME "IDLE"
159 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
161 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
162 * performed in a generic way that is not optimised to any particular
163 * microcontroller architecture. */
165 /* uxTopReadyPriority holds the priority of the highest priority ready
167 #define taskRECORD_READY_PRIORITY( uxPriority ) \
169 if( ( uxPriority ) > uxTopReadyPriority ) \
171 uxTopReadyPriority = ( uxPriority ); \
173 } while( 0 ) /* taskRECORD_READY_PRIORITY */
175 /*-----------------------------------------------------------*/
177 #if ( configNUMBER_OF_CORES == 1 )
178 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
180 UBaseType_t uxTopPriority = uxTopReadyPriority; \
182 /* Find the highest priority queue that contains ready tasks. */ \
183 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) != pdFALSE ) \
185 configASSERT( uxTopPriority ); \
189 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
190 * the same priority get an equal share of the processor time. */ \
191 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
192 uxTopReadyPriority = uxTopPriority; \
193 } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */
194 #else /* if ( configNUMBER_OF_CORES == 1 ) */
196 #define taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID ) prvSelectHighestPriorityTask( xCoreID )
198 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
200 /*-----------------------------------------------------------*/
202 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
203 * they are only required when a port optimised method of task selection is
205 #define taskRESET_READY_PRIORITY( uxPriority )
206 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
208 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
210 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
211 * performed in a way that is tailored to the particular microcontroller
212 * architecture being used. */
214 /* A port optimised version is provided. Call the port defined macros. */
215 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( ( uxPriority ), uxTopReadyPriority )
217 /*-----------------------------------------------------------*/
219 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
221 UBaseType_t uxTopPriority; \
223 /* Find the highest priority list that contains ready tasks. */ \
224 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
225 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
226 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
229 /*-----------------------------------------------------------*/
231 /* A port optimised version is provided, call it only if the TCB being reset
232 * is being referenced from a ready list. If it is referenced from a delayed
233 * or suspended list then it won't be in a ready list. */
234 #define taskRESET_READY_PRIORITY( uxPriority ) \
236 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
238 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
242 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
244 /*-----------------------------------------------------------*/
246 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
247 * count overflows. */
248 #define taskSWITCH_DELAYED_LISTS() \
252 /* The delayed tasks list should be empty when the lists are switched. */ \
253 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
255 pxTemp = pxDelayedTaskList; \
256 pxDelayedTaskList = pxOverflowDelayedTaskList; \
257 pxOverflowDelayedTaskList = pxTemp; \
258 xNumOfOverflows = ( BaseType_t ) ( xNumOfOverflows + 1 ); \
259 prvResetNextTaskUnblockTime(); \
262 /*-----------------------------------------------------------*/
265 * Place the task represented by pxTCB into the appropriate ready list for
266 * the task. It is inserted at the end of the list.
268 #define prvAddTaskToReadyList( pxTCB ) \
270 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
271 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
272 listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
273 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB ); \
275 /*-----------------------------------------------------------*/
278 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
279 * where NULL is used to indicate that the handle of the currently executing
280 * task should be used in place of the parameter. This macro simply checks to
281 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
283 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
285 /* The item value of the event list item is normally used to hold the priority
286 * of the task to which it belongs (coded to allow it to be held in reverse
287 * priority order). However, it is occasionally borrowed for other purposes. It
288 * is important its value is not updated due to a task priority change while it is
289 * being used for another purpose. The following bit definition is used to inform
290 * the scheduler that the value should not be changed - in which case it is the
291 * responsibility of whichever module is using the value to ensure it gets set back
292 * to its original value when it is released. */
293 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
294 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint16_t ) 0x8000U )
295 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
296 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint32_t ) 0x80000000U )
297 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
298 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint64_t ) 0x8000000000000000U )
301 /* Indicates that the task is not actively running on any core. */
302 #define taskTASK_NOT_RUNNING ( ( BaseType_t ) ( -1 ) )
304 /* Indicates that the task is actively running but scheduled to yield. */
305 #define taskTASK_SCHEDULED_TO_YIELD ( ( BaseType_t ) ( -2 ) )
307 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
308 #if ( configNUMBER_OF_CORES == 1 )
309 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
310 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
312 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) )
313 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB )->xTaskRunState != taskTASK_NOT_RUNNING ) ? ( pdTRUE ) : ( pdFALSE ) )
316 /* Indicates that the task is an Idle task. */
317 #define taskATTRIBUTE_IS_IDLE ( UBaseType_t ) ( 1U << 0U )
319 #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) )
320 #define portGET_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting )
321 #define portSET_CRITICAL_NESTING_COUNT( x ) ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting = ( x ) )
322 #define portINCREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting++ )
323 #define portDECREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting-- )
324 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) */
326 #define taskBITS_PER_BYTE ( ( size_t ) 8 )
328 #if ( configNUMBER_OF_CORES > 1 )
330 /* Yields the given core. This must be called from a critical section and xCoreID
331 * must be valid. This macro is not required in single core since there is only
332 * one core to yield. */
333 #define prvYieldCore( xCoreID ) \
335 if( ( xCoreID ) == ( BaseType_t ) portGET_CORE_ID() ) \
337 /* Pending a yield for this core since it is in the critical section. */ \
338 xYieldPendings[ ( xCoreID ) ] = pdTRUE; \
342 /* Request other core to yield if it is not requested before. */ \
343 if( pxCurrentTCBs[ ( xCoreID ) ]->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD ) \
345 portYIELD_CORE( xCoreID ); \
346 pxCurrentTCBs[ ( xCoreID ) ]->xTaskRunState = taskTASK_SCHEDULED_TO_YIELD; \
350 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
351 /*-----------------------------------------------------------*/
354 * Task control block. A task control block (TCB) is allocated for each task,
355 * and stores task state information, including a pointer to the task's context
356 * (the task's run time environment, including register values)
358 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
360 volatile StackType_t * pxTopOfStack; /**< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
362 #if ( portUSING_MPU_WRAPPERS == 1 )
363 xMPU_SETTINGS xMPUSettings; /**< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
366 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
367 UBaseType_t uxCoreAffinityMask; /**< Used to link the task to certain cores. UBaseType_t must have greater than or equal to the number of bits as configNUMBER_OF_CORES. */
370 ListItem_t xStateListItem; /**< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
371 ListItem_t xEventListItem; /**< Used to reference a task from an event list. */
372 UBaseType_t uxPriority; /**< The priority of the task. 0 is the lowest priority. */
373 StackType_t * pxStack; /**< Points to the start of the stack. */
374 #if ( configNUMBER_OF_CORES > 1 )
375 volatile BaseType_t xTaskRunState; /**< Used to identify the core the task is running on, if the task is running. Otherwise, identifies the task's state - not running or yielding. */
376 UBaseType_t uxTaskAttributes; /**< Task's attributes - currently used to identify the idle tasks. */
378 char pcTaskName[ configMAX_TASK_NAME_LEN ]; /**< Descriptive name given to the task when created. Facilitates debugging only. */
380 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
381 BaseType_t xPreemptionDisable; /**< Used to prevent the task from being preempted. */
384 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
385 StackType_t * pxEndOfStack; /**< Points to the highest valid address for the stack. */
388 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
389 UBaseType_t uxCriticalNesting; /**< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
392 #if ( configUSE_TRACE_FACILITY == 1 )
393 UBaseType_t uxTCBNumber; /**< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
394 UBaseType_t uxTaskNumber; /**< Stores a number specifically for use by third party trace code. */
397 #if ( configUSE_MUTEXES == 1 )
398 UBaseType_t uxBasePriority; /**< The priority last assigned to the task - used by the priority inheritance mechanism. */
399 UBaseType_t uxMutexesHeld;
402 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
403 TaskHookFunction_t pxTaskTag;
406 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
407 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
410 #if ( configGENERATE_RUN_TIME_STATS == 1 )
411 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /**< Stores the amount of time the task has spent in the Running state. */
414 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
415 configTLS_BLOCK_TYPE xTLSBlock; /**< Memory block used as Thread Local Storage (TLS) Block for the task. */
418 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
419 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
420 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
423 /* See the comments in FreeRTOS.h with the definition of
424 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
425 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
426 uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
429 #if ( INCLUDE_xTaskAbortDelay == 1 )
430 uint8_t ucDelayAborted;
433 #if ( configUSE_POSIX_ERRNO == 1 )
438 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
439 * below to enable the use of older kernel aware debuggers. */
440 typedef tskTCB TCB_t;
442 #if ( configNUMBER_OF_CORES == 1 )
443 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
444 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
445 /* coverity[misra_c_2012_rule_8_4_violation] */
446 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
448 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
449 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
450 /* coverity[misra_c_2012_rule_8_4_violation] */
451 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ];
452 #define pxCurrentTCB xTaskGetCurrentTaskHandle()
455 /* Lists for ready and blocked tasks. --------------------
456 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
457 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
458 * the static qualifier. */
459 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
460 PRIVILEGED_DATA static List_t xDelayedTaskList1; /**< Delayed tasks. */
461 PRIVILEGED_DATA static List_t xDelayedTaskList2; /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
462 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /**< Points to the delayed task list currently being used. */
463 PRIVILEGED_DATA static List_t * volatile pxOverflowDelayedTaskList; /**< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
464 PRIVILEGED_DATA static List_t xPendingReadyList; /**< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready list when the scheduler is resumed. */
466 #if ( INCLUDE_vTaskDelete == 1 )
468 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
469 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
473 #if ( INCLUDE_vTaskSuspend == 1 )
475 PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
479 /* Global POSIX errno. Its value is changed upon context switching to match
480 * the errno of the currently running task. */
481 #if ( configUSE_POSIX_ERRNO == 1 )
482 int FreeRTOS_errno = 0;
485 /* Other file private variables. --------------------------------*/
486 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
487 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
488 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
489 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
490 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
491 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
492 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
493 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
494 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
495 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandles[ configNUMBER_OF_CORES ]; /**< Holds the handles of the idle tasks. The idle tasks are created automatically when the scheduler is started. */
497 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
498 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
499 * to determine the number of priority lists to read back from the remote target. */
500 static const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
502 /* Context switches are held pending while the scheduler is suspended. Also,
503 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
504 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
505 * If an interrupt needs to unblock a task while the scheduler is suspended then it
506 * moves the task's event list item into the xPendingReadyList, ready for the
507 * kernel to move the task from the pending ready list into the real ready list
508 * when the scheduler is unsuspended. The pending ready list itself can only be
509 * accessed from a critical section.
511 * Updates to uxSchedulerSuspended must be protected by both the task lock and the ISR lock
512 * and must not be done from an ISR. Reads must be protected by either lock and may be done
513 * from either an ISR or a task. */
514 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
516 #if ( configGENERATE_RUN_TIME_STATS == 1 )
518 /* Do not move these variables to function scope as doing so prevents the
519 * code working with debuggers that need to remove the static qualifier. */
520 PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the value of a timer/counter the last time a task was switched in. */
521 PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the total amount of execution time as defined by the run time counter clock. */
525 /*-----------------------------------------------------------*/
527 /* File private functions. --------------------------------*/
530 * Creates the idle tasks during scheduler start.
532 static BaseType_t prvCreateIdleTasks( void );
534 #if ( configNUMBER_OF_CORES > 1 )
537 * Checks to see if another task moved the current task out of the ready
538 * list while it was waiting to enter a critical section and yields, if so.
540 static void prvCheckForRunStateChange( void );
541 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
543 #if ( configNUMBER_OF_CORES > 1 )
546 * Yields a core, or cores if multiple priorities are not allowed to run
547 * simultaneously, to allow the task pxTCB to run.
549 static void prvYieldForTask( const TCB_t * pxTCB );
550 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
552 #if ( configNUMBER_OF_CORES > 1 )
555 * Selects the highest priority available task for the given core.
557 static void prvSelectHighestPriorityTask( BaseType_t xCoreID );
558 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
561 * Utility task that simply returns pdTRUE if the task referenced by xTask is
562 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
563 * is in any other state.
565 #if ( INCLUDE_vTaskSuspend == 1 )
567 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
569 #endif /* INCLUDE_vTaskSuspend */
572 * Utility to ready all the lists used by the scheduler. This is called
573 * automatically upon the creation of the first task.
575 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
578 * The idle task, which as all tasks is implemented as a never ending loop.
579 * The idle task is automatically created and added to the ready lists upon
580 * creation of the first user task.
582 * In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks are also
583 * created to ensure that each core has an idle task to run when no other
584 * task is available to run.
586 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
587 * language extensions. The equivalent prototype for these functions are:
589 * void prvIdleTask( void *pvParameters );
590 * void prvPassiveIdleTask( void *pvParameters );
593 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
594 #if ( configNUMBER_OF_CORES > 1 )
595 static portTASK_FUNCTION_PROTO( prvPassiveIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
599 * Utility to free all memory allocated by the scheduler to hold a TCB,
600 * including the stack pointed to by the TCB.
602 * This does not free memory allocated by the task itself (i.e. memory
603 * allocated by calls to pvPortMalloc from within the tasks application code).
605 #if ( INCLUDE_vTaskDelete == 1 )
607 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
612 * Used only by the idle task. This checks to see if anything has been placed
613 * in the list of tasks waiting to be deleted. If so the task is cleaned up
614 * and its TCB deleted.
616 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
619 * The currently executing task is entering the Blocked state. Add the task to
620 * either the current or the overflow delayed task list.
622 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
623 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
626 * Fills an TaskStatus_t structure with information on each task that is
627 * referenced from the pxList list (which may be a ready list, a delayed list,
628 * a suspended list, etc.).
630 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
631 * NORMAL APPLICATION CODE.
633 #if ( configUSE_TRACE_FACILITY == 1 )
635 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
637 eTaskState eState ) PRIVILEGED_FUNCTION;
642 * Searches pxList for a task with name pcNameToQuery - returning a handle to
643 * the task if it is found, or NULL if the task is not found.
645 #if ( INCLUDE_xTaskGetHandle == 1 )
647 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
648 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
653 * When a task is created, the stack of the task is filled with a known value.
654 * This function determines the 'high water mark' of the task stack by
655 * determining how much of the stack remains at the original preset value.
657 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
659 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
664 * Return the amount of time, in ticks, that will pass before the kernel will
665 * next move a task from the Blocked state to the Running state.
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 = ( BaseType_t ) ( 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 ) == 0U ) );
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 ) == 0U ) );
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 ) ( uxCurrentNumberOfTasks + 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 /* MISRA Ref 4.7.1 [Return value shall be checked] */
2670 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
2671 /* coverity[misra_c_2012_directive_4_7_violation] */
2672 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
2674 /* If null is passed in here then it is the priority of the calling
2675 * task that is being queried. */
2676 pxTCB = prvGetTCBFromHandle( xTask );
2677 uxReturn = pxTCB->uxPriority;
2679 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2681 traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
2686 #endif /* INCLUDE_uxTaskPriorityGet */
2687 /*-----------------------------------------------------------*/
2689 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2691 UBaseType_t uxTaskBasePriorityGet( const TaskHandle_t xTask )
2693 TCB_t const * pxTCB;
2694 UBaseType_t uxReturn;
2696 traceENTER_uxTaskBasePriorityGet( xTask );
2698 taskENTER_CRITICAL();
2700 /* If null is passed in here then it is the base priority of the task
2701 * that called uxTaskBasePriorityGet() that is being queried. */
2702 pxTCB = prvGetTCBFromHandle( xTask );
2703 uxReturn = pxTCB->uxBasePriority;
2705 taskEXIT_CRITICAL();
2707 traceRETURN_uxTaskBasePriorityGet( uxReturn );
2712 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2713 /*-----------------------------------------------------------*/
2715 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2717 UBaseType_t uxTaskBasePriorityGetFromISR( const TaskHandle_t xTask )
2719 TCB_t const * pxTCB;
2720 UBaseType_t uxReturn;
2721 UBaseType_t uxSavedInterruptStatus;
2723 traceENTER_uxTaskBasePriorityGetFromISR( xTask );
2725 /* RTOS ports that support interrupt nesting have the concept of a
2726 * maximum system call (or maximum API call) interrupt priority.
2727 * Interrupts that are above the maximum system call priority are keep
2728 * permanently enabled, even when the RTOS kernel is in a critical section,
2729 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2730 * is defined in FreeRTOSConfig.h then
2731 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2732 * failure if a FreeRTOS API function is called from an interrupt that has
2733 * been assigned a priority above the configured maximum system call
2734 * priority. Only FreeRTOS functions that end in FromISR can be called
2735 * from interrupts that have been assigned a priority at or (logically)
2736 * below the maximum system call interrupt priority. FreeRTOS maintains a
2737 * separate interrupt safe API to ensure interrupt entry is as fast and as
2738 * simple as possible. More information (albeit Cortex-M specific) is
2739 * provided on the following link:
2740 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2741 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2743 /* MISRA Ref 4.7.1 [Return value shall be checked] */
2744 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
2745 /* coverity[misra_c_2012_directive_4_7_violation] */
2746 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
2748 /* If null is passed in here then it is the base priority of the calling
2749 * task that is being queried. */
2750 pxTCB = prvGetTCBFromHandle( xTask );
2751 uxReturn = pxTCB->uxBasePriority;
2753 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2755 traceRETURN_uxTaskBasePriorityGetFromISR( uxReturn );
2760 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2761 /*-----------------------------------------------------------*/
2763 #if ( INCLUDE_vTaskPrioritySet == 1 )
2765 void vTaskPrioritySet( TaskHandle_t xTask,
2766 UBaseType_t uxNewPriority )
2769 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2770 BaseType_t xYieldRequired = pdFALSE;
2772 #if ( configNUMBER_OF_CORES > 1 )
2773 BaseType_t xYieldForTask = pdFALSE;
2776 traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
2778 configASSERT( uxNewPriority < configMAX_PRIORITIES );
2780 /* Ensure the new priority is valid. */
2781 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2783 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2787 mtCOVERAGE_TEST_MARKER();
2790 taskENTER_CRITICAL();
2792 /* If null is passed in here then it is the priority of the calling
2793 * task that is being changed. */
2794 pxTCB = prvGetTCBFromHandle( xTask );
2796 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2798 #if ( configUSE_MUTEXES == 1 )
2800 uxCurrentBasePriority = pxTCB->uxBasePriority;
2804 uxCurrentBasePriority = pxTCB->uxPriority;
2808 if( uxCurrentBasePriority != uxNewPriority )
2810 /* The priority change may have readied a task of higher
2811 * priority than a running task. */
2812 if( uxNewPriority > uxCurrentBasePriority )
2814 #if ( configNUMBER_OF_CORES == 1 )
2816 if( pxTCB != pxCurrentTCB )
2818 /* The priority of a task other than the currently
2819 * running task is being raised. Is the priority being
2820 * raised above that of the running task? */
2821 if( uxNewPriority > pxCurrentTCB->uxPriority )
2823 xYieldRequired = pdTRUE;
2827 mtCOVERAGE_TEST_MARKER();
2832 /* The priority of the running task is being raised,
2833 * but the running task must already be the highest
2834 * priority task able to run so no yield is required. */
2837 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2839 /* The priority of a task is being raised so
2840 * perform a yield for this task later. */
2841 xYieldForTask = pdTRUE;
2843 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2845 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2847 /* Setting the priority of a running task down means
2848 * there may now be another task of higher priority that
2849 * is ready to execute. */
2850 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2851 if( pxTCB->xPreemptionDisable == pdFALSE )
2854 xYieldRequired = pdTRUE;
2859 /* Setting the priority of any other task down does not
2860 * require a yield as the running task must be above the
2861 * new priority of the task being modified. */
2864 /* Remember the ready list the task might be referenced from
2865 * before its uxPriority member is changed so the
2866 * taskRESET_READY_PRIORITY() macro can function correctly. */
2867 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2869 #if ( configUSE_MUTEXES == 1 )
2871 /* Only change the priority being used if the task is not
2872 * currently using an inherited priority or the new priority
2873 * is bigger than the inherited priority. */
2874 if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2876 pxTCB->uxPriority = uxNewPriority;
2880 mtCOVERAGE_TEST_MARKER();
2883 /* The base priority gets set whatever. */
2884 pxTCB->uxBasePriority = uxNewPriority;
2886 #else /* if ( configUSE_MUTEXES == 1 ) */
2888 pxTCB->uxPriority = uxNewPriority;
2890 #endif /* if ( configUSE_MUTEXES == 1 ) */
2892 /* Only reset the event list item value if the value is not
2893 * being used for anything else. */
2894 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
2896 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) );
2900 mtCOVERAGE_TEST_MARKER();
2903 /* If the task is in the blocked or suspended list we need do
2904 * nothing more than change its priority variable. However, if
2905 * the task is in a ready list it needs to be removed and placed
2906 * in the list appropriate to its new priority. */
2907 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2909 /* The task is currently in its ready list - remove before
2910 * adding it to its new ready list. As we are in a critical
2911 * section we can do this even if the scheduler is suspended. */
2912 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2914 /* It is known that the task is in its ready list so
2915 * there is no need to check again and the port level
2916 * reset macro can be called directly. */
2917 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2921 mtCOVERAGE_TEST_MARKER();
2924 prvAddTaskToReadyList( pxTCB );
2928 #if ( configNUMBER_OF_CORES == 1 )
2930 mtCOVERAGE_TEST_MARKER();
2934 /* It's possible that xYieldForTask was already set to pdTRUE because
2935 * its priority is being raised. However, since it is not in a ready list
2936 * we don't actually need to yield for it. */
2937 xYieldForTask = pdFALSE;
2942 if( xYieldRequired != pdFALSE )
2944 /* The running task priority is set down. Request the task to yield. */
2945 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2949 #if ( configNUMBER_OF_CORES > 1 )
2950 if( xYieldForTask != pdFALSE )
2952 /* The priority of the task is being raised. If a running
2953 * task has priority lower than this task, it should yield
2955 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2958 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2960 mtCOVERAGE_TEST_MARKER();
2964 /* Remove compiler warning about unused variables when the port
2965 * optimised task selection is not being used. */
2966 ( void ) uxPriorityUsedOnEntry;
2969 taskEXIT_CRITICAL();
2971 traceRETURN_vTaskPrioritySet();
2974 #endif /* INCLUDE_vTaskPrioritySet */
2975 /*-----------------------------------------------------------*/
2977 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2978 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2979 UBaseType_t uxCoreAffinityMask )
2983 UBaseType_t uxPrevCoreAffinityMask;
2985 #if ( configUSE_PREEMPTION == 1 )
2986 UBaseType_t uxPrevNotAllowedCores;
2989 traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
2991 taskENTER_CRITICAL();
2993 pxTCB = prvGetTCBFromHandle( xTask );
2995 uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2996 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2998 if( xSchedulerRunning != pdFALSE )
3000 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3002 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
3004 /* If the task can no longer run on the core it was running,
3005 * request the core to yield. */
3006 if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
3008 prvYieldCore( xCoreID );
3013 #if ( configUSE_PREEMPTION == 1 )
3015 /* Calculate the cores on which this task was not allowed to
3016 * run previously. */
3017 uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
3019 /* Does the new core mask enables this task to run on any of the
3020 * previously not allowed cores? If yes, check if this task can be
3021 * scheduled on any of those cores. */
3022 if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
3024 prvYieldForTask( pxTCB );
3027 #else /* #if( configUSE_PREEMPTION == 1 ) */
3029 mtCOVERAGE_TEST_MARKER();
3031 #endif /* #if( configUSE_PREEMPTION == 1 ) */
3035 taskEXIT_CRITICAL();
3037 traceRETURN_vTaskCoreAffinitySet();
3039 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3040 /*-----------------------------------------------------------*/
3042 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
3043 UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
3045 const TCB_t * pxTCB;
3046 UBaseType_t uxCoreAffinityMask;
3048 traceENTER_vTaskCoreAffinityGet( xTask );
3050 taskENTER_CRITICAL();
3052 pxTCB = prvGetTCBFromHandle( xTask );
3053 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
3055 taskEXIT_CRITICAL();
3057 traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
3059 return uxCoreAffinityMask;
3061 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3063 /*-----------------------------------------------------------*/
3065 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3067 void vTaskPreemptionDisable( const TaskHandle_t xTask )
3071 traceENTER_vTaskPreemptionDisable( xTask );
3073 taskENTER_CRITICAL();
3075 pxTCB = prvGetTCBFromHandle( xTask );
3077 pxTCB->xPreemptionDisable = pdTRUE;
3079 taskEXIT_CRITICAL();
3081 traceRETURN_vTaskPreemptionDisable();
3084 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3085 /*-----------------------------------------------------------*/
3087 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3089 void vTaskPreemptionEnable( const TaskHandle_t xTask )
3094 traceENTER_vTaskPreemptionEnable( xTask );
3096 taskENTER_CRITICAL();
3098 pxTCB = prvGetTCBFromHandle( xTask );
3100 pxTCB->xPreemptionDisable = pdFALSE;
3102 if( xSchedulerRunning != pdFALSE )
3104 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3106 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
3107 prvYieldCore( xCoreID );
3111 taskEXIT_CRITICAL();
3113 traceRETURN_vTaskPreemptionEnable();
3116 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3117 /*-----------------------------------------------------------*/
3119 #if ( INCLUDE_vTaskSuspend == 1 )
3121 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
3125 traceENTER_vTaskSuspend( xTaskToSuspend );
3127 taskENTER_CRITICAL();
3129 /* If null is passed in here then it is the running task that is
3130 * being suspended. */
3131 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
3133 traceTASK_SUSPEND( pxTCB );
3135 /* Remove task from the ready/delayed list and place in the
3136 * suspended list. */
3137 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
3139 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
3143 mtCOVERAGE_TEST_MARKER();
3146 /* Is the task waiting on an event also? */
3147 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3149 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3153 mtCOVERAGE_TEST_MARKER();
3156 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
3158 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3162 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3164 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3166 /* The task was blocked to wait for a notification, but is
3167 * now suspended, so no notification was received. */
3168 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
3172 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3174 /* In the case of SMP, it is possible that the task being suspended
3175 * is running on another core. We must evict the task before
3176 * exiting the critical section to ensure that the task cannot
3177 * take an action which puts it back on ready/state/event list,
3178 * thereby nullifying the suspend operation. Once evicted, the
3179 * task won't be scheduled before it is resumed as it will no longer
3180 * be on the ready list. */
3181 #if ( configNUMBER_OF_CORES > 1 )
3183 if( xSchedulerRunning != pdFALSE )
3185 /* Reset the next expected unblock time in case it referred to the
3186 * task that is now in the Suspended state. */
3187 prvResetNextTaskUnblockTime();
3189 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3191 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
3193 /* The current task has just been suspended. */
3194 configASSERT( uxSchedulerSuspended == 0 );
3195 vTaskYieldWithinAPI();
3199 prvYieldCore( pxTCB->xTaskRunState );
3204 mtCOVERAGE_TEST_MARKER();
3209 mtCOVERAGE_TEST_MARKER();
3212 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
3214 taskEXIT_CRITICAL();
3216 #if ( configNUMBER_OF_CORES == 1 )
3218 UBaseType_t uxCurrentListLength;
3220 if( xSchedulerRunning != pdFALSE )
3222 /* Reset the next expected unblock time in case it referred to the
3223 * task that is now in the Suspended state. */
3224 taskENTER_CRITICAL();
3226 prvResetNextTaskUnblockTime();
3228 taskEXIT_CRITICAL();
3232 mtCOVERAGE_TEST_MARKER();
3235 if( pxTCB == pxCurrentTCB )
3237 if( xSchedulerRunning != pdFALSE )
3239 /* The current task has just been suspended. */
3240 configASSERT( uxSchedulerSuspended == 0 );
3241 portYIELD_WITHIN_API();
3245 /* The scheduler is not running, but the task that was pointed
3246 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
3247 * must be adjusted to point to a different task. */
3249 /* Use a temp variable as a distinct sequence point for reading
3250 * volatile variables prior to a comparison to ensure compliance
3251 * with MISRA C 2012 Rule 13.2. */
3252 uxCurrentListLength = listCURRENT_LIST_LENGTH( &xSuspendedTaskList );
3254 if( uxCurrentListLength == uxCurrentNumberOfTasks )
3256 /* No other tasks are ready, so set pxCurrentTCB back to
3257 * NULL so when the next task is created pxCurrentTCB will
3258 * be set to point to it no matter what its relative priority
3260 pxCurrentTCB = NULL;
3264 vTaskSwitchContext();
3270 mtCOVERAGE_TEST_MARKER();
3273 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3275 traceRETURN_vTaskSuspend();
3278 #endif /* INCLUDE_vTaskSuspend */
3279 /*-----------------------------------------------------------*/
3281 #if ( INCLUDE_vTaskSuspend == 1 )
3283 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
3285 BaseType_t xReturn = pdFALSE;
3286 const TCB_t * const pxTCB = xTask;
3288 /* Accesses xPendingReadyList so must be called from a critical
3291 /* It does not make sense to check if the calling task is suspended. */
3292 configASSERT( xTask );
3294 /* Is the task being resumed actually in the suspended list? */
3295 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
3297 /* Has the task already been resumed from within an ISR? */
3298 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
3300 /* Is it in the suspended list because it is in the Suspended
3301 * state, or because it is blocked with no timeout? */
3302 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE )
3304 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3308 /* The task does not appear on the event list item of
3309 * and of the RTOS objects, but could still be in the
3310 * blocked state if it is waiting on its notification
3311 * rather than waiting on an object. If not, is
3315 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3317 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3324 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3328 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3332 mtCOVERAGE_TEST_MARKER();
3337 mtCOVERAGE_TEST_MARKER();
3342 mtCOVERAGE_TEST_MARKER();
3348 #endif /* INCLUDE_vTaskSuspend */
3349 /*-----------------------------------------------------------*/
3351 #if ( INCLUDE_vTaskSuspend == 1 )
3353 void vTaskResume( TaskHandle_t xTaskToResume )
3355 TCB_t * const pxTCB = xTaskToResume;
3357 traceENTER_vTaskResume( xTaskToResume );
3359 /* It does not make sense to resume the calling task. */
3360 configASSERT( xTaskToResume );
3362 #if ( configNUMBER_OF_CORES == 1 )
3364 /* The parameter cannot be NULL as it is impossible to resume the
3365 * currently executing task. */
3366 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
3369 /* The parameter cannot be NULL as it is impossible to resume the
3370 * currently executing task. It is also impossible to resume a task
3371 * that is actively running on another core but it is not safe
3372 * to check their run state here. Therefore, we get into a critical
3373 * section and check if the task is actually suspended or not. */
3377 taskENTER_CRITICAL();
3379 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3381 traceTASK_RESUME( pxTCB );
3383 /* The ready list can be accessed even if the scheduler is
3384 * suspended because this is inside a critical section. */
3385 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3386 prvAddTaskToReadyList( pxTCB );
3388 /* This yield may not cause the task just resumed to run,
3389 * but will leave the lists in the correct state for the
3391 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
3395 mtCOVERAGE_TEST_MARKER();
3398 taskEXIT_CRITICAL();
3402 mtCOVERAGE_TEST_MARKER();
3405 traceRETURN_vTaskResume();
3408 #endif /* INCLUDE_vTaskSuspend */
3410 /*-----------------------------------------------------------*/
3412 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
3414 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
3416 BaseType_t xYieldRequired = pdFALSE;
3417 TCB_t * const pxTCB = xTaskToResume;
3418 UBaseType_t uxSavedInterruptStatus;
3420 traceENTER_xTaskResumeFromISR( xTaskToResume );
3422 configASSERT( xTaskToResume );
3424 /* RTOS ports that support interrupt nesting have the concept of a
3425 * maximum system call (or maximum API call) interrupt priority.
3426 * Interrupts that are above the maximum system call priority are keep
3427 * permanently enabled, even when the RTOS kernel is in a critical section,
3428 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
3429 * is defined in FreeRTOSConfig.h then
3430 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3431 * failure if a FreeRTOS API function is called from an interrupt that has
3432 * been assigned a priority above the configured maximum system call
3433 * priority. Only FreeRTOS functions that end in FromISR can be called
3434 * from interrupts that have been assigned a priority at or (logically)
3435 * below the maximum system call interrupt priority. FreeRTOS maintains a
3436 * separate interrupt safe API to ensure interrupt entry is as fast and as
3437 * simple as possible. More information (albeit Cortex-M specific) is
3438 * provided on the following link:
3439 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3440 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3442 /* MISRA Ref 4.7.1 [Return value shall be checked] */
3443 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
3444 /* coverity[misra_c_2012_directive_4_7_violation] */
3445 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3447 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3449 traceTASK_RESUME_FROM_ISR( pxTCB );
3451 /* Check the ready lists can be accessed. */
3452 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3454 #if ( configNUMBER_OF_CORES == 1 )
3456 /* Ready lists can be accessed so move the task from the
3457 * suspended list to the ready list directly. */
3458 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3460 xYieldRequired = pdTRUE;
3462 /* Mark that a yield is pending in case the user is not
3463 * using the return value to initiate a context switch
3464 * from the ISR using the port specific portYIELD_FROM_ISR(). */
3465 xYieldPendings[ 0 ] = pdTRUE;
3469 mtCOVERAGE_TEST_MARKER();
3472 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3474 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3475 prvAddTaskToReadyList( pxTCB );
3479 /* The delayed or ready lists cannot be accessed so the task
3480 * is held in the pending ready list until the scheduler is
3482 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3485 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3487 prvYieldForTask( pxTCB );
3489 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3491 xYieldRequired = pdTRUE;
3494 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3498 mtCOVERAGE_TEST_MARKER();
3501 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3503 traceRETURN_xTaskResumeFromISR( xYieldRequired );
3505 return xYieldRequired;
3508 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3509 /*-----------------------------------------------------------*/
3511 static BaseType_t prvCreateIdleTasks( void )
3513 BaseType_t xReturn = pdPASS;
3515 char cIdleName[ configMAX_TASK_NAME_LEN ];
3516 TaskFunction_t pxIdleTaskFunction = NULL;
3517 BaseType_t xIdleTaskNameIndex;
3519 for( xIdleTaskNameIndex = ( BaseType_t ) 0; xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN; xIdleTaskNameIndex++ )
3521 cIdleName[ xIdleTaskNameIndex ] = configIDLE_TASK_NAME[ xIdleTaskNameIndex ];
3523 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3524 * configMAX_TASK_NAME_LEN characters just in case the memory after the
3525 * string is not accessible (extremely unlikely). */
3526 if( cIdleName[ xIdleTaskNameIndex ] == ( char ) 0x00 )
3532 mtCOVERAGE_TEST_MARKER();
3536 /* Add each idle task at the lowest priority. */
3537 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3539 #if ( configNUMBER_OF_CORES == 1 )
3541 pxIdleTaskFunction = prvIdleTask;
3543 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3545 /* In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks
3546 * are also created to ensure that each core has an idle task to
3547 * run when no other task is available to run. */
3550 pxIdleTaskFunction = prvIdleTask;
3554 pxIdleTaskFunction = prvPassiveIdleTask;
3557 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3559 /* Update the idle task name with suffix to differentiate the idle tasks.
3560 * This function is not required in single core FreeRTOS since there is
3561 * only one idle task. */
3562 #if ( configNUMBER_OF_CORES > 1 )
3564 /* Append the idle task number to the end of the name if there is space. */
3565 if( xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3567 cIdleName[ xIdleTaskNameIndex ] = ( char ) ( xCoreID + '0' );
3569 /* And append a null character if there is space. */
3570 if( ( xIdleTaskNameIndex + 1 ) < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3572 cIdleName[ xIdleTaskNameIndex + 1 ] = '\0';
3576 mtCOVERAGE_TEST_MARKER();
3581 mtCOVERAGE_TEST_MARKER();
3584 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
3586 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3588 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3589 StackType_t * pxIdleTaskStackBuffer = NULL;
3590 configSTACK_DEPTH_TYPE uxIdleTaskStackSize;
3592 /* The Idle task is created using user provided RAM - obtain the
3593 * address of the RAM then create the idle task. */
3594 #if ( configNUMBER_OF_CORES == 1 )
3596 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
3602 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
3606 vApplicationGetPassiveIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize, ( BaseType_t ) ( xCoreID - 1 ) );
3609 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
3610 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( pxIdleTaskFunction,
3612 uxIdleTaskStackSize,
3614 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3615 pxIdleTaskStackBuffer,
3616 pxIdleTaskTCBBuffer );
3618 if( xIdleTaskHandles[ xCoreID ] != NULL )
3627 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3629 /* The Idle task is being created using dynamically allocated RAM. */
3630 xReturn = xTaskCreate( pxIdleTaskFunction,
3632 configMINIMAL_STACK_SIZE,
3634 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3635 &xIdleTaskHandles[ xCoreID ] );
3637 #endif /* configSUPPORT_STATIC_ALLOCATION */
3639 /* Break the loop if any of the idle task is failed to be created. */
3640 if( xReturn == pdFAIL )
3646 #if ( configNUMBER_OF_CORES == 1 )
3648 mtCOVERAGE_TEST_MARKER();
3652 /* Assign idle task to each core before SMP scheduler is running. */
3653 xIdleTaskHandles[ xCoreID ]->xTaskRunState = xCoreID;
3654 pxCurrentTCBs[ xCoreID ] = xIdleTaskHandles[ xCoreID ];
3663 /*-----------------------------------------------------------*/
3665 void vTaskStartScheduler( void )
3669 traceENTER_vTaskStartScheduler();
3671 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3673 /* Sanity check that the UBaseType_t must have greater than or equal to
3674 * the number of bits as confNUMBER_OF_CORES. */
3675 configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3677 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3679 xReturn = prvCreateIdleTasks();
3681 #if ( configUSE_TIMERS == 1 )
3683 if( xReturn == pdPASS )
3685 xReturn = xTimerCreateTimerTask();
3689 mtCOVERAGE_TEST_MARKER();
3692 #endif /* configUSE_TIMERS */
3694 if( xReturn == pdPASS )
3696 /* freertos_tasks_c_additions_init() should only be called if the user
3697 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3698 * the only macro called by the function. */
3699 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3701 freertos_tasks_c_additions_init();
3705 /* Interrupts are turned off here, to ensure a tick does not occur
3706 * before or during the call to xPortStartScheduler(). The stacks of
3707 * the created tasks contain a status word with interrupts switched on
3708 * so interrupts will automatically get re-enabled when the first task
3710 portDISABLE_INTERRUPTS();
3712 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3714 /* Switch C-Runtime's TLS Block to point to the TLS
3715 * block specific to the task that will run first. */
3716 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3720 xNextTaskUnblockTime = portMAX_DELAY;
3721 xSchedulerRunning = pdTRUE;
3722 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3724 /* If configGENERATE_RUN_TIME_STATS is defined then the following
3725 * macro must be defined to configure the timer/counter used to generate
3726 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
3727 * is set to 0 and the following line fails to build then ensure you do not
3728 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3729 * FreeRTOSConfig.h file. */
3730 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3732 traceTASK_SWITCHED_IN();
3734 /* Setting up the timer tick is hardware specific and thus in the
3735 * portable interface. */
3737 /* The return value for xPortStartScheduler is not required
3738 * hence using a void datatype. */
3739 ( void ) xPortStartScheduler();
3741 /* In most cases, xPortStartScheduler() will not return. If it
3742 * returns pdTRUE then there was not enough heap memory available
3743 * to create either the Idle or the Timer task. If it returned
3744 * pdFALSE, then the application called xTaskEndScheduler().
3745 * Most ports don't implement xTaskEndScheduler() as there is
3746 * nothing to return to. */
3750 /* This line will only be reached if the kernel could not be started,
3751 * because there was not enough FreeRTOS heap to create the idle task
3752 * or the timer task. */
3753 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3756 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3757 * meaning xIdleTaskHandles are not used anywhere else. */
3758 ( void ) xIdleTaskHandles;
3760 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3761 * from getting optimized out as it is no longer used by the kernel. */
3762 ( void ) uxTopUsedPriority;
3764 traceRETURN_vTaskStartScheduler();
3766 /*-----------------------------------------------------------*/
3768 void vTaskEndScheduler( void )
3770 traceENTER_vTaskEndScheduler();
3772 #if ( INCLUDE_vTaskDelete == 1 )
3776 #if ( configUSE_TIMERS == 1 )
3778 /* Delete the timer task created by the kernel. */
3779 vTaskDelete( xTimerGetTimerDaemonTaskHandle() );
3781 #endif /* #if ( configUSE_TIMERS == 1 ) */
3783 /* Delete Idle tasks created by the kernel.*/
3784 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3786 vTaskDelete( xIdleTaskHandles[ xCoreID ] );
3789 /* Idle task is responsible for reclaiming the resources of the tasks in
3790 * xTasksWaitingTermination list. Since the idle task is now deleted and
3791 * no longer going to run, we need to reclaim resources of all the tasks
3792 * in the xTasksWaitingTermination list. */
3793 prvCheckTasksWaitingTermination();
3795 #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
3797 /* Stop the scheduler interrupts and call the portable scheduler end
3798 * routine so the original ISRs can be restored if necessary. The port
3799 * layer must ensure interrupts enable bit is left in the correct state. */
3800 portDISABLE_INTERRUPTS();
3801 xSchedulerRunning = pdFALSE;
3803 /* This function must be called from a task and the application is
3804 * responsible for deleting that task after the scheduler is stopped. */
3805 vPortEndScheduler();
3807 traceRETURN_vTaskEndScheduler();
3809 /*----------------------------------------------------------*/
3811 void vTaskSuspendAll( void )
3813 traceENTER_vTaskSuspendAll();
3815 #if ( configNUMBER_OF_CORES == 1 )
3817 /* A critical section is not required as the variable is of type
3818 * BaseType_t. Please read Richard Barry's reply in the following link to a
3819 * post in the FreeRTOS support forum before reporting this as a bug! -
3820 * https://goo.gl/wu4acr */
3822 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3823 * do not otherwise exhibit real time behaviour. */
3824 portSOFTWARE_BARRIER();
3826 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3827 * is used to allow calls to vTaskSuspendAll() to nest. */
3828 uxSchedulerSuspended = ( UBaseType_t ) ( uxSchedulerSuspended + 1U );
3830 /* Enforces ordering for ports and optimised compilers that may otherwise place
3831 * the above increment elsewhere. */
3832 portMEMORY_BARRIER();
3834 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3836 UBaseType_t ulState;
3838 /* This must only be called from within a task. */
3839 portASSERT_IF_IN_ISR();
3841 if( xSchedulerRunning != pdFALSE )
3843 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3844 * We must disable interrupts before we grab the locks in the event that this task is
3845 * interrupted and switches context before incrementing uxSchedulerSuspended.
3846 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3847 * uxSchedulerSuspended since that will prevent context switches. */
3848 ulState = portSET_INTERRUPT_MASK();
3850 /* This must never be called from inside a critical section. */
3851 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
3853 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3854 * do not otherwise exhibit real time behaviour. */
3855 portSOFTWARE_BARRIER();
3857 portGET_TASK_LOCK();
3859 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3860 * purpose is to prevent altering the variable when fromISR APIs are readying
3862 if( uxSchedulerSuspended == 0U )
3864 prvCheckForRunStateChange();
3868 mtCOVERAGE_TEST_MARKER();
3873 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3874 * is used to allow calls to vTaskSuspendAll() to nest. */
3875 ++uxSchedulerSuspended;
3876 portRELEASE_ISR_LOCK();
3878 portCLEAR_INTERRUPT_MASK( ulState );
3882 mtCOVERAGE_TEST_MARKER();
3885 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3887 traceRETURN_vTaskSuspendAll();
3890 /*----------------------------------------------------------*/
3892 #if ( configUSE_TICKLESS_IDLE != 0 )
3894 static TickType_t prvGetExpectedIdleTime( void )
3897 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
3899 /* uxHigherPriorityReadyTasks takes care of the case where
3900 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3901 * task that are in the Ready state, even though the idle task is
3903 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3905 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3907 uxHigherPriorityReadyTasks = pdTRUE;
3912 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3914 /* When port optimised task selection is used the uxTopReadyPriority
3915 * variable is used as a bit map. If bits other than the least
3916 * significant bit are set then there are tasks that have a priority
3917 * above the idle priority that are in the Ready state. This takes
3918 * care of the case where the co-operative scheduler is in use. */
3919 if( uxTopReadyPriority > uxLeastSignificantBit )
3921 uxHigherPriorityReadyTasks = pdTRUE;
3924 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3926 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3930 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1U )
3932 /* There are other idle priority tasks in the ready state. If
3933 * time slicing is used then the very next tick interrupt must be
3937 else if( uxHigherPriorityReadyTasks != pdFALSE )
3939 /* There are tasks in the Ready state that have a priority above the
3940 * idle priority. This path can only be reached if
3941 * configUSE_PREEMPTION is 0. */
3946 xReturn = xNextTaskUnblockTime;
3947 xReturn -= xTickCount;
3953 #endif /* configUSE_TICKLESS_IDLE */
3954 /*----------------------------------------------------------*/
3956 BaseType_t xTaskResumeAll( void )
3958 TCB_t * pxTCB = NULL;
3959 BaseType_t xAlreadyYielded = pdFALSE;
3961 traceENTER_xTaskResumeAll();
3963 #if ( configNUMBER_OF_CORES > 1 )
3964 if( xSchedulerRunning != pdFALSE )
3967 /* It is possible that an ISR caused a task to be removed from an event
3968 * list while the scheduler was suspended. If this was the case then the
3969 * removed task will have been added to the xPendingReadyList. Once the
3970 * scheduler has been resumed it is safe to move all the pending ready
3971 * tasks from this list into their appropriate ready list. */
3972 taskENTER_CRITICAL();
3975 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3977 /* If uxSchedulerSuspended is zero then this function does not match a
3978 * previous call to vTaskSuspendAll(). */
3979 configASSERT( uxSchedulerSuspended != 0U );
3981 uxSchedulerSuspended = ( UBaseType_t ) ( uxSchedulerSuspended - 1U );
3982 portRELEASE_TASK_LOCK();
3984 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3986 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3988 /* Move any readied tasks from the pending list into the
3989 * appropriate ready list. */
3990 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3992 /* MISRA Ref 11.5.3 [Void pointer assignment] */
3993 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
3994 /* coverity[misra_c_2012_rule_11_5_violation] */
3995 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) );
3996 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
3997 portMEMORY_BARRIER();
3998 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
3999 prvAddTaskToReadyList( pxTCB );
4001 #if ( configNUMBER_OF_CORES == 1 )
4003 /* If the moved task has a priority higher than the current
4004 * task then a yield must be performed. */
4005 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4007 xYieldPendings[ xCoreID ] = pdTRUE;
4011 mtCOVERAGE_TEST_MARKER();
4014 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4016 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
4017 * If the current core yielded then vTaskSwitchContext() has already been called
4018 * which sets xYieldPendings for the current core to pdTRUE. */
4020 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4025 /* A task was unblocked while the scheduler was suspended,
4026 * which may have prevented the next unblock time from being
4027 * re-calculated, in which case re-calculate it now. Mainly
4028 * important for low power tickless implementations, where
4029 * this can prevent an unnecessary exit from low power
4031 prvResetNextTaskUnblockTime();
4034 /* If any ticks occurred while the scheduler was suspended then
4035 * they should be processed now. This ensures the tick count does
4036 * not slip, and that any delayed tasks are resumed at the correct
4039 * It should be safe to call xTaskIncrementTick here from any core
4040 * since we are in a critical section and xTaskIncrementTick itself
4041 * protects itself within a critical section. Suspending the scheduler
4042 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
4044 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
4046 if( xPendedCounts > ( TickType_t ) 0U )
4050 if( xTaskIncrementTick() != pdFALSE )
4052 /* Other cores are interrupted from
4053 * within xTaskIncrementTick(). */
4054 xYieldPendings[ xCoreID ] = pdTRUE;
4058 mtCOVERAGE_TEST_MARKER();
4062 } while( xPendedCounts > ( TickType_t ) 0U );
4068 mtCOVERAGE_TEST_MARKER();
4072 if( xYieldPendings[ xCoreID ] != pdFALSE )
4074 #if ( configUSE_PREEMPTION != 0 )
4076 xAlreadyYielded = pdTRUE;
4078 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
4080 #if ( configNUMBER_OF_CORES == 1 )
4082 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
4084 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4088 mtCOVERAGE_TEST_MARKER();
4094 mtCOVERAGE_TEST_MARKER();
4097 taskEXIT_CRITICAL();
4100 traceRETURN_xTaskResumeAll( xAlreadyYielded );
4102 return xAlreadyYielded;
4104 /*-----------------------------------------------------------*/
4106 TickType_t xTaskGetTickCount( void )
4110 traceENTER_xTaskGetTickCount();
4112 /* Critical section required if running on a 16 bit processor. */
4113 portTICK_TYPE_ENTER_CRITICAL();
4115 xTicks = xTickCount;
4117 portTICK_TYPE_EXIT_CRITICAL();
4119 traceRETURN_xTaskGetTickCount( xTicks );
4123 /*-----------------------------------------------------------*/
4125 TickType_t xTaskGetTickCountFromISR( void )
4128 UBaseType_t uxSavedInterruptStatus;
4130 traceENTER_xTaskGetTickCountFromISR();
4132 /* RTOS ports that support interrupt nesting have the concept of a maximum
4133 * system call (or maximum API call) interrupt priority. Interrupts that are
4134 * above the maximum system call priority are kept permanently enabled, even
4135 * when the RTOS kernel is in a critical section, but cannot make any calls to
4136 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
4137 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
4138 * failure if a FreeRTOS API function is called from an interrupt that has been
4139 * assigned a priority above the configured maximum system call priority.
4140 * Only FreeRTOS functions that end in FromISR can be called from interrupts
4141 * that have been assigned a priority at or (logically) below the maximum
4142 * system call interrupt priority. FreeRTOS maintains a separate interrupt
4143 * safe API to ensure interrupt entry is as fast and as simple as possible.
4144 * More information (albeit Cortex-M specific) is provided on the following
4145 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
4146 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
4148 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
4150 xReturn = xTickCount;
4152 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
4154 traceRETURN_xTaskGetTickCountFromISR( xReturn );
4158 /*-----------------------------------------------------------*/
4160 UBaseType_t uxTaskGetNumberOfTasks( void )
4162 traceENTER_uxTaskGetNumberOfTasks();
4164 /* A critical section is not required because the variables are of type
4166 traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
4168 return uxCurrentNumberOfTasks;
4170 /*-----------------------------------------------------------*/
4172 char * pcTaskGetName( TaskHandle_t xTaskToQuery )
4176 traceENTER_pcTaskGetName( xTaskToQuery );
4178 /* If null is passed in here then the name of the calling task is being
4180 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4181 configASSERT( pxTCB );
4183 traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
4185 return &( pxTCB->pcTaskName[ 0 ] );
4187 /*-----------------------------------------------------------*/
4189 #if ( INCLUDE_xTaskGetHandle == 1 )
4190 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4191 const char pcNameToQuery[] )
4193 TCB_t * pxReturn = NULL;
4194 TCB_t * pxTCB = NULL;
4197 BaseType_t xBreakLoop;
4198 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
4199 ListItem_t * pxIterator;
4201 /* This function is called with the scheduler suspended. */
4203 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4205 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
4207 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4208 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4209 /* coverity[misra_c_2012_rule_11_5_violation] */
4210 pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
4212 /* Check each character in the name looking for a match or
4214 xBreakLoop = pdFALSE;
4216 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4218 cNextChar = pxTCB->pcTaskName[ x ];
4220 if( cNextChar != pcNameToQuery[ x ] )
4222 /* Characters didn't match. */
4223 xBreakLoop = pdTRUE;
4225 else if( cNextChar == ( char ) 0x00 )
4227 /* Both strings terminated, a match must have been
4230 xBreakLoop = pdTRUE;
4234 mtCOVERAGE_TEST_MARKER();
4237 if( xBreakLoop != pdFALSE )
4243 if( pxReturn != NULL )
4245 /* The handle has been found. */
4252 mtCOVERAGE_TEST_MARKER();
4258 #endif /* INCLUDE_xTaskGetHandle */
4259 /*-----------------------------------------------------------*/
4261 #if ( INCLUDE_xTaskGetHandle == 1 )
4263 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery )
4265 UBaseType_t uxQueue = configMAX_PRIORITIES;
4268 traceENTER_xTaskGetHandle( pcNameToQuery );
4270 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
4271 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
4275 /* Search the ready lists. */
4279 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
4283 /* Found the handle. */
4286 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4288 /* Search the delayed lists. */
4291 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
4296 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
4299 #if ( INCLUDE_vTaskSuspend == 1 )
4303 /* Search the suspended list. */
4304 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
4309 #if ( INCLUDE_vTaskDelete == 1 )
4313 /* Search the deleted list. */
4314 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
4319 ( void ) xTaskResumeAll();
4321 traceRETURN_xTaskGetHandle( pxTCB );
4326 #endif /* INCLUDE_xTaskGetHandle */
4327 /*-----------------------------------------------------------*/
4329 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4331 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4332 StackType_t ** ppuxStackBuffer,
4333 StaticTask_t ** ppxTaskBuffer )
4338 traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4340 configASSERT( ppuxStackBuffer != NULL );
4341 configASSERT( ppxTaskBuffer != NULL );
4343 pxTCB = prvGetTCBFromHandle( xTask );
4345 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4347 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4349 *ppuxStackBuffer = pxTCB->pxStack;
4350 /* MISRA Ref 11.3.1 [Misaligned access] */
4351 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
4352 /* coverity[misra_c_2012_rule_11_3_violation] */
4353 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4356 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4358 *ppuxStackBuffer = pxTCB->pxStack;
4359 *ppxTaskBuffer = NULL;
4367 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4369 *ppuxStackBuffer = pxTCB->pxStack;
4370 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4373 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4375 traceRETURN_xTaskGetStaticBuffers( xReturn );
4380 #endif /* configSUPPORT_STATIC_ALLOCATION */
4381 /*-----------------------------------------------------------*/
4383 #if ( configUSE_TRACE_FACILITY == 1 )
4385 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4386 const UBaseType_t uxArraySize,
4387 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4389 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4391 traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4395 /* Is there a space in the array for each task in the system? */
4396 if( uxArraySize >= uxCurrentNumberOfTasks )
4398 /* Fill in an TaskStatus_t structure with information on each
4399 * task in the Ready state. */
4403 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4404 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4406 /* Fill in an TaskStatus_t structure with information on each
4407 * task in the Blocked state. */
4408 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4409 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4411 #if ( INCLUDE_vTaskDelete == 1 )
4413 /* Fill in an TaskStatus_t structure with information on
4414 * each task that has been deleted but not yet cleaned up. */
4415 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4419 #if ( INCLUDE_vTaskSuspend == 1 )
4421 /* Fill in an TaskStatus_t structure with information on
4422 * each task in the Suspended state. */
4423 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4427 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4429 if( pulTotalRunTime != NULL )
4431 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4432 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4434 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4438 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4440 if( pulTotalRunTime != NULL )
4442 *pulTotalRunTime = 0;
4445 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4449 mtCOVERAGE_TEST_MARKER();
4452 ( void ) xTaskResumeAll();
4454 traceRETURN_uxTaskGetSystemState( uxTask );
4459 #endif /* configUSE_TRACE_FACILITY */
4460 /*----------------------------------------------------------*/
4462 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4464 #if ( configNUMBER_OF_CORES == 1 )
4465 TaskHandle_t xTaskGetIdleTaskHandle( void )
4467 traceENTER_xTaskGetIdleTaskHandle();
4469 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4470 * started, then xIdleTaskHandles will be NULL. */
4471 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4473 traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4475 return xIdleTaskHandles[ 0 ];
4477 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
4479 TaskHandle_t xTaskGetIdleTaskHandleForCore( BaseType_t xCoreID )
4481 traceENTER_xTaskGetIdleTaskHandleForCore( xCoreID );
4483 /* Ensure the core ID is valid. */
4484 configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE );
4486 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4487 * started, then xIdleTaskHandles will be NULL. */
4488 configASSERT( ( xIdleTaskHandles[ xCoreID ] != NULL ) );
4490 traceRETURN_xTaskGetIdleTaskHandleForCore( xIdleTaskHandles[ xCoreID ] );
4492 return xIdleTaskHandles[ xCoreID ];
4495 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4496 /*----------------------------------------------------------*/
4498 /* This conditional compilation should use inequality to 0, not equality to 1.
4499 * This is to ensure vTaskStepTick() is available when user defined low power mode
4500 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4502 #if ( configUSE_TICKLESS_IDLE != 0 )
4504 void vTaskStepTick( TickType_t xTicksToJump )
4506 TickType_t xUpdatedTickCount;
4508 traceENTER_vTaskStepTick( xTicksToJump );
4510 /* Correct the tick count value after a period during which the tick
4511 * was suppressed. Note this does *not* call the tick hook function for
4512 * each stepped tick. */
4513 xUpdatedTickCount = xTickCount + xTicksToJump;
4514 configASSERT( xUpdatedTickCount <= xNextTaskUnblockTime );
4516 if( xUpdatedTickCount == xNextTaskUnblockTime )
4518 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4519 * xTaskIncrementTick() when the scheduler resumes. This ensures
4520 * that any delayed tasks are resumed at the correct time. */
4521 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4522 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4524 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4525 taskENTER_CRITICAL();
4529 taskEXIT_CRITICAL();
4534 mtCOVERAGE_TEST_MARKER();
4537 xTickCount += xTicksToJump;
4539 traceINCREASE_TICK_COUNT( xTicksToJump );
4540 traceRETURN_vTaskStepTick();
4543 #endif /* configUSE_TICKLESS_IDLE */
4544 /*----------------------------------------------------------*/
4546 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4548 BaseType_t xYieldOccurred;
4550 traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4552 /* Must not be called with the scheduler suspended as the implementation
4553 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4554 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4556 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4557 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4560 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4561 taskENTER_CRITICAL();
4563 xPendedTicks += xTicksToCatchUp;
4565 taskEXIT_CRITICAL();
4566 xYieldOccurred = xTaskResumeAll();
4568 traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4570 return xYieldOccurred;
4572 /*----------------------------------------------------------*/
4574 #if ( INCLUDE_xTaskAbortDelay == 1 )
4576 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4578 TCB_t * pxTCB = xTask;
4581 traceENTER_xTaskAbortDelay( xTask );
4583 configASSERT( pxTCB );
4587 /* A task can only be prematurely removed from the Blocked state if
4588 * it is actually in the Blocked state. */
4589 if( eTaskGetState( xTask ) == eBlocked )
4593 /* Remove the reference to the task from the blocked list. An
4594 * interrupt won't touch the xStateListItem because the
4595 * scheduler is suspended. */
4596 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4598 /* Is the task waiting on an event also? If so remove it from
4599 * the event list too. Interrupts can touch the event list item,
4600 * even though the scheduler is suspended, so a critical section
4602 taskENTER_CRITICAL();
4604 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4606 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4608 /* This lets the task know it was forcibly removed from the
4609 * blocked state so it should not re-evaluate its block time and
4610 * then block again. */
4611 pxTCB->ucDelayAborted = ( uint8_t ) pdTRUE;
4615 mtCOVERAGE_TEST_MARKER();
4618 taskEXIT_CRITICAL();
4620 /* Place the unblocked task into the appropriate ready list. */
4621 prvAddTaskToReadyList( pxTCB );
4623 /* A task being unblocked cannot cause an immediate context
4624 * switch if preemption is turned off. */
4625 #if ( configUSE_PREEMPTION == 1 )
4627 #if ( configNUMBER_OF_CORES == 1 )
4629 /* Preemption is on, but a context switch should only be
4630 * performed if the unblocked task has a priority that is
4631 * higher than the currently executing task. */
4632 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4634 /* Pend the yield to be performed when the scheduler
4635 * is unsuspended. */
4636 xYieldPendings[ 0 ] = pdTRUE;
4640 mtCOVERAGE_TEST_MARKER();
4643 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4645 taskENTER_CRITICAL();
4647 prvYieldForTask( pxTCB );
4649 taskEXIT_CRITICAL();
4651 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4653 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4660 ( void ) xTaskResumeAll();
4662 traceRETURN_xTaskAbortDelay( xReturn );
4667 #endif /* INCLUDE_xTaskAbortDelay */
4668 /*----------------------------------------------------------*/
4670 BaseType_t xTaskIncrementTick( void )
4673 TickType_t xItemValue;
4674 BaseType_t xSwitchRequired = pdFALSE;
4676 #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4677 BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4678 #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4680 traceENTER_xTaskIncrementTick();
4682 /* Called by the portable layer each time a tick interrupt occurs.
4683 * Increments the tick then checks to see if the new tick value will cause any
4684 * tasks to be unblocked. */
4685 traceTASK_INCREMENT_TICK( xTickCount );
4687 /* Tick increment should occur on every kernel timer event. Core 0 has the
4688 * responsibility to increment the tick, or increment the pended ticks if the
4689 * scheduler is suspended. If pended ticks is greater than zero, the core that
4690 * calls xTaskResumeAll has the responsibility to increment the tick. */
4691 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4693 /* Minor optimisation. The tick count cannot change in this
4695 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4697 /* Increment the RTOS tick, switching the delayed and overflowed
4698 * delayed lists if it wraps to 0. */
4699 xTickCount = xConstTickCount;
4701 if( xConstTickCount == ( TickType_t ) 0U )
4703 taskSWITCH_DELAYED_LISTS();
4707 mtCOVERAGE_TEST_MARKER();
4710 /* See if this tick has made a timeout expire. Tasks are stored in
4711 * the queue in the order of their wake time - meaning once one task
4712 * has been found whose block time has not expired there is no need to
4713 * look any further down the list. */
4714 if( xConstTickCount >= xNextTaskUnblockTime )
4718 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4720 /* The delayed list is empty. Set xNextTaskUnblockTime
4721 * to the maximum possible value so it is extremely
4723 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4724 * next time through. */
4725 xNextTaskUnblockTime = portMAX_DELAY;
4730 /* The delayed list is not empty, get the value of the
4731 * item at the head of the delayed list. This is the time
4732 * at which the task at the head of the delayed list must
4733 * be removed from the Blocked state. */
4734 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4735 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4736 /* coverity[misra_c_2012_rule_11_5_violation] */
4737 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
4738 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4740 if( xConstTickCount < xItemValue )
4742 /* It is not time to unblock this item yet, but the
4743 * item value is the time at which the task at the head
4744 * of the blocked list must be removed from the Blocked
4745 * state - so record the item value in
4746 * xNextTaskUnblockTime. */
4747 xNextTaskUnblockTime = xItemValue;
4752 mtCOVERAGE_TEST_MARKER();
4755 /* It is time to remove the item from the Blocked state. */
4756 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4758 /* Is the task waiting on an event also? If so remove
4759 * it from the event list. */
4760 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4762 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4766 mtCOVERAGE_TEST_MARKER();
4769 /* Place the unblocked task into the appropriate ready
4771 prvAddTaskToReadyList( pxTCB );
4773 /* A task being unblocked cannot cause an immediate
4774 * context switch if preemption is turned off. */
4775 #if ( configUSE_PREEMPTION == 1 )
4777 #if ( configNUMBER_OF_CORES == 1 )
4779 /* Preemption is on, but a context switch should
4780 * only be performed if the unblocked task's
4781 * priority is higher than the currently executing
4783 * The case of equal priority tasks sharing
4784 * processing time (which happens when both
4785 * preemption and time slicing are on) is
4787 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4789 xSwitchRequired = pdTRUE;
4793 mtCOVERAGE_TEST_MARKER();
4796 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4798 prvYieldForTask( pxTCB );
4800 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4802 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4807 /* Tasks of equal priority to the currently running task will share
4808 * processing time (time slice) if preemption is on, and the application
4809 * writer has not explicitly turned time slicing off. */
4810 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4812 #if ( configNUMBER_OF_CORES == 1 )
4814 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > 1U )
4816 xSwitchRequired = pdTRUE;
4820 mtCOVERAGE_TEST_MARKER();
4823 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4827 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4829 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1U )
4831 xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4835 mtCOVERAGE_TEST_MARKER();
4839 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4841 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4843 #if ( configUSE_TICK_HOOK == 1 )
4845 /* Guard against the tick hook being called when the pended tick
4846 * count is being unwound (when the scheduler is being unlocked). */
4847 if( xPendedTicks == ( TickType_t ) 0 )
4849 vApplicationTickHook();
4853 mtCOVERAGE_TEST_MARKER();
4856 #endif /* configUSE_TICK_HOOK */
4858 #if ( configUSE_PREEMPTION == 1 )
4860 #if ( configNUMBER_OF_CORES == 1 )
4862 /* For single core the core ID is always 0. */
4863 if( xYieldPendings[ 0 ] != pdFALSE )
4865 xSwitchRequired = pdTRUE;
4869 mtCOVERAGE_TEST_MARKER();
4872 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4874 BaseType_t xCoreID, xCurrentCoreID;
4875 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4877 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4879 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4880 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4883 if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4885 if( xCoreID == xCurrentCoreID )
4887 xSwitchRequired = pdTRUE;
4891 prvYieldCore( xCoreID );
4896 mtCOVERAGE_TEST_MARKER();
4901 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4903 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4909 /* The tick hook gets called at regular intervals, even if the
4910 * scheduler is locked. */
4911 #if ( configUSE_TICK_HOOK == 1 )
4913 vApplicationTickHook();
4918 traceRETURN_xTaskIncrementTick( xSwitchRequired );
4920 return xSwitchRequired;
4922 /*-----------------------------------------------------------*/
4924 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4926 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4927 TaskHookFunction_t pxHookFunction )
4931 traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4933 /* If xTask is NULL then it is the task hook of the calling task that is
4937 xTCB = ( TCB_t * ) pxCurrentTCB;
4944 /* Save the hook function in the TCB. A critical section is required as
4945 * the value can be accessed from an interrupt. */
4946 taskENTER_CRITICAL();
4948 xTCB->pxTaskTag = pxHookFunction;
4950 taskEXIT_CRITICAL();
4952 traceRETURN_vTaskSetApplicationTaskTag();
4955 #endif /* configUSE_APPLICATION_TASK_TAG */
4956 /*-----------------------------------------------------------*/
4958 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4960 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4963 TaskHookFunction_t xReturn;
4965 traceENTER_xTaskGetApplicationTaskTag( xTask );
4967 /* If xTask is NULL then set the calling task's hook. */
4968 pxTCB = prvGetTCBFromHandle( xTask );
4970 /* Save the hook function in the TCB. A critical section is required as
4971 * the value can be accessed from an interrupt. */
4972 taskENTER_CRITICAL();
4974 xReturn = pxTCB->pxTaskTag;
4976 taskEXIT_CRITICAL();
4978 traceRETURN_xTaskGetApplicationTaskTag( xReturn );
4983 #endif /* configUSE_APPLICATION_TASK_TAG */
4984 /*-----------------------------------------------------------*/
4986 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4988 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
4991 TaskHookFunction_t xReturn;
4992 UBaseType_t uxSavedInterruptStatus;
4994 traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
4996 /* If xTask is NULL then set the calling task's hook. */
4997 pxTCB = prvGetTCBFromHandle( xTask );
4999 /* Save the hook function in the TCB. A critical section is required as
5000 * the value can be accessed from an interrupt. */
5001 /* MISRA Ref 4.7.1 [Return value shall be checked] */
5002 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
5003 /* coverity[misra_c_2012_directive_4_7_violation] */
5004 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
5006 xReturn = pxTCB->pxTaskTag;
5008 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
5010 traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
5015 #endif /* configUSE_APPLICATION_TASK_TAG */
5016 /*-----------------------------------------------------------*/
5018 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5020 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
5021 void * pvParameter )
5026 traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
5028 /* If xTask is NULL then we are calling our own task hook. */
5031 xTCB = pxCurrentTCB;
5038 if( xTCB->pxTaskTag != NULL )
5040 xReturn = xTCB->pxTaskTag( pvParameter );
5047 traceRETURN_xTaskCallApplicationTaskHook( xReturn );
5052 #endif /* configUSE_APPLICATION_TASK_TAG */
5053 /*-----------------------------------------------------------*/
5055 #if ( configNUMBER_OF_CORES == 1 )
5056 void vTaskSwitchContext( void )
5058 traceENTER_vTaskSwitchContext();
5060 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5062 /* The scheduler is currently suspended - do not allow a context
5064 xYieldPendings[ 0 ] = pdTRUE;
5068 xYieldPendings[ 0 ] = pdFALSE;
5069 traceTASK_SWITCHED_OUT();
5071 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5073 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5074 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
5076 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
5079 /* Add the amount of time the task has been running to the
5080 * accumulated time so far. The time the task started running was
5081 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5082 * protection here so count values are only valid until the timer
5083 * overflows. The guard against negative values is to protect
5084 * against suspect run time stat counter implementations - which
5085 * are provided by the application, not the kernel. */
5086 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
5088 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
5092 mtCOVERAGE_TEST_MARKER();
5095 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
5097 #endif /* configGENERATE_RUN_TIME_STATS */
5099 /* Check for stack overflow, if configured. */
5100 taskCHECK_FOR_STACK_OVERFLOW();
5102 /* Before the currently running task is switched out, save its errno. */
5103 #if ( configUSE_POSIX_ERRNO == 1 )
5105 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
5109 /* Select a new task to run using either the generic C or port
5110 * optimised asm code. */
5111 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5112 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5113 /* coverity[misra_c_2012_rule_11_5_violation] */
5114 taskSELECT_HIGHEST_PRIORITY_TASK();
5115 traceTASK_SWITCHED_IN();
5117 /* Macro to inject port specific behaviour immediately after
5118 * switching tasks, such as setting an end of stack watchpoint
5119 * or reconfiguring the MPU. */
5120 portTASK_SWITCH_HOOK( pxCurrentTCB );
5122 /* After the new task is switched in, update the global errno. */
5123 #if ( configUSE_POSIX_ERRNO == 1 )
5125 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
5129 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5131 /* Switch C-Runtime's TLS Block to point to the TLS
5132 * Block specific to this task. */
5133 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
5138 traceRETURN_vTaskSwitchContext();
5140 #else /* if ( configNUMBER_OF_CORES == 1 ) */
5141 void vTaskSwitchContext( BaseType_t xCoreID )
5143 traceENTER_vTaskSwitchContext();
5145 /* Acquire both locks:
5146 * - The ISR lock protects the ready list from simultaneous access by
5147 * both other ISRs and tasks.
5148 * - We also take the task lock to pause here in case another core has
5149 * suspended the scheduler. We don't want to simply set xYieldPending
5150 * and move on if another core suspended the scheduler. We should only
5151 * do that if the current core has suspended the scheduler. */
5153 portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
5156 /* vTaskSwitchContext() must never be called from within a critical section.
5157 * This is not necessarily true for single core FreeRTOS, but it is for this
5159 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
5161 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5163 /* The scheduler is currently suspended - do not allow a context
5165 xYieldPendings[ xCoreID ] = pdTRUE;
5169 xYieldPendings[ xCoreID ] = pdFALSE;
5170 traceTASK_SWITCHED_OUT();
5172 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5174 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5175 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
5177 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
5180 /* Add the amount of time the task has been running to the
5181 * accumulated time so far. The time the task started running was
5182 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5183 * protection here so count values are only valid until the timer
5184 * overflows. The guard against negative values is to protect
5185 * against suspect run time stat counter implementations - which
5186 * are provided by the application, not the kernel. */
5187 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
5189 pxCurrentTCBs[ xCoreID ]->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
5193 mtCOVERAGE_TEST_MARKER();
5196 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
5198 #endif /* configGENERATE_RUN_TIME_STATS */
5200 /* Check for stack overflow, if configured. */
5201 taskCHECK_FOR_STACK_OVERFLOW();
5203 /* Before the currently running task is switched out, save its errno. */
5204 #if ( configUSE_POSIX_ERRNO == 1 )
5206 pxCurrentTCBs[ xCoreID ]->iTaskErrno = FreeRTOS_errno;
5210 /* Select a new task to run. */
5211 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
5212 traceTASK_SWITCHED_IN();
5214 /* Macro to inject port specific behaviour immediately after
5215 * switching tasks, such as setting an end of stack watchpoint
5216 * or reconfiguring the MPU. */
5217 portTASK_SWITCH_HOOK( pxCurrentTCBs[ portGET_CORE_ID() ] );
5219 /* After the new task is switched in, update the global errno. */
5220 #if ( configUSE_POSIX_ERRNO == 1 )
5222 FreeRTOS_errno = pxCurrentTCBs[ xCoreID ]->iTaskErrno;
5226 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5228 /* Switch C-Runtime's TLS Block to point to the TLS
5229 * Block specific to this task. */
5230 configSET_TLS_BLOCK( pxCurrentTCBs[ xCoreID ]->xTLSBlock );
5235 portRELEASE_ISR_LOCK();
5236 portRELEASE_TASK_LOCK();
5238 traceRETURN_vTaskSwitchContext();
5240 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
5241 /*-----------------------------------------------------------*/
5243 void vTaskPlaceOnEventList( List_t * const pxEventList,
5244 const TickType_t xTicksToWait )
5246 traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
5248 configASSERT( pxEventList );
5250 /* THIS FUNCTION MUST BE CALLED WITH THE
5251 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
5253 /* Place the event list item of the TCB in the appropriate event list.
5254 * This is placed in the list in priority order so the highest priority task
5255 * is the first to be woken by the event.
5257 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
5258 * Normally, the xItemValue of a TCB's ListItem_t members is:
5259 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
5260 * Therefore, the event list is sorted in descending priority order.
5262 * The queue that contains the event list is locked, preventing
5263 * simultaneous access from interrupts. */
5264 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5266 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5268 traceRETURN_vTaskPlaceOnEventList();
5270 /*-----------------------------------------------------------*/
5272 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
5273 const TickType_t xItemValue,
5274 const TickType_t xTicksToWait )
5276 traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
5278 configASSERT( pxEventList );
5280 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5281 * the event groups implementation. */
5282 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5284 /* Store the item value in the event list item. It is safe to access the
5285 * event list item here as interrupts won't access the event list item of a
5286 * task that is not in the Blocked state. */
5287 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5289 /* Place the event list item of the TCB at the end of the appropriate event
5290 * list. It is safe to access the event list here because it is part of an
5291 * event group implementation - and interrupts don't access event groups
5292 * directly (instead they access them indirectly by pending function calls to
5293 * the task level). */
5294 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5296 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5298 traceRETURN_vTaskPlaceOnUnorderedEventList();
5300 /*-----------------------------------------------------------*/
5302 #if ( configUSE_TIMERS == 1 )
5304 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
5305 TickType_t xTicksToWait,
5306 const BaseType_t xWaitIndefinitely )
5308 traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
5310 configASSERT( pxEventList );
5312 /* This function should not be called by application code hence the
5313 * 'Restricted' in its name. It is not part of the public API. It is
5314 * designed for use by kernel code, and has special calling requirements -
5315 * it should be called with the scheduler suspended. */
5318 /* Place the event list item of the TCB in the appropriate event list.
5319 * In this case it is assume that this is the only task that is going to
5320 * be waiting on this event list, so the faster vListInsertEnd() function
5321 * can be used in place of vListInsert. */
5322 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5324 /* If the task should block indefinitely then set the block time to a
5325 * value that will be recognised as an indefinite delay inside the
5326 * prvAddCurrentTaskToDelayedList() function. */
5327 if( xWaitIndefinitely != pdFALSE )
5329 xTicksToWait = portMAX_DELAY;
5332 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
5333 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
5335 traceRETURN_vTaskPlaceOnEventListRestricted();
5338 #endif /* configUSE_TIMERS */
5339 /*-----------------------------------------------------------*/
5341 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
5343 TCB_t * pxUnblockedTCB;
5346 traceENTER_xTaskRemoveFromEventList( pxEventList );
5348 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
5349 * called from a critical section within an ISR. */
5351 /* The event list is sorted in priority order, so the first in the list can
5352 * be removed as it is known to be the highest priority. Remove the TCB from
5353 * the delayed list, and add it to the ready list.
5355 * If an event is for a queue that is locked then this function will never
5356 * get called - the lock count on the queue will get modified instead. This
5357 * means exclusive access to the event list is guaranteed here.
5359 * This function assumes that a check has already been made to ensure that
5360 * pxEventList is not empty. */
5361 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5362 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5363 /* coverity[misra_c_2012_rule_11_5_violation] */
5364 pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
5365 configASSERT( pxUnblockedTCB );
5366 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5368 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5370 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5371 prvAddTaskToReadyList( pxUnblockedTCB );
5373 #if ( configUSE_TICKLESS_IDLE != 0 )
5375 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5376 * might be set to the blocked task's time out time. If the task is
5377 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5378 * normally left unchanged, because it is automatically reset to a new
5379 * value when the tick count equals xNextTaskUnblockTime. However if
5380 * tickless idling is used it might be more important to enter sleep mode
5381 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5382 * ensure it is updated at the earliest possible time. */
5383 prvResetNextTaskUnblockTime();
5389 /* The delayed and ready lists cannot be accessed, so hold this task
5390 * pending until the scheduler is resumed. */
5391 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5394 #if ( configNUMBER_OF_CORES == 1 )
5396 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5398 /* Return true if the task removed from the event list has a higher
5399 * priority than the calling task. This allows the calling task to know if
5400 * it should force a context switch now. */
5403 /* Mark that a yield is pending in case the user is not using the
5404 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5405 xYieldPendings[ 0 ] = pdTRUE;
5412 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5416 #if ( configUSE_PREEMPTION == 1 )
5418 prvYieldForTask( pxUnblockedTCB );
5420 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5425 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5427 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5429 traceRETURN_xTaskRemoveFromEventList( xReturn );
5432 /*-----------------------------------------------------------*/
5434 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5435 const TickType_t xItemValue )
5437 TCB_t * pxUnblockedTCB;
5439 traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5441 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5442 * the event flags implementation. */
5443 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5445 /* Store the new item value in the event list. */
5446 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5448 /* Remove the event list form the event flag. Interrupts do not access
5450 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5451 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5452 /* coverity[misra_c_2012_rule_11_5_violation] */
5453 pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem );
5454 configASSERT( pxUnblockedTCB );
5455 listREMOVE_ITEM( pxEventListItem );
5457 #if ( configUSE_TICKLESS_IDLE != 0 )
5459 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5460 * might be set to the blocked task's time out time. If the task is
5461 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5462 * normally left unchanged, because it is automatically reset to a new
5463 * value when the tick count equals xNextTaskUnblockTime. However if
5464 * tickless idling is used it might be more important to enter sleep mode
5465 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5466 * ensure it is updated at the earliest possible time. */
5467 prvResetNextTaskUnblockTime();
5471 /* Remove the task from the delayed list and add it to the ready list. The
5472 * scheduler is suspended so interrupts will not be accessing the ready
5474 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5475 prvAddTaskToReadyList( pxUnblockedTCB );
5477 #if ( configNUMBER_OF_CORES == 1 )
5479 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5481 /* The unblocked task has a priority above that of the calling task, so
5482 * a context switch is required. This function is called with the
5483 * scheduler suspended so xYieldPending is set so the context switch
5484 * occurs immediately that the scheduler is resumed (unsuspended). */
5485 xYieldPendings[ 0 ] = pdTRUE;
5488 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5490 #if ( configUSE_PREEMPTION == 1 )
5492 taskENTER_CRITICAL();
5494 prvYieldForTask( pxUnblockedTCB );
5496 taskEXIT_CRITICAL();
5500 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5502 traceRETURN_vTaskRemoveFromUnorderedEventList();
5504 /*-----------------------------------------------------------*/
5506 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5508 traceENTER_vTaskSetTimeOutState( pxTimeOut );
5510 configASSERT( pxTimeOut );
5511 taskENTER_CRITICAL();
5513 pxTimeOut->xOverflowCount = xNumOfOverflows;
5514 pxTimeOut->xTimeOnEntering = xTickCount;
5516 taskEXIT_CRITICAL();
5518 traceRETURN_vTaskSetTimeOutState();
5520 /*-----------------------------------------------------------*/
5522 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5524 traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5526 /* For internal use only as it does not use a critical section. */
5527 pxTimeOut->xOverflowCount = xNumOfOverflows;
5528 pxTimeOut->xTimeOnEntering = xTickCount;
5530 traceRETURN_vTaskInternalSetTimeOutState();
5532 /*-----------------------------------------------------------*/
5534 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5535 TickType_t * const pxTicksToWait )
5539 traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5541 configASSERT( pxTimeOut );
5542 configASSERT( pxTicksToWait );
5544 taskENTER_CRITICAL();
5546 /* Minor optimisation. The tick count cannot change in this block. */
5547 const TickType_t xConstTickCount = xTickCount;
5548 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5550 #if ( INCLUDE_xTaskAbortDelay == 1 )
5551 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5553 /* The delay was aborted, which is not the same as a time out,
5554 * but has the same result. */
5555 pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
5561 #if ( INCLUDE_vTaskSuspend == 1 )
5562 if( *pxTicksToWait == portMAX_DELAY )
5564 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5565 * specified is the maximum block time then the task should block
5566 * indefinitely, and therefore never time out. */
5572 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) )
5574 /* The tick count is greater than the time at which
5575 * vTaskSetTimeout() was called, but has also overflowed since
5576 * vTaskSetTimeOut() was called. It must have wrapped all the way
5577 * around and gone past again. This passed since vTaskSetTimeout()
5580 *pxTicksToWait = ( TickType_t ) 0;
5582 else if( xElapsedTime < *pxTicksToWait )
5584 /* Not a genuine timeout. Adjust parameters for time remaining. */
5585 *pxTicksToWait -= xElapsedTime;
5586 vTaskInternalSetTimeOutState( pxTimeOut );
5591 *pxTicksToWait = ( TickType_t ) 0;
5595 taskEXIT_CRITICAL();
5597 traceRETURN_xTaskCheckForTimeOut( xReturn );
5601 /*-----------------------------------------------------------*/
5603 void vTaskMissedYield( void )
5605 traceENTER_vTaskMissedYield();
5607 /* Must be called from within a critical section. */
5608 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5610 traceRETURN_vTaskMissedYield();
5612 /*-----------------------------------------------------------*/
5614 #if ( configUSE_TRACE_FACILITY == 1 )
5616 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5618 UBaseType_t uxReturn;
5619 TCB_t const * pxTCB;
5621 traceENTER_uxTaskGetTaskNumber( xTask );
5626 uxReturn = pxTCB->uxTaskNumber;
5633 traceRETURN_uxTaskGetTaskNumber( uxReturn );
5638 #endif /* configUSE_TRACE_FACILITY */
5639 /*-----------------------------------------------------------*/
5641 #if ( configUSE_TRACE_FACILITY == 1 )
5643 void vTaskSetTaskNumber( TaskHandle_t xTask,
5644 const UBaseType_t uxHandle )
5648 traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5653 pxTCB->uxTaskNumber = uxHandle;
5656 traceRETURN_vTaskSetTaskNumber();
5659 #endif /* configUSE_TRACE_FACILITY */
5660 /*-----------------------------------------------------------*/
5663 * -----------------------------------------------------------
5664 * The passive idle task.
5665 * ----------------------------------------------------------
5667 * The passive idle task is used for all the additional cores in a SMP
5668 * system. There must be only 1 active idle task and the rest are passive
5671 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5672 * language extensions. The equivalent prototype for this function is:
5674 * void prvPassiveIdleTask( void *pvParameters );
5677 #if ( configNUMBER_OF_CORES > 1 )
5678 static portTASK_FUNCTION( prvPassiveIdleTask, pvParameters )
5680 ( void ) pvParameters;
5684 for( ; configCONTROL_INFINITE_LOOP(); )
5686 #if ( configUSE_PREEMPTION == 0 )
5688 /* If we are not using preemption we keep forcing a task switch to
5689 * see if any other task has become available. If we are using
5690 * preemption we don't need to do this as any task becoming available
5691 * will automatically get the processor anyway. */
5694 #endif /* configUSE_PREEMPTION */
5696 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5698 /* When using preemption tasks of equal priority will be
5699 * timesliced. If a task that is sharing the idle priority is ready
5700 * to run then the idle task should yield before the end of the
5703 * A critical region is not required here as we are just reading from
5704 * the list, and an occasional incorrect value will not matter. If
5705 * the ready list at the idle priority contains one more task than the
5706 * number of idle tasks, which is equal to the configured numbers of cores
5707 * then a task other than the idle task is ready to execute. */
5708 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5714 mtCOVERAGE_TEST_MARKER();
5717 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5719 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
5721 /* Call the user defined function from within the idle task. This
5722 * allows the application designer to add background functionality
5723 * without the overhead of a separate task.
5725 * This hook is intended to manage core activity such as disabling cores that go idle.
5727 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5728 * CALL A FUNCTION THAT MIGHT BLOCK. */
5729 vApplicationPassiveIdleHook();
5731 #endif /* configUSE_PASSIVE_IDLE_HOOK */
5734 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5737 * -----------------------------------------------------------
5739 * ----------------------------------------------------------
5741 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5742 * language extensions. The equivalent prototype for this function is:
5744 * void prvIdleTask( void *pvParameters );
5748 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5750 /* Stop warnings. */
5751 ( void ) pvParameters;
5753 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5754 * SCHEDULER IS STARTED. **/
5756 /* In case a task that has a secure context deletes itself, in which case
5757 * the idle task is responsible for deleting the task's secure context, if
5759 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5761 #if ( configNUMBER_OF_CORES > 1 )
5763 /* SMP all cores start up in the idle task. This initial yield gets the application
5767 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5769 for( ; configCONTROL_INFINITE_LOOP(); )
5771 /* See if any tasks have deleted themselves - if so then the idle task
5772 * is responsible for freeing the deleted task's TCB and stack. */
5773 prvCheckTasksWaitingTermination();
5775 #if ( configUSE_PREEMPTION == 0 )
5777 /* If we are not using preemption we keep forcing a task switch to
5778 * see if any other task has become available. If we are using
5779 * preemption we don't need to do this as any task becoming available
5780 * will automatically get the processor anyway. */
5783 #endif /* configUSE_PREEMPTION */
5785 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5787 /* When using preemption tasks of equal priority will be
5788 * timesliced. If a task that is sharing the idle priority is ready
5789 * to run then the idle task should yield before the end of the
5792 * A critical region is not required here as we are just reading from
5793 * the list, and an occasional incorrect value will not matter. If
5794 * the ready list at the idle priority contains one more task than the
5795 * number of idle tasks, which is equal to the configured numbers of cores
5796 * then a task other than the idle task is ready to execute. */
5797 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5803 mtCOVERAGE_TEST_MARKER();
5806 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5808 #if ( configUSE_IDLE_HOOK == 1 )
5810 /* Call the user defined function from within the idle task. */
5811 vApplicationIdleHook();
5813 #endif /* configUSE_IDLE_HOOK */
5815 /* This conditional compilation should use inequality to 0, not equality
5816 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5817 * user defined low power mode implementations require
5818 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5819 #if ( configUSE_TICKLESS_IDLE != 0 )
5821 TickType_t xExpectedIdleTime;
5823 /* It is not desirable to suspend then resume the scheduler on
5824 * each iteration of the idle task. Therefore, a preliminary
5825 * test of the expected idle time is performed without the
5826 * scheduler suspended. The result here is not necessarily
5828 xExpectedIdleTime = prvGetExpectedIdleTime();
5830 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5834 /* Now the scheduler is suspended, the expected idle
5835 * time can be sampled again, and this time its value can
5837 configASSERT( xNextTaskUnblockTime >= xTickCount );
5838 xExpectedIdleTime = prvGetExpectedIdleTime();
5840 /* Define the following macro to set xExpectedIdleTime to 0
5841 * if the application does not want
5842 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5843 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5845 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5847 traceLOW_POWER_IDLE_BEGIN();
5848 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5849 traceLOW_POWER_IDLE_END();
5853 mtCOVERAGE_TEST_MARKER();
5856 ( void ) xTaskResumeAll();
5860 mtCOVERAGE_TEST_MARKER();
5863 #endif /* configUSE_TICKLESS_IDLE */
5865 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) )
5867 /* Call the user defined function from within the idle task. This
5868 * allows the application designer to add background functionality
5869 * without the overhead of a separate task.
5871 * This hook is intended to manage core activity such as disabling cores that go idle.
5873 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5874 * CALL A FUNCTION THAT MIGHT BLOCK. */
5875 vApplicationPassiveIdleHook();
5877 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) ) */
5880 /*-----------------------------------------------------------*/
5882 #if ( configUSE_TICKLESS_IDLE != 0 )
5884 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5886 #if ( INCLUDE_vTaskSuspend == 1 )
5887 /* The idle task exists in addition to the application tasks. */
5888 const UBaseType_t uxNonApplicationTasks = configNUMBER_OF_CORES;
5889 #endif /* INCLUDE_vTaskSuspend */
5891 eSleepModeStatus eReturn = eStandardSleep;
5893 traceENTER_eTaskConfirmSleepModeStatus();
5895 /* This function must be called from a critical section. */
5897 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0U )
5899 /* A task was made ready while the scheduler was suspended. */
5900 eReturn = eAbortSleep;
5902 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5904 /* A yield was pended while the scheduler was suspended. */
5905 eReturn = eAbortSleep;
5907 else if( xPendedTicks != 0U )
5909 /* A tick interrupt has already occurred but was held pending
5910 * because the scheduler is suspended. */
5911 eReturn = eAbortSleep;
5914 #if ( INCLUDE_vTaskSuspend == 1 )
5915 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5917 /* If all the tasks are in the suspended list (which might mean they
5918 * have an infinite block time rather than actually being suspended)
5919 * then it is safe to turn all clocks off and just wait for external
5921 eReturn = eNoTasksWaitingTimeout;
5923 #endif /* INCLUDE_vTaskSuspend */
5926 mtCOVERAGE_TEST_MARKER();
5929 traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5934 #endif /* configUSE_TICKLESS_IDLE */
5935 /*-----------------------------------------------------------*/
5937 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5939 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5945 traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
5947 if( ( xIndex >= 0 ) &&
5948 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5950 pxTCB = prvGetTCBFromHandle( xTaskToSet );
5951 configASSERT( pxTCB != NULL );
5952 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5955 traceRETURN_vTaskSetThreadLocalStoragePointer();
5958 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5959 /*-----------------------------------------------------------*/
5961 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5963 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5966 void * pvReturn = NULL;
5969 traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
5971 if( ( xIndex >= 0 ) &&
5972 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5974 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
5975 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
5982 traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
5987 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5988 /*-----------------------------------------------------------*/
5990 #if ( portUSING_MPU_WRAPPERS == 1 )
5992 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
5993 const MemoryRegion_t * const pxRegions )
5997 traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
5999 /* If null is passed in here then we are modifying the MPU settings of
6000 * the calling task. */
6001 pxTCB = prvGetTCBFromHandle( xTaskToModify );
6003 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
6005 traceRETURN_vTaskAllocateMPURegions();
6008 #endif /* portUSING_MPU_WRAPPERS */
6009 /*-----------------------------------------------------------*/
6011 static void prvInitialiseTaskLists( void )
6013 UBaseType_t uxPriority;
6015 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
6017 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
6020 vListInitialise( &xDelayedTaskList1 );
6021 vListInitialise( &xDelayedTaskList2 );
6022 vListInitialise( &xPendingReadyList );
6024 #if ( INCLUDE_vTaskDelete == 1 )
6026 vListInitialise( &xTasksWaitingTermination );
6028 #endif /* INCLUDE_vTaskDelete */
6030 #if ( INCLUDE_vTaskSuspend == 1 )
6032 vListInitialise( &xSuspendedTaskList );
6034 #endif /* INCLUDE_vTaskSuspend */
6036 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
6038 pxDelayedTaskList = &xDelayedTaskList1;
6039 pxOverflowDelayedTaskList = &xDelayedTaskList2;
6041 /*-----------------------------------------------------------*/
6043 static void prvCheckTasksWaitingTermination( void )
6045 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
6047 #if ( INCLUDE_vTaskDelete == 1 )
6051 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
6052 * being called too often in the idle task. */
6053 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6055 #if ( configNUMBER_OF_CORES == 1 )
6057 taskENTER_CRITICAL();
6060 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6061 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6062 /* coverity[misra_c_2012_rule_11_5_violation] */
6063 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6064 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6065 --uxCurrentNumberOfTasks;
6066 --uxDeletedTasksWaitingCleanUp;
6069 taskEXIT_CRITICAL();
6071 prvDeleteTCB( pxTCB );
6073 #else /* #if( configNUMBER_OF_CORES == 1 ) */
6077 taskENTER_CRITICAL();
6079 /* For SMP, multiple idles can be running simultaneously
6080 * and we need to check that other idles did not cleanup while we were
6081 * waiting to enter the critical section. */
6082 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6084 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6085 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6086 /* coverity[misra_c_2012_rule_11_5_violation] */
6087 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6089 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
6091 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6092 --uxCurrentNumberOfTasks;
6093 --uxDeletedTasksWaitingCleanUp;
6097 /* The TCB to be deleted still has not yet been switched out
6098 * by the scheduler, so we will just exit this loop early and
6099 * try again next time. */
6100 taskEXIT_CRITICAL();
6105 taskEXIT_CRITICAL();
6109 prvDeleteTCB( pxTCB );
6112 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
6115 #endif /* INCLUDE_vTaskDelete */
6117 /*-----------------------------------------------------------*/
6119 #if ( configUSE_TRACE_FACILITY == 1 )
6121 void vTaskGetInfo( TaskHandle_t xTask,
6122 TaskStatus_t * pxTaskStatus,
6123 BaseType_t xGetFreeStackSpace,
6128 traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
6130 /* xTask is NULL then get the state of the calling task. */
6131 pxTCB = prvGetTCBFromHandle( xTask );
6133 pxTaskStatus->xHandle = pxTCB;
6134 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
6135 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
6136 pxTaskStatus->pxStackBase = pxTCB->pxStack;
6137 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
6138 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
6139 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
6141 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
6143 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
6145 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
6149 #if ( configUSE_MUTEXES == 1 )
6151 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
6155 pxTaskStatus->uxBasePriority = 0;
6159 #if ( configGENERATE_RUN_TIME_STATS == 1 )
6161 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
6165 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
6169 /* Obtaining the task state is a little fiddly, so is only done if the
6170 * value of eState passed into this function is eInvalid - otherwise the
6171 * state is just set to whatever is passed in. */
6172 if( eState != eInvalid )
6174 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6176 pxTaskStatus->eCurrentState = eRunning;
6180 pxTaskStatus->eCurrentState = eState;
6182 #if ( INCLUDE_vTaskSuspend == 1 )
6184 /* If the task is in the suspended list then there is a
6185 * chance it is actually just blocked indefinitely - so really
6186 * it should be reported as being in the Blocked state. */
6187 if( eState == eSuspended )
6191 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
6193 pxTaskStatus->eCurrentState = eBlocked;
6197 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6201 /* The task does not appear on the event list item of
6202 * and of the RTOS objects, but could still be in the
6203 * blocked state if it is waiting on its notification
6204 * rather than waiting on an object. If not, is
6206 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
6208 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
6210 pxTaskStatus->eCurrentState = eBlocked;
6215 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
6218 ( void ) xTaskResumeAll();
6221 #endif /* INCLUDE_vTaskSuspend */
6223 /* Tasks can be in pending ready list and other state list at the
6224 * same time. These tasks are in ready state no matter what state
6225 * list the task is in. */
6226 taskENTER_CRITICAL();
6228 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
6230 pxTaskStatus->eCurrentState = eReady;
6233 taskEXIT_CRITICAL();
6238 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
6241 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
6242 * parameter is provided to allow it to be skipped. */
6243 if( xGetFreeStackSpace != pdFALSE )
6245 #if ( portSTACK_GROWTH > 0 )
6247 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
6251 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
6257 pxTaskStatus->usStackHighWaterMark = 0;
6260 traceRETURN_vTaskGetInfo();
6263 #endif /* configUSE_TRACE_FACILITY */
6264 /*-----------------------------------------------------------*/
6266 #if ( configUSE_TRACE_FACILITY == 1 )
6268 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
6272 UBaseType_t uxTask = 0;
6273 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
6274 ListItem_t * pxIterator;
6275 TCB_t * pxTCB = NULL;
6277 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
6279 /* Populate an TaskStatus_t structure within the
6280 * pxTaskStatusArray array for each task that is referenced from
6281 * pxList. See the definition of TaskStatus_t in task.h for the
6282 * meaning of each TaskStatus_t structure member. */
6283 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
6285 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6286 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6287 /* coverity[misra_c_2012_rule_11_5_violation] */
6288 pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
6290 vTaskGetInfo( ( TaskHandle_t ) pxTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
6296 mtCOVERAGE_TEST_MARKER();
6302 #endif /* configUSE_TRACE_FACILITY */
6303 /*-----------------------------------------------------------*/
6305 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
6307 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
6309 configSTACK_DEPTH_TYPE uxCount = 0U;
6311 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
6313 pucStackByte -= portSTACK_GROWTH;
6317 uxCount /= ( configSTACK_DEPTH_TYPE ) sizeof( StackType_t );
6322 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
6323 /*-----------------------------------------------------------*/
6325 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
6327 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
6328 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
6329 * user to determine the return type. It gets around the problem of the value
6330 * overflowing on 8-bit types without breaking backward compatibility for
6331 * applications that expect an 8-bit return type. */
6332 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
6335 uint8_t * pucEndOfStack;
6336 configSTACK_DEPTH_TYPE uxReturn;
6338 traceENTER_uxTaskGetStackHighWaterMark2( xTask );
6340 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
6341 * the same except for their return type. Using configSTACK_DEPTH_TYPE
6342 * allows the user to determine the return type. It gets around the
6343 * problem of the value overflowing on 8-bit types without breaking
6344 * backward compatibility for applications that expect an 8-bit return
6347 pxTCB = prvGetTCBFromHandle( xTask );
6349 #if portSTACK_GROWTH < 0
6351 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6355 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6359 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
6361 traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
6366 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
6367 /*-----------------------------------------------------------*/
6369 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
6371 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
6374 uint8_t * pucEndOfStack;
6375 UBaseType_t uxReturn;
6377 traceENTER_uxTaskGetStackHighWaterMark( xTask );
6379 pxTCB = prvGetTCBFromHandle( xTask );
6381 #if portSTACK_GROWTH < 0
6383 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6387 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6391 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
6393 traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6398 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6399 /*-----------------------------------------------------------*/
6401 #if ( INCLUDE_vTaskDelete == 1 )
6403 static void prvDeleteTCB( TCB_t * pxTCB )
6405 /* This call is required specifically for the TriCore port. It must be
6406 * above the vPortFree() calls. The call is also used by ports/demos that
6407 * want to allocate and clean RAM statically. */
6408 portCLEAN_UP_TCB( pxTCB );
6410 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6412 /* Free up the memory allocated for the task's TLS Block. */
6413 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6417 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6419 /* The task can only have been allocated dynamically - free both
6420 * the stack and TCB. */
6421 vPortFreeStack( pxTCB->pxStack );
6424 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
6426 /* The task could have been allocated statically or dynamically, so
6427 * check what was statically allocated before trying to free the
6429 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6431 /* Both the stack and TCB were allocated dynamically, so both
6433 vPortFreeStack( pxTCB->pxStack );
6436 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6438 /* Only the stack was statically allocated, so the TCB is the
6439 * only memory that must be freed. */
6444 /* Neither the stack nor the TCB were allocated dynamically, so
6445 * nothing needs to be freed. */
6446 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6447 mtCOVERAGE_TEST_MARKER();
6450 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6453 #endif /* INCLUDE_vTaskDelete */
6454 /*-----------------------------------------------------------*/
6456 static void prvResetNextTaskUnblockTime( void )
6458 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6460 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
6461 * the maximum possible value so it is extremely unlikely that the
6462 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6463 * there is an item in the delayed list. */
6464 xNextTaskUnblockTime = portMAX_DELAY;
6468 /* The new current delayed list is not empty, get the value of
6469 * the item at the head of the delayed list. This is the time at
6470 * which the task at the head of the delayed list should be removed
6471 * from the Blocked state. */
6472 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6475 /*-----------------------------------------------------------*/
6477 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6479 #if ( configNUMBER_OF_CORES == 1 )
6480 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6482 TaskHandle_t xReturn;
6484 traceENTER_xTaskGetCurrentTaskHandle();
6486 /* A critical section is not required as this is not called from
6487 * an interrupt and the current TCB will always be the same for any
6488 * individual execution thread. */
6489 xReturn = pxCurrentTCB;
6491 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6495 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6496 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6498 TaskHandle_t xReturn;
6499 UBaseType_t uxSavedInterruptStatus;
6501 traceENTER_xTaskGetCurrentTaskHandle();
6503 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6505 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6507 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6509 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6513 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6515 TaskHandle_t xTaskGetCurrentTaskHandleForCore( BaseType_t xCoreID )
6517 TaskHandle_t xReturn = NULL;
6519 traceENTER_xTaskGetCurrentTaskHandleForCore( xCoreID );
6521 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6523 #if ( configNUMBER_OF_CORES == 1 )
6524 xReturn = pxCurrentTCB;
6525 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6526 xReturn = pxCurrentTCBs[ xCoreID ];
6527 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6530 traceRETURN_xTaskGetCurrentTaskHandleForCore( xReturn );
6535 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
6536 /*-----------------------------------------------------------*/
6538 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6540 BaseType_t xTaskGetSchedulerState( void )
6544 traceENTER_xTaskGetSchedulerState();
6546 if( xSchedulerRunning == pdFALSE )
6548 xReturn = taskSCHEDULER_NOT_STARTED;
6552 #if ( configNUMBER_OF_CORES > 1 )
6553 taskENTER_CRITICAL();
6556 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6558 xReturn = taskSCHEDULER_RUNNING;
6562 xReturn = taskSCHEDULER_SUSPENDED;
6565 #if ( configNUMBER_OF_CORES > 1 )
6566 taskEXIT_CRITICAL();
6570 traceRETURN_xTaskGetSchedulerState( xReturn );
6575 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6576 /*-----------------------------------------------------------*/
6578 #if ( configUSE_MUTEXES == 1 )
6580 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6582 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6583 BaseType_t xReturn = pdFALSE;
6585 traceENTER_xTaskPriorityInherit( pxMutexHolder );
6587 /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6588 * inheritance is not applied in this scenario. */
6589 if( pxMutexHolder != NULL )
6591 /* If the holder of the mutex has a priority below the priority of
6592 * the task attempting to obtain the mutex then it will temporarily
6593 * inherit the priority of the task attempting to obtain the mutex. */
6594 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6596 /* Adjust the mutex holder state to account for its new
6597 * priority. Only reset the event list item value if the value is
6598 * not being used for anything else. */
6599 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
6601 listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority );
6605 mtCOVERAGE_TEST_MARKER();
6608 /* If the task being modified is in the ready state it will need
6609 * to be moved into a new list. */
6610 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6612 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6614 /* It is known that the task is in its ready list so
6615 * there is no need to check again and the port level
6616 * reset macro can be called directly. */
6617 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6621 mtCOVERAGE_TEST_MARKER();
6624 /* Inherit the priority before being moved into the new list. */
6625 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6626 prvAddTaskToReadyList( pxMutexHolderTCB );
6627 #if ( configNUMBER_OF_CORES > 1 )
6629 /* The priority of the task is raised. Yield for this task
6630 * if it is not running. */
6631 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6633 prvYieldForTask( pxMutexHolderTCB );
6636 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6640 /* Just inherit the priority. */
6641 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6644 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6646 /* Inheritance occurred. */
6651 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6653 /* The base priority of the mutex holder is lower than the
6654 * priority of the task attempting to take the mutex, but the
6655 * current priority of the mutex holder is not lower than the
6656 * priority of the task attempting to take the mutex.
6657 * Therefore the mutex holder must have already inherited a
6658 * priority, but inheritance would have occurred if that had
6659 * not been the case. */
6664 mtCOVERAGE_TEST_MARKER();
6670 mtCOVERAGE_TEST_MARKER();
6673 traceRETURN_xTaskPriorityInherit( xReturn );
6678 #endif /* configUSE_MUTEXES */
6679 /*-----------------------------------------------------------*/
6681 #if ( configUSE_MUTEXES == 1 )
6683 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6685 TCB_t * const pxTCB = pxMutexHolder;
6686 BaseType_t xReturn = pdFALSE;
6688 traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6690 if( pxMutexHolder != NULL )
6692 /* A task can only have an inherited priority if it holds the mutex.
6693 * If the mutex is held by a task then it cannot be given from an
6694 * interrupt, and if a mutex is given by the holding task then it must
6695 * be the running state task. */
6696 configASSERT( pxTCB == pxCurrentTCB );
6697 configASSERT( pxTCB->uxMutexesHeld );
6698 ( pxTCB->uxMutexesHeld )--;
6700 /* Has the holder of the mutex inherited the priority of another
6702 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6704 /* Only disinherit if no other mutexes are held. */
6705 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6707 /* A task can only have an inherited priority if it holds
6708 * the mutex. If the mutex is held by a task then it cannot be
6709 * given from an interrupt, and if a mutex is given by the
6710 * holding task then it must be the running state task. Remove
6711 * the holding task from the ready list. */
6712 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6714 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6718 mtCOVERAGE_TEST_MARKER();
6721 /* Disinherit the priority before adding the task into the
6722 * new ready list. */
6723 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6724 pxTCB->uxPriority = pxTCB->uxBasePriority;
6726 /* Reset the event list item value. It cannot be in use for
6727 * any other purpose if this task is running, and it must be
6728 * running to give back the mutex. */
6729 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority );
6730 prvAddTaskToReadyList( pxTCB );
6731 #if ( configNUMBER_OF_CORES > 1 )
6733 /* The priority of the task is dropped. Yield the core on
6734 * which the task is running. */
6735 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6737 prvYieldCore( pxTCB->xTaskRunState );
6740 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6742 /* Return true to indicate that a context switch is required.
6743 * This is only actually required in the corner case whereby
6744 * multiple mutexes were held and the mutexes were given back
6745 * in an order different to that in which they were taken.
6746 * If a context switch did not occur when the first mutex was
6747 * returned, even if a task was waiting on it, then a context
6748 * switch should occur when the last mutex is returned whether
6749 * a task is waiting on it or not. */
6754 mtCOVERAGE_TEST_MARKER();
6759 mtCOVERAGE_TEST_MARKER();
6764 mtCOVERAGE_TEST_MARKER();
6767 traceRETURN_xTaskPriorityDisinherit( xReturn );
6772 #endif /* configUSE_MUTEXES */
6773 /*-----------------------------------------------------------*/
6775 #if ( configUSE_MUTEXES == 1 )
6777 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6778 UBaseType_t uxHighestPriorityWaitingTask )
6780 TCB_t * const pxTCB = pxMutexHolder;
6781 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6782 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6784 traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6786 if( pxMutexHolder != NULL )
6788 /* If pxMutexHolder is not NULL then the holder must hold at least
6790 configASSERT( pxTCB->uxMutexesHeld );
6792 /* Determine the priority to which the priority of the task that
6793 * holds the mutex should be set. This will be the greater of the
6794 * holding task's base priority and the priority of the highest
6795 * priority task that is waiting to obtain the mutex. */
6796 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6798 uxPriorityToUse = uxHighestPriorityWaitingTask;
6802 uxPriorityToUse = pxTCB->uxBasePriority;
6805 /* Does the priority need to change? */
6806 if( pxTCB->uxPriority != uxPriorityToUse )
6808 /* Only disinherit if no other mutexes are held. This is a
6809 * simplification in the priority inheritance implementation. If
6810 * the task that holds the mutex is also holding other mutexes then
6811 * the other mutexes may have caused the priority inheritance. */
6812 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6814 /* If a task has timed out because it already holds the
6815 * mutex it was trying to obtain then it cannot of inherited
6816 * its own priority. */
6817 configASSERT( pxTCB != pxCurrentTCB );
6819 /* Disinherit the priority, remembering the previous
6820 * priority to facilitate determining the subject task's
6822 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6823 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6824 pxTCB->uxPriority = uxPriorityToUse;
6826 /* Only reset the event list item value if the value is not
6827 * being used for anything else. */
6828 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
6830 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse );
6834 mtCOVERAGE_TEST_MARKER();
6837 /* If the running task is not the task that holds the mutex
6838 * then the task that holds the mutex could be in either the
6839 * Ready, Blocked or Suspended states. Only remove the task
6840 * from its current state list if it is in the Ready state as
6841 * the task's priority is going to change and there is one
6842 * Ready list per priority. */
6843 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6845 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6847 /* It is known that the task is in its ready list so
6848 * there is no need to check again and the port level
6849 * reset macro can be called directly. */
6850 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6854 mtCOVERAGE_TEST_MARKER();
6857 prvAddTaskToReadyList( pxTCB );
6858 #if ( configNUMBER_OF_CORES > 1 )
6860 /* The priority of the task is dropped. Yield the core on
6861 * which the task is running. */
6862 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6864 prvYieldCore( pxTCB->xTaskRunState );
6867 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6871 mtCOVERAGE_TEST_MARKER();
6876 mtCOVERAGE_TEST_MARKER();
6881 mtCOVERAGE_TEST_MARKER();
6886 mtCOVERAGE_TEST_MARKER();
6889 traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6892 #endif /* configUSE_MUTEXES */
6893 /*-----------------------------------------------------------*/
6895 #if ( configNUMBER_OF_CORES > 1 )
6897 /* If not in a critical section then yield immediately.
6898 * Otherwise set xYieldPendings to true to wait to
6899 * yield until exiting the critical section.
6901 void vTaskYieldWithinAPI( void )
6903 traceENTER_vTaskYieldWithinAPI();
6905 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6911 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6914 traceRETURN_vTaskYieldWithinAPI();
6916 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6918 /*-----------------------------------------------------------*/
6920 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6922 void vTaskEnterCritical( void )
6924 traceENTER_vTaskEnterCritical();
6926 portDISABLE_INTERRUPTS();
6928 if( xSchedulerRunning != pdFALSE )
6930 ( pxCurrentTCB->uxCriticalNesting )++;
6932 /* This is not the interrupt safe version of the enter critical
6933 * function so assert() if it is being called from an interrupt
6934 * context. Only API functions that end in "FromISR" can be used in an
6935 * interrupt. Only assert if the critical nesting count is 1 to
6936 * protect against recursive calls if the assert function also uses a
6937 * critical section. */
6938 if( pxCurrentTCB->uxCriticalNesting == 1U )
6940 portASSERT_IF_IN_ISR();
6945 mtCOVERAGE_TEST_MARKER();
6948 traceRETURN_vTaskEnterCritical();
6951 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6952 /*-----------------------------------------------------------*/
6954 #if ( configNUMBER_OF_CORES > 1 )
6956 void vTaskEnterCritical( void )
6958 traceENTER_vTaskEnterCritical();
6960 portDISABLE_INTERRUPTS();
6962 if( xSchedulerRunning != pdFALSE )
6964 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6966 portGET_TASK_LOCK();
6970 portINCREMENT_CRITICAL_NESTING_COUNT();
6972 /* This is not the interrupt safe version of the enter critical
6973 * function so assert() if it is being called from an interrupt
6974 * context. Only API functions that end in "FromISR" can be used in an
6975 * interrupt. Only assert if the critical nesting count is 1 to
6976 * protect against recursive calls if the assert function also uses a
6977 * critical section. */
6978 if( portGET_CRITICAL_NESTING_COUNT() == 1U )
6980 portASSERT_IF_IN_ISR();
6982 if( uxSchedulerSuspended == 0U )
6984 /* The only time there would be a problem is if this is called
6985 * before a context switch and vTaskExitCritical() is called
6986 * after pxCurrentTCB changes. Therefore this should not be
6987 * used within vTaskSwitchContext(). */
6988 prvCheckForRunStateChange();
6994 mtCOVERAGE_TEST_MARKER();
6997 traceRETURN_vTaskEnterCritical();
7000 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7002 /*-----------------------------------------------------------*/
7004 #if ( configNUMBER_OF_CORES > 1 )
7006 UBaseType_t vTaskEnterCriticalFromISR( void )
7008 UBaseType_t uxSavedInterruptStatus = 0;
7010 traceENTER_vTaskEnterCriticalFromISR();
7012 if( xSchedulerRunning != pdFALSE )
7014 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
7016 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7021 portINCREMENT_CRITICAL_NESTING_COUNT();
7025 mtCOVERAGE_TEST_MARKER();
7028 traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
7030 return uxSavedInterruptStatus;
7033 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7034 /*-----------------------------------------------------------*/
7036 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
7038 void vTaskExitCritical( void )
7040 traceENTER_vTaskExitCritical();
7042 if( xSchedulerRunning != pdFALSE )
7044 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
7045 * does not match a previous call to vTaskEnterCritical(). */
7046 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
7048 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7049 * to exit critical section from ISR. */
7050 portASSERT_IF_IN_ISR();
7052 if( pxCurrentTCB->uxCriticalNesting > 0U )
7054 ( pxCurrentTCB->uxCriticalNesting )--;
7056 if( pxCurrentTCB->uxCriticalNesting == 0U )
7058 portENABLE_INTERRUPTS();
7062 mtCOVERAGE_TEST_MARKER();
7067 mtCOVERAGE_TEST_MARKER();
7072 mtCOVERAGE_TEST_MARKER();
7075 traceRETURN_vTaskExitCritical();
7078 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
7079 /*-----------------------------------------------------------*/
7081 #if ( configNUMBER_OF_CORES > 1 )
7083 void vTaskExitCritical( void )
7085 traceENTER_vTaskExitCritical();
7087 if( xSchedulerRunning != pdFALSE )
7089 /* If critical nesting count is zero then this function
7090 * does not match a previous call to vTaskEnterCritical(). */
7091 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7093 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7094 * to exit critical section from ISR. */
7095 portASSERT_IF_IN_ISR();
7097 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7099 portDECREMENT_CRITICAL_NESTING_COUNT();
7101 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7103 BaseType_t xYieldCurrentTask;
7105 /* Get the xYieldPending stats inside the critical section. */
7106 xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
7108 portRELEASE_ISR_LOCK();
7109 portRELEASE_TASK_LOCK();
7110 portENABLE_INTERRUPTS();
7112 /* When a task yields in a critical section it just sets
7113 * xYieldPending to true. So now that we have exited the
7114 * critical section check if xYieldPending is true, and
7116 if( xYieldCurrentTask != pdFALSE )
7123 mtCOVERAGE_TEST_MARKER();
7128 mtCOVERAGE_TEST_MARKER();
7133 mtCOVERAGE_TEST_MARKER();
7136 traceRETURN_vTaskExitCritical();
7139 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7140 /*-----------------------------------------------------------*/
7142 #if ( configNUMBER_OF_CORES > 1 )
7144 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
7146 traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
7148 if( xSchedulerRunning != pdFALSE )
7150 /* If critical nesting count is zero then this function
7151 * does not match a previous call to vTaskEnterCritical(). */
7152 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7154 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7156 portDECREMENT_CRITICAL_NESTING_COUNT();
7158 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7160 portRELEASE_ISR_LOCK();
7161 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
7165 mtCOVERAGE_TEST_MARKER();
7170 mtCOVERAGE_TEST_MARKER();
7175 mtCOVERAGE_TEST_MARKER();
7178 traceRETURN_vTaskExitCriticalFromISR();
7181 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7182 /*-----------------------------------------------------------*/
7184 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
7186 static char * prvWriteNameToBuffer( char * pcBuffer,
7187 const char * pcTaskName )
7191 /* Start by copying the entire string. */
7192 ( void ) strcpy( pcBuffer, pcTaskName );
7194 /* Pad the end of the string with spaces to ensure columns line up when
7196 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
7198 pcBuffer[ x ] = ' ';
7202 pcBuffer[ x ] = ( char ) 0x00;
7204 /* Return the new end of string. */
7205 return &( pcBuffer[ x ] );
7208 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
7209 /*-----------------------------------------------------------*/
7211 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
7213 void vTaskListTasks( char * pcWriteBuffer,
7214 size_t uxBufferLength )
7216 TaskStatus_t * pxTaskStatusArray;
7217 size_t uxConsumedBufferLength = 0;
7218 size_t uxCharsWrittenBySnprintf;
7219 int iSnprintfReturnValue;
7220 BaseType_t xOutputBufferFull = pdFALSE;
7221 UBaseType_t uxArraySize, x;
7224 traceENTER_vTaskListTasks( pcWriteBuffer, uxBufferLength );
7229 * This function is provided for convenience only, and is used by many
7230 * of the demo applications. Do not consider it to be part of the
7233 * vTaskListTasks() calls uxTaskGetSystemState(), then formats part of the
7234 * uxTaskGetSystemState() output into a human readable table that
7235 * displays task: names, states, priority, stack usage and task number.
7236 * Stack usage specified as the number of unused StackType_t words stack can hold
7237 * on top of stack - not the number of bytes.
7239 * vTaskListTasks() has a dependency on the snprintf() C library function that
7240 * might bloat the code size, use a lot of stack, and provide different
7241 * results on different platforms. An alternative, tiny, third party,
7242 * and limited functionality implementation of snprintf() is provided in
7243 * many of the FreeRTOS/Demo sub-directories in a file called
7244 * printf-stdarg.c (note printf-stdarg.c does not provide a full
7245 * snprintf() implementation!).
7247 * It is recommended that production systems call uxTaskGetSystemState()
7248 * directly to get access to raw stats data, rather than indirectly
7249 * through a call to vTaskListTasks().
7253 /* Make sure the write buffer does not contain a string. */
7254 *pcWriteBuffer = ( char ) 0x00;
7256 /* Take a snapshot of the number of tasks in case it changes while this
7257 * function is executing. */
7258 uxArraySize = uxCurrentNumberOfTasks;
7260 /* Allocate an array index for each task. NOTE! if
7261 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7262 * equate to NULL. */
7263 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7264 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7265 /* coverity[misra_c_2012_rule_11_5_violation] */
7266 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7268 if( pxTaskStatusArray != NULL )
7270 /* Generate the (binary) data. */
7271 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
7273 /* Create a human readable table from the binary data. */
7274 for( x = 0; x < uxArraySize; x++ )
7276 switch( pxTaskStatusArray[ x ].eCurrentState )
7279 cStatus = tskRUNNING_CHAR;
7283 cStatus = tskREADY_CHAR;
7287 cStatus = tskBLOCKED_CHAR;
7291 cStatus = tskSUSPENDED_CHAR;
7295 cStatus = tskDELETED_CHAR;
7298 case eInvalid: /* Fall through. */
7299 default: /* Should not get here, but it is included
7300 * to prevent static checking errors. */
7301 cStatus = ( char ) 0x00;
7305 /* Is there enough space in the buffer to hold task name? */
7306 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7308 /* Write the task name to the string, padding with spaces so it
7309 * can be printed in tabular form more easily. */
7310 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7311 /* Do not count the terminating null character. */
7312 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7314 /* Is there space left in the buffer? -1 is done because snprintf
7315 * writes a terminating null character. So we are essentially
7316 * checking if the buffer has space to write at least one non-null
7318 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7320 /* Write the rest of the string. */
7321 #if ( ( 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\t0x%x\r\n",
7329 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7330 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7331 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber,
7332 ( unsigned int ) pxTaskStatusArray[ x ].uxCoreAffinityMask );
7333 #else /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7334 /* MISRA Ref 21.6.1 [snprintf for utility] */
7335 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7336 /* coverity[misra_c_2012_rule_21_6_violation] */
7337 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7338 uxBufferLength - uxConsumedBufferLength,
7339 "\t%c\t%u\t%u\t%u\r\n",
7341 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7342 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7343 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber );
7344 #endif /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7345 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7347 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7348 pcWriteBuffer += uxCharsWrittenBySnprintf;
7352 xOutputBufferFull = pdTRUE;
7357 xOutputBufferFull = pdTRUE;
7360 if( xOutputBufferFull == pdTRUE )
7366 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7367 * is 0 then vPortFree() will be #defined to nothing. */
7368 vPortFree( pxTaskStatusArray );
7372 mtCOVERAGE_TEST_MARKER();
7375 traceRETURN_vTaskListTasks();
7378 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7379 /*----------------------------------------------------------*/
7381 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
7383 void vTaskGetRunTimeStatistics( char * pcWriteBuffer,
7384 size_t uxBufferLength )
7386 TaskStatus_t * pxTaskStatusArray;
7387 size_t uxConsumedBufferLength = 0;
7388 size_t uxCharsWrittenBySnprintf;
7389 int iSnprintfReturnValue;
7390 BaseType_t xOutputBufferFull = pdFALSE;
7391 UBaseType_t uxArraySize, x;
7392 configRUN_TIME_COUNTER_TYPE ulTotalTime = 0;
7393 configRUN_TIME_COUNTER_TYPE ulStatsAsPercentage;
7395 traceENTER_vTaskGetRunTimeStatistics( pcWriteBuffer, uxBufferLength );
7400 * This function is provided for convenience only, and is used by many
7401 * of the demo applications. Do not consider it to be part of the
7404 * vTaskGetRunTimeStatistics() calls uxTaskGetSystemState(), then formats part
7405 * of the uxTaskGetSystemState() output into a human readable table that
7406 * displays the amount of time each task has spent in the Running state
7407 * in both absolute and percentage terms.
7409 * vTaskGetRunTimeStatistics() has a dependency on the snprintf() C library
7410 * function that might bloat the code size, use a lot of stack, and
7411 * provide different results on different platforms. An alternative,
7412 * tiny, third party, and limited functionality implementation of
7413 * snprintf() is provided in many of the FreeRTOS/Demo sub-directories in
7414 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
7415 * a full snprintf() implementation!).
7417 * It is recommended that production systems call uxTaskGetSystemState()
7418 * directly to get access to raw stats data, rather than indirectly
7419 * through a call to vTaskGetRunTimeStatistics().
7422 /* Make sure the write buffer does not contain a string. */
7423 *pcWriteBuffer = ( char ) 0x00;
7425 /* Take a snapshot of the number of tasks in case it changes while this
7426 * function is executing. */
7427 uxArraySize = uxCurrentNumberOfTasks;
7429 /* Allocate an array index for each task. NOTE! If
7430 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7431 * equate to NULL. */
7432 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7433 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7434 /* coverity[misra_c_2012_rule_11_5_violation] */
7435 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7437 if( pxTaskStatusArray != NULL )
7439 /* Generate the (binary) data. */
7440 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
7442 /* For percentage calculations. */
7443 ulTotalTime /= ( ( configRUN_TIME_COUNTER_TYPE ) 100U );
7445 /* Avoid divide by zero errors. */
7446 if( ulTotalTime > 0U )
7448 /* Create a human readable table from the binary data. */
7449 for( x = 0; x < uxArraySize; x++ )
7451 /* What percentage of the total run time has the task used?
7452 * This will always be rounded down to the nearest integer.
7453 * ulTotalRunTime has already been divided by 100. */
7454 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
7456 /* Is there enough space in the buffer to hold task name? */
7457 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7459 /* Write the task name to the string, padding with
7460 * spaces so it can be printed in tabular form more
7462 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7463 /* Do not count the terminating null character. */
7464 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7466 /* Is there space left in the buffer? -1 is done because snprintf
7467 * writes a terminating null character. So we are essentially
7468 * checking if the buffer has space to write at least one non-null
7470 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7472 if( ulStatsAsPercentage > 0U )
7474 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7476 /* MISRA Ref 21.6.1 [snprintf for utility] */
7477 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7478 /* coverity[misra_c_2012_rule_21_6_violation] */
7479 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7480 uxBufferLength - uxConsumedBufferLength,
7481 "\t%lu\t\t%lu%%\r\n",
7482 pxTaskStatusArray[ x ].ulRunTimeCounter,
7483 ulStatsAsPercentage );
7485 #else /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7487 /* sizeof( int ) == sizeof( long ) so a smaller
7488 * printf() library can be used. */
7489 /* MISRA Ref 21.6.1 [snprintf for utility] */
7490 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7491 /* coverity[misra_c_2012_rule_21_6_violation] */
7492 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7493 uxBufferLength - uxConsumedBufferLength,
7495 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter,
7496 ( unsigned int ) ulStatsAsPercentage );
7498 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7502 /* If the percentage is zero here then the task has
7503 * consumed less than 1% of the total run time. */
7504 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
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,
7511 "\t%lu\t\t<1%%\r\n",
7512 pxTaskStatusArray[ x ].ulRunTimeCounter );
7516 /* sizeof( int ) == sizeof( long ) so a smaller
7517 * printf() library can be used. */
7518 /* MISRA Ref 21.6.1 [snprintf for utility] */
7519 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7520 /* coverity[misra_c_2012_rule_21_6_violation] */
7521 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7522 uxBufferLength - uxConsumedBufferLength,
7524 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter );
7526 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7529 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7530 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7531 pcWriteBuffer += uxCharsWrittenBySnprintf;
7535 xOutputBufferFull = pdTRUE;
7540 xOutputBufferFull = pdTRUE;
7543 if( xOutputBufferFull == pdTRUE )
7551 mtCOVERAGE_TEST_MARKER();
7554 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7555 * is 0 then vPortFree() will be #defined to nothing. */
7556 vPortFree( pxTaskStatusArray );
7560 mtCOVERAGE_TEST_MARKER();
7563 traceRETURN_vTaskGetRunTimeStatistics();
7566 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7567 /*-----------------------------------------------------------*/
7569 TickType_t uxTaskResetEventItemValue( void )
7571 TickType_t uxReturn;
7573 traceENTER_uxTaskResetEventItemValue();
7575 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7577 /* Reset the event list item to its normal value - so it can be used with
7578 * queues and semaphores. */
7579 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) );
7581 traceRETURN_uxTaskResetEventItemValue( uxReturn );
7585 /*-----------------------------------------------------------*/
7587 #if ( configUSE_MUTEXES == 1 )
7589 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7593 traceENTER_pvTaskIncrementMutexHeldCount();
7595 pxTCB = pxCurrentTCB;
7597 /* If xSemaphoreCreateMutex() is called before any tasks have been created
7598 * then pxCurrentTCB will be NULL. */
7601 ( pxTCB->uxMutexesHeld )++;
7604 traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7609 #endif /* configUSE_MUTEXES */
7610 /*-----------------------------------------------------------*/
7612 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7614 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7615 BaseType_t xClearCountOnExit,
7616 TickType_t xTicksToWait )
7619 BaseType_t xAlreadyYielded, xShouldBlock = pdFALSE;
7621 traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7623 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7625 /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
7626 * non-deterministic operation. */
7629 /* We MUST enter a critical section to atomically check if a notification
7630 * has occurred and set the flag to indicate that we are waiting for
7631 * a notification. If we do not do so, a notification sent from an ISR
7633 taskENTER_CRITICAL();
7635 /* Only block if the notification count is not already non-zero. */
7636 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0U )
7638 /* Mark this task as waiting for a notification. */
7639 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7641 if( xTicksToWait > ( TickType_t ) 0 )
7643 xShouldBlock = pdTRUE;
7647 mtCOVERAGE_TEST_MARKER();
7652 mtCOVERAGE_TEST_MARKER();
7655 taskEXIT_CRITICAL();
7657 /* We are now out of the critical section but the scheduler is still
7658 * suspended, so we are safe to do non-deterministic operations such
7659 * as prvAddCurrentTaskToDelayedList. */
7660 if( xShouldBlock == pdTRUE )
7662 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7663 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7667 mtCOVERAGE_TEST_MARKER();
7670 xAlreadyYielded = xTaskResumeAll();
7672 /* Force a reschedule if xTaskResumeAll has not already done so. */
7673 if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
7675 taskYIELD_WITHIN_API();
7679 mtCOVERAGE_TEST_MARKER();
7682 taskENTER_CRITICAL();
7684 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7685 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7687 if( ulReturn != 0U )
7689 if( xClearCountOnExit != pdFALSE )
7691 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ( uint32_t ) 0U;
7695 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7700 mtCOVERAGE_TEST_MARKER();
7703 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7705 taskEXIT_CRITICAL();
7707 traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7712 #endif /* configUSE_TASK_NOTIFICATIONS */
7713 /*-----------------------------------------------------------*/
7715 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7717 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7718 uint32_t ulBitsToClearOnEntry,
7719 uint32_t ulBitsToClearOnExit,
7720 uint32_t * pulNotificationValue,
7721 TickType_t xTicksToWait )
7723 BaseType_t xReturn, xAlreadyYielded, xShouldBlock = pdFALSE;
7725 traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7727 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7729 /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
7730 * non-deterministic operation. */
7733 /* We MUST enter a critical section to atomically check and update the
7734 * task notification value. If we do not do so, a notification from
7735 * an ISR will get lost. */
7736 taskENTER_CRITICAL();
7738 /* Only block if a notification is not already pending. */
7739 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7741 /* Clear bits in the task's notification value as bits may get
7742 * set by the notifying task or interrupt. This can be used
7743 * to clear the value to zero. */
7744 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7746 /* Mark this task as waiting for a notification. */
7747 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7749 if( xTicksToWait > ( TickType_t ) 0 )
7751 xShouldBlock = pdTRUE;
7755 mtCOVERAGE_TEST_MARKER();
7760 mtCOVERAGE_TEST_MARKER();
7763 taskEXIT_CRITICAL();
7765 /* We are now out of the critical section but the scheduler is still
7766 * suspended, so we are safe to do non-deterministic operations such
7767 * as prvAddCurrentTaskToDelayedList. */
7768 if( xShouldBlock == pdTRUE )
7770 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7771 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7775 mtCOVERAGE_TEST_MARKER();
7778 xAlreadyYielded = xTaskResumeAll();
7780 /* Force a reschedule if xTaskResumeAll has not already done so. */
7781 if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
7783 taskYIELD_WITHIN_API();
7787 mtCOVERAGE_TEST_MARKER();
7790 taskENTER_CRITICAL();
7792 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7794 if( pulNotificationValue != NULL )
7796 /* Output the current notification value, which may or may not
7798 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7801 /* If ucNotifyValue is set then either the task never entered the
7802 * blocked state (because a notification was already pending) or the
7803 * task unblocked because of a notification. Otherwise the task
7804 * unblocked because of a timeout. */
7805 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7807 /* A notification was not received. */
7812 /* A notification was already pending or a notification was
7813 * received while the task was waiting. */
7814 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7818 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7820 taskEXIT_CRITICAL();
7822 traceRETURN_xTaskGenericNotifyWait( xReturn );
7827 #endif /* configUSE_TASK_NOTIFICATIONS */
7828 /*-----------------------------------------------------------*/
7830 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7832 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7833 UBaseType_t uxIndexToNotify,
7835 eNotifyAction eAction,
7836 uint32_t * pulPreviousNotificationValue )
7839 BaseType_t xReturn = pdPASS;
7840 uint8_t ucOriginalNotifyState;
7842 traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7844 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7845 configASSERT( xTaskToNotify );
7846 pxTCB = xTaskToNotify;
7848 taskENTER_CRITICAL();
7850 if( pulPreviousNotificationValue != NULL )
7852 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7855 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7857 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7862 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7866 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7869 case eSetValueWithOverwrite:
7870 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7873 case eSetValueWithoutOverwrite:
7875 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7877 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7881 /* The value could not be written to the task. */
7889 /* The task is being notified without its notify value being
7895 /* Should not get here if all enums are handled.
7896 * Artificially force an assert by testing a value the
7897 * compiler can't assume is const. */
7898 configASSERT( xTickCount == ( TickType_t ) 0 );
7903 traceTASK_NOTIFY( uxIndexToNotify );
7905 /* If the task is in the blocked state specifically to wait for a
7906 * notification then unblock it now. */
7907 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7909 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7910 prvAddTaskToReadyList( pxTCB );
7912 /* The task should not have been on an event list. */
7913 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7915 #if ( configUSE_TICKLESS_IDLE != 0 )
7917 /* If a task is blocked waiting for a notification then
7918 * xNextTaskUnblockTime might be set to the blocked task's time
7919 * out time. If the task is unblocked for a reason other than
7920 * a timeout xNextTaskUnblockTime is normally left unchanged,
7921 * because it will automatically get reset to a new value when
7922 * the tick count equals xNextTaskUnblockTime. However if
7923 * tickless idling is used it might be more important to enter
7924 * sleep mode at the earliest possible time - so reset
7925 * xNextTaskUnblockTime here to ensure it is updated at the
7926 * earliest possible time. */
7927 prvResetNextTaskUnblockTime();
7931 /* Check if the notified task has a priority above the currently
7932 * executing task. */
7933 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7937 mtCOVERAGE_TEST_MARKER();
7940 taskEXIT_CRITICAL();
7942 traceRETURN_xTaskGenericNotify( xReturn );
7947 #endif /* configUSE_TASK_NOTIFICATIONS */
7948 /*-----------------------------------------------------------*/
7950 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7952 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
7953 UBaseType_t uxIndexToNotify,
7955 eNotifyAction eAction,
7956 uint32_t * pulPreviousNotificationValue,
7957 BaseType_t * pxHigherPriorityTaskWoken )
7960 uint8_t ucOriginalNotifyState;
7961 BaseType_t xReturn = pdPASS;
7962 UBaseType_t uxSavedInterruptStatus;
7964 traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
7966 configASSERT( xTaskToNotify );
7967 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7969 /* RTOS ports that support interrupt nesting have the concept of a
7970 * maximum system call (or maximum API call) interrupt priority.
7971 * Interrupts that are above the maximum system call priority are keep
7972 * permanently enabled, even when the RTOS kernel is in a critical section,
7973 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7974 * is defined in FreeRTOSConfig.h then
7975 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7976 * failure if a FreeRTOS API function is called from an interrupt that has
7977 * been assigned a priority above the configured maximum system call
7978 * priority. Only FreeRTOS functions that end in FromISR can be called
7979 * from interrupts that have been assigned a priority at or (logically)
7980 * below the maximum system call interrupt priority. FreeRTOS maintains a
7981 * separate interrupt safe API to ensure interrupt entry is as fast and as
7982 * simple as possible. More information (albeit Cortex-M specific) is
7983 * provided on the following link:
7984 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7985 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7987 pxTCB = xTaskToNotify;
7989 /* MISRA Ref 4.7.1 [Return value shall be checked] */
7990 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
7991 /* coverity[misra_c_2012_directive_4_7_violation] */
7992 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
7994 if( pulPreviousNotificationValue != NULL )
7996 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7999 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8000 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8005 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
8009 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8012 case eSetValueWithOverwrite:
8013 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8016 case eSetValueWithoutOverwrite:
8018 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
8020 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8024 /* The value could not be written to the task. */
8032 /* The task is being notified without its notify value being
8038 /* Should not get here if all enums are handled.
8039 * Artificially force an assert by testing a value the
8040 * compiler can't assume is const. */
8041 configASSERT( xTickCount == ( TickType_t ) 0 );
8045 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
8047 /* If the task is in the blocked state specifically to wait for a
8048 * notification then unblock it now. */
8049 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8051 /* The task should not have been on an event list. */
8052 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8054 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8056 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8057 prvAddTaskToReadyList( pxTCB );
8061 /* The delayed and ready lists cannot be accessed, so hold
8062 * this task pending until the scheduler is resumed. */
8063 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8066 #if ( configNUMBER_OF_CORES == 1 )
8068 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8070 /* The notified task has a priority above the currently
8071 * executing task so a yield is required. */
8072 if( pxHigherPriorityTaskWoken != NULL )
8074 *pxHigherPriorityTaskWoken = pdTRUE;
8077 /* Mark that a yield is pending in case the user is not
8078 * using the "xHigherPriorityTaskWoken" parameter to an ISR
8079 * safe FreeRTOS function. */
8080 xYieldPendings[ 0 ] = pdTRUE;
8084 mtCOVERAGE_TEST_MARKER();
8087 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8089 #if ( configUSE_PREEMPTION == 1 )
8091 prvYieldForTask( pxTCB );
8093 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8095 if( pxHigherPriorityTaskWoken != NULL )
8097 *pxHigherPriorityTaskWoken = pdTRUE;
8101 #endif /* if ( configUSE_PREEMPTION == 1 ) */
8103 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8106 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8108 traceRETURN_xTaskGenericNotifyFromISR( xReturn );
8113 #endif /* configUSE_TASK_NOTIFICATIONS */
8114 /*-----------------------------------------------------------*/
8116 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8118 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
8119 UBaseType_t uxIndexToNotify,
8120 BaseType_t * pxHigherPriorityTaskWoken )
8123 uint8_t ucOriginalNotifyState;
8124 UBaseType_t uxSavedInterruptStatus;
8126 traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
8128 configASSERT( xTaskToNotify );
8129 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8131 /* RTOS ports that support interrupt nesting have the concept of a
8132 * maximum system call (or maximum API call) interrupt priority.
8133 * Interrupts that are above the maximum system call priority are keep
8134 * permanently enabled, even when the RTOS kernel is in a critical section,
8135 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
8136 * is defined in FreeRTOSConfig.h then
8137 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
8138 * failure if a FreeRTOS API function is called from an interrupt that has
8139 * been assigned a priority above the configured maximum system call
8140 * priority. Only FreeRTOS functions that end in FromISR can be called
8141 * from interrupts that have been assigned a priority at or (logically)
8142 * below the maximum system call interrupt priority. FreeRTOS maintains a
8143 * separate interrupt safe API to ensure interrupt entry is as fast and as
8144 * simple as possible. More information (albeit Cortex-M specific) is
8145 * provided on the following link:
8146 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
8147 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
8149 pxTCB = xTaskToNotify;
8151 /* MISRA Ref 4.7.1 [Return value shall be checked] */
8152 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
8153 /* coverity[misra_c_2012_directive_4_7_violation] */
8154 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
8156 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8157 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8159 /* 'Giving' is equivalent to incrementing a count in a counting
8161 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8163 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
8165 /* If the task is in the blocked state specifically to wait for a
8166 * notification then unblock it now. */
8167 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8169 /* The task should not have been on an event list. */
8170 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8172 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8174 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8175 prvAddTaskToReadyList( pxTCB );
8179 /* The delayed and ready lists cannot be accessed, so hold
8180 * this task pending until the scheduler is resumed. */
8181 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8184 #if ( configNUMBER_OF_CORES == 1 )
8186 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8188 /* The notified task has a priority above the currently
8189 * executing task so a yield is required. */
8190 if( pxHigherPriorityTaskWoken != NULL )
8192 *pxHigherPriorityTaskWoken = pdTRUE;
8195 /* Mark that a yield is pending in case the user is not
8196 * using the "xHigherPriorityTaskWoken" parameter in an ISR
8197 * safe FreeRTOS function. */
8198 xYieldPendings[ 0 ] = pdTRUE;
8202 mtCOVERAGE_TEST_MARKER();
8205 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8207 #if ( configUSE_PREEMPTION == 1 )
8209 prvYieldForTask( pxTCB );
8211 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8213 if( pxHigherPriorityTaskWoken != NULL )
8215 *pxHigherPriorityTaskWoken = pdTRUE;
8219 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
8221 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8224 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8226 traceRETURN_vTaskGenericNotifyGiveFromISR();
8229 #endif /* configUSE_TASK_NOTIFICATIONS */
8230 /*-----------------------------------------------------------*/
8232 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8234 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
8235 UBaseType_t uxIndexToClear )
8240 traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
8242 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8244 /* If null is passed in here then it is the calling task that is having
8245 * its notification state cleared. */
8246 pxTCB = prvGetTCBFromHandle( xTask );
8248 taskENTER_CRITICAL();
8250 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
8252 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
8260 taskEXIT_CRITICAL();
8262 traceRETURN_xTaskGenericNotifyStateClear( xReturn );
8267 #endif /* configUSE_TASK_NOTIFICATIONS */
8268 /*-----------------------------------------------------------*/
8270 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8272 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
8273 UBaseType_t uxIndexToClear,
8274 uint32_t ulBitsToClear )
8279 traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
8281 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8283 /* If null is passed in here then it is the calling task that is having
8284 * its notification state cleared. */
8285 pxTCB = prvGetTCBFromHandle( xTask );
8287 taskENTER_CRITICAL();
8289 /* Return the notification as it was before the bits were cleared,
8290 * then clear the bit mask. */
8291 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
8292 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
8294 taskEXIT_CRITICAL();
8296 traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
8301 #endif /* configUSE_TASK_NOTIFICATIONS */
8302 /*-----------------------------------------------------------*/
8304 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8306 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
8310 traceENTER_ulTaskGetRunTimeCounter( xTask );
8312 pxTCB = prvGetTCBFromHandle( xTask );
8314 traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
8316 return pxTCB->ulRunTimeCounter;
8319 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8320 /*-----------------------------------------------------------*/
8322 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8324 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
8327 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8329 traceENTER_ulTaskGetRunTimePercent( xTask );
8331 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
8333 /* For percentage calculations. */
8334 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8336 /* Avoid divide by zero errors. */
8337 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8339 pxTCB = prvGetTCBFromHandle( xTask );
8340 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
8347 traceRETURN_ulTaskGetRunTimePercent( ulReturn );
8352 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8353 /*-----------------------------------------------------------*/
8355 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8357 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
8359 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
8362 traceENTER_ulTaskGetIdleRunTimeCounter();
8364 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8366 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8369 traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
8374 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8375 /*-----------------------------------------------------------*/
8377 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8379 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
8381 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8382 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
8385 traceENTER_ulTaskGetIdleRunTimePercent();
8387 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
8389 /* For percentage calculations. */
8390 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8392 /* Avoid divide by zero errors. */
8393 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8395 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8397 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8400 ulReturn = ulRunTimeCounter / ulTotalTime;
8407 traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
8412 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8413 /*-----------------------------------------------------------*/
8415 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
8416 const BaseType_t xCanBlockIndefinitely )
8418 TickType_t xTimeToWake;
8419 const TickType_t xConstTickCount = xTickCount;
8420 List_t * const pxDelayedList = pxDelayedTaskList;
8421 List_t * const pxOverflowDelayedList = pxOverflowDelayedTaskList;
8423 #if ( INCLUDE_xTaskAbortDelay == 1 )
8425 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
8426 * reset to pdFALSE so it can be detected as having been set to pdTRUE
8427 * when the task leaves the Blocked state. */
8428 pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
8432 /* Remove the task from the ready list before adding it to the blocked list
8433 * as the same list item is used for both lists. */
8434 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
8436 /* The current task must be in a ready list, so there is no need to
8437 * check, and the port reset macro can be called directly. */
8438 portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
8442 mtCOVERAGE_TEST_MARKER();
8445 #if ( INCLUDE_vTaskSuspend == 1 )
8447 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
8449 /* Add the task to the suspended task list instead of a delayed task
8450 * list to ensure it is not woken by a timing event. It will block
8452 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
8456 /* Calculate the time at which the task should be woken if the event
8457 * does not occur. This may overflow but this doesn't matter, the
8458 * kernel will manage it correctly. */
8459 xTimeToWake = xConstTickCount + xTicksToWait;
8461 /* The list item will be inserted in wake time order. */
8462 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8464 if( xTimeToWake < xConstTickCount )
8466 /* Wake time has overflowed. Place this item in the overflow
8468 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8469 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8473 /* The wake time has not overflowed, so the current block list
8475 traceMOVED_TASK_TO_DELAYED_LIST();
8476 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8478 /* If the task entering the blocked state was placed at the
8479 * head of the list of blocked tasks then xNextTaskUnblockTime
8480 * needs to be updated too. */
8481 if( xTimeToWake < xNextTaskUnblockTime )
8483 xNextTaskUnblockTime = xTimeToWake;
8487 mtCOVERAGE_TEST_MARKER();
8492 #else /* INCLUDE_vTaskSuspend */
8494 /* Calculate the time at which the task should be woken if the event
8495 * does not occur. This may overflow but this doesn't matter, the kernel
8496 * will manage it correctly. */
8497 xTimeToWake = xConstTickCount + xTicksToWait;
8499 /* The list item will be inserted in wake time order. */
8500 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8502 if( xTimeToWake < xConstTickCount )
8504 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8505 /* Wake time has overflowed. Place this item in the overflow list. */
8506 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8510 traceMOVED_TASK_TO_DELAYED_LIST();
8511 /* The wake time has not overflowed, so the current block list is used. */
8512 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8514 /* If the task entering the blocked state was placed at the head of the
8515 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
8517 if( xTimeToWake < xNextTaskUnblockTime )
8519 xNextTaskUnblockTime = xTimeToWake;
8523 mtCOVERAGE_TEST_MARKER();
8527 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
8528 ( void ) xCanBlockIndefinitely;
8530 #endif /* INCLUDE_vTaskSuspend */
8532 /*-----------------------------------------------------------*/
8534 #if ( portUSING_MPU_WRAPPERS == 1 )
8536 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
8540 traceENTER_xTaskGetMPUSettings( xTask );
8542 pxTCB = prvGetTCBFromHandle( xTask );
8544 traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
8546 return &( pxTCB->xMPUSettings );
8549 #endif /* portUSING_MPU_WRAPPERS */
8550 /*-----------------------------------------------------------*/
8552 /* Code below here allows additional code to be inserted into this source file,
8553 * especially where access to file scope functions and data is needed (for example
8554 * when performing module tests). */
8556 #ifdef FREERTOS_MODULE_TEST
8557 #include "tasks_test_access_functions.h"
8561 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
8563 #include "freertos_tasks_c_additions.h"
8565 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8566 static void freertos_tasks_c_additions_init( void )
8568 FREERTOS_TASKS_C_ADDITIONS_INIT();
8572 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8573 /*-----------------------------------------------------------*/
8575 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8578 * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8579 * to provide the memory that is used by the Idle task. It is used when
8580 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8581 * it's own implementation of vApplicationGetIdleTaskMemory by setting
8582 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8584 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8585 StackType_t ** ppxIdleTaskStackBuffer,
8586 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize )
8588 static StaticTask_t xIdleTaskTCB;
8589 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8591 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8592 *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8593 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8596 #if ( configNUMBER_OF_CORES > 1 )
8598 void vApplicationGetPassiveIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8599 StackType_t ** ppxIdleTaskStackBuffer,
8600 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize,
8601 BaseType_t xPassiveIdleTaskIndex )
8603 static StaticTask_t xIdleTaskTCBs[ configNUMBER_OF_CORES - 1 ];
8604 static StackType_t uxIdleTaskStacks[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
8606 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCBs[ xPassiveIdleTaskIndex ] );
8607 *ppxIdleTaskStackBuffer = &( uxIdleTaskStacks[ xPassiveIdleTaskIndex ][ 0 ] );
8608 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8611 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
8613 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8614 /*-----------------------------------------------------------*/
8616 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8619 * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8620 * to provide the memory that is used by the Timer service task. It is used when
8621 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8622 * it's own implementation of vApplicationGetTimerTaskMemory by setting
8623 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8625 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8626 StackType_t ** ppxTimerTaskStackBuffer,
8627 configSTACK_DEPTH_TYPE * puxTimerTaskStackSize )
8629 static StaticTask_t xTimerTaskTCB;
8630 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8632 *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8633 *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8634 *puxTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8637 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8638 /*-----------------------------------------------------------*/
8641 * Reset the state in this file. This state is normally initialized at start up.
8642 * This function must be called by the application before restarting the
8645 void vTaskResetState( void )
8649 /* Task control block. */
8650 #if ( configNUMBER_OF_CORES == 1 )
8652 pxCurrentTCB = NULL;
8654 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8656 #if ( INCLUDE_vTaskDelete == 1 )
8658 uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
8660 #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
8662 #if ( configUSE_POSIX_ERRNO == 1 )
8666 #endif /* #if ( configUSE_POSIX_ERRNO == 1 ) */
8668 /* Other file private variables. */
8669 uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
8670 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
8671 uxTopReadyPriority = tskIDLE_PRIORITY;
8672 xSchedulerRunning = pdFALSE;
8673 xPendedTicks = ( TickType_t ) 0U;
8675 for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
8677 xYieldPendings[ xCoreID ] = pdFALSE;
8680 xNumOfOverflows = ( BaseType_t ) 0;
8681 uxTaskNumber = ( UBaseType_t ) 0U;
8682 xNextTaskUnblockTime = ( TickType_t ) 0U;
8684 uxSchedulerSuspended = ( UBaseType_t ) 0U;
8686 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8688 for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
8690 ulTaskSwitchedInTime[ xCoreID ] = 0U;
8691 ulTotalRunTime[ xCoreID ] = 0U;
8694 #endif /* #if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8696 /*-----------------------------------------------------------*/