2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /* Standard includes. */
33 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
34 * all the API functions to use the MPU wrappers. That should only be done when
35 * task.h is included from an application file. */
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
38 /* FreeRTOS includes. */
42 #include "stack_macros.h"
44 /* The default definitions are only available for non-MPU ports. The
45 * reason is that the stack alignment requirements vary for different
47 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS != 0 ) )
48 #error configKERNEL_PROVIDED_STATIC_MEMORY cannot be set to 1 when using an MPU port. The vApplicationGet*TaskMemory() functions must be provided manually.
51 /* The MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
52 * for the header files above, but not in this file, in order to generate the
53 * correct privileged Vs unprivileged linkage and placement. */
54 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
56 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
57 * functions but without including stdio.h here. */
58 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
60 /* At the bottom of this file are two optional functions that can be used
61 * to generate human readable text from the raw data generated by the
62 * uxTaskGetSystemState() function. Note the formatting functions are provided
63 * for convenience only, and are NOT considered part of the kernel. */
65 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
67 #if ( configUSE_PREEMPTION == 0 )
69 /* If the cooperative scheduler is being used then a yield should not be
70 * performed just because a higher priority task has been woken. */
71 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB )
72 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB )
75 #if ( configNUMBER_OF_CORES == 1 )
77 /* This macro requests the running task pxTCB to yield. In single core
78 * scheduler, a running task always runs on core 0 and portYIELD_WITHIN_API()
79 * can be used to request the task running on core 0 to yield. Therefore, pxTCB
80 * is not used in this macro. */
81 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) \
84 portYIELD_WITHIN_API(); \
87 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \
89 if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \
91 portYIELD_WITHIN_API(); \
95 mtCOVERAGE_TEST_MARKER(); \
99 #else /* if ( configNUMBER_OF_CORES == 1 ) */
101 /* Yield the core on which this task is running. */
102 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldCore( ( pxTCB )->xTaskRunState )
104 /* Yield for the task if a running task has priority lower than this task. */
105 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldForTask( pxTCB )
107 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
109 #endif /* if ( configUSE_PREEMPTION == 0 ) */
111 /* Values that can be assigned to the ucNotifyState member of the TCB. */
112 #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
113 #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
114 #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
117 * The value used to fill the stack of a task when the task is created. This
118 * is used purely for checking the high water mark for tasks.
120 #define tskSTACK_FILL_BYTE ( 0xa5U )
122 /* Bits used to record how a task's stack and TCB were allocated. */
123 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
124 #define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
125 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
127 /* If any of the following are set then task stacks are filled with a known
128 * value so the high water mark can be determined. If none of the following are
129 * set then don't fill the stack so there is no unnecessary dependency on memset. */
130 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
131 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
133 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
137 * Macros used by vListTask to indicate which state a task is in.
139 #define tskRUNNING_CHAR ( 'X' )
140 #define tskBLOCKED_CHAR ( 'B' )
141 #define tskREADY_CHAR ( 'R' )
142 #define tskDELETED_CHAR ( 'D' )
143 #define tskSUSPENDED_CHAR ( 'S' )
146 * Some kernel aware debuggers require the data the debugger needs access to be
147 * global, rather than file scope.
149 #ifdef portREMOVE_STATIC_QUALIFIER
153 /* The name allocated to the Idle task. This can be overridden by defining
154 * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
155 #ifndef configIDLE_TASK_NAME
156 #define configIDLE_TASK_NAME "IDLE"
159 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
161 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
162 * performed in a generic way that is not optimised to any particular
163 * microcontroller architecture. */
165 /* uxTopReadyPriority holds the priority of the highest priority ready
167 #define taskRECORD_READY_PRIORITY( uxPriority ) \
169 if( ( uxPriority ) > uxTopReadyPriority ) \
171 uxTopReadyPriority = ( uxPriority ); \
173 } while( 0 ) /* taskRECORD_READY_PRIORITY */
175 /*-----------------------------------------------------------*/
177 #if ( configNUMBER_OF_CORES == 1 )
178 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
180 UBaseType_t uxTopPriority = uxTopReadyPriority; \
182 /* Find the highest priority queue that contains ready tasks. */ \
183 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
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; \
259 prvResetNextTaskUnblockTime(); \
262 /*-----------------------------------------------------------*/
265 * Place the task represented by pxTCB into the appropriate ready list for
266 * the task. It is inserted at the end of the list.
268 #define prvAddTaskToReadyList( pxTCB ) \
270 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
271 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
272 listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
273 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB ); \
275 /*-----------------------------------------------------------*/
278 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
279 * where NULL is used to indicate that the handle of the currently executing
280 * task should be used in place of the parameter. This macro simply checks to
281 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
283 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
285 /* The item value of the event list item is normally used to hold the priority
286 * of the task to which it belongs (coded to allow it to be held in reverse
287 * priority order). However, it is occasionally borrowed for other purposes. It
288 * is important its value is not updated due to a task priority change while it is
289 * being used for another purpose. The following bit definition is used to inform
290 * the scheduler that the value should not be changed - in which case it is the
291 * responsibility of whichever module is using the value to ensure it gets set back
292 * to its original value when it is released. */
293 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
294 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint16_t ) 0x8000U )
295 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
296 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint32_t ) 0x80000000UL )
297 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
298 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint64_t ) 0x8000000000000000ULL )
301 /* Indicates that the task is not actively running on any core. */
302 #define taskTASK_NOT_RUNNING ( ( BaseType_t ) ( -1 ) )
304 /* Indicates that the task is actively running but scheduled to yield. */
305 #define taskTASK_SCHEDULED_TO_YIELD ( ( BaseType_t ) ( -2 ) )
307 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
308 #if ( configNUMBER_OF_CORES == 1 )
309 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
310 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
312 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) )
313 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB )->xTaskRunState != taskTASK_NOT_RUNNING ) ? ( pdTRUE ) : ( pdFALSE ) )
316 /* Indicates that the task is an Idle task. */
317 #define taskATTRIBUTE_IS_IDLE ( UBaseType_t ) ( 1UL << 0UL )
319 #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) )
320 #define portGET_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting )
321 #define portSET_CRITICAL_NESTING_COUNT( x ) ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting = ( x ) )
322 #define portINCREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting++ )
323 #define portDECREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting-- )
324 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) */
326 #define taskBITS_PER_BYTE ( ( size_t ) 8 )
328 #if ( configNUMBER_OF_CORES > 1 )
330 /* Yields the given core. This must be called from a critical section and xCoreID
331 * must be valid. This macro is not required in single core since there is only
332 * one core to yield. */
333 #define prvYieldCore( xCoreID ) \
335 if( ( xCoreID ) == ( BaseType_t ) portGET_CORE_ID() ) \
337 /* Pending a yield for this core since it is in the critical section. */ \
338 xYieldPendings[ ( xCoreID ) ] = pdTRUE; \
342 /* Request other core to yield if it is not requested before. */ \
343 if( pxCurrentTCBs[ ( xCoreID ) ]->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD ) \
345 portYIELD_CORE( xCoreID ); \
346 pxCurrentTCBs[ ( xCoreID ) ]->xTaskRunState = taskTASK_SCHEDULED_TO_YIELD; \
350 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
351 /*-----------------------------------------------------------*/
354 * Task control block. A task control block (TCB) is allocated for each task,
355 * and stores task state information, including a pointer to the task's context
356 * (the task's run time environment, including register values)
358 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
360 volatile StackType_t * pxTopOfStack; /**< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
362 #if ( portUSING_MPU_WRAPPERS == 1 )
363 xMPU_SETTINGS xMPUSettings; /**< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
366 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
367 UBaseType_t uxCoreAffinityMask; /**< Used to link the task to certain cores. UBaseType_t must have greater than or equal to the number of bits as configNUMBER_OF_CORES. */
370 ListItem_t xStateListItem; /**< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
371 ListItem_t xEventListItem; /**< Used to reference a task from an event list. */
372 UBaseType_t uxPriority; /**< The priority of the task. 0 is the lowest priority. */
373 StackType_t * pxStack; /**< Points to the start of the stack. */
374 #if ( configNUMBER_OF_CORES > 1 )
375 volatile BaseType_t xTaskRunState; /**< Used to identify the core the task is running on, if the task is running. Otherwise, identifies the task's state - not running or yielding. */
376 UBaseType_t uxTaskAttributes; /**< Task's attributes - currently used to identify the idle tasks. */
378 char pcTaskName[ configMAX_TASK_NAME_LEN ]; /**< Descriptive name given to the task when created. Facilitates debugging only. */
380 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
381 BaseType_t xPreemptionDisable; /**< Used to prevent the task from being preempted. */
384 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
385 StackType_t * pxEndOfStack; /**< Points to the highest valid address for the stack. */
388 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
389 UBaseType_t uxCriticalNesting; /**< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
392 #if ( configUSE_TRACE_FACILITY == 1 )
393 UBaseType_t uxTCBNumber; /**< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
394 UBaseType_t uxTaskNumber; /**< Stores a number specifically for use by third party trace code. */
397 #if ( configUSE_MUTEXES == 1 )
398 UBaseType_t uxBasePriority; /**< The priority last assigned to the task - used by the priority inheritance mechanism. */
399 UBaseType_t uxMutexesHeld;
402 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
403 TaskHookFunction_t pxTaskTag;
406 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
407 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
410 #if ( configGENERATE_RUN_TIME_STATS == 1 )
411 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /**< Stores the amount of time the task has spent in the Running state. */
414 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
415 configTLS_BLOCK_TYPE xTLSBlock; /**< Memory block used as Thread Local Storage (TLS) Block for the task. */
418 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
419 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
420 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
423 /* See the comments in FreeRTOS.h with the definition of
424 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
425 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
426 uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
429 #if ( INCLUDE_xTaskAbortDelay == 1 )
430 uint8_t ucDelayAborted;
433 #if ( configUSE_POSIX_ERRNO == 1 )
438 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
439 * below to enable the use of older kernel aware debuggers. */
440 typedef tskTCB TCB_t;
442 #if ( configNUMBER_OF_CORES == 1 )
443 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
444 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
445 /* coverity[misra_c_2012_rule_8_4_violation] */
446 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
448 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
449 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
450 /* coverity[misra_c_2012_rule_8_4_violation] */
451 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ];
452 #define pxCurrentTCB xTaskGetCurrentTaskHandle()
455 /* Lists for ready and blocked tasks. --------------------
456 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
457 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
458 * the static qualifier. */
459 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
460 PRIVILEGED_DATA static List_t xDelayedTaskList1; /**< Delayed tasks. */
461 PRIVILEGED_DATA static List_t xDelayedTaskList2; /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
462 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /**< Points to the delayed task list currently being used. */
463 PRIVILEGED_DATA static List_t * volatile pxOverflowDelayedTaskList; /**< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
464 PRIVILEGED_DATA static List_t xPendingReadyList; /**< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready list when the scheduler is resumed. */
466 #if ( INCLUDE_vTaskDelete == 1 )
468 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
469 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
473 #if ( INCLUDE_vTaskSuspend == 1 )
475 PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
479 /* Global POSIX errno. Its value is changed upon context switching to match
480 * the errno of the currently running task. */
481 #if ( configUSE_POSIX_ERRNO == 1 )
482 int FreeRTOS_errno = 0;
485 /* Other file private variables. --------------------------------*/
486 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
487 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
488 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
489 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
490 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
491 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
492 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
493 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
494 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
495 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandles[ configNUMBER_OF_CORES ]; /**< Holds the handles of the idle tasks. The idle tasks are created automatically when the scheduler is started. */
497 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
498 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
499 * to determine the number of priority lists to read back from the remote target. */
500 static const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
502 /* Context switches are held pending while the scheduler is suspended. Also,
503 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
504 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
505 * If an interrupt needs to unblock a task while the scheduler is suspended then it
506 * moves the task's event list item into the xPendingReadyList, ready for the
507 * kernel to move the task from the pending ready list into the real ready list
508 * when the scheduler is unsuspended. The pending ready list itself can only be
509 * accessed from a critical section.
511 * Updates to uxSchedulerSuspended must be protected by both the task lock and the ISR lock
512 * and must not be done from an ISR. Reads must be protected by either lock and may be done
513 * from either an ISR or a task. */
514 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
516 #if ( configGENERATE_RUN_TIME_STATS == 1 )
518 /* Do not move these variables to function scope as doing so prevents the
519 * code working with debuggers that need to remove the static qualifier. */
520 PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the value of a timer/counter the last time a task was switched in. */
521 PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the total amount of execution time as defined by the run time counter clock. */
525 /*-----------------------------------------------------------*/
527 /* File private functions. --------------------------------*/
530 * Creates the idle tasks during scheduler start.
532 static BaseType_t prvCreateIdleTasks( void );
534 #if ( configNUMBER_OF_CORES > 1 )
537 * Checks to see if another task moved the current task out of the ready
538 * list while it was waiting to enter a critical section and yields, if so.
540 static void prvCheckForRunStateChange( void );
541 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
543 #if ( configNUMBER_OF_CORES > 1 )
546 * Yields a core, or cores if multiple priorities are not allowed to run
547 * simultaneously, to allow the task pxTCB to run.
549 static void prvYieldForTask( const TCB_t * pxTCB );
550 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
552 #if ( configNUMBER_OF_CORES > 1 )
555 * Selects the highest priority available task for the given core.
557 static void prvSelectHighestPriorityTask( BaseType_t xCoreID );
558 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
561 * Utility task that simply returns pdTRUE if the task referenced by xTask is
562 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
563 * is in any other state.
565 #if ( INCLUDE_vTaskSuspend == 1 )
567 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
569 #endif /* INCLUDE_vTaskSuspend */
572 * Utility to ready all the lists used by the scheduler. This is called
573 * automatically upon the creation of the first task.
575 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
578 * The idle task, which as all tasks is implemented as a never ending loop.
579 * The idle task is automatically created and added to the ready lists upon
580 * creation of the first user task.
582 * In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks are also
583 * created to ensure that each core has an idle task to run when no other
584 * task is available to run.
586 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
587 * language extensions. The equivalent prototype for these functions are:
589 * void prvIdleTask( void *pvParameters );
590 * void prvPassiveIdleTask( void *pvParameters );
593 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
594 #if ( configNUMBER_OF_CORES > 1 )
595 static portTASK_FUNCTION_PROTO( prvPassiveIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
599 * Utility to free all memory allocated by the scheduler to hold a TCB,
600 * including the stack pointed to by the TCB.
602 * This does not free memory allocated by the task itself (i.e. memory
603 * allocated by calls to pvPortMalloc from within the tasks application code).
605 #if ( INCLUDE_vTaskDelete == 1 )
607 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
612 * Used only by the idle task. This checks to see if anything has been placed
613 * in the list of tasks waiting to be deleted. If so the task is cleaned up
614 * and its TCB deleted.
616 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
619 * The currently executing task is entering the Blocked state. Add the task to
620 * either the current or the overflow delayed task list.
622 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
623 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
626 * Fills an TaskStatus_t structure with information on each task that is
627 * referenced from the pxList list (which may be a ready list, a delayed list,
628 * a suspended list, etc.).
630 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
631 * NORMAL APPLICATION CODE.
633 #if ( configUSE_TRACE_FACILITY == 1 )
635 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
637 eTaskState eState ) PRIVILEGED_FUNCTION;
642 * Searches pxList for a task with name pcNameToQuery - returning a handle to
643 * the task if it is found, or NULL if the task is not found.
645 #if ( INCLUDE_xTaskGetHandle == 1 )
647 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
648 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
653 * When a task is created, the stack of the task is filled with a known value.
654 * This function determines the 'high water mark' of the task stack by
655 * determining how much of the stack remains at the original preset value.
657 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
659 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
664 * Return the amount of time, in ticks, that will pass before the kernel will
665 * next move a task from the Blocked state to the Running state.
667 * This conditional compilation should use inequality to 0, not equality to 1.
668 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
669 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
670 * set to a value other than 1.
672 #if ( configUSE_TICKLESS_IDLE != 0 )
674 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
679 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
680 * will exit the Blocked state.
682 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
684 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
687 * Helper function used to pad task names with spaces when printing out
688 * human readable tables of task information.
690 static char * prvWriteNameToBuffer( char * pcBuffer,
691 const char * pcTaskName ) PRIVILEGED_FUNCTION;
696 * Called after a Task_t structure has been allocated either statically or
697 * dynamically to fill in the structure's members.
699 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
700 const char * const pcName,
701 const configSTACK_DEPTH_TYPE uxStackDepth,
702 void * const pvParameters,
703 UBaseType_t uxPriority,
704 TaskHandle_t * const pxCreatedTask,
706 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
709 * Called after a new task has been created and initialised to place the task
710 * under the control of the scheduler.
712 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
715 * Create a task with static buffer for both TCB and stack. Returns a handle to
716 * the task if it is created successfully. Otherwise, returns NULL.
718 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
719 static TCB_t * prvCreateStaticTask( TaskFunction_t pxTaskCode,
720 const char * const pcName,
721 const configSTACK_DEPTH_TYPE uxStackDepth,
722 void * const pvParameters,
723 UBaseType_t uxPriority,
724 StackType_t * const puxStackBuffer,
725 StaticTask_t * const pxTaskBuffer,
726 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
727 #endif /* #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
730 * Create a restricted task with static buffer for both TCB and stack. Returns
731 * a handle to the task if it is created successfully. Otherwise, returns NULL.
733 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
734 static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
735 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
736 #endif /* #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
739 * Create a restricted task with static buffer for task stack and allocated buffer
740 * for TCB. Returns a handle to the task if it is created successfully. Otherwise,
743 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
744 static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
745 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
746 #endif /* #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
749 * Create a task with allocated buffer for both TCB and stack. Returns a handle to
750 * the task if it is created successfully. Otherwise, returns NULL.
752 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
753 static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
754 const char * const pcName,
755 const configSTACK_DEPTH_TYPE uxStackDepth,
756 void * const pvParameters,
757 UBaseType_t uxPriority,
758 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
759 #endif /* #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) */
762 * freertos_tasks_c_additions_init() should only be called if the user definable
763 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
764 * called by the function.
766 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
768 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
772 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
773 extern void vApplicationPassiveIdleHook( void );
774 #endif /* #if ( configUSE_PASSIVE_IDLE_HOOK == 1 ) */
776 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
779 * Convert the snprintf return value to the number of characters
780 * written. The following are the possible cases:
782 * 1. The buffer supplied to snprintf is large enough to hold the
783 * generated string. The return value in this case is the number
784 * of characters actually written, not counting the terminating
786 * 2. The buffer supplied to snprintf is NOT large enough to hold
787 * the generated string. The return value in this case is the
788 * number of characters that would have been written if the
789 * buffer had been sufficiently large, not counting the
790 * terminating null character.
791 * 3. Encoding error. The return value in this case is a negative
794 * From 1 and 2 above ==> Only when the return value is non-negative
795 * and less than the supplied buffer length, the string has been
796 * completely written.
798 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
801 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
802 /*-----------------------------------------------------------*/
804 #if ( configNUMBER_OF_CORES > 1 )
805 static void prvCheckForRunStateChange( void )
807 UBaseType_t uxPrevCriticalNesting;
808 const TCB_t * pxThisTCB;
810 /* This must only be called from within a task. */
811 portASSERT_IF_IN_ISR();
813 /* This function is always called with interrupts disabled
814 * so this is safe. */
815 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
817 while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD )
819 /* We are only here if we just entered a critical section
820 * or if we just suspended the scheduler, and another task
821 * has requested that we yield.
823 * This is slightly complicated since we need to save and restore
824 * the suspension and critical nesting counts, as well as release
825 * and reacquire the correct locks. And then, do it all over again
826 * if our state changed again during the reacquisition. */
827 uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
829 if( uxPrevCriticalNesting > 0U )
831 portSET_CRITICAL_NESTING_COUNT( 0U );
832 portRELEASE_ISR_LOCK();
836 /* The scheduler is suspended. uxSchedulerSuspended is updated
837 * only when the task is not requested to yield. */
838 mtCOVERAGE_TEST_MARKER();
841 portRELEASE_TASK_LOCK();
842 portMEMORY_BARRIER();
843 configASSERT( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD );
845 portENABLE_INTERRUPTS();
847 /* Enabling interrupts should cause this core to immediately
848 * service the pending interrupt and yield. If the run state is still
849 * yielding here then that is a problem. */
850 configASSERT( pxThisTCB->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD );
852 portDISABLE_INTERRUPTS();
856 portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
858 if( uxPrevCriticalNesting == 0U )
860 portRELEASE_ISR_LOCK();
864 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
866 /*-----------------------------------------------------------*/
868 #if ( configNUMBER_OF_CORES > 1 )
869 static void prvYieldForTask( const TCB_t * pxTCB )
871 BaseType_t xLowestPriorityToPreempt;
872 BaseType_t xCurrentCoreTaskPriority;
873 BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
876 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
877 BaseType_t xYieldCount = 0;
878 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
880 /* This must be called from a critical section. */
881 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
883 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
885 /* No task should yield for this one if it is a lower priority
886 * than priority level of currently ready tasks. */
887 if( pxTCB->uxPriority >= uxTopReadyPriority )
889 /* Yield is not required for a task which is already running. */
890 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
893 xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
895 /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
896 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
897 --xLowestPriorityToPreempt;
899 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
901 xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
903 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
904 if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
906 xCurrentCoreTaskPriority = xCurrentCoreTaskPriority - 1;
909 if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
911 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
912 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
915 if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
917 #if ( configUSE_CORE_AFFINITY == 1 )
918 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
921 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
922 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
925 xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
926 xLowestPriorityCore = xCoreID;
932 mtCOVERAGE_TEST_MARKER();
936 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
938 /* Yield all currently running non-idle tasks with a priority lower than
939 * the task that needs to run. */
940 if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
941 ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
943 prvYieldCore( xCoreID );
948 mtCOVERAGE_TEST_MARKER();
951 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
955 mtCOVERAGE_TEST_MARKER();
959 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
960 if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
961 #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
962 if( xLowestPriorityCore >= 0 )
963 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
965 prvYieldCore( xLowestPriorityCore );
968 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
969 /* Verify that the calling core always yields to higher priority tasks. */
970 if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) &&
971 ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
973 configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) ||
974 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) );
979 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
980 /*-----------------------------------------------------------*/
982 #if ( configNUMBER_OF_CORES > 1 )
983 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
985 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
986 BaseType_t xTaskScheduled = pdFALSE;
987 BaseType_t xDecrementTopPriority = pdTRUE;
989 #if ( configUSE_CORE_AFFINITY == 1 )
990 const TCB_t * pxPreviousTCB = NULL;
992 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
993 BaseType_t xPriorityDropped = pdFALSE;
996 /* This function should be called when scheduler is running. */
997 configASSERT( xSchedulerRunning == pdTRUE );
999 /* A new task is created and a running task with the same priority yields
1000 * itself to run the new task. When a running task yields itself, it is still
1001 * in the ready list. This running task will be selected before the new task
1002 * since the new task is always added to the end of the ready list.
1003 * The other problem is that the running task still in the same position of
1004 * the ready list when it yields itself. It is possible that it will be selected
1005 * earlier then other tasks which waits longer than this task.
1007 * To fix these problems, the running task should be put to the end of the
1008 * ready list before searching for the ready task in the ready list. */
1009 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
1010 &pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
1012 ( void ) uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
1013 vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
1014 &pxCurrentTCBs[ xCoreID ]->xStateListItem );
1017 while( xTaskScheduled == pdFALSE )
1019 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1021 if( uxCurrentPriority < uxTopReadyPriority )
1023 /* We can't schedule any tasks, other than idle, that have a
1024 * priority lower than the priority of a task currently running
1025 * on another core. */
1026 uxCurrentPriority = tskIDLE_PRIORITY;
1031 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
1033 const List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
1034 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxReadyList );
1035 ListItem_t * pxIterator;
1037 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
1038 * must not be decremented any further. */
1039 xDecrementTopPriority = pdFALSE;
1041 for( pxIterator = listGET_HEAD_ENTRY( pxReadyList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
1043 /* MISRA Ref 11.5.3 [Void pointer assignment] */
1044 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1045 /* coverity[misra_c_2012_rule_11_5_violation] */
1046 TCB_t * pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
1048 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1050 /* When falling back to the idle priority because only one priority
1051 * level is allowed to run at a time, we should ONLY schedule the true
1052 * idle tasks, not user tasks at the idle priority. */
1053 if( uxCurrentPriority < uxTopReadyPriority )
1055 if( ( pxTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U )
1061 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1063 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
1065 #if ( configUSE_CORE_AFFINITY == 1 )
1066 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1069 /* If the task is not being executed by any core swap it in. */
1070 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
1071 #if ( configUSE_CORE_AFFINITY == 1 )
1072 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
1074 pxTCB->xTaskRunState = xCoreID;
1075 pxCurrentTCBs[ xCoreID ] = pxTCB;
1076 xTaskScheduled = pdTRUE;
1079 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
1081 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) );
1083 #if ( configUSE_CORE_AFFINITY == 1 )
1084 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1087 /* The task is already running on this core, mark it as scheduled. */
1088 pxTCB->xTaskRunState = xCoreID;
1089 xTaskScheduled = pdTRUE;
1094 /* This task is running on the core other than xCoreID. */
1095 mtCOVERAGE_TEST_MARKER();
1098 if( xTaskScheduled != pdFALSE )
1100 /* A task has been selected to run on this core. */
1107 if( xDecrementTopPriority != pdFALSE )
1109 uxTopReadyPriority--;
1110 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1112 xPriorityDropped = pdTRUE;
1118 /* There are configNUMBER_OF_CORES Idle tasks created when scheduler started.
1119 * The scheduler should be able to select a task to run when uxCurrentPriority
1120 * is tskIDLE_PRIORITY. uxCurrentPriority is never decreased to value blow
1121 * tskIDLE_PRIORITY. */
1122 if( uxCurrentPriority > tskIDLE_PRIORITY )
1124 uxCurrentPriority--;
1128 /* This function is called when idle task is not created. Break the
1129 * loop to prevent uxCurrentPriority overrun. */
1134 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1136 if( xTaskScheduled == pdTRUE )
1138 if( xPriorityDropped != pdFALSE )
1140 /* There may be several ready tasks that were being prevented from running because there was
1141 * a higher priority task running. Now that the last of the higher priority tasks is no longer
1142 * running, make sure all the other idle tasks yield. */
1145 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUMBER_OF_CORES; x++ )
1147 if( ( pxCurrentTCBs[ x ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1155 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1157 #if ( configUSE_CORE_AFFINITY == 1 )
1159 if( xTaskScheduled == pdTRUE )
1161 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
1163 /* A ready task was just evicted from this core. See if it can be
1164 * scheduled on any other core. */
1165 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
1166 BaseType_t xLowestPriority = ( BaseType_t ) pxPreviousTCB->uxPriority;
1167 BaseType_t xLowestPriorityCore = -1;
1170 if( ( pxPreviousTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1172 xLowestPriority = xLowestPriority - 1;
1175 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1177 /* pxPreviousTCB was removed from this core and this core is not excluded
1178 * from it's core affinity mask.
1180 * pxPreviousTCB is preempted by the new higher priority task
1181 * pxCurrentTCBs[ xCoreID ]. When searching a new core for pxPreviousTCB,
1182 * we do not need to look at the cores on which pxCurrentTCBs[ xCoreID ]
1183 * is allowed to run. The reason is - when more than one cores are
1184 * eligible for an incoming task, we preempt the core with the minimum
1185 * priority task. Because this core (i.e. xCoreID) was preempted for
1186 * pxCurrentTCBs[ xCoreID ], this means that all the others cores
1187 * where pxCurrentTCBs[ xCoreID ] can run, are running tasks with priority
1188 * no lower than pxPreviousTCB's priority. Therefore, the only cores where
1189 * which can be preempted for pxPreviousTCB are the ones where
1190 * pxCurrentTCBs[ xCoreID ] is not allowed to run (and obviously,
1191 * pxPreviousTCB is allowed to run).
1193 * This is an optimization which reduces the number of cores needed to be
1194 * searched for pxPreviousTCB to run. */
1195 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1199 /* pxPreviousTCB's core affinity mask is changed and it is no longer
1200 * allowed to run on this core. Searching all the cores in pxPreviousTCB's
1201 * new core affinity mask to find a core on which it can run. */
1204 uxCoreMap &= ( ( 1U << configNUMBER_OF_CORES ) - 1U );
1206 for( x = ( ( BaseType_t ) configNUMBER_OF_CORES - 1 ); x >= ( BaseType_t ) 0; x-- )
1208 UBaseType_t uxCore = ( UBaseType_t ) x;
1209 BaseType_t xTaskPriority;
1211 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << uxCore ) ) != 0U )
1213 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority;
1215 if( ( pxCurrentTCBs[ uxCore ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1217 xTaskPriority = xTaskPriority - ( BaseType_t ) 1;
1220 uxCoreMap &= ~( ( UBaseType_t ) 1U << uxCore );
1222 if( ( xTaskPriority < xLowestPriority ) &&
1223 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ] ) != pdFALSE ) &&
1224 ( xYieldPendings[ uxCore ] == pdFALSE ) )
1226 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1227 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1230 xLowestPriority = xTaskPriority;
1231 xLowestPriorityCore = ( BaseType_t ) uxCore;
1237 if( xLowestPriorityCore >= 0 )
1239 prvYieldCore( xLowestPriorityCore );
1244 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) */
1247 #endif /* ( configNUMBER_OF_CORES > 1 ) */
1249 /*-----------------------------------------------------------*/
1251 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1253 static TCB_t * prvCreateStaticTask( TaskFunction_t pxTaskCode,
1254 const char * const pcName,
1255 const configSTACK_DEPTH_TYPE uxStackDepth,
1256 void * const pvParameters,
1257 UBaseType_t uxPriority,
1258 StackType_t * const puxStackBuffer,
1259 StaticTask_t * const pxTaskBuffer,
1260 TaskHandle_t * const pxCreatedTask )
1264 configASSERT( puxStackBuffer != NULL );
1265 configASSERT( pxTaskBuffer != NULL );
1267 #if ( configASSERT_DEFINED == 1 )
1269 /* Sanity check that the size of the structure used to declare a
1270 * variable of type StaticTask_t equals the size of the real task
1272 volatile size_t xSize = sizeof( StaticTask_t );
1273 configASSERT( xSize == sizeof( TCB_t ) );
1274 ( void ) xSize; /* Prevent unused variable warning when configASSERT() is not used. */
1276 #endif /* configASSERT_DEFINED */
1278 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1280 /* The memory used for the task's TCB and stack are passed into this
1281 * function - use them. */
1282 /* MISRA Ref 11.3.1 [Misaligned access] */
1283 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
1284 /* coverity[misra_c_2012_rule_11_3_violation] */
1285 pxNewTCB = ( TCB_t * ) pxTaskBuffer;
1286 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1287 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1289 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1291 /* Tasks can be created statically or dynamically, so note this
1292 * task was created statically in case the task is later deleted. */
1293 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1295 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1297 prvInitialiseNewTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1306 /*-----------------------------------------------------------*/
1308 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1309 const char * const pcName,
1310 const configSTACK_DEPTH_TYPE uxStackDepth,
1311 void * const pvParameters,
1312 UBaseType_t uxPriority,
1313 StackType_t * const puxStackBuffer,
1314 StaticTask_t * const pxTaskBuffer )
1316 TaskHandle_t xReturn = NULL;
1319 traceENTER_xTaskCreateStatic( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer );
1321 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1323 if( pxNewTCB != NULL )
1325 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1327 /* Set the task's affinity before scheduling it. */
1328 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1332 prvAddNewTaskToReadyList( pxNewTCB );
1336 mtCOVERAGE_TEST_MARKER();
1339 traceRETURN_xTaskCreateStatic( xReturn );
1343 /*-----------------------------------------------------------*/
1345 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1346 TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1347 const char * const pcName,
1348 const configSTACK_DEPTH_TYPE uxStackDepth,
1349 void * const pvParameters,
1350 UBaseType_t uxPriority,
1351 StackType_t * const puxStackBuffer,
1352 StaticTask_t * const pxTaskBuffer,
1353 UBaseType_t uxCoreAffinityMask )
1355 TaskHandle_t xReturn = NULL;
1358 traceENTER_xTaskCreateStaticAffinitySet( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, uxCoreAffinityMask );
1360 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1362 if( pxNewTCB != NULL )
1364 /* Set the task's affinity before scheduling it. */
1365 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1367 prvAddNewTaskToReadyList( pxNewTCB );
1371 mtCOVERAGE_TEST_MARKER();
1374 traceRETURN_xTaskCreateStaticAffinitySet( xReturn );
1378 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1380 #endif /* SUPPORT_STATIC_ALLOCATION */
1381 /*-----------------------------------------------------------*/
1383 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1384 static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
1385 TaskHandle_t * const pxCreatedTask )
1389 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1390 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1392 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1394 /* Allocate space for the TCB. Where the memory comes from depends
1395 * on the implementation of the port malloc function and whether or
1396 * not static allocation is being used. */
1397 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1398 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1400 /* Store the stack location in the TCB. */
1401 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1403 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1405 /* Tasks can be created statically or dynamically, so note this
1406 * task was created statically in case the task is later deleted. */
1407 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1409 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1411 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1412 pxTaskDefinition->pcName,
1413 pxTaskDefinition->usStackDepth,
1414 pxTaskDefinition->pvParameters,
1415 pxTaskDefinition->uxPriority,
1416 pxCreatedTask, pxNewTCB,
1417 pxTaskDefinition->xRegions );
1426 /*-----------------------------------------------------------*/
1428 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1429 TaskHandle_t * pxCreatedTask )
1434 traceENTER_xTaskCreateRestrictedStatic( pxTaskDefinition, pxCreatedTask );
1436 configASSERT( pxTaskDefinition != NULL );
1438 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1440 if( pxNewTCB != NULL )
1442 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1444 /* Set the task's affinity before scheduling it. */
1445 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1449 prvAddNewTaskToReadyList( pxNewTCB );
1454 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1457 traceRETURN_xTaskCreateRestrictedStatic( xReturn );
1461 /*-----------------------------------------------------------*/
1463 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1464 BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1465 UBaseType_t uxCoreAffinityMask,
1466 TaskHandle_t * pxCreatedTask )
1471 traceENTER_xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1473 configASSERT( pxTaskDefinition != NULL );
1475 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1477 if( pxNewTCB != NULL )
1479 /* Set the task's affinity before scheduling it. */
1480 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1482 prvAddNewTaskToReadyList( pxNewTCB );
1487 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1490 traceRETURN_xTaskCreateRestrictedStaticAffinitySet( xReturn );
1494 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1496 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1497 /*-----------------------------------------------------------*/
1499 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1500 static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
1501 TaskHandle_t * const pxCreatedTask )
1505 configASSERT( pxTaskDefinition->puxStackBuffer );
1507 if( pxTaskDefinition->puxStackBuffer != NULL )
1509 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1510 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1511 /* coverity[misra_c_2012_rule_11_5_violation] */
1512 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1514 if( pxNewTCB != NULL )
1516 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1518 /* Store the stack location in the TCB. */
1519 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1521 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1523 /* Tasks can be created statically or dynamically, so note
1524 * this task had a statically allocated stack in case it is
1525 * later deleted. The TCB was allocated dynamically. */
1526 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1528 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1530 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1531 pxTaskDefinition->pcName,
1532 pxTaskDefinition->usStackDepth,
1533 pxTaskDefinition->pvParameters,
1534 pxTaskDefinition->uxPriority,
1535 pxCreatedTask, pxNewTCB,
1536 pxTaskDefinition->xRegions );
1546 /*-----------------------------------------------------------*/
1548 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1549 TaskHandle_t * pxCreatedTask )
1554 traceENTER_xTaskCreateRestricted( pxTaskDefinition, pxCreatedTask );
1556 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1558 if( pxNewTCB != NULL )
1560 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1562 /* Set the task's affinity before scheduling it. */
1563 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1565 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1567 prvAddNewTaskToReadyList( pxNewTCB );
1573 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1576 traceRETURN_xTaskCreateRestricted( xReturn );
1580 /*-----------------------------------------------------------*/
1582 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1583 BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1584 UBaseType_t uxCoreAffinityMask,
1585 TaskHandle_t * pxCreatedTask )
1590 traceENTER_xTaskCreateRestrictedAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1592 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1594 if( pxNewTCB != NULL )
1596 /* Set the task's affinity before scheduling it. */
1597 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1599 prvAddNewTaskToReadyList( pxNewTCB );
1605 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1608 traceRETURN_xTaskCreateRestrictedAffinitySet( xReturn );
1612 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1615 #endif /* portUSING_MPU_WRAPPERS */
1616 /*-----------------------------------------------------------*/
1618 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1619 static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
1620 const char * const pcName,
1621 const configSTACK_DEPTH_TYPE uxStackDepth,
1622 void * const pvParameters,
1623 UBaseType_t uxPriority,
1624 TaskHandle_t * const pxCreatedTask )
1628 /* If the stack grows down then allocate the stack then the TCB so the stack
1629 * does not grow into the TCB. Likewise if the stack grows up then allocate
1630 * the TCB then the stack. */
1631 #if ( portSTACK_GROWTH > 0 )
1633 /* Allocate space for the TCB. Where the memory comes from depends on
1634 * the implementation of the port malloc function and whether or not static
1635 * allocation is being used. */
1636 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1637 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1638 /* coverity[misra_c_2012_rule_11_5_violation] */
1639 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1641 if( pxNewTCB != NULL )
1643 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1645 /* Allocate space for the stack used by the task being created.
1646 * The base of the stack memory stored in the TCB so the task can
1647 * be deleted later if required. */
1648 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1649 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1650 /* coverity[misra_c_2012_rule_11_5_violation] */
1651 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) uxStackDepth ) * sizeof( StackType_t ) ) );
1653 if( pxNewTCB->pxStack == NULL )
1655 /* Could not allocate the stack. Delete the allocated TCB. */
1656 vPortFree( pxNewTCB );
1661 #else /* portSTACK_GROWTH */
1663 StackType_t * pxStack;
1665 /* Allocate space for the stack used by the task being created. */
1666 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1667 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1668 /* coverity[misra_c_2012_rule_11_5_violation] */
1669 pxStack = pvPortMallocStack( ( ( ( size_t ) uxStackDepth ) * sizeof( StackType_t ) ) );
1671 if( pxStack != NULL )
1673 /* Allocate space for the TCB. */
1674 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1675 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1676 /* coverity[misra_c_2012_rule_11_5_violation] */
1677 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1679 if( pxNewTCB != NULL )
1681 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1683 /* Store the stack location in the TCB. */
1684 pxNewTCB->pxStack = pxStack;
1688 /* The stack cannot be used as the TCB was not created. Free
1690 vPortFreeStack( pxStack );
1698 #endif /* portSTACK_GROWTH */
1700 if( pxNewTCB != NULL )
1702 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1704 /* Tasks can be created statically or dynamically, so note this
1705 * task was created dynamically in case it is later deleted. */
1706 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1708 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1710 prvInitialiseNewTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1715 /*-----------------------------------------------------------*/
1717 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1718 const char * const pcName,
1719 const configSTACK_DEPTH_TYPE uxStackDepth,
1720 void * const pvParameters,
1721 UBaseType_t uxPriority,
1722 TaskHandle_t * const pxCreatedTask )
1727 traceENTER_xTaskCreate( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
1729 pxNewTCB = prvCreateTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
1731 if( pxNewTCB != NULL )
1733 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1735 /* Set the task's affinity before scheduling it. */
1736 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1740 prvAddNewTaskToReadyList( pxNewTCB );
1745 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1748 traceRETURN_xTaskCreate( xReturn );
1752 /*-----------------------------------------------------------*/
1754 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1755 BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1756 const char * const pcName,
1757 const configSTACK_DEPTH_TYPE uxStackDepth,
1758 void * const pvParameters,
1759 UBaseType_t uxPriority,
1760 UBaseType_t uxCoreAffinityMask,
1761 TaskHandle_t * const pxCreatedTask )
1766 traceENTER_xTaskCreateAffinitySet( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, uxCoreAffinityMask, pxCreatedTask );
1768 pxNewTCB = prvCreateTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
1770 if( pxNewTCB != NULL )
1772 /* Set the task's affinity before scheduling it. */
1773 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1775 prvAddNewTaskToReadyList( pxNewTCB );
1780 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1783 traceRETURN_xTaskCreateAffinitySet( xReturn );
1787 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1789 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1790 /*-----------------------------------------------------------*/
1792 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1793 const char * const pcName,
1794 const configSTACK_DEPTH_TYPE uxStackDepth,
1795 void * const pvParameters,
1796 UBaseType_t uxPriority,
1797 TaskHandle_t * const pxCreatedTask,
1799 const MemoryRegion_t * const xRegions )
1801 StackType_t * pxTopOfStack;
1804 #if ( portUSING_MPU_WRAPPERS == 1 )
1805 /* Should the task be created in privileged mode? */
1806 BaseType_t xRunPrivileged;
1808 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1810 xRunPrivileged = pdTRUE;
1814 xRunPrivileged = pdFALSE;
1816 uxPriority &= ~portPRIVILEGE_BIT;
1817 #endif /* portUSING_MPU_WRAPPERS == 1 */
1819 /* Avoid dependency on memset() if it is not required. */
1820 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1822 /* Fill the stack with a known value to assist debugging. */
1823 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) uxStackDepth * sizeof( StackType_t ) );
1825 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1827 /* Calculate the top of stack address. This depends on whether the stack
1828 * grows from high memory to low (as per the 80x86) or vice versa.
1829 * portSTACK_GROWTH is used to make the result positive or negative as required
1831 #if ( portSTACK_GROWTH < 0 )
1833 pxTopOfStack = &( pxNewTCB->pxStack[ uxStackDepth - ( configSTACK_DEPTH_TYPE ) 1 ] );
1834 pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
1836 /* Check the alignment of the calculated top of stack is correct. */
1837 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1839 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1841 /* Also record the stack's high address, which may assist
1843 pxNewTCB->pxEndOfStack = pxTopOfStack;
1845 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1847 #else /* portSTACK_GROWTH */
1849 pxTopOfStack = pxNewTCB->pxStack;
1850 pxTopOfStack = ( StackType_t * ) ( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) + portBYTE_ALIGNMENT_MASK ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
1852 /* Check the alignment of the calculated top of stack is correct. */
1853 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1855 /* The other extreme of the stack space is required if stack checking is
1857 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( uxStackDepth - ( configSTACK_DEPTH_TYPE ) 1 );
1859 #endif /* portSTACK_GROWTH */
1861 /* Store the task name in the TCB. */
1862 if( pcName != NULL )
1864 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1866 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1868 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1869 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1870 * string is not accessible (extremely unlikely). */
1871 if( pcName[ x ] == ( char ) 0x00 )
1877 mtCOVERAGE_TEST_MARKER();
1881 /* Ensure the name string is terminated in the case that the string length
1882 * was greater or equal to configMAX_TASK_NAME_LEN. */
1883 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1U ] = '\0';
1887 mtCOVERAGE_TEST_MARKER();
1890 /* This is used as an array index so must ensure it's not too large. */
1891 configASSERT( uxPriority < configMAX_PRIORITIES );
1893 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1895 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1899 mtCOVERAGE_TEST_MARKER();
1902 pxNewTCB->uxPriority = uxPriority;
1903 #if ( configUSE_MUTEXES == 1 )
1905 pxNewTCB->uxBasePriority = uxPriority;
1907 #endif /* configUSE_MUTEXES */
1909 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1910 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1912 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1913 * back to the containing TCB from a generic item in a list. */
1914 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1916 /* Event lists are always in priority order. */
1917 listSET_LIST_ITEM_VALUE( &( pxNewTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority );
1918 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1920 #if ( portUSING_MPU_WRAPPERS == 1 )
1922 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, uxStackDepth );
1926 /* Avoid compiler warning about unreferenced parameter. */
1931 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
1933 /* Allocate and initialize memory for the task's TLS Block. */
1934 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
1938 /* Initialize the TCB stack to look as if the task was already running,
1939 * but had been interrupted by the scheduler. The return address is set
1940 * to the start of the task function. Once the stack has been initialised
1941 * the top of stack variable is updated. */
1942 #if ( portUSING_MPU_WRAPPERS == 1 )
1944 /* If the port has capability to detect stack overflow,
1945 * pass the stack end address to the stack initialization
1946 * function as well. */
1947 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1949 #if ( portSTACK_GROWTH < 0 )
1951 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1953 #else /* portSTACK_GROWTH */
1955 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1957 #endif /* portSTACK_GROWTH */
1959 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1961 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1963 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1965 #else /* portUSING_MPU_WRAPPERS */
1967 /* If the port has capability to detect stack overflow,
1968 * pass the stack end address to the stack initialization
1969 * function as well. */
1970 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1972 #if ( portSTACK_GROWTH < 0 )
1974 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1976 #else /* portSTACK_GROWTH */
1978 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1980 #endif /* portSTACK_GROWTH */
1982 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1984 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1986 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1988 #endif /* portUSING_MPU_WRAPPERS */
1990 /* Initialize task state and task attributes. */
1991 #if ( configNUMBER_OF_CORES > 1 )
1993 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1995 /* Is this an idle task? */
1996 if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvPassiveIdleTask ) )
1998 pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
2001 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
2003 if( pxCreatedTask != NULL )
2005 /* Pass the handle out in an anonymous way. The handle can be used to
2006 * change the created task's priority, delete the created task, etc.*/
2007 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
2011 mtCOVERAGE_TEST_MARKER();
2014 /*-----------------------------------------------------------*/
2016 #if ( configNUMBER_OF_CORES == 1 )
2018 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
2020 /* Ensure interrupts don't access the task lists while the lists are being
2022 taskENTER_CRITICAL();
2024 uxCurrentNumberOfTasks++;
2026 if( pxCurrentTCB == NULL )
2028 /* There are no other tasks, or all the other tasks are in
2029 * the suspended state - make this the current task. */
2030 pxCurrentTCB = pxNewTCB;
2032 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2034 /* This is the first task to be created so do the preliminary
2035 * initialisation required. We will not recover if this call
2036 * fails, but we will report the failure. */
2037 prvInitialiseTaskLists();
2041 mtCOVERAGE_TEST_MARKER();
2046 /* If the scheduler is not already running, make this task the
2047 * current task if it is the highest priority task to be created
2049 if( xSchedulerRunning == pdFALSE )
2051 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
2053 pxCurrentTCB = pxNewTCB;
2057 mtCOVERAGE_TEST_MARKER();
2062 mtCOVERAGE_TEST_MARKER();
2068 #if ( configUSE_TRACE_FACILITY == 1 )
2070 /* Add a counter into the TCB for tracing only. */
2071 pxNewTCB->uxTCBNumber = uxTaskNumber;
2073 #endif /* configUSE_TRACE_FACILITY */
2074 traceTASK_CREATE( pxNewTCB );
2076 prvAddTaskToReadyList( pxNewTCB );
2078 portSETUP_TCB( pxNewTCB );
2080 taskEXIT_CRITICAL();
2082 if( xSchedulerRunning != pdFALSE )
2084 /* If the created task is of a higher priority than the current task
2085 * then it should run now. */
2086 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2090 mtCOVERAGE_TEST_MARKER();
2094 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2096 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
2098 /* Ensure interrupts don't access the task lists while the lists are being
2100 taskENTER_CRITICAL();
2102 uxCurrentNumberOfTasks++;
2104 if( xSchedulerRunning == pdFALSE )
2106 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2108 /* This is the first task to be created so do the preliminary
2109 * initialisation required. We will not recover if this call
2110 * fails, but we will report the failure. */
2111 prvInitialiseTaskLists();
2115 mtCOVERAGE_TEST_MARKER();
2118 /* All the cores start with idle tasks before the SMP scheduler
2119 * is running. Idle tasks are assigned to cores when they are
2120 * created in prvCreateIdleTasks(). */
2125 #if ( configUSE_TRACE_FACILITY == 1 )
2127 /* Add a counter into the TCB for tracing only. */
2128 pxNewTCB->uxTCBNumber = uxTaskNumber;
2130 #endif /* configUSE_TRACE_FACILITY */
2131 traceTASK_CREATE( pxNewTCB );
2133 prvAddTaskToReadyList( pxNewTCB );
2135 portSETUP_TCB( pxNewTCB );
2137 if( xSchedulerRunning != pdFALSE )
2139 /* If the created task is of a higher priority than another
2140 * currently running task and preemption is on then it should
2142 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2146 mtCOVERAGE_TEST_MARKER();
2149 taskEXIT_CRITICAL();
2152 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2153 /*-----------------------------------------------------------*/
2155 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
2157 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
2160 size_t uxCharsWritten;
2162 if( iSnprintfReturnValue < 0 )
2164 /* Encoding error - Return 0 to indicate that nothing
2165 * was written to the buffer. */
2168 else if( iSnprintfReturnValue >= ( int ) n )
2170 /* This is the case when the supplied buffer is not
2171 * large to hold the generated string. Return the
2172 * number of characters actually written without
2173 * counting the terminating NULL character. */
2174 uxCharsWritten = n - 1U;
2178 /* Complete string was written to the buffer. */
2179 uxCharsWritten = ( size_t ) iSnprintfReturnValue;
2182 return uxCharsWritten;
2185 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
2186 /*-----------------------------------------------------------*/
2188 #if ( INCLUDE_vTaskDelete == 1 )
2190 void vTaskDelete( TaskHandle_t xTaskToDelete )
2193 BaseType_t xDeleteTCBInIdleTask = pdFALSE;
2195 traceENTER_vTaskDelete( xTaskToDelete );
2197 taskENTER_CRITICAL();
2199 /* If null is passed in here then it is the calling task that is
2201 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
2203 /* Remove task from the ready/delayed list. */
2204 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2206 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2210 mtCOVERAGE_TEST_MARKER();
2213 /* Is the task waiting on an event also? */
2214 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2216 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2220 mtCOVERAGE_TEST_MARKER();
2223 /* Increment the uxTaskNumber also so kernel aware debuggers can
2224 * detect that the task lists need re-generating. This is done before
2225 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
2229 /* If the task is running (or yielding), we must add it to the
2230 * termination list so that an idle task can delete it when it is
2231 * no longer running. */
2232 if( taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) != pdFALSE )
2234 /* A running task or a task which is scheduled to yield is being
2235 * deleted. This cannot complete when the task is still running
2236 * on a core, as a context switch to another task is required.
2237 * Place the task in the termination list. The idle task will check
2238 * the termination list and free up any memory allocated by the
2239 * scheduler for the TCB and stack of the deleted task. */
2240 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
2242 /* Increment the ucTasksDeleted variable so the idle task knows
2243 * there is a task that has been deleted and that it should therefore
2244 * check the xTasksWaitingTermination list. */
2245 ++uxDeletedTasksWaitingCleanUp;
2247 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
2248 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
2249 traceTASK_DELETE( pxTCB );
2251 /* Delete the task TCB in idle task. */
2252 xDeleteTCBInIdleTask = pdTRUE;
2254 /* The pre-delete hook is primarily for the Windows simulator,
2255 * in which Windows specific clean up operations are performed,
2256 * after which it is not possible to yield away from this task -
2257 * hence xYieldPending is used to latch that a context switch is
2259 #if ( configNUMBER_OF_CORES == 1 )
2260 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
2262 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
2267 --uxCurrentNumberOfTasks;
2268 traceTASK_DELETE( pxTCB );
2270 /* Reset the next expected unblock time in case it referred to
2271 * the task that has just been deleted. */
2272 prvResetNextTaskUnblockTime();
2275 taskEXIT_CRITICAL();
2277 /* If the task is not deleting itself, call prvDeleteTCB from outside of
2278 * critical section. If a task deletes itself, prvDeleteTCB is called
2279 * from prvCheckTasksWaitingTermination which is called from Idle task. */
2280 if( xDeleteTCBInIdleTask != pdTRUE )
2282 prvDeleteTCB( pxTCB );
2285 /* Force a reschedule if it is the currently running task that has just
2287 if( xSchedulerRunning != pdFALSE )
2289 #if ( configNUMBER_OF_CORES == 1 )
2291 if( pxTCB == pxCurrentTCB )
2293 configASSERT( uxSchedulerSuspended == 0 );
2294 taskYIELD_WITHIN_API();
2298 mtCOVERAGE_TEST_MARKER();
2301 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2303 /* It is important to use critical section here because
2304 * checking run state of a task must be done inside a
2305 * critical section. */
2306 taskENTER_CRITICAL();
2308 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2310 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2312 configASSERT( uxSchedulerSuspended == 0 );
2313 taskYIELD_WITHIN_API();
2317 prvYieldCore( pxTCB->xTaskRunState );
2321 taskEXIT_CRITICAL();
2323 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2326 traceRETURN_vTaskDelete();
2329 #endif /* INCLUDE_vTaskDelete */
2330 /*-----------------------------------------------------------*/
2332 #if ( INCLUDE_xTaskDelayUntil == 1 )
2334 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2335 const TickType_t xTimeIncrement )
2337 TickType_t xTimeToWake;
2338 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2340 traceENTER_xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
2342 configASSERT( pxPreviousWakeTime );
2343 configASSERT( ( xTimeIncrement > 0U ) );
2347 /* Minor optimisation. The tick count cannot change in this
2349 const TickType_t xConstTickCount = xTickCount;
2351 configASSERT( uxSchedulerSuspended == 1U );
2353 /* Generate the tick time at which the task wants to wake. */
2354 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2356 if( xConstTickCount < *pxPreviousWakeTime )
2358 /* The tick count has overflowed since this function was
2359 * lasted called. In this case the only time we should ever
2360 * actually delay is if the wake time has also overflowed,
2361 * and the wake time is greater than the tick time. When this
2362 * is the case it is as if neither time had overflowed. */
2363 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2365 xShouldDelay = pdTRUE;
2369 mtCOVERAGE_TEST_MARKER();
2374 /* The tick time has not overflowed. In this case we will
2375 * delay if either the wake time has overflowed, and/or the
2376 * tick time is less than the wake time. */
2377 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2379 xShouldDelay = pdTRUE;
2383 mtCOVERAGE_TEST_MARKER();
2387 /* Update the wake time ready for the next call. */
2388 *pxPreviousWakeTime = xTimeToWake;
2390 if( xShouldDelay != pdFALSE )
2392 traceTASK_DELAY_UNTIL( xTimeToWake );
2394 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2395 * the time to wake, so subtract the current tick count. */
2396 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2400 mtCOVERAGE_TEST_MARKER();
2403 xAlreadyYielded = xTaskResumeAll();
2405 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2406 * have put ourselves to sleep. */
2407 if( xAlreadyYielded == pdFALSE )
2409 taskYIELD_WITHIN_API();
2413 mtCOVERAGE_TEST_MARKER();
2416 traceRETURN_xTaskDelayUntil( xShouldDelay );
2418 return xShouldDelay;
2421 #endif /* INCLUDE_xTaskDelayUntil */
2422 /*-----------------------------------------------------------*/
2424 #if ( INCLUDE_vTaskDelay == 1 )
2426 void vTaskDelay( const TickType_t xTicksToDelay )
2428 BaseType_t xAlreadyYielded = pdFALSE;
2430 traceENTER_vTaskDelay( xTicksToDelay );
2432 /* A delay time of zero just forces a reschedule. */
2433 if( xTicksToDelay > ( TickType_t ) 0U )
2437 configASSERT( uxSchedulerSuspended == 1U );
2441 /* A task that is removed from the event list while the
2442 * scheduler is suspended will not get placed in the ready
2443 * list or removed from the blocked list until the scheduler
2446 * This task cannot be in an event list as it is the currently
2447 * executing task. */
2448 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2450 xAlreadyYielded = xTaskResumeAll();
2454 mtCOVERAGE_TEST_MARKER();
2457 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2458 * have put ourselves to sleep. */
2459 if( xAlreadyYielded == pdFALSE )
2461 taskYIELD_WITHIN_API();
2465 mtCOVERAGE_TEST_MARKER();
2468 traceRETURN_vTaskDelay();
2471 #endif /* INCLUDE_vTaskDelay */
2472 /*-----------------------------------------------------------*/
2474 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2476 eTaskState eTaskGetState( TaskHandle_t xTask )
2479 List_t const * pxStateList;
2480 List_t const * pxEventList;
2481 List_t const * pxDelayedList;
2482 List_t const * pxOverflowedDelayedList;
2483 const TCB_t * const pxTCB = xTask;
2485 traceENTER_eTaskGetState( xTask );
2487 configASSERT( pxTCB );
2489 #if ( configNUMBER_OF_CORES == 1 )
2490 if( pxTCB == pxCurrentTCB )
2492 /* The task calling this function is querying its own state. */
2498 taskENTER_CRITICAL();
2500 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2501 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2502 pxDelayedList = pxDelayedTaskList;
2503 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2505 taskEXIT_CRITICAL();
2507 if( pxEventList == &xPendingReadyList )
2509 /* The task has been placed on the pending ready list, so its
2510 * state is eReady regardless of what list the task's state list
2511 * item is currently placed on. */
2514 else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2516 /* The task being queried is referenced from one of the Blocked
2521 #if ( INCLUDE_vTaskSuspend == 1 )
2522 else if( pxStateList == &xSuspendedTaskList )
2524 /* The task being queried is referenced from the suspended
2525 * list. Is it genuinely suspended or is it blocked
2527 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2529 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2533 /* The task does not appear on the event list item of
2534 * and of the RTOS objects, but could still be in the
2535 * blocked state if it is waiting on its notification
2536 * rather than waiting on an object. If not, is
2538 eReturn = eSuspended;
2540 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2542 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2549 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2551 eReturn = eSuspended;
2553 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2560 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2562 #if ( INCLUDE_vTaskDelete == 1 )
2563 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2565 /* The task being queried is referenced from the deleted
2566 * tasks list, or it is not referenced from any lists at
2574 #if ( configNUMBER_OF_CORES == 1 )
2576 /* If the task is not in any other state, it must be in the
2577 * Ready (including pending ready) state. */
2580 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2582 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2584 /* Is it actively running on a core? */
2589 /* If the task is not in any other state, it must be in the
2590 * Ready (including pending ready) state. */
2594 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2598 traceRETURN_eTaskGetState( eReturn );
2603 #endif /* INCLUDE_eTaskGetState */
2604 /*-----------------------------------------------------------*/
2606 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2608 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2610 TCB_t const * pxTCB;
2611 UBaseType_t uxReturn;
2613 traceENTER_uxTaskPriorityGet( xTask );
2615 taskENTER_CRITICAL();
2617 /* If null is passed in here then it is the priority of the task
2618 * that called uxTaskPriorityGet() that is being queried. */
2619 pxTCB = prvGetTCBFromHandle( xTask );
2620 uxReturn = pxTCB->uxPriority;
2622 taskEXIT_CRITICAL();
2624 traceRETURN_uxTaskPriorityGet( uxReturn );
2629 #endif /* INCLUDE_uxTaskPriorityGet */
2630 /*-----------------------------------------------------------*/
2632 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2634 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2636 TCB_t const * pxTCB;
2637 UBaseType_t uxReturn;
2638 UBaseType_t uxSavedInterruptStatus;
2640 traceENTER_uxTaskPriorityGetFromISR( xTask );
2642 /* RTOS ports that support interrupt nesting have the concept of a
2643 * maximum system call (or maximum API call) interrupt priority.
2644 * Interrupts that are above the maximum system call priority are keep
2645 * permanently enabled, even when the RTOS kernel is in a critical section,
2646 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2647 * is defined in FreeRTOSConfig.h then
2648 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2649 * failure if a FreeRTOS API function is called from an interrupt that has
2650 * been assigned a priority above the configured maximum system call
2651 * priority. Only FreeRTOS functions that end in FromISR can be called
2652 * from interrupts that have been assigned a priority at or (logically)
2653 * below the maximum system call interrupt priority. FreeRTOS maintains a
2654 * separate interrupt safe API to ensure interrupt entry is as fast and as
2655 * simple as possible. More information (albeit Cortex-M specific) is
2656 * provided on the following link:
2657 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2658 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2660 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
2662 /* If null is passed in here then it is the priority of the calling
2663 * task that is being queried. */
2664 pxTCB = prvGetTCBFromHandle( xTask );
2665 uxReturn = pxTCB->uxPriority;
2667 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2669 traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
2674 #endif /* INCLUDE_uxTaskPriorityGet */
2675 /*-----------------------------------------------------------*/
2677 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2679 UBaseType_t uxTaskBasePriorityGet( const TaskHandle_t xTask )
2681 TCB_t const * pxTCB;
2682 UBaseType_t uxReturn;
2684 traceENTER_uxTaskBasePriorityGet( xTask );
2686 taskENTER_CRITICAL();
2688 /* If null is passed in here then it is the base priority of the task
2689 * that called uxTaskBasePriorityGet() that is being queried. */
2690 pxTCB = prvGetTCBFromHandle( xTask );
2691 uxReturn = pxTCB->uxBasePriority;
2693 taskEXIT_CRITICAL();
2695 traceRETURN_uxTaskBasePriorityGet( uxReturn );
2700 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2701 /*-----------------------------------------------------------*/
2703 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2705 UBaseType_t uxTaskBasePriorityGetFromISR( const TaskHandle_t xTask )
2707 TCB_t const * pxTCB;
2708 UBaseType_t uxReturn;
2709 UBaseType_t uxSavedInterruptStatus;
2711 traceENTER_uxTaskBasePriorityGetFromISR( xTask );
2713 /* RTOS ports that support interrupt nesting have the concept of a
2714 * maximum system call (or maximum API call) interrupt priority.
2715 * Interrupts that are above the maximum system call priority are keep
2716 * permanently enabled, even when the RTOS kernel is in a critical section,
2717 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2718 * is defined in FreeRTOSConfig.h then
2719 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2720 * failure if a FreeRTOS API function is called from an interrupt that has
2721 * been assigned a priority above the configured maximum system call
2722 * priority. Only FreeRTOS functions that end in FromISR can be called
2723 * from interrupts that have been assigned a priority at or (logically)
2724 * below the maximum system call interrupt priority. FreeRTOS maintains a
2725 * separate interrupt safe API to ensure interrupt entry is as fast and as
2726 * simple as possible. More information (albeit Cortex-M specific) is
2727 * provided on the following link:
2728 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2729 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2731 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
2733 /* If null is passed in here then it is the base priority of the calling
2734 * task that is being queried. */
2735 pxTCB = prvGetTCBFromHandle( xTask );
2736 uxReturn = pxTCB->uxBasePriority;
2738 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2740 traceRETURN_uxTaskBasePriorityGetFromISR( uxReturn );
2745 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2746 /*-----------------------------------------------------------*/
2748 #if ( INCLUDE_vTaskPrioritySet == 1 )
2750 void vTaskPrioritySet( TaskHandle_t xTask,
2751 UBaseType_t uxNewPriority )
2754 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2755 BaseType_t xYieldRequired = pdFALSE;
2757 #if ( configNUMBER_OF_CORES > 1 )
2758 BaseType_t xYieldForTask = pdFALSE;
2761 traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
2763 configASSERT( uxNewPriority < configMAX_PRIORITIES );
2765 /* Ensure the new priority is valid. */
2766 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2768 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2772 mtCOVERAGE_TEST_MARKER();
2775 taskENTER_CRITICAL();
2777 /* If null is passed in here then it is the priority of the calling
2778 * task that is being changed. */
2779 pxTCB = prvGetTCBFromHandle( xTask );
2781 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2783 #if ( configUSE_MUTEXES == 1 )
2785 uxCurrentBasePriority = pxTCB->uxBasePriority;
2789 uxCurrentBasePriority = pxTCB->uxPriority;
2793 if( uxCurrentBasePriority != uxNewPriority )
2795 /* The priority change may have readied a task of higher
2796 * priority than a running task. */
2797 if( uxNewPriority > uxCurrentBasePriority )
2799 #if ( configNUMBER_OF_CORES == 1 )
2801 if( pxTCB != pxCurrentTCB )
2803 /* The priority of a task other than the currently
2804 * running task is being raised. Is the priority being
2805 * raised above that of the running task? */
2806 if( uxNewPriority > pxCurrentTCB->uxPriority )
2808 xYieldRequired = pdTRUE;
2812 mtCOVERAGE_TEST_MARKER();
2817 /* The priority of the running task is being raised,
2818 * but the running task must already be the highest
2819 * priority task able to run so no yield is required. */
2822 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2824 /* The priority of a task is being raised so
2825 * perform a yield for this task later. */
2826 xYieldForTask = pdTRUE;
2828 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2830 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2832 /* Setting the priority of a running task down means
2833 * there may now be another task of higher priority that
2834 * is ready to execute. */
2835 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2836 if( pxTCB->xPreemptionDisable == pdFALSE )
2839 xYieldRequired = pdTRUE;
2844 /* Setting the priority of any other task down does not
2845 * require a yield as the running task must be above the
2846 * new priority of the task being modified. */
2849 /* Remember the ready list the task might be referenced from
2850 * before its uxPriority member is changed so the
2851 * taskRESET_READY_PRIORITY() macro can function correctly. */
2852 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2854 #if ( configUSE_MUTEXES == 1 )
2856 /* Only change the priority being used if the task is not
2857 * currently using an inherited priority or the new priority
2858 * is bigger than the inherited priority. */
2859 if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2861 pxTCB->uxPriority = uxNewPriority;
2865 mtCOVERAGE_TEST_MARKER();
2868 /* The base priority gets set whatever. */
2869 pxTCB->uxBasePriority = uxNewPriority;
2871 #else /* if ( configUSE_MUTEXES == 1 ) */
2873 pxTCB->uxPriority = uxNewPriority;
2875 #endif /* if ( configUSE_MUTEXES == 1 ) */
2877 /* Only reset the event list item value if the value is not
2878 * being used for anything else. */
2879 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0UL ) )
2881 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) );
2885 mtCOVERAGE_TEST_MARKER();
2888 /* If the task is in the blocked or suspended list we need do
2889 * nothing more than change its priority variable. However, if
2890 * the task is in a ready list it needs to be removed and placed
2891 * in the list appropriate to its new priority. */
2892 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2894 /* The task is currently in its ready list - remove before
2895 * adding it to its new ready list. As we are in a critical
2896 * section we can do this even if the scheduler is suspended. */
2897 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2899 /* It is known that the task is in its ready list so
2900 * there is no need to check again and the port level
2901 * reset macro can be called directly. */
2902 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2906 mtCOVERAGE_TEST_MARKER();
2909 prvAddTaskToReadyList( pxTCB );
2913 #if ( configNUMBER_OF_CORES == 1 )
2915 mtCOVERAGE_TEST_MARKER();
2919 /* It's possible that xYieldForTask was already set to pdTRUE because
2920 * its priority is being raised. However, since it is not in a ready list
2921 * we don't actually need to yield for it. */
2922 xYieldForTask = pdFALSE;
2927 if( xYieldRequired != pdFALSE )
2929 /* The running task priority is set down. Request the task to yield. */
2930 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2934 #if ( configNUMBER_OF_CORES > 1 )
2935 if( xYieldForTask != pdFALSE )
2937 /* The priority of the task is being raised. If a running
2938 * task has priority lower than this task, it should yield
2940 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2943 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2945 mtCOVERAGE_TEST_MARKER();
2949 /* Remove compiler warning about unused variables when the port
2950 * optimised task selection is not being used. */
2951 ( void ) uxPriorityUsedOnEntry;
2954 taskEXIT_CRITICAL();
2956 traceRETURN_vTaskPrioritySet();
2959 #endif /* INCLUDE_vTaskPrioritySet */
2960 /*-----------------------------------------------------------*/
2962 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2963 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2964 UBaseType_t uxCoreAffinityMask )
2968 UBaseType_t uxPrevCoreAffinityMask;
2970 #if ( configUSE_PREEMPTION == 1 )
2971 UBaseType_t uxPrevNotAllowedCores;
2974 traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
2976 taskENTER_CRITICAL();
2978 pxTCB = prvGetTCBFromHandle( xTask );
2980 uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2981 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2983 if( xSchedulerRunning != pdFALSE )
2985 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2987 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2989 /* If the task can no longer run on the core it was running,
2990 * request the core to yield. */
2991 if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
2993 prvYieldCore( xCoreID );
2998 #if ( configUSE_PREEMPTION == 1 )
3000 /* Calculate the cores on which this task was not allowed to
3001 * run previously. */
3002 uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
3004 /* Does the new core mask enables this task to run on any of the
3005 * previously not allowed cores? If yes, check if this task can be
3006 * scheduled on any of those cores. */
3007 if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
3009 prvYieldForTask( pxTCB );
3012 #else /* #if( configUSE_PREEMPTION == 1 ) */
3014 mtCOVERAGE_TEST_MARKER();
3016 #endif /* #if( configUSE_PREEMPTION == 1 ) */
3020 taskEXIT_CRITICAL();
3022 traceRETURN_vTaskCoreAffinitySet();
3024 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3025 /*-----------------------------------------------------------*/
3027 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
3028 UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
3030 const TCB_t * pxTCB;
3031 UBaseType_t uxCoreAffinityMask;
3033 traceENTER_vTaskCoreAffinityGet( xTask );
3035 taskENTER_CRITICAL();
3037 pxTCB = prvGetTCBFromHandle( xTask );
3038 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
3040 taskEXIT_CRITICAL();
3042 traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
3044 return uxCoreAffinityMask;
3046 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3048 /*-----------------------------------------------------------*/
3050 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3052 void vTaskPreemptionDisable( const TaskHandle_t xTask )
3056 traceENTER_vTaskPreemptionDisable( xTask );
3058 taskENTER_CRITICAL();
3060 pxTCB = prvGetTCBFromHandle( xTask );
3062 pxTCB->xPreemptionDisable = pdTRUE;
3064 taskEXIT_CRITICAL();
3066 traceRETURN_vTaskPreemptionDisable();
3069 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3070 /*-----------------------------------------------------------*/
3072 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3074 void vTaskPreemptionEnable( const TaskHandle_t xTask )
3079 traceENTER_vTaskPreemptionEnable( xTask );
3081 taskENTER_CRITICAL();
3083 pxTCB = prvGetTCBFromHandle( xTask );
3085 pxTCB->xPreemptionDisable = pdFALSE;
3087 if( xSchedulerRunning != pdFALSE )
3089 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3091 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
3092 prvYieldCore( xCoreID );
3096 taskEXIT_CRITICAL();
3098 traceRETURN_vTaskPreemptionEnable();
3101 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3102 /*-----------------------------------------------------------*/
3104 #if ( INCLUDE_vTaskSuspend == 1 )
3106 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
3110 traceENTER_vTaskSuspend( xTaskToSuspend );
3112 taskENTER_CRITICAL();
3114 /* If null is passed in here then it is the running task that is
3115 * being suspended. */
3116 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
3118 traceTASK_SUSPEND( pxTCB );
3120 /* Remove task from the ready/delayed list and place in the
3121 * suspended list. */
3122 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
3124 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
3128 mtCOVERAGE_TEST_MARKER();
3131 /* Is the task waiting on an event also? */
3132 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3134 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3138 mtCOVERAGE_TEST_MARKER();
3141 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
3143 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3147 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3149 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3151 /* The task was blocked to wait for a notification, but is
3152 * now suspended, so no notification was received. */
3153 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
3157 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3159 taskEXIT_CRITICAL();
3161 if( xSchedulerRunning != pdFALSE )
3163 /* Reset the next expected unblock time in case it referred to the
3164 * task that is now in the Suspended state. */
3165 taskENTER_CRITICAL();
3167 prvResetNextTaskUnblockTime();
3169 taskEXIT_CRITICAL();
3173 mtCOVERAGE_TEST_MARKER();
3176 #if ( configNUMBER_OF_CORES == 1 )
3178 if( pxTCB == pxCurrentTCB )
3180 if( xSchedulerRunning != pdFALSE )
3182 /* The current task has just been suspended. */
3183 configASSERT( uxSchedulerSuspended == 0 );
3184 portYIELD_WITHIN_API();
3188 /* The scheduler is not running, but the task that was pointed
3189 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
3190 * must be adjusted to point to a different task. */
3191 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks )
3193 /* No other tasks are ready, so set pxCurrentTCB back to
3194 * NULL so when the next task is created pxCurrentTCB will
3195 * be set to point to it no matter what its relative priority
3197 pxCurrentTCB = NULL;
3201 vTaskSwitchContext();
3207 mtCOVERAGE_TEST_MARKER();
3210 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3212 /* Enter critical section here to check run state of a task. */
3213 taskENTER_CRITICAL();
3215 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3217 if( xSchedulerRunning != pdFALSE )
3219 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
3221 /* The current task has just been suspended. */
3222 configASSERT( uxSchedulerSuspended == 0 );
3223 vTaskYieldWithinAPI();
3227 prvYieldCore( pxTCB->xTaskRunState );
3232 /* This code path is not possible because only Idle tasks are
3233 * assigned a core before the scheduler is started ( i.e.
3234 * taskTASK_IS_RUNNING is only true for idle tasks before
3235 * the scheduler is started ) and idle tasks cannot be
3237 mtCOVERAGE_TEST_MARKER();
3242 mtCOVERAGE_TEST_MARKER();
3245 taskEXIT_CRITICAL();
3247 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3249 traceRETURN_vTaskSuspend();
3252 #endif /* INCLUDE_vTaskSuspend */
3253 /*-----------------------------------------------------------*/
3255 #if ( INCLUDE_vTaskSuspend == 1 )
3257 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
3259 BaseType_t xReturn = pdFALSE;
3260 const TCB_t * const pxTCB = xTask;
3262 /* Accesses xPendingReadyList so must be called from a critical
3265 /* It does not make sense to check if the calling task is suspended. */
3266 configASSERT( xTask );
3268 /* Is the task being resumed actually in the suspended list? */
3269 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
3271 /* Has the task already been resumed from within an ISR? */
3272 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
3274 /* Is it in the suspended list because it is in the Suspended
3275 * state, or because it is blocked with no timeout? */
3276 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE )
3278 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3282 /* The task does not appear on the event list item of
3283 * and of the RTOS objects, but could still be in the
3284 * blocked state if it is waiting on its notification
3285 * rather than waiting on an object. If not, is
3289 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3291 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3298 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3302 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3306 mtCOVERAGE_TEST_MARKER();
3311 mtCOVERAGE_TEST_MARKER();
3316 mtCOVERAGE_TEST_MARKER();
3322 #endif /* INCLUDE_vTaskSuspend */
3323 /*-----------------------------------------------------------*/
3325 #if ( INCLUDE_vTaskSuspend == 1 )
3327 void vTaskResume( TaskHandle_t xTaskToResume )
3329 TCB_t * const pxTCB = xTaskToResume;
3331 traceENTER_vTaskResume( xTaskToResume );
3333 /* It does not make sense to resume the calling task. */
3334 configASSERT( xTaskToResume );
3336 #if ( configNUMBER_OF_CORES == 1 )
3338 /* The parameter cannot be NULL as it is impossible to resume the
3339 * currently executing task. */
3340 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
3343 /* The parameter cannot be NULL as it is impossible to resume the
3344 * currently executing task. It is also impossible to resume a task
3345 * that is actively running on another core but it is not safe
3346 * to check their run state here. Therefore, we get into a critical
3347 * section and check if the task is actually suspended or not. */
3351 taskENTER_CRITICAL();
3353 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3355 traceTASK_RESUME( pxTCB );
3357 /* The ready list can be accessed even if the scheduler is
3358 * suspended because this is inside a critical section. */
3359 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3360 prvAddTaskToReadyList( pxTCB );
3362 /* This yield may not cause the task just resumed to run,
3363 * but will leave the lists in the correct state for the
3365 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
3369 mtCOVERAGE_TEST_MARKER();
3372 taskEXIT_CRITICAL();
3376 mtCOVERAGE_TEST_MARKER();
3379 traceRETURN_vTaskResume();
3382 #endif /* INCLUDE_vTaskSuspend */
3384 /*-----------------------------------------------------------*/
3386 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
3388 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
3390 BaseType_t xYieldRequired = pdFALSE;
3391 TCB_t * const pxTCB = xTaskToResume;
3392 UBaseType_t uxSavedInterruptStatus;
3394 traceENTER_xTaskResumeFromISR( xTaskToResume );
3396 configASSERT( xTaskToResume );
3398 /* RTOS ports that support interrupt nesting have the concept of a
3399 * maximum system call (or maximum API call) interrupt priority.
3400 * Interrupts that are above the maximum system call priority are keep
3401 * permanently enabled, even when the RTOS kernel is in a critical section,
3402 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
3403 * is defined in FreeRTOSConfig.h then
3404 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3405 * failure if a FreeRTOS API function is called from an interrupt that has
3406 * been assigned a priority above the configured maximum system call
3407 * priority. Only FreeRTOS functions that end in FromISR can be called
3408 * from interrupts that have been assigned a priority at or (logically)
3409 * below the maximum system call interrupt priority. FreeRTOS maintains a
3410 * separate interrupt safe API to ensure interrupt entry is as fast and as
3411 * simple as possible. More information (albeit Cortex-M specific) is
3412 * provided on the following link:
3413 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3414 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3416 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3418 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3420 traceTASK_RESUME_FROM_ISR( pxTCB );
3422 /* Check the ready lists can be accessed. */
3423 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3425 #if ( configNUMBER_OF_CORES == 1 )
3427 /* Ready lists can be accessed so move the task from the
3428 * suspended list to the ready list directly. */
3429 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3431 xYieldRequired = pdTRUE;
3433 /* Mark that a yield is pending in case the user is not
3434 * using the return value to initiate a context switch
3435 * from the ISR using the port specific portYIELD_FROM_ISR(). */
3436 xYieldPendings[ 0 ] = pdTRUE;
3440 mtCOVERAGE_TEST_MARKER();
3443 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3445 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3446 prvAddTaskToReadyList( pxTCB );
3450 /* The delayed or ready lists cannot be accessed so the task
3451 * is held in the pending ready list until the scheduler is
3453 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3456 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3458 prvYieldForTask( pxTCB );
3460 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3462 xYieldRequired = pdTRUE;
3465 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3469 mtCOVERAGE_TEST_MARKER();
3472 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3474 traceRETURN_xTaskResumeFromISR( xYieldRequired );
3476 return xYieldRequired;
3479 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3480 /*-----------------------------------------------------------*/
3482 static BaseType_t prvCreateIdleTasks( void )
3484 BaseType_t xReturn = pdPASS;
3486 char cIdleName[ configMAX_TASK_NAME_LEN ];
3487 TaskFunction_t pxIdleTaskFunction = NULL;
3488 BaseType_t xIdleTaskNameIndex;
3490 for( xIdleTaskNameIndex = ( BaseType_t ) 0; xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN; xIdleTaskNameIndex++ )
3492 cIdleName[ xIdleTaskNameIndex ] = configIDLE_TASK_NAME[ xIdleTaskNameIndex ];
3494 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3495 * configMAX_TASK_NAME_LEN characters just in case the memory after the
3496 * string is not accessible (extremely unlikely). */
3497 if( cIdleName[ xIdleTaskNameIndex ] == ( char ) 0x00 )
3503 mtCOVERAGE_TEST_MARKER();
3507 /* Add each idle task at the lowest priority. */
3508 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3510 #if ( configNUMBER_OF_CORES == 1 )
3512 pxIdleTaskFunction = prvIdleTask;
3514 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3516 /* In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks
3517 * are also created to ensure that each core has an idle task to
3518 * run when no other task is available to run. */
3521 pxIdleTaskFunction = prvIdleTask;
3525 pxIdleTaskFunction = prvPassiveIdleTask;
3528 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3530 /* Update the idle task name with suffix to differentiate the idle tasks.
3531 * This function is not required in single core FreeRTOS since there is
3532 * only one idle task. */
3533 #if ( configNUMBER_OF_CORES > 1 )
3535 /* Append the idle task number to the end of the name if there is space. */
3536 if( xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3538 cIdleName[ xIdleTaskNameIndex ] = ( char ) ( xCoreID + '0' );
3540 /* And append a null character if there is space. */
3541 if( ( xIdleTaskNameIndex + 1 ) < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3543 cIdleName[ xIdleTaskNameIndex + 1 ] = '\0';
3547 mtCOVERAGE_TEST_MARKER();
3552 mtCOVERAGE_TEST_MARKER();
3555 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
3557 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3559 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3560 StackType_t * pxIdleTaskStackBuffer = NULL;
3561 configSTACK_DEPTH_TYPE uxIdleTaskStackSize;
3563 /* The Idle task is created using user provided RAM - obtain the
3564 * address of the RAM then create the idle task. */
3565 #if ( configNUMBER_OF_CORES == 1 )
3567 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
3573 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
3577 vApplicationGetPassiveIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize, xCoreID - 1 );
3580 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
3581 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( pxIdleTaskFunction,
3583 uxIdleTaskStackSize,
3585 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3586 pxIdleTaskStackBuffer,
3587 pxIdleTaskTCBBuffer );
3589 if( xIdleTaskHandles[ xCoreID ] != NULL )
3598 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3600 /* The Idle task is being created using dynamically allocated RAM. */
3601 xReturn = xTaskCreate( pxIdleTaskFunction,
3603 configMINIMAL_STACK_SIZE,
3605 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3606 &xIdleTaskHandles[ xCoreID ] );
3608 #endif /* configSUPPORT_STATIC_ALLOCATION */
3610 /* Break the loop if any of the idle task is failed to be created. */
3611 if( xReturn == pdFAIL )
3617 #if ( configNUMBER_OF_CORES == 1 )
3619 mtCOVERAGE_TEST_MARKER();
3623 /* Assign idle task to each core before SMP scheduler is running. */
3624 xIdleTaskHandles[ xCoreID ]->xTaskRunState = xCoreID;
3625 pxCurrentTCBs[ xCoreID ] = xIdleTaskHandles[ xCoreID ];
3634 /*-----------------------------------------------------------*/
3636 void vTaskStartScheduler( void )
3640 traceENTER_vTaskStartScheduler();
3642 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3644 /* Sanity check that the UBaseType_t must have greater than or equal to
3645 * the number of bits as confNUMBER_OF_CORES. */
3646 configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3648 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3650 xReturn = prvCreateIdleTasks();
3652 #if ( configUSE_TIMERS == 1 )
3654 if( xReturn == pdPASS )
3656 xReturn = xTimerCreateTimerTask();
3660 mtCOVERAGE_TEST_MARKER();
3663 #endif /* configUSE_TIMERS */
3665 if( xReturn == pdPASS )
3667 /* freertos_tasks_c_additions_init() should only be called if the user
3668 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3669 * the only macro called by the function. */
3670 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3672 freertos_tasks_c_additions_init();
3676 /* Interrupts are turned off here, to ensure a tick does not occur
3677 * before or during the call to xPortStartScheduler(). The stacks of
3678 * the created tasks contain a status word with interrupts switched on
3679 * so interrupts will automatically get re-enabled when the first task
3681 portDISABLE_INTERRUPTS();
3683 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3685 /* Switch C-Runtime's TLS Block to point to the TLS
3686 * block specific to the task that will run first. */
3687 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3691 xNextTaskUnblockTime = portMAX_DELAY;
3692 xSchedulerRunning = pdTRUE;
3693 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3695 /* If configGENERATE_RUN_TIME_STATS is defined then the following
3696 * macro must be defined to configure the timer/counter used to generate
3697 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
3698 * is set to 0 and the following line fails to build then ensure you do not
3699 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3700 * FreeRTOSConfig.h file. */
3701 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3703 traceTASK_SWITCHED_IN();
3705 /* Setting up the timer tick is hardware specific and thus in the
3706 * portable interface. */
3708 /* The return value for xPortStartScheduler is not required
3709 * hence using a void datatype. */
3710 ( void ) xPortStartScheduler();
3712 /* In most cases, xPortStartScheduler() will not return. If it
3713 * returns pdTRUE then there was not enough heap memory available
3714 * to create either the Idle or the Timer task. If it returned
3715 * pdFALSE, then the application called xTaskEndScheduler().
3716 * Most ports don't implement xTaskEndScheduler() as there is
3717 * nothing to return to. */
3721 /* This line will only be reached if the kernel could not be started,
3722 * because there was not enough FreeRTOS heap to create the idle task
3723 * or the timer task. */
3724 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3727 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3728 * meaning xIdleTaskHandles are not used anywhere else. */
3729 ( void ) xIdleTaskHandles;
3731 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3732 * from getting optimized out as it is no longer used by the kernel. */
3733 ( void ) uxTopUsedPriority;
3735 traceRETURN_vTaskStartScheduler();
3737 /*-----------------------------------------------------------*/
3739 void vTaskEndScheduler( void )
3741 traceENTER_vTaskEndScheduler();
3743 /* Stop the scheduler interrupts and call the portable scheduler end
3744 * routine so the original ISRs can be restored if necessary. The port
3745 * layer must ensure interrupts enable bit is left in the correct state. */
3746 portDISABLE_INTERRUPTS();
3747 xSchedulerRunning = pdFALSE;
3748 vPortEndScheduler();
3750 traceRETURN_vTaskEndScheduler();
3752 /*----------------------------------------------------------*/
3754 void vTaskSuspendAll( void )
3756 traceENTER_vTaskSuspendAll();
3758 #if ( configNUMBER_OF_CORES == 1 )
3760 /* A critical section is not required as the variable is of type
3761 * BaseType_t. Please read Richard Barry's reply in the following link to a
3762 * post in the FreeRTOS support forum before reporting this as a bug! -
3763 * https://goo.gl/wu4acr */
3765 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3766 * do not otherwise exhibit real time behaviour. */
3767 portSOFTWARE_BARRIER();
3769 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3770 * is used to allow calls to vTaskSuspendAll() to nest. */
3771 ++uxSchedulerSuspended;
3773 /* Enforces ordering for ports and optimised compilers that may otherwise place
3774 * the above increment elsewhere. */
3775 portMEMORY_BARRIER();
3777 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3779 UBaseType_t ulState;
3781 /* This must only be called from within a task. */
3782 portASSERT_IF_IN_ISR();
3784 if( xSchedulerRunning != pdFALSE )
3786 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3787 * We must disable interrupts before we grab the locks in the event that this task is
3788 * interrupted and switches context before incrementing uxSchedulerSuspended.
3789 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3790 * uxSchedulerSuspended since that will prevent context switches. */
3791 ulState = portSET_INTERRUPT_MASK();
3793 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3794 * do not otherwise exhibit real time behaviour. */
3795 portSOFTWARE_BARRIER();
3797 portGET_TASK_LOCK();
3799 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3800 * purpose is to prevent altering the variable when fromISR APIs are readying
3802 if( uxSchedulerSuspended == 0U )
3804 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
3806 prvCheckForRunStateChange();
3810 mtCOVERAGE_TEST_MARKER();
3815 mtCOVERAGE_TEST_MARKER();
3820 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3821 * is used to allow calls to vTaskSuspendAll() to nest. */
3822 ++uxSchedulerSuspended;
3823 portRELEASE_ISR_LOCK();
3825 portCLEAR_INTERRUPT_MASK( ulState );
3829 mtCOVERAGE_TEST_MARKER();
3832 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3834 traceRETURN_vTaskSuspendAll();
3837 /*----------------------------------------------------------*/
3839 #if ( configUSE_TICKLESS_IDLE != 0 )
3841 static TickType_t prvGetExpectedIdleTime( void )
3844 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
3846 /* uxHigherPriorityReadyTasks takes care of the case where
3847 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3848 * task that are in the Ready state, even though the idle task is
3850 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3852 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3854 uxHigherPriorityReadyTasks = pdTRUE;
3859 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3861 /* When port optimised task selection is used the uxTopReadyPriority
3862 * variable is used as a bit map. If bits other than the least
3863 * significant bit are set then there are tasks that have a priority
3864 * above the idle priority that are in the Ready state. This takes
3865 * care of the case where the co-operative scheduler is in use. */
3866 if( uxTopReadyPriority > uxLeastSignificantBit )
3868 uxHigherPriorityReadyTasks = pdTRUE;
3871 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3873 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3877 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1U )
3879 /* There are other idle priority tasks in the ready state. If
3880 * time slicing is used then the very next tick interrupt must be
3884 else if( uxHigherPriorityReadyTasks != pdFALSE )
3886 /* There are tasks in the Ready state that have a priority above the
3887 * idle priority. This path can only be reached if
3888 * configUSE_PREEMPTION is 0. */
3893 xReturn = xNextTaskUnblockTime;
3894 xReturn -= xTickCount;
3900 #endif /* configUSE_TICKLESS_IDLE */
3901 /*----------------------------------------------------------*/
3903 BaseType_t xTaskResumeAll( void )
3905 TCB_t * pxTCB = NULL;
3906 BaseType_t xAlreadyYielded = pdFALSE;
3908 traceENTER_xTaskResumeAll();
3910 #if ( configNUMBER_OF_CORES > 1 )
3911 if( xSchedulerRunning != pdFALSE )
3914 /* It is possible that an ISR caused a task to be removed from an event
3915 * list while the scheduler was suspended. If this was the case then the
3916 * removed task will have been added to the xPendingReadyList. Once the
3917 * scheduler has been resumed it is safe to move all the pending ready
3918 * tasks from this list into their appropriate ready list. */
3919 taskENTER_CRITICAL();
3922 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3924 /* If uxSchedulerSuspended is zero then this function does not match a
3925 * previous call to vTaskSuspendAll(). */
3926 configASSERT( uxSchedulerSuspended != 0U );
3928 --uxSchedulerSuspended;
3929 portRELEASE_TASK_LOCK();
3931 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3933 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3935 /* Move any readied tasks from the pending list into the
3936 * appropriate ready list. */
3937 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3939 /* MISRA Ref 11.5.3 [Void pointer assignment] */
3940 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
3941 /* coverity[misra_c_2012_rule_11_5_violation] */
3942 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) );
3943 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
3944 portMEMORY_BARRIER();
3945 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
3946 prvAddTaskToReadyList( pxTCB );
3948 #if ( configNUMBER_OF_CORES == 1 )
3950 /* If the moved task has a priority higher than the current
3951 * task then a yield must be performed. */
3952 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3954 xYieldPendings[ xCoreID ] = pdTRUE;
3958 mtCOVERAGE_TEST_MARKER();
3961 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3963 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
3964 * If the current core yielded then vTaskSwitchContext() has already been called
3965 * which sets xYieldPendings for the current core to pdTRUE. */
3967 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3972 /* A task was unblocked while the scheduler was suspended,
3973 * which may have prevented the next unblock time from being
3974 * re-calculated, in which case re-calculate it now. Mainly
3975 * important for low power tickless implementations, where
3976 * this can prevent an unnecessary exit from low power
3978 prvResetNextTaskUnblockTime();
3981 /* If any ticks occurred while the scheduler was suspended then
3982 * they should be processed now. This ensures the tick count does
3983 * not slip, and that any delayed tasks are resumed at the correct
3986 * It should be safe to call xTaskIncrementTick here from any core
3987 * since we are in a critical section and xTaskIncrementTick itself
3988 * protects itself within a critical section. Suspending the scheduler
3989 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
3991 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3993 if( xPendedCounts > ( TickType_t ) 0U )
3997 if( xTaskIncrementTick() != pdFALSE )
3999 /* Other cores are interrupted from
4000 * within xTaskIncrementTick(). */
4001 xYieldPendings[ xCoreID ] = pdTRUE;
4005 mtCOVERAGE_TEST_MARKER();
4009 } while( xPendedCounts > ( TickType_t ) 0U );
4015 mtCOVERAGE_TEST_MARKER();
4019 if( xYieldPendings[ xCoreID ] != pdFALSE )
4021 #if ( configUSE_PREEMPTION != 0 )
4023 xAlreadyYielded = pdTRUE;
4025 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
4027 #if ( configNUMBER_OF_CORES == 1 )
4029 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
4031 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4035 mtCOVERAGE_TEST_MARKER();
4041 mtCOVERAGE_TEST_MARKER();
4044 taskEXIT_CRITICAL();
4047 traceRETURN_xTaskResumeAll( xAlreadyYielded );
4049 return xAlreadyYielded;
4051 /*-----------------------------------------------------------*/
4053 TickType_t xTaskGetTickCount( void )
4057 traceENTER_xTaskGetTickCount();
4059 /* Critical section required if running on a 16 bit processor. */
4060 portTICK_TYPE_ENTER_CRITICAL();
4062 xTicks = xTickCount;
4064 portTICK_TYPE_EXIT_CRITICAL();
4066 traceRETURN_xTaskGetTickCount( xTicks );
4070 /*-----------------------------------------------------------*/
4072 TickType_t xTaskGetTickCountFromISR( void )
4075 UBaseType_t uxSavedInterruptStatus;
4077 traceENTER_xTaskGetTickCountFromISR();
4079 /* RTOS ports that support interrupt nesting have the concept of a maximum
4080 * system call (or maximum API call) interrupt priority. Interrupts that are
4081 * above the maximum system call priority are kept permanently enabled, even
4082 * when the RTOS kernel is in a critical section, but cannot make any calls to
4083 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
4084 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
4085 * failure if a FreeRTOS API function is called from an interrupt that has been
4086 * assigned a priority above the configured maximum system call priority.
4087 * Only FreeRTOS functions that end in FromISR can be called from interrupts
4088 * that have been assigned a priority at or (logically) below the maximum
4089 * system call interrupt priority. FreeRTOS maintains a separate interrupt
4090 * safe API to ensure interrupt entry is as fast and as simple as possible.
4091 * More information (albeit Cortex-M specific) is provided on the following
4092 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
4093 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
4095 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
4097 xReturn = xTickCount;
4099 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
4101 traceRETURN_xTaskGetTickCountFromISR( xReturn );
4105 /*-----------------------------------------------------------*/
4107 UBaseType_t uxTaskGetNumberOfTasks( void )
4109 traceENTER_uxTaskGetNumberOfTasks();
4111 /* A critical section is not required because the variables are of type
4113 traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
4115 return uxCurrentNumberOfTasks;
4117 /*-----------------------------------------------------------*/
4119 char * pcTaskGetName( TaskHandle_t xTaskToQuery )
4123 traceENTER_pcTaskGetName( xTaskToQuery );
4125 /* If null is passed in here then the name of the calling task is being
4127 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4128 configASSERT( pxTCB );
4130 traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
4132 return &( pxTCB->pcTaskName[ 0 ] );
4134 /*-----------------------------------------------------------*/
4136 #if ( INCLUDE_xTaskGetHandle == 1 )
4138 #if ( configNUMBER_OF_CORES == 1 )
4139 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4140 const char pcNameToQuery[] )
4144 TCB_t * pxReturn = NULL;
4147 BaseType_t xBreakLoop;
4149 /* This function is called with the scheduler suspended. */
4151 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4153 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4154 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4155 /* coverity[misra_c_2012_rule_11_5_violation] */
4156 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
4160 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4161 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4162 /* coverity[misra_c_2012_rule_11_5_violation] */
4163 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
4165 /* Check each character in the name looking for a match or
4167 xBreakLoop = pdFALSE;
4169 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4171 cNextChar = pxNextTCB->pcTaskName[ x ];
4173 if( cNextChar != pcNameToQuery[ x ] )
4175 /* Characters didn't match. */
4176 xBreakLoop = pdTRUE;
4178 else if( cNextChar == ( char ) 0x00 )
4180 /* Both strings terminated, a match must have been
4182 pxReturn = pxNextTCB;
4183 xBreakLoop = pdTRUE;
4187 mtCOVERAGE_TEST_MARKER();
4190 if( xBreakLoop != pdFALSE )
4196 if( pxReturn != NULL )
4198 /* The handle has been found. */
4201 } while( pxNextTCB != pxFirstTCB );
4205 mtCOVERAGE_TEST_MARKER();
4210 #else /* if ( configNUMBER_OF_CORES == 1 ) */
4211 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4212 const char pcNameToQuery[] )
4214 TCB_t * pxReturn = NULL;
4217 BaseType_t xBreakLoop;
4218 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
4219 ListItem_t * pxIterator;
4221 /* This function is called with the scheduler suspended. */
4223 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4225 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
4227 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4228 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4229 /* coverity[misra_c_2012_rule_11_5_violation] */
4230 TCB_t * pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
4232 /* Check each character in the name looking for a match or
4234 xBreakLoop = pdFALSE;
4236 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4238 cNextChar = pxTCB->pcTaskName[ x ];
4240 if( cNextChar != pcNameToQuery[ x ] )
4242 /* Characters didn't match. */
4243 xBreakLoop = pdTRUE;
4245 else if( cNextChar == ( char ) 0x00 )
4247 /* Both strings terminated, a match must have been
4250 xBreakLoop = pdTRUE;
4254 mtCOVERAGE_TEST_MARKER();
4257 if( xBreakLoop != pdFALSE )
4263 if( pxReturn != NULL )
4265 /* The handle has been found. */
4272 mtCOVERAGE_TEST_MARKER();
4277 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4279 #endif /* INCLUDE_xTaskGetHandle */
4280 /*-----------------------------------------------------------*/
4282 #if ( INCLUDE_xTaskGetHandle == 1 )
4284 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery )
4286 UBaseType_t uxQueue = configMAX_PRIORITIES;
4289 traceENTER_xTaskGetHandle( pcNameToQuery );
4291 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
4292 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
4296 /* Search the ready lists. */
4300 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
4304 /* Found the handle. */
4307 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4309 /* Search the delayed lists. */
4312 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
4317 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
4320 #if ( INCLUDE_vTaskSuspend == 1 )
4324 /* Search the suspended list. */
4325 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
4330 #if ( INCLUDE_vTaskDelete == 1 )
4334 /* Search the deleted list. */
4335 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
4340 ( void ) xTaskResumeAll();
4342 traceRETURN_xTaskGetHandle( pxTCB );
4347 #endif /* INCLUDE_xTaskGetHandle */
4348 /*-----------------------------------------------------------*/
4350 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4352 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4353 StackType_t ** ppuxStackBuffer,
4354 StaticTask_t ** ppxTaskBuffer )
4359 traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4361 configASSERT( ppuxStackBuffer != NULL );
4362 configASSERT( ppxTaskBuffer != NULL );
4364 pxTCB = prvGetTCBFromHandle( xTask );
4366 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4368 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4370 *ppuxStackBuffer = pxTCB->pxStack;
4371 /* MISRA Ref 11.3.1 [Misaligned access] */
4372 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
4373 /* coverity[misra_c_2012_rule_11_3_violation] */
4374 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4377 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4379 *ppuxStackBuffer = pxTCB->pxStack;
4380 *ppxTaskBuffer = NULL;
4388 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4390 *ppuxStackBuffer = pxTCB->pxStack;
4391 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4394 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4396 traceRETURN_xTaskGetStaticBuffers( xReturn );
4401 #endif /* configSUPPORT_STATIC_ALLOCATION */
4402 /*-----------------------------------------------------------*/
4404 #if ( configUSE_TRACE_FACILITY == 1 )
4406 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4407 const UBaseType_t uxArraySize,
4408 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4410 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4412 traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4416 /* Is there a space in the array for each task in the system? */
4417 if( uxArraySize >= uxCurrentNumberOfTasks )
4419 /* Fill in an TaskStatus_t structure with information on each
4420 * task in the Ready state. */
4424 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4425 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4427 /* Fill in an TaskStatus_t structure with information on each
4428 * task in the Blocked state. */
4429 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4430 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4432 #if ( INCLUDE_vTaskDelete == 1 )
4434 /* Fill in an TaskStatus_t structure with information on
4435 * each task that has been deleted but not yet cleaned up. */
4436 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4440 #if ( INCLUDE_vTaskSuspend == 1 )
4442 /* Fill in an TaskStatus_t structure with information on
4443 * each task in the Suspended state. */
4444 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4448 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4450 if( pulTotalRunTime != NULL )
4452 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4453 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4455 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4459 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4461 if( pulTotalRunTime != NULL )
4463 *pulTotalRunTime = 0;
4466 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4470 mtCOVERAGE_TEST_MARKER();
4473 ( void ) xTaskResumeAll();
4475 traceRETURN_uxTaskGetSystemState( uxTask );
4480 #endif /* configUSE_TRACE_FACILITY */
4481 /*----------------------------------------------------------*/
4483 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4485 #if ( configNUMBER_OF_CORES == 1 )
4486 TaskHandle_t xTaskGetIdleTaskHandle( void )
4488 traceENTER_xTaskGetIdleTaskHandle();
4490 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4491 * started, then xIdleTaskHandles will be NULL. */
4492 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4494 traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4496 return xIdleTaskHandles[ 0 ];
4498 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
4500 TaskHandle_t xTaskGetIdleTaskHandleForCore( BaseType_t xCoreID )
4502 traceENTER_xTaskGetIdleTaskHandleForCore( xCoreID );
4504 /* Ensure the core ID is valid. */
4505 configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE );
4507 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4508 * started, then xIdleTaskHandles will be NULL. */
4509 configASSERT( ( xIdleTaskHandles[ xCoreID ] != NULL ) );
4511 traceRETURN_xTaskGetIdleTaskHandleForCore( xIdleTaskHandles[ xCoreID ] );
4513 return xIdleTaskHandles[ xCoreID ];
4516 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4517 /*----------------------------------------------------------*/
4519 /* This conditional compilation should use inequality to 0, not equality to 1.
4520 * This is to ensure vTaskStepTick() is available when user defined low power mode
4521 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4523 #if ( configUSE_TICKLESS_IDLE != 0 )
4525 void vTaskStepTick( TickType_t xTicksToJump )
4527 TickType_t xUpdatedTickCount;
4529 traceENTER_vTaskStepTick( xTicksToJump );
4531 /* Correct the tick count value after a period during which the tick
4532 * was suppressed. Note this does *not* call the tick hook function for
4533 * each stepped tick. */
4534 xUpdatedTickCount = xTickCount + xTicksToJump;
4535 configASSERT( xUpdatedTickCount <= xNextTaskUnblockTime );
4537 if( xUpdatedTickCount == xNextTaskUnblockTime )
4539 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4540 * xTaskIncrementTick() when the scheduler resumes. This ensures
4541 * that any delayed tasks are resumed at the correct time. */
4542 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4543 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4545 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4546 taskENTER_CRITICAL();
4550 taskEXIT_CRITICAL();
4555 mtCOVERAGE_TEST_MARKER();
4558 xTickCount += xTicksToJump;
4560 traceINCREASE_TICK_COUNT( xTicksToJump );
4561 traceRETURN_vTaskStepTick();
4564 #endif /* configUSE_TICKLESS_IDLE */
4565 /*----------------------------------------------------------*/
4567 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4569 BaseType_t xYieldOccurred;
4571 traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4573 /* Must not be called with the scheduler suspended as the implementation
4574 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4575 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4577 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4578 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4581 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4582 taskENTER_CRITICAL();
4584 xPendedTicks += xTicksToCatchUp;
4586 taskEXIT_CRITICAL();
4587 xYieldOccurred = xTaskResumeAll();
4589 traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4591 return xYieldOccurred;
4593 /*----------------------------------------------------------*/
4595 #if ( INCLUDE_xTaskAbortDelay == 1 )
4597 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4599 TCB_t * pxTCB = xTask;
4602 traceENTER_xTaskAbortDelay( xTask );
4604 configASSERT( pxTCB );
4608 /* A task can only be prematurely removed from the Blocked state if
4609 * it is actually in the Blocked state. */
4610 if( eTaskGetState( xTask ) == eBlocked )
4614 /* Remove the reference to the task from the blocked list. An
4615 * interrupt won't touch the xStateListItem because the
4616 * scheduler is suspended. */
4617 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4619 /* Is the task waiting on an event also? If so remove it from
4620 * the event list too. Interrupts can touch the event list item,
4621 * even though the scheduler is suspended, so a critical section
4623 taskENTER_CRITICAL();
4625 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4627 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4629 /* This lets the task know it was forcibly removed from the
4630 * blocked state so it should not re-evaluate its block time and
4631 * then block again. */
4632 pxTCB->ucDelayAborted = pdTRUE;
4636 mtCOVERAGE_TEST_MARKER();
4639 taskEXIT_CRITICAL();
4641 /* Place the unblocked task into the appropriate ready list. */
4642 prvAddTaskToReadyList( pxTCB );
4644 /* A task being unblocked cannot cause an immediate context
4645 * switch if preemption is turned off. */
4646 #if ( configUSE_PREEMPTION == 1 )
4648 #if ( configNUMBER_OF_CORES == 1 )
4650 /* Preemption is on, but a context switch should only be
4651 * performed if the unblocked task has a priority that is
4652 * higher than the currently executing task. */
4653 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4655 /* Pend the yield to be performed when the scheduler
4656 * is unsuspended. */
4657 xYieldPendings[ 0 ] = pdTRUE;
4661 mtCOVERAGE_TEST_MARKER();
4664 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4666 taskENTER_CRITICAL();
4668 prvYieldForTask( pxTCB );
4670 taskEXIT_CRITICAL();
4672 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4674 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4681 ( void ) xTaskResumeAll();
4683 traceRETURN_xTaskAbortDelay( xReturn );
4688 #endif /* INCLUDE_xTaskAbortDelay */
4689 /*----------------------------------------------------------*/
4691 BaseType_t xTaskIncrementTick( void )
4694 TickType_t xItemValue;
4695 BaseType_t xSwitchRequired = pdFALSE;
4697 #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4698 BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4699 #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4701 traceENTER_xTaskIncrementTick();
4703 /* Called by the portable layer each time a tick interrupt occurs.
4704 * Increments the tick then checks to see if the new tick value will cause any
4705 * tasks to be unblocked. */
4706 traceTASK_INCREMENT_TICK( xTickCount );
4708 /* Tick increment should occur on every kernel timer event. Core 0 has the
4709 * responsibility to increment the tick, or increment the pended ticks if the
4710 * scheduler is suspended. If pended ticks is greater than zero, the core that
4711 * calls xTaskResumeAll has the responsibility to increment the tick. */
4712 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4714 /* Minor optimisation. The tick count cannot change in this
4716 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4718 /* Increment the RTOS tick, switching the delayed and overflowed
4719 * delayed lists if it wraps to 0. */
4720 xTickCount = xConstTickCount;
4722 if( xConstTickCount == ( TickType_t ) 0U )
4724 taskSWITCH_DELAYED_LISTS();
4728 mtCOVERAGE_TEST_MARKER();
4731 /* See if this tick has made a timeout expire. Tasks are stored in
4732 * the queue in the order of their wake time - meaning once one task
4733 * has been found whose block time has not expired there is no need to
4734 * look any further down the list. */
4735 if( xConstTickCount >= xNextTaskUnblockTime )
4739 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4741 /* The delayed list is empty. Set xNextTaskUnblockTime
4742 * to the maximum possible value so it is extremely
4744 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4745 * next time through. */
4746 xNextTaskUnblockTime = portMAX_DELAY;
4751 /* The delayed list is not empty, get the value of the
4752 * item at the head of the delayed list. This is the time
4753 * at which the task at the head of the delayed list must
4754 * be removed from the Blocked state. */
4755 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4756 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4757 /* coverity[misra_c_2012_rule_11_5_violation] */
4758 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
4759 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4761 if( xConstTickCount < xItemValue )
4763 /* It is not time to unblock this item yet, but the
4764 * item value is the time at which the task at the head
4765 * of the blocked list must be removed from the Blocked
4766 * state - so record the item value in
4767 * xNextTaskUnblockTime. */
4768 xNextTaskUnblockTime = xItemValue;
4773 mtCOVERAGE_TEST_MARKER();
4776 /* It is time to remove the item from the Blocked state. */
4777 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4779 /* Is the task waiting on an event also? If so remove
4780 * it from the event list. */
4781 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4783 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4787 mtCOVERAGE_TEST_MARKER();
4790 /* Place the unblocked task into the appropriate ready
4792 prvAddTaskToReadyList( pxTCB );
4794 /* A task being unblocked cannot cause an immediate
4795 * context switch if preemption is turned off. */
4796 #if ( configUSE_PREEMPTION == 1 )
4798 #if ( configNUMBER_OF_CORES == 1 )
4800 /* Preemption is on, but a context switch should
4801 * only be performed if the unblocked task's
4802 * priority is higher than the currently executing
4804 * The case of equal priority tasks sharing
4805 * processing time (which happens when both
4806 * preemption and time slicing are on) is
4808 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4810 xSwitchRequired = pdTRUE;
4814 mtCOVERAGE_TEST_MARKER();
4817 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4819 prvYieldForTask( pxTCB );
4821 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4823 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4828 /* Tasks of equal priority to the currently running task will share
4829 * processing time (time slice) if preemption is on, and the application
4830 * writer has not explicitly turned time slicing off. */
4831 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4833 #if ( configNUMBER_OF_CORES == 1 )
4835 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > 1U )
4837 xSwitchRequired = pdTRUE;
4841 mtCOVERAGE_TEST_MARKER();
4844 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4848 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4850 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1U )
4852 xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4856 mtCOVERAGE_TEST_MARKER();
4860 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4862 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4864 #if ( configUSE_TICK_HOOK == 1 )
4866 /* Guard against the tick hook being called when the pended tick
4867 * count is being unwound (when the scheduler is being unlocked). */
4868 if( xPendedTicks == ( TickType_t ) 0 )
4870 vApplicationTickHook();
4874 mtCOVERAGE_TEST_MARKER();
4877 #endif /* configUSE_TICK_HOOK */
4879 #if ( configUSE_PREEMPTION == 1 )
4881 #if ( configNUMBER_OF_CORES == 1 )
4883 /* For single core the core ID is always 0. */
4884 if( xYieldPendings[ 0 ] != pdFALSE )
4886 xSwitchRequired = pdTRUE;
4890 mtCOVERAGE_TEST_MARKER();
4893 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4895 BaseType_t xCoreID, xCurrentCoreID;
4896 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4898 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4900 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4901 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4904 if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4906 if( xCoreID == xCurrentCoreID )
4908 xSwitchRequired = pdTRUE;
4912 prvYieldCore( xCoreID );
4917 mtCOVERAGE_TEST_MARKER();
4922 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4924 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4930 /* The tick hook gets called at regular intervals, even if the
4931 * scheduler is locked. */
4932 #if ( configUSE_TICK_HOOK == 1 )
4934 vApplicationTickHook();
4939 traceRETURN_xTaskIncrementTick( xSwitchRequired );
4941 return xSwitchRequired;
4943 /*-----------------------------------------------------------*/
4945 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4947 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4948 TaskHookFunction_t pxHookFunction )
4952 traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4954 /* If xTask is NULL then it is the task hook of the calling task that is
4958 xTCB = ( TCB_t * ) pxCurrentTCB;
4965 /* Save the hook function in the TCB. A critical section is required as
4966 * the value can be accessed from an interrupt. */
4967 taskENTER_CRITICAL();
4969 xTCB->pxTaskTag = pxHookFunction;
4971 taskEXIT_CRITICAL();
4973 traceRETURN_vTaskSetApplicationTaskTag();
4976 #endif /* configUSE_APPLICATION_TASK_TAG */
4977 /*-----------------------------------------------------------*/
4979 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4981 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4984 TaskHookFunction_t xReturn;
4986 traceENTER_xTaskGetApplicationTaskTag( xTask );
4988 /* If xTask is NULL then set the calling task's hook. */
4989 pxTCB = prvGetTCBFromHandle( xTask );
4991 /* Save the hook function in the TCB. A critical section is required as
4992 * the value can be accessed from an interrupt. */
4993 taskENTER_CRITICAL();
4995 xReturn = pxTCB->pxTaskTag;
4997 taskEXIT_CRITICAL();
4999 traceRETURN_xTaskGetApplicationTaskTag( xReturn );
5004 #endif /* configUSE_APPLICATION_TASK_TAG */
5005 /*-----------------------------------------------------------*/
5007 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5009 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
5012 TaskHookFunction_t xReturn;
5013 UBaseType_t uxSavedInterruptStatus;
5015 traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
5017 /* If xTask is NULL then set the calling task's hook. */
5018 pxTCB = prvGetTCBFromHandle( xTask );
5020 /* Save the hook function in the TCB. A critical section is required as
5021 * the value can be accessed from an interrupt. */
5022 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
5024 xReturn = pxTCB->pxTaskTag;
5026 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
5028 traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
5033 #endif /* configUSE_APPLICATION_TASK_TAG */
5034 /*-----------------------------------------------------------*/
5036 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5038 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
5039 void * pvParameter )
5044 traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
5046 /* If xTask is NULL then we are calling our own task hook. */
5049 xTCB = pxCurrentTCB;
5056 if( xTCB->pxTaskTag != NULL )
5058 xReturn = xTCB->pxTaskTag( pvParameter );
5065 traceRETURN_xTaskCallApplicationTaskHook( xReturn );
5070 #endif /* configUSE_APPLICATION_TASK_TAG */
5071 /*-----------------------------------------------------------*/
5073 #if ( configNUMBER_OF_CORES == 1 )
5074 void vTaskSwitchContext( void )
5076 traceENTER_vTaskSwitchContext();
5078 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5080 /* The scheduler is currently suspended - do not allow a context
5082 xYieldPendings[ 0 ] = pdTRUE;
5086 xYieldPendings[ 0 ] = pdFALSE;
5087 traceTASK_SWITCHED_OUT();
5089 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5091 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5092 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
5094 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
5097 /* Add the amount of time the task has been running to the
5098 * accumulated time so far. The time the task started running was
5099 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5100 * protection here so count values are only valid until the timer
5101 * overflows. The guard against negative values is to protect
5102 * against suspect run time stat counter implementations - which
5103 * are provided by the application, not the kernel. */
5104 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
5106 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
5110 mtCOVERAGE_TEST_MARKER();
5113 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
5115 #endif /* configGENERATE_RUN_TIME_STATS */
5117 /* Check for stack overflow, if configured. */
5118 taskCHECK_FOR_STACK_OVERFLOW();
5120 /* Before the currently running task is switched out, save its errno. */
5121 #if ( configUSE_POSIX_ERRNO == 1 )
5123 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
5127 /* Select a new task to run using either the generic C or port
5128 * optimised asm code. */
5129 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5130 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5131 /* coverity[misra_c_2012_rule_11_5_violation] */
5132 taskSELECT_HIGHEST_PRIORITY_TASK();
5133 traceTASK_SWITCHED_IN();
5135 /* Macro to inject port specific behaviour immediately after
5136 * switching tasks, such as setting an end of stack watchpoint
5137 * or reconfiguring the MPU. */
5138 portTASK_SWITCH_HOOK( pxCurrentTCB );
5140 /* After the new task is switched in, update the global errno. */
5141 #if ( configUSE_POSIX_ERRNO == 1 )
5143 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
5147 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5149 /* Switch C-Runtime's TLS Block to point to the TLS
5150 * Block specific to this task. */
5151 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
5156 traceRETURN_vTaskSwitchContext();
5158 #else /* if ( configNUMBER_OF_CORES == 1 ) */
5159 void vTaskSwitchContext( BaseType_t xCoreID )
5161 traceENTER_vTaskSwitchContext();
5163 /* Acquire both locks:
5164 * - The ISR lock protects the ready list from simultaneous access by
5165 * both other ISRs and tasks.
5166 * - We also take the task lock to pause here in case another core has
5167 * suspended the scheduler. We don't want to simply set xYieldPending
5168 * and move on if another core suspended the scheduler. We should only
5169 * do that if the current core has suspended the scheduler. */
5171 portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
5174 /* vTaskSwitchContext() must never be called from within a critical section.
5175 * This is not necessarily true for single core FreeRTOS, but it is for this
5177 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
5179 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5181 /* The scheduler is currently suspended - do not allow a context
5183 xYieldPendings[ xCoreID ] = pdTRUE;
5187 xYieldPendings[ xCoreID ] = pdFALSE;
5188 traceTASK_SWITCHED_OUT();
5190 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5192 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5193 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
5195 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
5198 /* Add the amount of time the task has been running to the
5199 * accumulated time so far. The time the task started running was
5200 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5201 * protection here so count values are only valid until the timer
5202 * overflows. The guard against negative values is to protect
5203 * against suspect run time stat counter implementations - which
5204 * are provided by the application, not the kernel. */
5205 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
5207 pxCurrentTCBs[ xCoreID ]->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
5211 mtCOVERAGE_TEST_MARKER();
5214 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
5216 #endif /* configGENERATE_RUN_TIME_STATS */
5218 /* Check for stack overflow, if configured. */
5219 taskCHECK_FOR_STACK_OVERFLOW();
5221 /* Before the currently running task is switched out, save its errno. */
5222 #if ( configUSE_POSIX_ERRNO == 1 )
5224 pxCurrentTCBs[ xCoreID ]->iTaskErrno = FreeRTOS_errno;
5228 /* Select a new task to run. */
5229 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
5230 traceTASK_SWITCHED_IN();
5232 /* Macro to inject port specific behaviour immediately after
5233 * switching tasks, such as setting an end of stack watchpoint
5234 * or reconfiguring the MPU. */
5235 portTASK_SWITCH_HOOK( pxCurrentTCBs[ portGET_CORE_ID() ] );
5237 /* After the new task is switched in, update the global errno. */
5238 #if ( configUSE_POSIX_ERRNO == 1 )
5240 FreeRTOS_errno = pxCurrentTCBs[ xCoreID ]->iTaskErrno;
5244 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5246 /* Switch C-Runtime's TLS Block to point to the TLS
5247 * Block specific to this task. */
5248 configSET_TLS_BLOCK( pxCurrentTCBs[ xCoreID ]->xTLSBlock );
5253 portRELEASE_ISR_LOCK();
5254 portRELEASE_TASK_LOCK();
5256 traceRETURN_vTaskSwitchContext();
5258 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
5259 /*-----------------------------------------------------------*/
5261 void vTaskPlaceOnEventList( List_t * const pxEventList,
5262 const TickType_t xTicksToWait )
5264 traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
5266 configASSERT( pxEventList );
5268 /* THIS FUNCTION MUST BE CALLED WITH THE
5269 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
5271 /* Place the event list item of the TCB in the appropriate event list.
5272 * This is placed in the list in priority order so the highest priority task
5273 * is the first to be woken by the event.
5275 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
5276 * Normally, the xItemValue of a TCB's ListItem_t members is:
5277 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
5278 * Therefore, the event list is sorted in descending priority order.
5280 * The queue that contains the event list is locked, preventing
5281 * simultaneous access from interrupts. */
5282 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5284 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5286 traceRETURN_vTaskPlaceOnEventList();
5288 /*-----------------------------------------------------------*/
5290 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
5291 const TickType_t xItemValue,
5292 const TickType_t xTicksToWait )
5294 traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
5296 configASSERT( pxEventList );
5298 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5299 * the event groups implementation. */
5300 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5302 /* Store the item value in the event list item. It is safe to access the
5303 * event list item here as interrupts won't access the event list item of a
5304 * task that is not in the Blocked state. */
5305 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5307 /* Place the event list item of the TCB at the end of the appropriate event
5308 * list. It is safe to access the event list here because it is part of an
5309 * event group implementation - and interrupts don't access event groups
5310 * directly (instead they access them indirectly by pending function calls to
5311 * the task level). */
5312 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5314 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5316 traceRETURN_vTaskPlaceOnUnorderedEventList();
5318 /*-----------------------------------------------------------*/
5320 #if ( configUSE_TIMERS == 1 )
5322 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
5323 TickType_t xTicksToWait,
5324 const BaseType_t xWaitIndefinitely )
5326 traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
5328 configASSERT( pxEventList );
5330 /* This function should not be called by application code hence the
5331 * 'Restricted' in its name. It is not part of the public API. It is
5332 * designed for use by kernel code, and has special calling requirements -
5333 * it should be called with the scheduler suspended. */
5336 /* Place the event list item of the TCB in the appropriate event list.
5337 * In this case it is assume that this is the only task that is going to
5338 * be waiting on this event list, so the faster vListInsertEnd() function
5339 * can be used in place of vListInsert. */
5340 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5342 /* If the task should block indefinitely then set the block time to a
5343 * value that will be recognised as an indefinite delay inside the
5344 * prvAddCurrentTaskToDelayedList() function. */
5345 if( xWaitIndefinitely != pdFALSE )
5347 xTicksToWait = portMAX_DELAY;
5350 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
5351 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
5353 traceRETURN_vTaskPlaceOnEventListRestricted();
5356 #endif /* configUSE_TIMERS */
5357 /*-----------------------------------------------------------*/
5359 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
5361 TCB_t * pxUnblockedTCB;
5364 traceENTER_xTaskRemoveFromEventList( pxEventList );
5366 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
5367 * called from a critical section within an ISR. */
5369 /* The event list is sorted in priority order, so the first in the list can
5370 * be removed as it is known to be the highest priority. Remove the TCB from
5371 * the delayed list, and add it to the ready list.
5373 * If an event is for a queue that is locked then this function will never
5374 * get called - the lock count on the queue will get modified instead. This
5375 * means exclusive access to the event list is guaranteed here.
5377 * This function assumes that a check has already been made to ensure that
5378 * pxEventList is not empty. */
5379 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5380 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5381 /* coverity[misra_c_2012_rule_11_5_violation] */
5382 pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
5383 configASSERT( pxUnblockedTCB );
5384 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5386 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5388 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5389 prvAddTaskToReadyList( pxUnblockedTCB );
5391 #if ( configUSE_TICKLESS_IDLE != 0 )
5393 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5394 * might be set to the blocked task's time out time. If the task is
5395 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5396 * normally left unchanged, because it is automatically reset to a new
5397 * value when the tick count equals xNextTaskUnblockTime. However if
5398 * tickless idling is used it might be more important to enter sleep mode
5399 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5400 * ensure it is updated at the earliest possible time. */
5401 prvResetNextTaskUnblockTime();
5407 /* The delayed and ready lists cannot be accessed, so hold this task
5408 * pending until the scheduler is resumed. */
5409 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5412 #if ( configNUMBER_OF_CORES == 1 )
5414 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5416 /* Return true if the task removed from the event list has a higher
5417 * priority than the calling task. This allows the calling task to know if
5418 * it should force a context switch now. */
5421 /* Mark that a yield is pending in case the user is not using the
5422 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5423 xYieldPendings[ 0 ] = pdTRUE;
5430 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5434 #if ( configUSE_PREEMPTION == 1 )
5436 prvYieldForTask( pxUnblockedTCB );
5438 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5443 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5445 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5447 traceRETURN_xTaskRemoveFromEventList( xReturn );
5450 /*-----------------------------------------------------------*/
5452 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5453 const TickType_t xItemValue )
5455 TCB_t * pxUnblockedTCB;
5457 traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5459 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5460 * the event flags implementation. */
5461 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5463 /* Store the new item value in the event list. */
5464 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5466 /* Remove the event list form the event flag. Interrupts do not access
5468 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5469 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5470 /* coverity[misra_c_2012_rule_11_5_violation] */
5471 pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem );
5472 configASSERT( pxUnblockedTCB );
5473 listREMOVE_ITEM( pxEventListItem );
5475 #if ( configUSE_TICKLESS_IDLE != 0 )
5477 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5478 * might be set to the blocked task's time out time. If the task is
5479 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5480 * normally left unchanged, because it is automatically reset to a new
5481 * value when the tick count equals xNextTaskUnblockTime. However if
5482 * tickless idling is used it might be more important to enter sleep mode
5483 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5484 * ensure it is updated at the earliest possible time. */
5485 prvResetNextTaskUnblockTime();
5489 /* Remove the task from the delayed list and add it to the ready list. The
5490 * scheduler is suspended so interrupts will not be accessing the ready
5492 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5493 prvAddTaskToReadyList( pxUnblockedTCB );
5495 #if ( configNUMBER_OF_CORES == 1 )
5497 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5499 /* The unblocked task has a priority above that of the calling task, so
5500 * a context switch is required. This function is called with the
5501 * scheduler suspended so xYieldPending is set so the context switch
5502 * occurs immediately that the scheduler is resumed (unsuspended). */
5503 xYieldPendings[ 0 ] = pdTRUE;
5506 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5508 #if ( configUSE_PREEMPTION == 1 )
5510 taskENTER_CRITICAL();
5512 prvYieldForTask( pxUnblockedTCB );
5514 taskEXIT_CRITICAL();
5518 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5520 traceRETURN_vTaskRemoveFromUnorderedEventList();
5522 /*-----------------------------------------------------------*/
5524 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5526 traceENTER_vTaskSetTimeOutState( pxTimeOut );
5528 configASSERT( pxTimeOut );
5529 taskENTER_CRITICAL();
5531 pxTimeOut->xOverflowCount = xNumOfOverflows;
5532 pxTimeOut->xTimeOnEntering = xTickCount;
5534 taskEXIT_CRITICAL();
5536 traceRETURN_vTaskSetTimeOutState();
5538 /*-----------------------------------------------------------*/
5540 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5542 traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5544 /* For internal use only as it does not use a critical section. */
5545 pxTimeOut->xOverflowCount = xNumOfOverflows;
5546 pxTimeOut->xTimeOnEntering = xTickCount;
5548 traceRETURN_vTaskInternalSetTimeOutState();
5550 /*-----------------------------------------------------------*/
5552 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5553 TickType_t * const pxTicksToWait )
5557 traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5559 configASSERT( pxTimeOut );
5560 configASSERT( pxTicksToWait );
5562 taskENTER_CRITICAL();
5564 /* Minor optimisation. The tick count cannot change in this block. */
5565 const TickType_t xConstTickCount = xTickCount;
5566 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5568 #if ( INCLUDE_xTaskAbortDelay == 1 )
5569 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5571 /* The delay was aborted, which is not the same as a time out,
5572 * but has the same result. */
5573 pxCurrentTCB->ucDelayAborted = pdFALSE;
5579 #if ( INCLUDE_vTaskSuspend == 1 )
5580 if( *pxTicksToWait == portMAX_DELAY )
5582 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5583 * specified is the maximum block time then the task should block
5584 * indefinitely, and therefore never time out. */
5590 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) )
5592 /* The tick count is greater than the time at which
5593 * vTaskSetTimeout() was called, but has also overflowed since
5594 * vTaskSetTimeOut() was called. It must have wrapped all the way
5595 * around and gone past again. This passed since vTaskSetTimeout()
5598 *pxTicksToWait = ( TickType_t ) 0;
5600 else if( xElapsedTime < *pxTicksToWait )
5602 /* Not a genuine timeout. Adjust parameters for time remaining. */
5603 *pxTicksToWait -= xElapsedTime;
5604 vTaskInternalSetTimeOutState( pxTimeOut );
5609 *pxTicksToWait = ( TickType_t ) 0;
5613 taskEXIT_CRITICAL();
5615 traceRETURN_xTaskCheckForTimeOut( xReturn );
5619 /*-----------------------------------------------------------*/
5621 void vTaskMissedYield( void )
5623 traceENTER_vTaskMissedYield();
5625 /* Must be called from within a critical section. */
5626 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5628 traceRETURN_vTaskMissedYield();
5630 /*-----------------------------------------------------------*/
5632 #if ( configUSE_TRACE_FACILITY == 1 )
5634 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5636 UBaseType_t uxReturn;
5637 TCB_t const * pxTCB;
5639 traceENTER_uxTaskGetTaskNumber( xTask );
5644 uxReturn = pxTCB->uxTaskNumber;
5651 traceRETURN_uxTaskGetTaskNumber( uxReturn );
5656 #endif /* configUSE_TRACE_FACILITY */
5657 /*-----------------------------------------------------------*/
5659 #if ( configUSE_TRACE_FACILITY == 1 )
5661 void vTaskSetTaskNumber( TaskHandle_t xTask,
5662 const UBaseType_t uxHandle )
5666 traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5671 pxTCB->uxTaskNumber = uxHandle;
5674 traceRETURN_vTaskSetTaskNumber();
5677 #endif /* configUSE_TRACE_FACILITY */
5678 /*-----------------------------------------------------------*/
5681 * -----------------------------------------------------------
5682 * The passive idle task.
5683 * ----------------------------------------------------------
5685 * The passive idle task is used for all the additional cores in a SMP
5686 * system. There must be only 1 active idle task and the rest are passive
5689 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5690 * language extensions. The equivalent prototype for this function is:
5692 * void prvPassiveIdleTask( void *pvParameters );
5695 #if ( configNUMBER_OF_CORES > 1 )
5696 static portTASK_FUNCTION( prvPassiveIdleTask, pvParameters )
5698 ( void ) pvParameters;
5702 for( ; configCONTROL_INFINITE_LOOP(); )
5704 #if ( configUSE_PREEMPTION == 0 )
5706 /* If we are not using preemption we keep forcing a task switch to
5707 * see if any other task has become available. If we are using
5708 * preemption we don't need to do this as any task becoming available
5709 * will automatically get the processor anyway. */
5712 #endif /* configUSE_PREEMPTION */
5714 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5716 /* When using preemption tasks of equal priority will be
5717 * timesliced. If a task that is sharing the idle priority is ready
5718 * to run then the idle task should yield before the end of the
5721 * A critical region is not required here as we are just reading from
5722 * the list, and an occasional incorrect value will not matter. If
5723 * the ready list at the idle priority contains one more task than the
5724 * number of idle tasks, which is equal to the configured numbers of cores
5725 * then a task other than the idle task is ready to execute. */
5726 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5732 mtCOVERAGE_TEST_MARKER();
5735 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5737 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
5739 /* Call the user defined function from within the idle task. This
5740 * allows the application designer to add background functionality
5741 * without the overhead of a separate task.
5743 * This hook is intended to manage core activity such as disabling cores that go idle.
5745 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5746 * CALL A FUNCTION THAT MIGHT BLOCK. */
5747 vApplicationPassiveIdleHook();
5749 #endif /* configUSE_PASSIVE_IDLE_HOOK */
5752 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5755 * -----------------------------------------------------------
5757 * ----------------------------------------------------------
5759 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5760 * language extensions. The equivalent prototype for this function is:
5762 * void prvIdleTask( void *pvParameters );
5766 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5768 /* Stop warnings. */
5769 ( void ) pvParameters;
5771 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5772 * SCHEDULER IS STARTED. **/
5774 /* In case a task that has a secure context deletes itself, in which case
5775 * the idle task is responsible for deleting the task's secure context, if
5777 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5779 #if ( configNUMBER_OF_CORES > 1 )
5781 /* SMP all cores start up in the idle task. This initial yield gets the application
5785 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5787 for( ; configCONTROL_INFINITE_LOOP(); )
5789 /* See if any tasks have deleted themselves - if so then the idle task
5790 * is responsible for freeing the deleted task's TCB and stack. */
5791 prvCheckTasksWaitingTermination();
5793 #if ( configUSE_PREEMPTION == 0 )
5795 /* If we are not using preemption we keep forcing a task switch to
5796 * see if any other task has become available. If we are using
5797 * preemption we don't need to do this as any task becoming available
5798 * will automatically get the processor anyway. */
5801 #endif /* configUSE_PREEMPTION */
5803 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5805 /* When using preemption tasks of equal priority will be
5806 * timesliced. If a task that is sharing the idle priority is ready
5807 * to run then the idle task should yield before the end of the
5810 * A critical region is not required here as we are just reading from
5811 * the list, and an occasional incorrect value will not matter. If
5812 * the ready list at the idle priority contains one more task than the
5813 * number of idle tasks, which is equal to the configured numbers of cores
5814 * then a task other than the idle task is ready to execute. */
5815 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5821 mtCOVERAGE_TEST_MARKER();
5824 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5826 #if ( configUSE_IDLE_HOOK == 1 )
5828 /* Call the user defined function from within the idle task. */
5829 vApplicationIdleHook();
5831 #endif /* configUSE_IDLE_HOOK */
5833 /* This conditional compilation should use inequality to 0, not equality
5834 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5835 * user defined low power mode implementations require
5836 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5837 #if ( configUSE_TICKLESS_IDLE != 0 )
5839 TickType_t xExpectedIdleTime;
5841 /* It is not desirable to suspend then resume the scheduler on
5842 * each iteration of the idle task. Therefore, a preliminary
5843 * test of the expected idle time is performed without the
5844 * scheduler suspended. The result here is not necessarily
5846 xExpectedIdleTime = prvGetExpectedIdleTime();
5848 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5852 /* Now the scheduler is suspended, the expected idle
5853 * time can be sampled again, and this time its value can
5855 configASSERT( xNextTaskUnblockTime >= xTickCount );
5856 xExpectedIdleTime = prvGetExpectedIdleTime();
5858 /* Define the following macro to set xExpectedIdleTime to 0
5859 * if the application does not want
5860 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5861 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5863 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5865 traceLOW_POWER_IDLE_BEGIN();
5866 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5867 traceLOW_POWER_IDLE_END();
5871 mtCOVERAGE_TEST_MARKER();
5874 ( void ) xTaskResumeAll();
5878 mtCOVERAGE_TEST_MARKER();
5881 #endif /* configUSE_TICKLESS_IDLE */
5883 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) )
5885 /* Call the user defined function from within the idle task. This
5886 * allows the application designer to add background functionality
5887 * without the overhead of a separate task.
5889 * This hook is intended to manage core activity such as disabling cores that go idle.
5891 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5892 * CALL A FUNCTION THAT MIGHT BLOCK. */
5893 vApplicationPassiveIdleHook();
5895 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) ) */
5898 /*-----------------------------------------------------------*/
5900 #if ( configUSE_TICKLESS_IDLE != 0 )
5902 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5904 #if ( INCLUDE_vTaskSuspend == 1 )
5905 /* The idle task exists in addition to the application tasks. */
5906 const UBaseType_t uxNonApplicationTasks = configNUMBER_OF_CORES;
5907 #endif /* INCLUDE_vTaskSuspend */
5909 eSleepModeStatus eReturn = eStandardSleep;
5911 traceENTER_eTaskConfirmSleepModeStatus();
5913 /* This function must be called from a critical section. */
5915 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0U )
5917 /* A task was made ready while the scheduler was suspended. */
5918 eReturn = eAbortSleep;
5920 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5922 /* A yield was pended while the scheduler was suspended. */
5923 eReturn = eAbortSleep;
5925 else if( xPendedTicks != 0U )
5927 /* A tick interrupt has already occurred but was held pending
5928 * because the scheduler is suspended. */
5929 eReturn = eAbortSleep;
5932 #if ( INCLUDE_vTaskSuspend == 1 )
5933 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5935 /* If all the tasks are in the suspended list (which might mean they
5936 * have an infinite block time rather than actually being suspended)
5937 * then it is safe to turn all clocks off and just wait for external
5939 eReturn = eNoTasksWaitingTimeout;
5941 #endif /* INCLUDE_vTaskSuspend */
5944 mtCOVERAGE_TEST_MARKER();
5947 traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5952 #endif /* configUSE_TICKLESS_IDLE */
5953 /*-----------------------------------------------------------*/
5955 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5957 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5963 traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
5965 if( ( xIndex >= 0 ) &&
5966 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5968 pxTCB = prvGetTCBFromHandle( xTaskToSet );
5969 configASSERT( pxTCB != NULL );
5970 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5973 traceRETURN_vTaskSetThreadLocalStoragePointer();
5976 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5977 /*-----------------------------------------------------------*/
5979 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5981 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5984 void * pvReturn = NULL;
5987 traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
5989 if( ( xIndex >= 0 ) &&
5990 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5992 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
5993 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
6000 traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
6005 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
6006 /*-----------------------------------------------------------*/
6008 #if ( portUSING_MPU_WRAPPERS == 1 )
6010 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
6011 const MemoryRegion_t * const pxRegions )
6015 traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
6017 /* If null is passed in here then we are modifying the MPU settings of
6018 * the calling task. */
6019 pxTCB = prvGetTCBFromHandle( xTaskToModify );
6021 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
6023 traceRETURN_vTaskAllocateMPURegions();
6026 #endif /* portUSING_MPU_WRAPPERS */
6027 /*-----------------------------------------------------------*/
6029 static void prvInitialiseTaskLists( void )
6031 UBaseType_t uxPriority;
6033 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
6035 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
6038 vListInitialise( &xDelayedTaskList1 );
6039 vListInitialise( &xDelayedTaskList2 );
6040 vListInitialise( &xPendingReadyList );
6042 #if ( INCLUDE_vTaskDelete == 1 )
6044 vListInitialise( &xTasksWaitingTermination );
6046 #endif /* INCLUDE_vTaskDelete */
6048 #if ( INCLUDE_vTaskSuspend == 1 )
6050 vListInitialise( &xSuspendedTaskList );
6052 #endif /* INCLUDE_vTaskSuspend */
6054 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
6056 pxDelayedTaskList = &xDelayedTaskList1;
6057 pxOverflowDelayedTaskList = &xDelayedTaskList2;
6059 /*-----------------------------------------------------------*/
6061 static void prvCheckTasksWaitingTermination( void )
6063 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
6065 #if ( INCLUDE_vTaskDelete == 1 )
6069 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
6070 * being called too often in the idle task. */
6071 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6073 #if ( configNUMBER_OF_CORES == 1 )
6075 taskENTER_CRITICAL();
6078 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6079 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6080 /* coverity[misra_c_2012_rule_11_5_violation] */
6081 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6082 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6083 --uxCurrentNumberOfTasks;
6084 --uxDeletedTasksWaitingCleanUp;
6087 taskEXIT_CRITICAL();
6089 prvDeleteTCB( pxTCB );
6091 #else /* #if( configNUMBER_OF_CORES == 1 ) */
6095 taskENTER_CRITICAL();
6097 /* For SMP, multiple idles can be running simultaneously
6098 * and we need to check that other idles did not cleanup while we were
6099 * waiting to enter the critical section. */
6100 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6102 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6103 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6104 /* coverity[misra_c_2012_rule_11_5_violation] */
6105 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6107 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
6109 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6110 --uxCurrentNumberOfTasks;
6111 --uxDeletedTasksWaitingCleanUp;
6115 /* The TCB to be deleted still has not yet been switched out
6116 * by the scheduler, so we will just exit this loop early and
6117 * try again next time. */
6118 taskEXIT_CRITICAL();
6123 taskEXIT_CRITICAL();
6127 prvDeleteTCB( pxTCB );
6130 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
6133 #endif /* INCLUDE_vTaskDelete */
6135 /*-----------------------------------------------------------*/
6137 #if ( configUSE_TRACE_FACILITY == 1 )
6139 void vTaskGetInfo( TaskHandle_t xTask,
6140 TaskStatus_t * pxTaskStatus,
6141 BaseType_t xGetFreeStackSpace,
6146 traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
6148 /* xTask is NULL then get the state of the calling task. */
6149 pxTCB = prvGetTCBFromHandle( xTask );
6151 pxTaskStatus->xHandle = pxTCB;
6152 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
6153 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
6154 pxTaskStatus->pxStackBase = pxTCB->pxStack;
6155 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
6156 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
6157 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
6159 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
6161 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
6163 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
6167 #if ( configUSE_MUTEXES == 1 )
6169 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
6173 pxTaskStatus->uxBasePriority = 0;
6177 #if ( configGENERATE_RUN_TIME_STATS == 1 )
6179 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
6183 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
6187 /* Obtaining the task state is a little fiddly, so is only done if the
6188 * value of eState passed into this function is eInvalid - otherwise the
6189 * state is just set to whatever is passed in. */
6190 if( eState != eInvalid )
6192 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6194 pxTaskStatus->eCurrentState = eRunning;
6198 pxTaskStatus->eCurrentState = eState;
6200 #if ( INCLUDE_vTaskSuspend == 1 )
6202 /* If the task is in the suspended list then there is a
6203 * chance it is actually just blocked indefinitely - so really
6204 * it should be reported as being in the Blocked state. */
6205 if( eState == eSuspended )
6209 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
6211 pxTaskStatus->eCurrentState = eBlocked;
6215 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6219 /* The task does not appear on the event list item of
6220 * and of the RTOS objects, but could still be in the
6221 * blocked state if it is waiting on its notification
6222 * rather than waiting on an object. If not, is
6224 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
6226 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
6228 pxTaskStatus->eCurrentState = eBlocked;
6233 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
6236 ( void ) xTaskResumeAll();
6239 #endif /* INCLUDE_vTaskSuspend */
6241 /* Tasks can be in pending ready list and other state list at the
6242 * same time. These tasks are in ready state no matter what state
6243 * list the task is in. */
6244 taskENTER_CRITICAL();
6246 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
6248 pxTaskStatus->eCurrentState = eReady;
6251 taskEXIT_CRITICAL();
6256 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
6259 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
6260 * parameter is provided to allow it to be skipped. */
6261 if( xGetFreeStackSpace != pdFALSE )
6263 #if ( portSTACK_GROWTH > 0 )
6265 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
6269 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
6275 pxTaskStatus->usStackHighWaterMark = 0;
6278 traceRETURN_vTaskGetInfo();
6281 #endif /* configUSE_TRACE_FACILITY */
6282 /*-----------------------------------------------------------*/
6284 #if ( configUSE_TRACE_FACILITY == 1 )
6286 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
6290 configLIST_VOLATILE TCB_t * pxNextTCB;
6291 configLIST_VOLATILE TCB_t * pxFirstTCB;
6292 UBaseType_t uxTask = 0;
6294 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
6296 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6297 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6298 /* coverity[misra_c_2012_rule_11_5_violation] */
6299 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
6301 /* Populate an TaskStatus_t structure within the
6302 * pxTaskStatusArray array for each task that is referenced from
6303 * pxList. See the definition of TaskStatus_t in task.h for the
6304 * meaning of each TaskStatus_t structure member. */
6307 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6308 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6309 /* coverity[misra_c_2012_rule_11_5_violation] */
6310 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
6311 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
6313 } while( pxNextTCB != pxFirstTCB );
6317 mtCOVERAGE_TEST_MARKER();
6323 #endif /* configUSE_TRACE_FACILITY */
6324 /*-----------------------------------------------------------*/
6326 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
6328 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
6330 configSTACK_DEPTH_TYPE uxCount = 0U;
6332 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
6334 pucStackByte -= portSTACK_GROWTH;
6338 uxCount /= ( configSTACK_DEPTH_TYPE ) sizeof( StackType_t );
6343 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
6344 /*-----------------------------------------------------------*/
6346 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
6348 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
6349 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
6350 * user to determine the return type. It gets around the problem of the value
6351 * overflowing on 8-bit types without breaking backward compatibility for
6352 * applications that expect an 8-bit return type. */
6353 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
6356 uint8_t * pucEndOfStack;
6357 configSTACK_DEPTH_TYPE uxReturn;
6359 traceENTER_uxTaskGetStackHighWaterMark2( xTask );
6361 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
6362 * the same except for their return type. Using configSTACK_DEPTH_TYPE
6363 * allows the user to determine the return type. It gets around the
6364 * problem of the value overflowing on 8-bit types without breaking
6365 * backward compatibility for applications that expect an 8-bit return
6368 pxTCB = prvGetTCBFromHandle( xTask );
6370 #if portSTACK_GROWTH < 0
6372 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6376 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6380 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
6382 traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
6387 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
6388 /*-----------------------------------------------------------*/
6390 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
6392 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
6395 uint8_t * pucEndOfStack;
6396 UBaseType_t uxReturn;
6398 traceENTER_uxTaskGetStackHighWaterMark( xTask );
6400 pxTCB = prvGetTCBFromHandle( xTask );
6402 #if portSTACK_GROWTH < 0
6404 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6408 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6412 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
6414 traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6419 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6420 /*-----------------------------------------------------------*/
6422 #if ( INCLUDE_vTaskDelete == 1 )
6424 static void prvDeleteTCB( TCB_t * pxTCB )
6426 /* This call is required specifically for the TriCore port. It must be
6427 * above the vPortFree() calls. The call is also used by ports/demos that
6428 * want to allocate and clean RAM statically. */
6429 portCLEAN_UP_TCB( pxTCB );
6431 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6433 /* Free up the memory allocated for the task's TLS Block. */
6434 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6438 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6440 /* The task can only have been allocated dynamically - free both
6441 * the stack and TCB. */
6442 vPortFreeStack( pxTCB->pxStack );
6445 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
6447 /* The task could have been allocated statically or dynamically, so
6448 * check what was statically allocated before trying to free the
6450 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6452 /* Both the stack and TCB were allocated dynamically, so both
6454 vPortFreeStack( pxTCB->pxStack );
6457 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6459 /* Only the stack was statically allocated, so the TCB is the
6460 * only memory that must be freed. */
6465 /* Neither the stack nor the TCB were allocated dynamically, so
6466 * nothing needs to be freed. */
6467 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6468 mtCOVERAGE_TEST_MARKER();
6471 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6474 #endif /* INCLUDE_vTaskDelete */
6475 /*-----------------------------------------------------------*/
6477 static void prvResetNextTaskUnblockTime( void )
6479 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6481 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
6482 * the maximum possible value so it is extremely unlikely that the
6483 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6484 * there is an item in the delayed list. */
6485 xNextTaskUnblockTime = portMAX_DELAY;
6489 /* The new current delayed list is not empty, get the value of
6490 * the item at the head of the delayed list. This is the time at
6491 * which the task at the head of the delayed list should be removed
6492 * from the Blocked state. */
6493 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6496 /*-----------------------------------------------------------*/
6498 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6500 #if ( configNUMBER_OF_CORES == 1 )
6501 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6503 TaskHandle_t xReturn;
6505 traceENTER_xTaskGetCurrentTaskHandle();
6507 /* A critical section is not required as this is not called from
6508 * an interrupt and the current TCB will always be the same for any
6509 * individual execution thread. */
6510 xReturn = pxCurrentTCB;
6512 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6516 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6517 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6519 TaskHandle_t xReturn;
6520 UBaseType_t uxSavedInterruptStatus;
6522 traceENTER_xTaskGetCurrentTaskHandle();
6524 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6526 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6528 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6530 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6535 TaskHandle_t xTaskGetCurrentTaskHandleForCore( BaseType_t xCoreID )
6537 TaskHandle_t xReturn = NULL;
6539 traceENTER_xTaskGetCurrentTaskHandleForCore( xCoreID );
6541 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6543 xReturn = pxCurrentTCBs[ xCoreID ];
6546 traceRETURN_xTaskGetCurrentTaskHandleForCore( xReturn );
6550 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6552 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
6553 /*-----------------------------------------------------------*/
6555 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6557 BaseType_t xTaskGetSchedulerState( void )
6561 traceENTER_xTaskGetSchedulerState();
6563 if( xSchedulerRunning == pdFALSE )
6565 xReturn = taskSCHEDULER_NOT_STARTED;
6569 #if ( configNUMBER_OF_CORES > 1 )
6570 taskENTER_CRITICAL();
6573 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6575 xReturn = taskSCHEDULER_RUNNING;
6579 xReturn = taskSCHEDULER_SUSPENDED;
6582 #if ( configNUMBER_OF_CORES > 1 )
6583 taskEXIT_CRITICAL();
6587 traceRETURN_xTaskGetSchedulerState( xReturn );
6592 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6593 /*-----------------------------------------------------------*/
6595 #if ( configUSE_MUTEXES == 1 )
6597 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6599 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6600 BaseType_t xReturn = pdFALSE;
6602 traceENTER_xTaskPriorityInherit( pxMutexHolder );
6604 /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6605 * inheritance is not applied in this scenario. */
6606 if( pxMutexHolder != NULL )
6608 /* If the holder of the mutex has a priority below the priority of
6609 * the task attempting to obtain the mutex then it will temporarily
6610 * inherit the priority of the task attempting to obtain the mutex. */
6611 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6613 /* Adjust the mutex holder state to account for its new
6614 * priority. Only reset the event list item value if the value is
6615 * not being used for anything else. */
6616 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0UL ) )
6618 listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority );
6622 mtCOVERAGE_TEST_MARKER();
6625 /* If the task being modified is in the ready state it will need
6626 * to be moved into a new list. */
6627 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6629 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6631 /* It is known that the task is in its ready list so
6632 * there is no need to check again and the port level
6633 * reset macro can be called directly. */
6634 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6638 mtCOVERAGE_TEST_MARKER();
6641 /* Inherit the priority before being moved into the new list. */
6642 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6643 prvAddTaskToReadyList( pxMutexHolderTCB );
6644 #if ( configNUMBER_OF_CORES > 1 )
6646 /* The priority of the task is raised. Yield for this task
6647 * if it is not running. */
6648 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6650 prvYieldForTask( pxMutexHolderTCB );
6653 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6657 /* Just inherit the priority. */
6658 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6661 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6663 /* Inheritance occurred. */
6668 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6670 /* The base priority of the mutex holder is lower than the
6671 * priority of the task attempting to take the mutex, but the
6672 * current priority of the mutex holder is not lower than the
6673 * priority of the task attempting to take the mutex.
6674 * Therefore the mutex holder must have already inherited a
6675 * priority, but inheritance would have occurred if that had
6676 * not been the case. */
6681 mtCOVERAGE_TEST_MARKER();
6687 mtCOVERAGE_TEST_MARKER();
6690 traceRETURN_xTaskPriorityInherit( xReturn );
6695 #endif /* configUSE_MUTEXES */
6696 /*-----------------------------------------------------------*/
6698 #if ( configUSE_MUTEXES == 1 )
6700 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6702 TCB_t * const pxTCB = pxMutexHolder;
6703 BaseType_t xReturn = pdFALSE;
6705 traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6707 if( pxMutexHolder != NULL )
6709 /* A task can only have an inherited priority if it holds the mutex.
6710 * If the mutex is held by a task then it cannot be given from an
6711 * interrupt, and if a mutex is given by the holding task then it must
6712 * be the running state task. */
6713 configASSERT( pxTCB == pxCurrentTCB );
6714 configASSERT( pxTCB->uxMutexesHeld );
6715 ( pxTCB->uxMutexesHeld )--;
6717 /* Has the holder of the mutex inherited the priority of another
6719 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6721 /* Only disinherit if no other mutexes are held. */
6722 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6724 /* A task can only have an inherited priority if it holds
6725 * the mutex. If the mutex is held by a task then it cannot be
6726 * given from an interrupt, and if a mutex is given by the
6727 * holding task then it must be the running state task. Remove
6728 * the holding task from the ready list. */
6729 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6731 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6735 mtCOVERAGE_TEST_MARKER();
6738 /* Disinherit the priority before adding the task into the
6739 * new ready list. */
6740 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6741 pxTCB->uxPriority = pxTCB->uxBasePriority;
6743 /* Reset the event list item value. It cannot be in use for
6744 * any other purpose if this task is running, and it must be
6745 * running to give back the mutex. */
6746 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority );
6747 prvAddTaskToReadyList( pxTCB );
6748 #if ( configNUMBER_OF_CORES > 1 )
6750 /* The priority of the task is dropped. Yield the core on
6751 * which the task is running. */
6752 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6754 prvYieldCore( pxTCB->xTaskRunState );
6757 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6759 /* Return true to indicate that a context switch is required.
6760 * This is only actually required in the corner case whereby
6761 * multiple mutexes were held and the mutexes were given back
6762 * in an order different to that in which they were taken.
6763 * If a context switch did not occur when the first mutex was
6764 * returned, even if a task was waiting on it, then a context
6765 * switch should occur when the last mutex is returned whether
6766 * a task is waiting on it or not. */
6771 mtCOVERAGE_TEST_MARKER();
6776 mtCOVERAGE_TEST_MARKER();
6781 mtCOVERAGE_TEST_MARKER();
6784 traceRETURN_xTaskPriorityDisinherit( xReturn );
6789 #endif /* configUSE_MUTEXES */
6790 /*-----------------------------------------------------------*/
6792 #if ( configUSE_MUTEXES == 1 )
6794 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6795 UBaseType_t uxHighestPriorityWaitingTask )
6797 TCB_t * const pxTCB = pxMutexHolder;
6798 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6799 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6801 traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6803 if( pxMutexHolder != NULL )
6805 /* If pxMutexHolder is not NULL then the holder must hold at least
6807 configASSERT( pxTCB->uxMutexesHeld );
6809 /* Determine the priority to which the priority of the task that
6810 * holds the mutex should be set. This will be the greater of the
6811 * holding task's base priority and the priority of the highest
6812 * priority task that is waiting to obtain the mutex. */
6813 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6815 uxPriorityToUse = uxHighestPriorityWaitingTask;
6819 uxPriorityToUse = pxTCB->uxBasePriority;
6822 /* Does the priority need to change? */
6823 if( pxTCB->uxPriority != uxPriorityToUse )
6825 /* Only disinherit if no other mutexes are held. This is a
6826 * simplification in the priority inheritance implementation. If
6827 * the task that holds the mutex is also holding other mutexes then
6828 * the other mutexes may have caused the priority inheritance. */
6829 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6831 /* If a task has timed out because it already holds the
6832 * mutex it was trying to obtain then it cannot of inherited
6833 * its own priority. */
6834 configASSERT( pxTCB != pxCurrentTCB );
6836 /* Disinherit the priority, remembering the previous
6837 * priority to facilitate determining the subject task's
6839 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6840 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6841 pxTCB->uxPriority = uxPriorityToUse;
6843 /* Only reset the event list item value if the value is not
6844 * being used for anything else. */
6845 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0UL ) )
6847 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse );
6851 mtCOVERAGE_TEST_MARKER();
6854 /* If the running task is not the task that holds the mutex
6855 * then the task that holds the mutex could be in either the
6856 * Ready, Blocked or Suspended states. Only remove the task
6857 * from its current state list if it is in the Ready state as
6858 * the task's priority is going to change and there is one
6859 * Ready list per priority. */
6860 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6862 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6864 /* It is known that the task is in its ready list so
6865 * there is no need to check again and the port level
6866 * reset macro can be called directly. */
6867 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6871 mtCOVERAGE_TEST_MARKER();
6874 prvAddTaskToReadyList( pxTCB );
6875 #if ( configNUMBER_OF_CORES > 1 )
6877 /* The priority of the task is dropped. Yield the core on
6878 * which the task is running. */
6879 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6881 prvYieldCore( pxTCB->xTaskRunState );
6884 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6888 mtCOVERAGE_TEST_MARKER();
6893 mtCOVERAGE_TEST_MARKER();
6898 mtCOVERAGE_TEST_MARKER();
6903 mtCOVERAGE_TEST_MARKER();
6906 traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6909 #endif /* configUSE_MUTEXES */
6910 /*-----------------------------------------------------------*/
6912 #if ( configNUMBER_OF_CORES > 1 )
6914 /* If not in a critical section then yield immediately.
6915 * Otherwise set xYieldPendings to true to wait to
6916 * yield until exiting the critical section.
6918 void vTaskYieldWithinAPI( void )
6920 traceENTER_vTaskYieldWithinAPI();
6922 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6928 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6931 traceRETURN_vTaskYieldWithinAPI();
6933 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6935 /*-----------------------------------------------------------*/
6937 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6939 void vTaskEnterCritical( void )
6941 traceENTER_vTaskEnterCritical();
6943 portDISABLE_INTERRUPTS();
6945 if( xSchedulerRunning != pdFALSE )
6947 ( pxCurrentTCB->uxCriticalNesting )++;
6949 /* This is not the interrupt safe version of the enter critical
6950 * function so assert() if it is being called from an interrupt
6951 * context. Only API functions that end in "FromISR" can be used in an
6952 * interrupt. Only assert if the critical nesting count is 1 to
6953 * protect against recursive calls if the assert function also uses a
6954 * critical section. */
6955 if( pxCurrentTCB->uxCriticalNesting == 1U )
6957 portASSERT_IF_IN_ISR();
6962 mtCOVERAGE_TEST_MARKER();
6965 traceRETURN_vTaskEnterCritical();
6968 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6969 /*-----------------------------------------------------------*/
6971 #if ( configNUMBER_OF_CORES > 1 )
6973 void vTaskEnterCritical( void )
6975 traceENTER_vTaskEnterCritical();
6977 portDISABLE_INTERRUPTS();
6979 if( xSchedulerRunning != pdFALSE )
6981 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6983 portGET_TASK_LOCK();
6987 portINCREMENT_CRITICAL_NESTING_COUNT();
6989 /* This is not the interrupt safe version of the enter critical
6990 * function so assert() if it is being called from an interrupt
6991 * context. Only API functions that end in "FromISR" can be used in an
6992 * interrupt. Only assert if the critical nesting count is 1 to
6993 * protect against recursive calls if the assert function also uses a
6994 * critical section. */
6995 if( portGET_CRITICAL_NESTING_COUNT() == 1U )
6997 portASSERT_IF_IN_ISR();
6999 if( uxSchedulerSuspended == 0U )
7001 /* The only time there would be a problem is if this is called
7002 * before a context switch and vTaskExitCritical() is called
7003 * after pxCurrentTCB changes. Therefore this should not be
7004 * used within vTaskSwitchContext(). */
7005 prvCheckForRunStateChange();
7011 mtCOVERAGE_TEST_MARKER();
7014 traceRETURN_vTaskEnterCritical();
7017 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7019 /*-----------------------------------------------------------*/
7021 #if ( configNUMBER_OF_CORES > 1 )
7023 UBaseType_t vTaskEnterCriticalFromISR( void )
7025 UBaseType_t uxSavedInterruptStatus = 0;
7027 traceENTER_vTaskEnterCriticalFromISR();
7029 if( xSchedulerRunning != pdFALSE )
7031 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
7033 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7038 portINCREMENT_CRITICAL_NESTING_COUNT();
7042 mtCOVERAGE_TEST_MARKER();
7045 traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
7047 return uxSavedInterruptStatus;
7050 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7051 /*-----------------------------------------------------------*/
7053 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
7055 void vTaskExitCritical( void )
7057 traceENTER_vTaskExitCritical();
7059 if( xSchedulerRunning != pdFALSE )
7061 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
7062 * does not match a previous call to vTaskEnterCritical(). */
7063 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
7065 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7066 * to exit critical section from ISR. */
7067 portASSERT_IF_IN_ISR();
7069 if( pxCurrentTCB->uxCriticalNesting > 0U )
7071 ( pxCurrentTCB->uxCriticalNesting )--;
7073 if( pxCurrentTCB->uxCriticalNesting == 0U )
7075 portENABLE_INTERRUPTS();
7079 mtCOVERAGE_TEST_MARKER();
7084 mtCOVERAGE_TEST_MARKER();
7089 mtCOVERAGE_TEST_MARKER();
7092 traceRETURN_vTaskExitCritical();
7095 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
7096 /*-----------------------------------------------------------*/
7098 #if ( configNUMBER_OF_CORES > 1 )
7100 void vTaskExitCritical( void )
7102 traceENTER_vTaskExitCritical();
7104 if( xSchedulerRunning != pdFALSE )
7106 /* If critical nesting count is zero then this function
7107 * does not match a previous call to vTaskEnterCritical(). */
7108 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7110 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7111 * to exit critical section from ISR. */
7112 portASSERT_IF_IN_ISR();
7114 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7116 portDECREMENT_CRITICAL_NESTING_COUNT();
7118 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7120 BaseType_t xYieldCurrentTask;
7122 /* Get the xYieldPending stats inside the critical section. */
7123 xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
7125 portRELEASE_ISR_LOCK();
7126 portRELEASE_TASK_LOCK();
7127 portENABLE_INTERRUPTS();
7129 /* When a task yields in a critical section it just sets
7130 * xYieldPending to true. So now that we have exited the
7131 * critical section check if xYieldPending is true, and
7133 if( xYieldCurrentTask != pdFALSE )
7140 mtCOVERAGE_TEST_MARKER();
7145 mtCOVERAGE_TEST_MARKER();
7150 mtCOVERAGE_TEST_MARKER();
7153 traceRETURN_vTaskExitCritical();
7156 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7157 /*-----------------------------------------------------------*/
7159 #if ( configNUMBER_OF_CORES > 1 )
7161 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
7163 traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
7165 if( xSchedulerRunning != pdFALSE )
7167 /* If critical nesting count is zero then this function
7168 * does not match a previous call to vTaskEnterCritical(). */
7169 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7171 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7173 portDECREMENT_CRITICAL_NESTING_COUNT();
7175 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7177 portRELEASE_ISR_LOCK();
7178 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
7182 mtCOVERAGE_TEST_MARKER();
7187 mtCOVERAGE_TEST_MARKER();
7192 mtCOVERAGE_TEST_MARKER();
7195 traceRETURN_vTaskExitCriticalFromISR();
7198 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7199 /*-----------------------------------------------------------*/
7201 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
7203 static char * prvWriteNameToBuffer( char * pcBuffer,
7204 const char * pcTaskName )
7208 /* Start by copying the entire string. */
7209 ( void ) strcpy( pcBuffer, pcTaskName );
7211 /* Pad the end of the string with spaces to ensure columns line up when
7213 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
7215 pcBuffer[ x ] = ' ';
7219 pcBuffer[ x ] = ( char ) 0x00;
7221 /* Return the new end of string. */
7222 return &( pcBuffer[ x ] );
7225 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
7226 /*-----------------------------------------------------------*/
7228 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
7230 void vTaskListTasks( char * pcWriteBuffer,
7231 size_t uxBufferLength )
7233 TaskStatus_t * pxTaskStatusArray;
7234 size_t uxConsumedBufferLength = 0;
7235 size_t uxCharsWrittenBySnprintf;
7236 int iSnprintfReturnValue;
7237 BaseType_t xOutputBufferFull = pdFALSE;
7238 UBaseType_t uxArraySize, x;
7241 traceENTER_vTaskListTasks( pcWriteBuffer, uxBufferLength );
7246 * This function is provided for convenience only, and is used by many
7247 * of the demo applications. Do not consider it to be part of the
7250 * vTaskListTasks() calls uxTaskGetSystemState(), then formats part of the
7251 * uxTaskGetSystemState() output into a human readable table that
7252 * displays task: names, states, priority, stack usage and task number.
7253 * Stack usage specified as the number of unused StackType_t words stack can hold
7254 * on top of stack - not the number of bytes.
7256 * vTaskListTasks() has a dependency on the snprintf() C library function that
7257 * might bloat the code size, use a lot of stack, and provide different
7258 * results on different platforms. An alternative, tiny, third party,
7259 * and limited functionality implementation of snprintf() is provided in
7260 * many of the FreeRTOS/Demo sub-directories in a file called
7261 * printf-stdarg.c (note printf-stdarg.c does not provide a full
7262 * snprintf() implementation!).
7264 * It is recommended that production systems call uxTaskGetSystemState()
7265 * directly to get access to raw stats data, rather than indirectly
7266 * through a call to vTaskListTasks().
7270 /* Make sure the write buffer does not contain a string. */
7271 *pcWriteBuffer = ( char ) 0x00;
7273 /* Take a snapshot of the number of tasks in case it changes while this
7274 * function is executing. */
7275 uxArraySize = uxCurrentNumberOfTasks;
7277 /* Allocate an array index for each task. NOTE! if
7278 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7279 * equate to NULL. */
7280 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7281 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7282 /* coverity[misra_c_2012_rule_11_5_violation] */
7283 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7285 if( pxTaskStatusArray != NULL )
7287 /* Generate the (binary) data. */
7288 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
7290 /* Create a human readable table from the binary data. */
7291 for( x = 0; x < uxArraySize; x++ )
7293 switch( pxTaskStatusArray[ x ].eCurrentState )
7296 cStatus = tskRUNNING_CHAR;
7300 cStatus = tskREADY_CHAR;
7304 cStatus = tskBLOCKED_CHAR;
7308 cStatus = tskSUSPENDED_CHAR;
7312 cStatus = tskDELETED_CHAR;
7315 case eInvalid: /* Fall through. */
7316 default: /* Should not get here, but it is included
7317 * to prevent static checking errors. */
7318 cStatus = ( char ) 0x00;
7322 /* Is there enough space in the buffer to hold task name? */
7323 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7325 /* Write the task name to the string, padding with spaces so it
7326 * can be printed in tabular form more easily. */
7327 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7328 /* Do not count the terminating null character. */
7329 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7331 /* Is there space left in the buffer? -1 is done because snprintf
7332 * writes a terminating null character. So we are essentially
7333 * checking if the buffer has space to write at least one non-null
7335 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7337 /* Write the rest of the string. */
7338 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
7339 /* MISRA Ref 21.6.1 [snprintf for utility] */
7340 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7341 /* coverity[misra_c_2012_rule_21_6_violation] */
7342 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7343 uxBufferLength - uxConsumedBufferLength,
7344 "\t%c\t%u\t%u\t%u\t0x%x\r\n",
7346 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7347 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7348 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber,
7349 ( unsigned int ) pxTaskStatusArray[ x ].uxCoreAffinityMask );
7350 #else /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7351 /* MISRA Ref 21.6.1 [snprintf for utility] */
7352 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7353 /* coverity[misra_c_2012_rule_21_6_violation] */
7354 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7355 uxBufferLength - uxConsumedBufferLength,
7356 "\t%c\t%u\t%u\t%u\r\n",
7358 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7359 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7360 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber );
7361 #endif /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7362 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7364 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7365 pcWriteBuffer += uxCharsWrittenBySnprintf;
7369 xOutputBufferFull = pdTRUE;
7374 xOutputBufferFull = pdTRUE;
7377 if( xOutputBufferFull == pdTRUE )
7383 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7384 * is 0 then vPortFree() will be #defined to nothing. */
7385 vPortFree( pxTaskStatusArray );
7389 mtCOVERAGE_TEST_MARKER();
7392 traceRETURN_vTaskListTasks();
7395 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7396 /*----------------------------------------------------------*/
7398 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
7400 void vTaskGetRunTimeStatistics( char * pcWriteBuffer,
7401 size_t uxBufferLength )
7403 TaskStatus_t * pxTaskStatusArray;
7404 size_t uxConsumedBufferLength = 0;
7405 size_t uxCharsWrittenBySnprintf;
7406 int iSnprintfReturnValue;
7407 BaseType_t xOutputBufferFull = pdFALSE;
7408 UBaseType_t uxArraySize, x;
7409 configRUN_TIME_COUNTER_TYPE ulTotalTime = 0;
7410 configRUN_TIME_COUNTER_TYPE ulStatsAsPercentage;
7412 traceENTER_vTaskGetRunTimeStatistics( pcWriteBuffer, uxBufferLength );
7417 * This function is provided for convenience only, and is used by many
7418 * of the demo applications. Do not consider it to be part of the
7421 * vTaskGetRunTimeStatistics() calls uxTaskGetSystemState(), then formats part
7422 * of the uxTaskGetSystemState() output into a human readable table that
7423 * displays the amount of time each task has spent in the Running state
7424 * in both absolute and percentage terms.
7426 * vTaskGetRunTimeStatistics() has a dependency on the snprintf() C library
7427 * function that might bloat the code size, use a lot of stack, and
7428 * provide different results on different platforms. An alternative,
7429 * tiny, third party, and limited functionality implementation of
7430 * snprintf() is provided in many of the FreeRTOS/Demo sub-directories in
7431 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
7432 * a full snprintf() implementation!).
7434 * It is recommended that production systems call uxTaskGetSystemState()
7435 * directly to get access to raw stats data, rather than indirectly
7436 * through a call to vTaskGetRunTimeStatistics().
7439 /* Make sure the write buffer does not contain a string. */
7440 *pcWriteBuffer = ( char ) 0x00;
7442 /* Take a snapshot of the number of tasks in case it changes while this
7443 * function is executing. */
7444 uxArraySize = uxCurrentNumberOfTasks;
7446 /* Allocate an array index for each task. NOTE! If
7447 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7448 * equate to NULL. */
7449 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7450 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7451 /* coverity[misra_c_2012_rule_11_5_violation] */
7452 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7454 if( pxTaskStatusArray != NULL )
7456 /* Generate the (binary) data. */
7457 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
7459 /* For percentage calculations. */
7460 ulTotalTime /= ( ( configRUN_TIME_COUNTER_TYPE ) 100UL );
7462 /* Avoid divide by zero errors. */
7463 if( ulTotalTime > 0UL )
7465 /* Create a human readable table from the binary data. */
7466 for( x = 0; x < uxArraySize; x++ )
7468 /* What percentage of the total run time has the task used?
7469 * This will always be rounded down to the nearest integer.
7470 * ulTotalRunTime has already been divided by 100. */
7471 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
7473 /* Is there enough space in the buffer to hold task name? */
7474 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7476 /* Write the task name to the string, padding with
7477 * spaces so it can be printed in tabular form more
7479 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7480 /* Do not count the terminating null character. */
7481 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7483 /* Is there space left in the buffer? -1 is done because snprintf
7484 * writes a terminating null character. So we are essentially
7485 * checking if the buffer has space to write at least one non-null
7487 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7489 if( ulStatsAsPercentage > 0UL )
7491 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7493 /* MISRA Ref 21.6.1 [snprintf for utility] */
7494 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7495 /* coverity[misra_c_2012_rule_21_6_violation] */
7496 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7497 uxBufferLength - uxConsumedBufferLength,
7498 "\t%lu\t\t%lu%%\r\n",
7499 pxTaskStatusArray[ x ].ulRunTimeCounter,
7500 ulStatsAsPercentage );
7502 #else /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7504 /* sizeof( int ) == sizeof( long ) so a smaller
7505 * printf() library can be used. */
7506 /* MISRA Ref 21.6.1 [snprintf for utility] */
7507 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7508 /* coverity[misra_c_2012_rule_21_6_violation] */
7509 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7510 uxBufferLength - uxConsumedBufferLength,
7512 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter,
7513 ( unsigned int ) ulStatsAsPercentage );
7515 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7519 /* If the percentage is zero here then the task has
7520 * consumed less than 1% of the total run time. */
7521 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7523 /* MISRA Ref 21.6.1 [snprintf for utility] */
7524 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7525 /* coverity[misra_c_2012_rule_21_6_violation] */
7526 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7527 uxBufferLength - uxConsumedBufferLength,
7528 "\t%lu\t\t<1%%\r\n",
7529 pxTaskStatusArray[ x ].ulRunTimeCounter );
7533 /* sizeof( int ) == sizeof( long ) so a smaller
7534 * printf() library can be used. */
7535 /* MISRA Ref 21.6.1 [snprintf for utility] */
7536 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7537 /* coverity[misra_c_2012_rule_21_6_violation] */
7538 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7539 uxBufferLength - uxConsumedBufferLength,
7541 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter );
7543 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7546 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7547 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7548 pcWriteBuffer += uxCharsWrittenBySnprintf;
7552 xOutputBufferFull = pdTRUE;
7557 xOutputBufferFull = pdTRUE;
7560 if( xOutputBufferFull == pdTRUE )
7568 mtCOVERAGE_TEST_MARKER();
7571 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7572 * is 0 then vPortFree() will be #defined to nothing. */
7573 vPortFree( pxTaskStatusArray );
7577 mtCOVERAGE_TEST_MARKER();
7580 traceRETURN_vTaskGetRunTimeStatistics();
7583 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7584 /*-----------------------------------------------------------*/
7586 TickType_t uxTaskResetEventItemValue( void )
7588 TickType_t uxReturn;
7590 traceENTER_uxTaskResetEventItemValue();
7592 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7594 /* Reset the event list item to its normal value - so it can be used with
7595 * queues and semaphores. */
7596 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) );
7598 traceRETURN_uxTaskResetEventItemValue( uxReturn );
7602 /*-----------------------------------------------------------*/
7604 #if ( configUSE_MUTEXES == 1 )
7606 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7610 traceENTER_pvTaskIncrementMutexHeldCount();
7612 pxTCB = pxCurrentTCB;
7614 /* If xSemaphoreCreateMutex() is called before any tasks have been created
7615 * then pxCurrentTCB will be NULL. */
7618 ( pxTCB->uxMutexesHeld )++;
7621 traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7626 #endif /* configUSE_MUTEXES */
7627 /*-----------------------------------------------------------*/
7629 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7631 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7632 BaseType_t xClearCountOnExit,
7633 TickType_t xTicksToWait )
7636 BaseType_t xAlreadyYielded;
7638 traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7640 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7642 taskENTER_CRITICAL();
7644 /* Only block if the notification count is not already non-zero. */
7645 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0UL )
7647 /* Mark this task as waiting for a notification. */
7648 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7650 if( xTicksToWait > ( TickType_t ) 0 )
7652 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7654 /* We MUST suspend the scheduler before exiting the critical
7655 * section (i.e. before enabling interrupts).
7657 * If we do not do so, a notification sent from an ISR, which
7658 * happens after exiting the critical section and before
7659 * suspending the scheduler, will get lost. The sequence of
7661 * 1. Exit critical section.
7662 * 2. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7663 * task to the Ready list.
7664 * 3. Suspend scheduler.
7665 * 4. prvAddCurrentTaskToDelayedList moves the task to the
7666 * delayed or suspended list.
7667 * 5. Resume scheduler does not touch the task (because it is
7668 * not on the pendingReady list), effectively losing the
7669 * notification from the ISR.
7671 * The same does not happen when we suspend the scheduler before
7672 * exiting the critical section. The sequence of events in this
7674 * 1. Suspend scheduler.
7675 * 2. Exit critical section.
7676 * 3. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7677 * task to the pendingReady list as the scheduler is
7679 * 4. prvAddCurrentTaskToDelayedList adds the task to delayed or
7680 * suspended list. Note that this operation does not nullify
7681 * the add to pendingReady list done in the above step because
7682 * a different list item, namely xEventListItem, is used for
7683 * adding the task to the pendingReady list. In other words,
7684 * the task still remains on the pendingReady list.
7685 * 5. Resume scheduler moves the task from pendingReady list to
7690 taskEXIT_CRITICAL();
7692 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7694 xAlreadyYielded = xTaskResumeAll();
7696 if( xAlreadyYielded == pdFALSE )
7698 taskYIELD_WITHIN_API();
7702 mtCOVERAGE_TEST_MARKER();
7707 taskEXIT_CRITICAL();
7712 taskEXIT_CRITICAL();
7715 taskENTER_CRITICAL();
7717 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7718 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7720 if( ulReturn != 0UL )
7722 if( xClearCountOnExit != pdFALSE )
7724 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ( uint32_t ) 0UL;
7728 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7733 mtCOVERAGE_TEST_MARKER();
7736 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7738 taskEXIT_CRITICAL();
7740 traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7745 #endif /* configUSE_TASK_NOTIFICATIONS */
7746 /*-----------------------------------------------------------*/
7748 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7750 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7751 uint32_t ulBitsToClearOnEntry,
7752 uint32_t ulBitsToClearOnExit,
7753 uint32_t * pulNotificationValue,
7754 TickType_t xTicksToWait )
7756 BaseType_t xReturn, xAlreadyYielded;
7758 traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7760 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7762 taskENTER_CRITICAL();
7764 /* Only block if a notification is not already pending. */
7765 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7767 /* Clear bits in the task's notification value as bits may get
7768 * set by the notifying task or interrupt. This can be used to
7769 * clear the value to zero. */
7770 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7772 /* Mark this task as waiting for a notification. */
7773 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7775 if( xTicksToWait > ( TickType_t ) 0 )
7777 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7779 /* We MUST suspend the scheduler before exiting the critical
7780 * section (i.e. before enabling interrupts).
7782 * If we do not do so, a notification sent from an ISR, which
7783 * happens after exiting the critical section and before
7784 * suspending the scheduler, will get lost. The sequence of
7786 * 1. Exit critical section.
7787 * 2. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7788 * task to the Ready list.
7789 * 3. Suspend scheduler.
7790 * 4. prvAddCurrentTaskToDelayedList moves the task to the
7791 * delayed or suspended list.
7792 * 5. Resume scheduler does not touch the task (because it is
7793 * not on the pendingReady list), effectively losing the
7794 * notification from the ISR.
7796 * The same does not happen when we suspend the scheduler before
7797 * exiting the critical section. The sequence of events in this
7799 * 1. Suspend scheduler.
7800 * 2. Exit critical section.
7801 * 3. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7802 * task to the pendingReady list as the scheduler is
7804 * 4. prvAddCurrentTaskToDelayedList adds the task to delayed or
7805 * suspended list. Note that this operation does not nullify
7806 * the add to pendingReady list done in the above step because
7807 * a different list item, namely xEventListItem, is used for
7808 * adding the task to the pendingReady list. In other words,
7809 * the task still remains on the pendingReady list.
7810 * 5. Resume scheduler moves the task from pendingReady list to
7815 taskEXIT_CRITICAL();
7817 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7819 xAlreadyYielded = xTaskResumeAll();
7821 if( xAlreadyYielded == pdFALSE )
7823 taskYIELD_WITHIN_API();
7827 mtCOVERAGE_TEST_MARKER();
7832 taskEXIT_CRITICAL();
7837 taskEXIT_CRITICAL();
7840 taskENTER_CRITICAL();
7842 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7844 if( pulNotificationValue != NULL )
7846 /* Output the current notification value, which may or may not
7848 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7851 /* If ucNotifyValue is set then either the task never entered the
7852 * blocked state (because a notification was already pending) or the
7853 * task unblocked because of a notification. Otherwise the task
7854 * unblocked because of a timeout. */
7855 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7857 /* A notification was not received. */
7862 /* A notification was already pending or a notification was
7863 * received while the task was waiting. */
7864 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7868 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7870 taskEXIT_CRITICAL();
7872 traceRETURN_xTaskGenericNotifyWait( xReturn );
7877 #endif /* configUSE_TASK_NOTIFICATIONS */
7878 /*-----------------------------------------------------------*/
7880 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7882 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7883 UBaseType_t uxIndexToNotify,
7885 eNotifyAction eAction,
7886 uint32_t * pulPreviousNotificationValue )
7889 BaseType_t xReturn = pdPASS;
7890 uint8_t ucOriginalNotifyState;
7892 traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7894 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7895 configASSERT( xTaskToNotify );
7896 pxTCB = xTaskToNotify;
7898 taskENTER_CRITICAL();
7900 if( pulPreviousNotificationValue != NULL )
7902 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7905 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7907 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7912 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7916 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7919 case eSetValueWithOverwrite:
7920 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7923 case eSetValueWithoutOverwrite:
7925 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7927 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7931 /* The value could not be written to the task. */
7939 /* The task is being notified without its notify value being
7945 /* Should not get here if all enums are handled.
7946 * Artificially force an assert by testing a value the
7947 * compiler can't assume is const. */
7948 configASSERT( xTickCount == ( TickType_t ) 0 );
7953 traceTASK_NOTIFY( uxIndexToNotify );
7955 /* If the task is in the blocked state specifically to wait for a
7956 * notification then unblock it now. */
7957 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7959 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7960 prvAddTaskToReadyList( pxTCB );
7962 /* The task should not have been on an event list. */
7963 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7965 #if ( configUSE_TICKLESS_IDLE != 0 )
7967 /* If a task is blocked waiting for a notification then
7968 * xNextTaskUnblockTime might be set to the blocked task's time
7969 * out time. If the task is unblocked for a reason other than
7970 * a timeout xNextTaskUnblockTime is normally left unchanged,
7971 * because it will automatically get reset to a new value when
7972 * the tick count equals xNextTaskUnblockTime. However if
7973 * tickless idling is used it might be more important to enter
7974 * sleep mode at the earliest possible time - so reset
7975 * xNextTaskUnblockTime here to ensure it is updated at the
7976 * earliest possible time. */
7977 prvResetNextTaskUnblockTime();
7981 /* Check if the notified task has a priority above the currently
7982 * executing task. */
7983 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7987 mtCOVERAGE_TEST_MARKER();
7990 taskEXIT_CRITICAL();
7992 traceRETURN_xTaskGenericNotify( xReturn );
7997 #endif /* configUSE_TASK_NOTIFICATIONS */
7998 /*-----------------------------------------------------------*/
8000 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8002 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
8003 UBaseType_t uxIndexToNotify,
8005 eNotifyAction eAction,
8006 uint32_t * pulPreviousNotificationValue,
8007 BaseType_t * pxHigherPriorityTaskWoken )
8010 uint8_t ucOriginalNotifyState;
8011 BaseType_t xReturn = pdPASS;
8012 UBaseType_t uxSavedInterruptStatus;
8014 traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
8016 configASSERT( xTaskToNotify );
8017 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8019 /* RTOS ports that support interrupt nesting have the concept of a
8020 * maximum system call (or maximum API call) interrupt priority.
8021 * Interrupts that are above the maximum system call priority are keep
8022 * permanently enabled, even when the RTOS kernel is in a critical section,
8023 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
8024 * is defined in FreeRTOSConfig.h then
8025 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
8026 * failure if a FreeRTOS API function is called from an interrupt that has
8027 * been assigned a priority above the configured maximum system call
8028 * priority. Only FreeRTOS functions that end in FromISR can be called
8029 * from interrupts that have been assigned a priority at or (logically)
8030 * below the maximum system call interrupt priority. FreeRTOS maintains a
8031 * separate interrupt safe API to ensure interrupt entry is as fast and as
8032 * simple as possible. More information (albeit Cortex-M specific) is
8033 * provided on the following link:
8034 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
8035 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
8037 pxTCB = xTaskToNotify;
8039 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
8041 if( pulPreviousNotificationValue != NULL )
8043 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
8046 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8047 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8052 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
8056 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8059 case eSetValueWithOverwrite:
8060 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8063 case eSetValueWithoutOverwrite:
8065 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
8067 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8071 /* The value could not be written to the task. */
8079 /* The task is being notified without its notify value being
8085 /* Should not get here if all enums are handled.
8086 * Artificially force an assert by testing a value the
8087 * compiler can't assume is const. */
8088 configASSERT( xTickCount == ( TickType_t ) 0 );
8092 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
8094 /* If the task is in the blocked state specifically to wait for a
8095 * notification then unblock it now. */
8096 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8098 /* The task should not have been on an event list. */
8099 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8101 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8103 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8104 prvAddTaskToReadyList( pxTCB );
8108 /* The delayed and ready lists cannot be accessed, so hold
8109 * this task pending until the scheduler is resumed. */
8110 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8113 #if ( configNUMBER_OF_CORES == 1 )
8115 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8117 /* The notified task has a priority above the currently
8118 * executing task so a yield is required. */
8119 if( pxHigherPriorityTaskWoken != NULL )
8121 *pxHigherPriorityTaskWoken = pdTRUE;
8124 /* Mark that a yield is pending in case the user is not
8125 * using the "xHigherPriorityTaskWoken" parameter to an ISR
8126 * safe FreeRTOS function. */
8127 xYieldPendings[ 0 ] = pdTRUE;
8131 mtCOVERAGE_TEST_MARKER();
8134 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8136 #if ( configUSE_PREEMPTION == 1 )
8138 prvYieldForTask( pxTCB );
8140 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8142 if( pxHigherPriorityTaskWoken != NULL )
8144 *pxHigherPriorityTaskWoken = pdTRUE;
8148 #endif /* if ( configUSE_PREEMPTION == 1 ) */
8150 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8153 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8155 traceRETURN_xTaskGenericNotifyFromISR( xReturn );
8160 #endif /* configUSE_TASK_NOTIFICATIONS */
8161 /*-----------------------------------------------------------*/
8163 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8165 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
8166 UBaseType_t uxIndexToNotify,
8167 BaseType_t * pxHigherPriorityTaskWoken )
8170 uint8_t ucOriginalNotifyState;
8171 UBaseType_t uxSavedInterruptStatus;
8173 traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
8175 configASSERT( xTaskToNotify );
8176 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8178 /* RTOS ports that support interrupt nesting have the concept of a
8179 * maximum system call (or maximum API call) interrupt priority.
8180 * Interrupts that are above the maximum system call priority are keep
8181 * permanently enabled, even when the RTOS kernel is in a critical section,
8182 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
8183 * is defined in FreeRTOSConfig.h then
8184 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
8185 * failure if a FreeRTOS API function is called from an interrupt that has
8186 * been assigned a priority above the configured maximum system call
8187 * priority. Only FreeRTOS functions that end in FromISR can be called
8188 * from interrupts that have been assigned a priority at or (logically)
8189 * below the maximum system call interrupt priority. FreeRTOS maintains a
8190 * separate interrupt safe API to ensure interrupt entry is as fast and as
8191 * simple as possible. More information (albeit Cortex-M specific) is
8192 * provided on the following link:
8193 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
8194 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
8196 pxTCB = xTaskToNotify;
8198 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
8200 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8201 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8203 /* 'Giving' is equivalent to incrementing a count in a counting
8205 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8207 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
8209 /* If the task is in the blocked state specifically to wait for a
8210 * notification then unblock it now. */
8211 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8213 /* The task should not have been on an event list. */
8214 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8216 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8218 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8219 prvAddTaskToReadyList( pxTCB );
8223 /* The delayed and ready lists cannot be accessed, so hold
8224 * this task pending until the scheduler is resumed. */
8225 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8228 #if ( configNUMBER_OF_CORES == 1 )
8230 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8232 /* The notified task has a priority above the currently
8233 * executing task so a yield is required. */
8234 if( pxHigherPriorityTaskWoken != NULL )
8236 *pxHigherPriorityTaskWoken = pdTRUE;
8239 /* Mark that a yield is pending in case the user is not
8240 * using the "xHigherPriorityTaskWoken" parameter in an ISR
8241 * safe FreeRTOS function. */
8242 xYieldPendings[ 0 ] = pdTRUE;
8246 mtCOVERAGE_TEST_MARKER();
8249 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8251 #if ( configUSE_PREEMPTION == 1 )
8253 prvYieldForTask( pxTCB );
8255 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8257 if( pxHigherPriorityTaskWoken != NULL )
8259 *pxHigherPriorityTaskWoken = pdTRUE;
8263 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
8265 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8268 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8270 traceRETURN_vTaskGenericNotifyGiveFromISR();
8273 #endif /* configUSE_TASK_NOTIFICATIONS */
8274 /*-----------------------------------------------------------*/
8276 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8278 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
8279 UBaseType_t uxIndexToClear )
8284 traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
8286 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8288 /* If null is passed in here then it is the calling task that is having
8289 * its notification state cleared. */
8290 pxTCB = prvGetTCBFromHandle( xTask );
8292 taskENTER_CRITICAL();
8294 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
8296 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
8304 taskEXIT_CRITICAL();
8306 traceRETURN_xTaskGenericNotifyStateClear( xReturn );
8311 #endif /* configUSE_TASK_NOTIFICATIONS */
8312 /*-----------------------------------------------------------*/
8314 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8316 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
8317 UBaseType_t uxIndexToClear,
8318 uint32_t ulBitsToClear )
8323 traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
8325 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8327 /* If null is passed in here then it is the calling task that is having
8328 * its notification state cleared. */
8329 pxTCB = prvGetTCBFromHandle( xTask );
8331 taskENTER_CRITICAL();
8333 /* Return the notification as it was before the bits were cleared,
8334 * then clear the bit mask. */
8335 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
8336 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
8338 taskEXIT_CRITICAL();
8340 traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
8345 #endif /* configUSE_TASK_NOTIFICATIONS */
8346 /*-----------------------------------------------------------*/
8348 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8350 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
8354 traceENTER_ulTaskGetRunTimeCounter( xTask );
8356 pxTCB = prvGetTCBFromHandle( xTask );
8358 traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
8360 return pxTCB->ulRunTimeCounter;
8363 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8364 /*-----------------------------------------------------------*/
8366 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8368 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
8371 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8373 traceENTER_ulTaskGetRunTimePercent( xTask );
8375 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
8377 /* For percentage calculations. */
8378 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8380 /* Avoid divide by zero errors. */
8381 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8383 pxTCB = prvGetTCBFromHandle( xTask );
8384 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
8391 traceRETURN_ulTaskGetRunTimePercent( ulReturn );
8396 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8397 /*-----------------------------------------------------------*/
8399 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8401 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
8403 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
8406 traceENTER_ulTaskGetIdleRunTimeCounter();
8408 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8410 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8413 traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
8418 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8419 /*-----------------------------------------------------------*/
8421 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8423 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
8425 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8426 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
8429 traceENTER_ulTaskGetIdleRunTimePercent();
8431 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
8433 /* For percentage calculations. */
8434 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8436 /* Avoid divide by zero errors. */
8437 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8439 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8441 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8444 ulReturn = ulRunTimeCounter / ulTotalTime;
8451 traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
8456 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8457 /*-----------------------------------------------------------*/
8459 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
8460 const BaseType_t xCanBlockIndefinitely )
8462 TickType_t xTimeToWake;
8463 const TickType_t xConstTickCount = xTickCount;
8464 List_t * const pxDelayedList = pxDelayedTaskList;
8465 List_t * const pxOverflowDelayedList = pxOverflowDelayedTaskList;
8467 #if ( INCLUDE_xTaskAbortDelay == 1 )
8469 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
8470 * reset to pdFALSE so it can be detected as having been set to pdTRUE
8471 * when the task leaves the Blocked state. */
8472 pxCurrentTCB->ucDelayAborted = pdFALSE;
8476 /* Remove the task from the ready list before adding it to the blocked list
8477 * as the same list item is used for both lists. */
8478 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
8480 /* The current task must be in a ready list, so there is no need to
8481 * check, and the port reset macro can be called directly. */
8482 portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
8486 mtCOVERAGE_TEST_MARKER();
8489 #if ( INCLUDE_vTaskSuspend == 1 )
8491 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
8493 /* Add the task to the suspended task list instead of a delayed task
8494 * list to ensure it is not woken by a timing event. It will block
8496 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
8500 /* Calculate the time at which the task should be woken if the event
8501 * does not occur. This may overflow but this doesn't matter, the
8502 * kernel will manage it correctly. */
8503 xTimeToWake = xConstTickCount + xTicksToWait;
8505 /* The list item will be inserted in wake time order. */
8506 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8508 if( xTimeToWake < xConstTickCount )
8510 /* Wake time has overflowed. Place this item in the overflow
8512 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8513 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8517 /* The wake time has not overflowed, so the current block list
8519 traceMOVED_TASK_TO_DELAYED_LIST();
8520 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8522 /* If the task entering the blocked state was placed at the
8523 * head of the list of blocked tasks then xNextTaskUnblockTime
8524 * needs to be updated too. */
8525 if( xTimeToWake < xNextTaskUnblockTime )
8527 xNextTaskUnblockTime = xTimeToWake;
8531 mtCOVERAGE_TEST_MARKER();
8536 #else /* INCLUDE_vTaskSuspend */
8538 /* Calculate the time at which the task should be woken if the event
8539 * does not occur. This may overflow but this doesn't matter, the kernel
8540 * will manage it correctly. */
8541 xTimeToWake = xConstTickCount + xTicksToWait;
8543 /* The list item will be inserted in wake time order. */
8544 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8546 if( xTimeToWake < xConstTickCount )
8548 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8549 /* Wake time has overflowed. Place this item in the overflow list. */
8550 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8554 traceMOVED_TASK_TO_DELAYED_LIST();
8555 /* The wake time has not overflowed, so the current block list is used. */
8556 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8558 /* If the task entering the blocked state was placed at the head of the
8559 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
8561 if( xTimeToWake < xNextTaskUnblockTime )
8563 xNextTaskUnblockTime = xTimeToWake;
8567 mtCOVERAGE_TEST_MARKER();
8571 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
8572 ( void ) xCanBlockIndefinitely;
8574 #endif /* INCLUDE_vTaskSuspend */
8576 /*-----------------------------------------------------------*/
8578 #if ( portUSING_MPU_WRAPPERS == 1 )
8580 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
8584 traceENTER_xTaskGetMPUSettings( xTask );
8586 pxTCB = prvGetTCBFromHandle( xTask );
8588 traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
8590 return &( pxTCB->xMPUSettings );
8593 #endif /* portUSING_MPU_WRAPPERS */
8594 /*-----------------------------------------------------------*/
8596 /* Code below here allows additional code to be inserted into this source file,
8597 * especially where access to file scope functions and data is needed (for example
8598 * when performing module tests). */
8600 #ifdef FREERTOS_MODULE_TEST
8601 #include "tasks_test_access_functions.h"
8605 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
8607 #include "freertos_tasks_c_additions.h"
8609 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8610 static void freertos_tasks_c_additions_init( void )
8612 FREERTOS_TASKS_C_ADDITIONS_INIT();
8616 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8617 /*-----------------------------------------------------------*/
8619 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8622 * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8623 * to provide the memory that is used by the Idle task. It is used when
8624 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8625 * it's own implementation of vApplicationGetIdleTaskMemory by setting
8626 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8628 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8629 StackType_t ** ppxIdleTaskStackBuffer,
8630 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize )
8632 static StaticTask_t xIdleTaskTCB;
8633 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8635 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8636 *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8637 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8640 #if ( configNUMBER_OF_CORES > 1 )
8642 void vApplicationGetPassiveIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8643 StackType_t ** ppxIdleTaskStackBuffer,
8644 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize,
8645 BaseType_t xPassiveIdleTaskIndex )
8647 static StaticTask_t xIdleTaskTCBs[ configNUMBER_OF_CORES - 1 ];
8648 static StackType_t uxIdleTaskStacks[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
8650 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCBs[ xPassiveIdleTaskIndex ] );
8651 *ppxIdleTaskStackBuffer = &( uxIdleTaskStacks[ xPassiveIdleTaskIndex ][ 0 ] );
8652 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8655 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
8657 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8658 /*-----------------------------------------------------------*/
8660 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8663 * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8664 * to provide the memory that is used by the Timer service task. It is used when
8665 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8666 * it's own implementation of vApplicationGetTimerTaskMemory by setting
8667 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8669 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8670 StackType_t ** ppxTimerTaskStackBuffer,
8671 configSTACK_DEPTH_TYPE * puxTimerTaskStackSize )
8673 static StaticTask_t xTimerTaskTCB;
8674 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8676 *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8677 *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8678 *puxTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8681 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8682 /*-----------------------------------------------------------*/