2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /* Standard includes. */
33 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
34 * all the API functions to use the MPU wrappers. That should only be done when
35 * task.h is included from an application file. */
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
38 /* FreeRTOS includes. */
42 #include "stack_macros.h"
44 /* The default definitions are only available for non-MPU ports. The
45 * reason is that the stack alignment requirements vary for different
47 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS != 0 ) )
48 #error configKERNEL_PROVIDED_STATIC_MEMORY cannot be set to 1 when using an MPU port. The vApplicationGet*TaskMemory() functions must be provided manually.
51 /* The MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
52 * for the header files above, but not in this file, in order to generate the
53 * correct privileged Vs unprivileged linkage and placement. */
54 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
56 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
57 * functions but without including stdio.h here. */
58 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
60 /* At the bottom of this file are two optional functions that can be used
61 * to generate human readable text from the raw data generated by the
62 * uxTaskGetSystemState() function. Note the formatting functions are provided
63 * for convenience only, and are NOT considered part of the kernel. */
65 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
67 #if ( configUSE_PREEMPTION == 0 )
69 /* If the cooperative scheduler is being used then a yield should not be
70 * performed just because a higher priority task has been woken. */
71 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB )
72 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB )
75 #if ( configNUMBER_OF_CORES == 1 )
77 /* This macro requests the running task pxTCB to yield. In single core
78 * scheduler, a running task always runs on core 0 and portYIELD_WITHIN_API()
79 * can be used to request the task running on core 0 to yield. Therefore, pxTCB
80 * is not used in this macro. */
81 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) \
84 portYIELD_WITHIN_API(); \
87 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \
89 if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \
91 portYIELD_WITHIN_API(); \
95 mtCOVERAGE_TEST_MARKER(); \
99 #else /* if ( configNUMBER_OF_CORES == 1 ) */
101 /* Yield the core on which this task is running. */
102 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldCore( ( pxTCB )->xTaskRunState )
104 /* Yield for the task if a running task has priority lower than this task. */
105 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldForTask( pxTCB )
107 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
109 #endif /* if ( configUSE_PREEMPTION == 0 ) */
111 /* Values that can be assigned to the ucNotifyState member of the TCB. */
112 #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
113 #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
114 #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
117 * The value used to fill the stack of a task when the task is created. This
118 * is used purely for checking the high water mark for tasks.
120 #define tskSTACK_FILL_BYTE ( 0xa5U )
122 /* Bits used to record how a task's stack and TCB were allocated. */
123 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
124 #define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
125 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
127 /* If any of the following are set then task stacks are filled with a known
128 * value so the high water mark can be determined. If none of the following are
129 * set then don't fill the stack so there is no unnecessary dependency on memset. */
130 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
131 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
133 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
137 * Macros used by vListTask to indicate which state a task is in.
139 #define tskRUNNING_CHAR ( 'X' )
140 #define tskBLOCKED_CHAR ( 'B' )
141 #define tskREADY_CHAR ( 'R' )
142 #define tskDELETED_CHAR ( 'D' )
143 #define tskSUSPENDED_CHAR ( 'S' )
146 * Some kernel aware debuggers require the data the debugger needs access to be
147 * global, rather than file scope.
149 #ifdef portREMOVE_STATIC_QUALIFIER
153 /* The name allocated to the Idle task. This can be overridden by defining
154 * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
155 #ifndef configIDLE_TASK_NAME
156 #define configIDLE_TASK_NAME "IDLE"
159 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
161 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
162 * performed in a generic way that is not optimised to any particular
163 * microcontroller architecture. */
165 /* uxTopReadyPriority holds the priority of the highest priority ready
167 #define taskRECORD_READY_PRIORITY( uxPriority ) \
169 if( ( uxPriority ) > uxTopReadyPriority ) \
171 uxTopReadyPriority = ( uxPriority ); \
173 } while( 0 ) /* taskRECORD_READY_PRIORITY */
175 /*-----------------------------------------------------------*/
177 #if ( configNUMBER_OF_CORES == 1 )
178 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
180 UBaseType_t uxTopPriority = uxTopReadyPriority; \
182 /* Find the highest priority queue that contains ready tasks. */ \
183 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) != pdFALSE ) \
185 configASSERT( uxTopPriority ); \
189 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
190 * the same priority get an equal share of the processor time. */ \
191 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
192 uxTopReadyPriority = uxTopPriority; \
193 } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */
194 #else /* if ( configNUMBER_OF_CORES == 1 ) */
196 #define taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID ) prvSelectHighestPriorityTask( xCoreID )
198 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
200 /*-----------------------------------------------------------*/
202 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
203 * they are only required when a port optimised method of task selection is
205 #define taskRESET_READY_PRIORITY( uxPriority )
206 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
208 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
210 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
211 * performed in a way that is tailored to the particular microcontroller
212 * architecture being used. */
214 /* A port optimised version is provided. Call the port defined macros. */
215 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( ( uxPriority ), uxTopReadyPriority )
217 /*-----------------------------------------------------------*/
219 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
221 UBaseType_t uxTopPriority; \
223 /* Find the highest priority list that contains ready tasks. */ \
224 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
225 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
226 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
229 /*-----------------------------------------------------------*/
231 /* A port optimised version is provided, call it only if the TCB being reset
232 * is being referenced from a ready list. If it is referenced from a delayed
233 * or suspended list then it won't be in a ready list. */
234 #define taskRESET_READY_PRIORITY( uxPriority ) \
236 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
238 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
242 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
244 /*-----------------------------------------------------------*/
246 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
247 * count overflows. */
248 #define taskSWITCH_DELAYED_LISTS() \
252 /* The delayed tasks list should be empty when the lists are switched. */ \
253 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
255 pxTemp = pxDelayedTaskList; \
256 pxDelayedTaskList = pxOverflowDelayedTaskList; \
257 pxOverflowDelayedTaskList = pxTemp; \
258 xNumOfOverflows += ( BaseType_t ) 1; \
259 prvResetNextTaskUnblockTime(); \
262 /*-----------------------------------------------------------*/
265 * Place the task represented by pxTCB into the appropriate ready list for
266 * the task. It is inserted at the end of the list.
268 #define prvAddTaskToReadyList( pxTCB ) \
270 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
271 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
272 listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
273 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB ); \
275 /*-----------------------------------------------------------*/
278 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
279 * where NULL is used to indicate that the handle of the currently executing
280 * task should be used in place of the parameter. This macro simply checks to
281 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
283 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
285 /* The item value of the event list item is normally used to hold the priority
286 * of the task to which it belongs (coded to allow it to be held in reverse
287 * priority order). However, it is occasionally borrowed for other purposes. It
288 * is important its value is not updated due to a task priority change while it is
289 * being used for another purpose. The following bit definition is used to inform
290 * the scheduler that the value should not be changed - in which case it is the
291 * responsibility of whichever module is using the value to ensure it gets set back
292 * to its original value when it is released. */
293 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
294 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint16_t ) 0x8000U )
295 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
296 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint32_t ) 0x80000000UL )
297 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
298 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint64_t ) 0x8000000000000000ULL )
301 /* Indicates that the task is not actively running on any core. */
302 #define taskTASK_NOT_RUNNING ( ( BaseType_t ) ( -1 ) )
304 /* Indicates that the task is actively running but scheduled to yield. */
305 #define taskTASK_SCHEDULED_TO_YIELD ( ( BaseType_t ) ( -2 ) )
307 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
308 #if ( configNUMBER_OF_CORES == 1 )
309 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
310 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
312 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) )
313 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB )->xTaskRunState != taskTASK_NOT_RUNNING ) ? ( pdTRUE ) : ( pdFALSE ) )
316 /* Indicates that the task is an Idle task. */
317 #define taskATTRIBUTE_IS_IDLE ( UBaseType_t ) ( 1UL << 0UL )
319 #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) )
320 #define portGET_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting )
321 #define portSET_CRITICAL_NESTING_COUNT( x ) ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting = ( x ) )
322 #define portINCREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting++ )
323 #define portDECREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting-- )
324 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) */
326 #define taskBITS_PER_BYTE ( ( size_t ) 8 )
328 #if ( configNUMBER_OF_CORES > 1 )
330 /* Yields the given core. This must be called from a critical section and xCoreID
331 * must be valid. This macro is not required in single core since there is only
332 * one core to yield. */
333 #define prvYieldCore( xCoreID ) \
335 if( ( xCoreID ) == ( BaseType_t ) portGET_CORE_ID() ) \
337 /* Pending a yield for this core since it is in the critical section. */ \
338 xYieldPendings[ ( xCoreID ) ] = pdTRUE; \
342 /* Request other core to yield if it is not requested before. */ \
343 if( pxCurrentTCBs[ ( xCoreID ) ]->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD ) \
345 portYIELD_CORE( xCoreID ); \
346 pxCurrentTCBs[ ( xCoreID ) ]->xTaskRunState = taskTASK_SCHEDULED_TO_YIELD; \
350 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
351 /*-----------------------------------------------------------*/
354 * Task control block. A task control block (TCB) is allocated for each task,
355 * and stores task state information, including a pointer to the task's context
356 * (the task's run time environment, including register values)
358 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
360 volatile StackType_t * pxTopOfStack; /**< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
362 #if ( portUSING_MPU_WRAPPERS == 1 )
363 xMPU_SETTINGS xMPUSettings; /**< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
366 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
367 UBaseType_t uxCoreAffinityMask; /**< Used to link the task to certain cores. UBaseType_t must have greater than or equal to the number of bits as configNUMBER_OF_CORES. */
370 ListItem_t xStateListItem; /**< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
371 ListItem_t xEventListItem; /**< Used to reference a task from an event list. */
372 UBaseType_t uxPriority; /**< The priority of the task. 0 is the lowest priority. */
373 StackType_t * pxStack; /**< Points to the start of the stack. */
374 #if ( configNUMBER_OF_CORES > 1 )
375 volatile BaseType_t xTaskRunState; /**< Used to identify the core the task is running on, if the task is running. Otherwise, identifies the task's state - not running or yielding. */
376 UBaseType_t uxTaskAttributes; /**< Task's attributes - currently used to identify the idle tasks. */
378 char pcTaskName[ configMAX_TASK_NAME_LEN ]; /**< Descriptive name given to the task when created. Facilitates debugging only. */
380 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
381 BaseType_t xPreemptionDisable; /**< Used to prevent the task from being preempted. */
384 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
385 StackType_t * pxEndOfStack; /**< Points to the highest valid address for the stack. */
388 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
389 UBaseType_t uxCriticalNesting; /**< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
392 #if ( configUSE_TRACE_FACILITY == 1 )
393 UBaseType_t uxTCBNumber; /**< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
394 UBaseType_t uxTaskNumber; /**< Stores a number specifically for use by third party trace code. */
397 #if ( configUSE_MUTEXES == 1 )
398 UBaseType_t uxBasePriority; /**< The priority last assigned to the task - used by the priority inheritance mechanism. */
399 UBaseType_t uxMutexesHeld;
402 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
403 TaskHookFunction_t pxTaskTag;
406 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
407 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
410 #if ( configGENERATE_RUN_TIME_STATS == 1 )
411 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /**< Stores the amount of time the task has spent in the Running state. */
414 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
415 configTLS_BLOCK_TYPE xTLSBlock; /**< Memory block used as Thread Local Storage (TLS) Block for the task. */
418 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
419 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
420 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
423 /* See the comments in FreeRTOS.h with the definition of
424 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
425 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
426 uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
429 #if ( INCLUDE_xTaskAbortDelay == 1 )
430 uint8_t ucDelayAborted;
433 #if ( configUSE_POSIX_ERRNO == 1 )
438 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
439 * below to enable the use of older kernel aware debuggers. */
440 typedef tskTCB TCB_t;
442 #if ( configNUMBER_OF_CORES == 1 )
443 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
444 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
445 /* coverity[misra_c_2012_rule_8_4_violation] */
446 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
448 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
449 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
450 /* coverity[misra_c_2012_rule_8_4_violation] */
451 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ];
452 #define pxCurrentTCB xTaskGetCurrentTaskHandle()
455 /* Lists for ready and blocked tasks. --------------------
456 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
457 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
458 * the static qualifier. */
459 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
460 PRIVILEGED_DATA static List_t xDelayedTaskList1; /**< Delayed tasks. */
461 PRIVILEGED_DATA static List_t xDelayedTaskList2; /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
462 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /**< Points to the delayed task list currently being used. */
463 PRIVILEGED_DATA static List_t * volatile pxOverflowDelayedTaskList; /**< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
464 PRIVILEGED_DATA static List_t xPendingReadyList; /**< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready list when the scheduler is resumed. */
466 #if ( INCLUDE_vTaskDelete == 1 )
468 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
469 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
473 #if ( INCLUDE_vTaskSuspend == 1 )
475 PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
479 /* Global POSIX errno. Its value is changed upon context switching to match
480 * the errno of the currently running task. */
481 #if ( configUSE_POSIX_ERRNO == 1 )
482 int FreeRTOS_errno = 0;
485 /* Other file private variables. --------------------------------*/
486 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
487 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
488 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
489 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
490 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
491 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
492 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
493 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
494 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
495 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandles[ configNUMBER_OF_CORES ]; /**< Holds the handles of the idle tasks. The idle tasks are created automatically when the scheduler is started. */
497 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
498 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
499 * to determine the number of priority lists to read back from the remote target. */
500 static const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
502 /* Context switches are held pending while the scheduler is suspended. Also,
503 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
504 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
505 * If an interrupt needs to unblock a task while the scheduler is suspended then it
506 * moves the task's event list item into the xPendingReadyList, ready for the
507 * kernel to move the task from the pending ready list into the real ready list
508 * when the scheduler is unsuspended. The pending ready list itself can only be
509 * accessed from a critical section.
511 * Updates to uxSchedulerSuspended must be protected by both the task lock and the ISR lock
512 * and must not be done from an ISR. Reads must be protected by either lock and may be done
513 * from either an ISR or a task. */
514 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
516 #if ( configGENERATE_RUN_TIME_STATS == 1 )
518 /* Do not move these variables to function scope as doing so prevents the
519 * code working with debuggers that need to remove the static qualifier. */
520 PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the value of a timer/counter the last time a task was switched in. */
521 PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the total amount of execution time as defined by the run time counter clock. */
525 /*-----------------------------------------------------------*/
527 /* File private functions. --------------------------------*/
530 * Creates the idle tasks during scheduler start.
532 static BaseType_t prvCreateIdleTasks( void );
534 #if ( configNUMBER_OF_CORES > 1 )
537 * Checks to see if another task moved the current task out of the ready
538 * list while it was waiting to enter a critical section and yields, if so.
540 static void prvCheckForRunStateChange( void );
541 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
543 #if ( configNUMBER_OF_CORES > 1 )
546 * Yields a core, or cores if multiple priorities are not allowed to run
547 * simultaneously, to allow the task pxTCB to run.
549 static void prvYieldForTask( const TCB_t * pxTCB );
550 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
552 #if ( configNUMBER_OF_CORES > 1 )
555 * Selects the highest priority available task for the given core.
557 static void prvSelectHighestPriorityTask( BaseType_t xCoreID );
558 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
561 * Utility task that simply returns pdTRUE if the task referenced by xTask is
562 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
563 * is in any other state.
565 #if ( INCLUDE_vTaskSuspend == 1 )
567 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
569 #endif /* INCLUDE_vTaskSuspend */
572 * Utility to ready all the lists used by the scheduler. This is called
573 * automatically upon the creation of the first task.
575 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
578 * The idle task, which as all tasks is implemented as a never ending loop.
579 * The idle task is automatically created and added to the ready lists upon
580 * creation of the first user task.
582 * In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks are also
583 * created to ensure that each core has an idle task to run when no other
584 * task is available to run.
586 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
587 * language extensions. The equivalent prototype for these functions are:
589 * void prvIdleTask( void *pvParameters );
590 * void prvPassiveIdleTask( void *pvParameters );
593 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
594 #if ( configNUMBER_OF_CORES > 1 )
595 static portTASK_FUNCTION_PROTO( prvPassiveIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
599 * Utility to free all memory allocated by the scheduler to hold a TCB,
600 * including the stack pointed to by the TCB.
602 * This does not free memory allocated by the task itself (i.e. memory
603 * allocated by calls to pvPortMalloc from within the tasks application code).
605 #if ( INCLUDE_vTaskDelete == 1 )
607 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
612 * Used only by the idle task. This checks to see if anything has been placed
613 * in the list of tasks waiting to be deleted. If so the task is cleaned up
614 * and its TCB deleted.
616 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
619 * The currently executing task is entering the Blocked state. Add the task to
620 * either the current or the overflow delayed task list.
622 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
623 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
626 * Fills an TaskStatus_t structure with information on each task that is
627 * referenced from the pxList list (which may be a ready list, a delayed list,
628 * a suspended list, etc.).
630 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
631 * NORMAL APPLICATION CODE.
633 #if ( configUSE_TRACE_FACILITY == 1 )
635 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
637 eTaskState eState ) PRIVILEGED_FUNCTION;
642 * Searches pxList for a task with name pcNameToQuery - returning a handle to
643 * the task if it is found, or NULL if the task is not found.
645 #if ( INCLUDE_xTaskGetHandle == 1 )
647 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
648 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
653 * When a task is created, the stack of the task is filled with a known value.
654 * This function determines the 'high water mark' of the task stack by
655 * determining how much of the stack remains at the original preset value.
657 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
659 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
664 * Return the amount of time, in ticks, that will pass before the kernel will
665 * next move a task from the Blocked state to the Running state.
667 * This conditional compilation should use inequality to 0, not equality to 1.
668 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
669 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
670 * set to a value other than 1.
672 #if ( configUSE_TICKLESS_IDLE != 0 )
674 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
679 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
680 * will exit the Blocked state.
682 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
684 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
687 * Helper function used to pad task names with spaces when printing out
688 * human readable tables of task information.
690 static char * prvWriteNameToBuffer( char * pcBuffer,
691 const char * pcTaskName ) PRIVILEGED_FUNCTION;
696 * Called after a Task_t structure has been allocated either statically or
697 * dynamically to fill in the structure's members.
699 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
700 const char * const pcName,
701 const configSTACK_DEPTH_TYPE uxStackDepth,
702 void * const pvParameters,
703 UBaseType_t uxPriority,
704 TaskHandle_t * const pxCreatedTask,
706 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
709 * Called after a new task has been created and initialised to place the task
710 * under the control of the scheduler.
712 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
715 * Create a task with static buffer for both TCB and stack. Returns a handle to
716 * the task if it is created successfully. Otherwise, returns NULL.
718 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
719 static TCB_t * prvCreateStaticTask( TaskFunction_t pxTaskCode,
720 const char * const pcName,
721 const configSTACK_DEPTH_TYPE uxStackDepth,
722 void * const pvParameters,
723 UBaseType_t uxPriority,
724 StackType_t * const puxStackBuffer,
725 StaticTask_t * const pxTaskBuffer,
726 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
727 #endif /* #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
730 * Create a restricted task with static buffer for both TCB and stack. Returns
731 * a handle to the task if it is created successfully. Otherwise, returns NULL.
733 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
734 static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
735 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
736 #endif /* #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
739 * Create a restricted task with static buffer for task stack and allocated buffer
740 * for TCB. Returns a handle to the task if it is created successfully. Otherwise,
743 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
744 static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
745 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
746 #endif /* #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
749 * Create a task with allocated buffer for both TCB and stack. Returns a handle to
750 * the task if it is created successfully. Otherwise, returns NULL.
752 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
753 static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
754 const char * const pcName,
755 const configSTACK_DEPTH_TYPE uxStackDepth,
756 void * const pvParameters,
757 UBaseType_t uxPriority,
758 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
759 #endif /* #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) */
762 * freertos_tasks_c_additions_init() should only be called if the user definable
763 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
764 * called by the function.
766 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
768 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
772 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
773 extern void vApplicationPassiveIdleHook( void );
774 #endif /* #if ( configUSE_PASSIVE_IDLE_HOOK == 1 ) */
776 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
779 * Convert the snprintf return value to the number of characters
780 * written. The following are the possible cases:
782 * 1. The buffer supplied to snprintf is large enough to hold the
783 * generated string. The return value in this case is the number
784 * of characters actually written, not counting the terminating
786 * 2. The buffer supplied to snprintf is NOT large enough to hold
787 * the generated string. The return value in this case is the
788 * number of characters that would have been written if the
789 * buffer had been sufficiently large, not counting the
790 * terminating null character.
791 * 3. Encoding error. The return value in this case is a negative
794 * From 1 and 2 above ==> Only when the return value is non-negative
795 * and less than the supplied buffer length, the string has been
796 * completely written.
798 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
801 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
802 /*-----------------------------------------------------------*/
804 #if ( configNUMBER_OF_CORES > 1 )
805 static void prvCheckForRunStateChange( void )
807 UBaseType_t uxPrevCriticalNesting;
808 const TCB_t * pxThisTCB;
810 /* This must only be called from within a task. */
811 portASSERT_IF_IN_ISR();
813 /* This function is always called with interrupts disabled
814 * so this is safe. */
815 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
817 while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD )
819 /* We are only here if we just entered a critical section
820 * or if we just suspended the scheduler, and another task
821 * has requested that we yield.
823 * This is slightly complicated since we need to save and restore
824 * the suspension and critical nesting counts, as well as release
825 * and reacquire the correct locks. And then, do it all over again
826 * if our state changed again during the reacquisition. */
827 uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
829 if( uxPrevCriticalNesting > 0U )
831 portSET_CRITICAL_NESTING_COUNT( 0U );
832 portRELEASE_ISR_LOCK();
836 /* The scheduler is suspended. uxSchedulerSuspended is updated
837 * only when the task is not requested to yield. */
838 mtCOVERAGE_TEST_MARKER();
841 portRELEASE_TASK_LOCK();
842 portMEMORY_BARRIER();
843 configASSERT( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD );
845 portENABLE_INTERRUPTS();
847 /* Enabling interrupts should cause this core to immediately
848 * service the pending interrupt and yield. If the run state is still
849 * yielding here then that is a problem. */
850 configASSERT( pxThisTCB->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD );
852 portDISABLE_INTERRUPTS();
856 portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
858 if( uxPrevCriticalNesting == 0U )
860 portRELEASE_ISR_LOCK();
864 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
866 /*-----------------------------------------------------------*/
868 #if ( configNUMBER_OF_CORES > 1 )
869 static void prvYieldForTask( const TCB_t * pxTCB )
871 BaseType_t xLowestPriorityToPreempt;
872 BaseType_t xCurrentCoreTaskPriority;
873 BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
876 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
877 BaseType_t xYieldCount = 0;
878 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
880 /* This must be called from a critical section. */
881 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
883 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
885 /* No task should yield for this one if it is a lower priority
886 * than priority level of currently ready tasks. */
887 if( pxTCB->uxPriority >= uxTopReadyPriority )
889 /* Yield is not required for a task which is already running. */
890 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
893 xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
895 /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
896 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
897 --xLowestPriorityToPreempt;
899 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
901 xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
903 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
904 if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
906 xCurrentCoreTaskPriority = xCurrentCoreTaskPriority - 1;
909 if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
911 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
912 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
915 if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
917 #if ( configUSE_CORE_AFFINITY == 1 )
918 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
921 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
922 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
925 xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
926 xLowestPriorityCore = xCoreID;
932 mtCOVERAGE_TEST_MARKER();
936 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
938 /* Yield all currently running non-idle tasks with a priority lower than
939 * the task that needs to run. */
940 if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
941 ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
943 prvYieldCore( xCoreID );
948 mtCOVERAGE_TEST_MARKER();
951 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
955 mtCOVERAGE_TEST_MARKER();
959 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
960 if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
961 #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
962 if( xLowestPriorityCore >= 0 )
963 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
965 prvYieldCore( xLowestPriorityCore );
968 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
969 /* Verify that the calling core always yields to higher priority tasks. */
970 if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) &&
971 ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
973 configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) ||
974 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) );
979 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
980 /*-----------------------------------------------------------*/
982 #if ( configNUMBER_OF_CORES > 1 )
983 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
985 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
986 BaseType_t xTaskScheduled = pdFALSE;
987 BaseType_t xDecrementTopPriority = pdTRUE;
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 = configTASK_DEFAULT_CORE_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 = configTASK_DEFAULT_CORE_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 = configTASK_DEFAULT_CORE_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 = configTASK_DEFAULT_CORE_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 += ( UBaseType_t ) 1U;
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;
2194 BaseType_t xTaskIsRunningOrYielding;
2196 traceENTER_vTaskDelete( xTaskToDelete );
2198 taskENTER_CRITICAL();
2200 /* If null is passed in here then it is the calling task that is
2202 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
2204 /* Remove task from the ready/delayed list. */
2205 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2207 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2211 mtCOVERAGE_TEST_MARKER();
2214 /* Is the task waiting on an event also? */
2215 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2217 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2221 mtCOVERAGE_TEST_MARKER();
2224 /* Increment the uxTaskNumber also so kernel aware debuggers can
2225 * detect that the task lists need re-generating. This is done before
2226 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
2230 /* Use temp variable as distinct sequence points for reading volatile
2231 * variables prior to a logical operator to ensure compliance with
2232 * MISRA C 2012 Rule 13.5. */
2233 xTaskIsRunningOrYielding = taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB );
2235 /* If the task is running (or yielding), we must add it to the
2236 * termination list so that an idle task can delete it when it is
2237 * no longer running. */
2238 if( ( xSchedulerRunning != pdFALSE ) && ( xTaskIsRunningOrYielding != pdFALSE ) )
2240 /* A running task or a task which is scheduled to yield is being
2241 * deleted. This cannot complete when the task is still running
2242 * on a core, as a context switch to another task is required.
2243 * Place the task in the termination list. The idle task will check
2244 * the termination list and free up any memory allocated by the
2245 * scheduler for the TCB and stack of the deleted task. */
2246 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
2248 /* Increment the ucTasksDeleted variable so the idle task knows
2249 * there is a task that has been deleted and that it should therefore
2250 * check the xTasksWaitingTermination list. */
2251 ++uxDeletedTasksWaitingCleanUp;
2253 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
2254 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
2255 traceTASK_DELETE( pxTCB );
2257 /* Delete the task TCB in idle task. */
2258 xDeleteTCBInIdleTask = pdTRUE;
2260 /* The pre-delete hook is primarily for the Windows simulator,
2261 * in which Windows specific clean up operations are performed,
2262 * after which it is not possible to yield away from this task -
2263 * hence xYieldPending is used to latch that a context switch is
2265 #if ( configNUMBER_OF_CORES == 1 )
2266 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
2268 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
2271 /* In the case of SMP, it is possible that the task being deleted
2272 * is running on another core. We must evict the task before
2273 * exiting the critical section to ensure that the task cannot
2274 * take an action which puts it back on ready/state/event list,
2275 * thereby nullifying the delete operation. Once evicted, the
2276 * task won't be scheduled ever as it will no longer be on the
2278 #if ( configNUMBER_OF_CORES > 1 )
2280 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2282 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2284 configASSERT( uxSchedulerSuspended == 0 );
2285 taskYIELD_WITHIN_API();
2289 prvYieldCore( pxTCB->xTaskRunState );
2293 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
2297 --uxCurrentNumberOfTasks;
2298 traceTASK_DELETE( pxTCB );
2300 /* Reset the next expected unblock time in case it referred to
2301 * the task that has just been deleted. */
2302 prvResetNextTaskUnblockTime();
2305 taskEXIT_CRITICAL();
2307 /* If the task is not deleting itself, call prvDeleteTCB from outside of
2308 * critical section. If a task deletes itself, prvDeleteTCB is called
2309 * from prvCheckTasksWaitingTermination which is called from Idle task. */
2310 if( xDeleteTCBInIdleTask != pdTRUE )
2312 prvDeleteTCB( pxTCB );
2315 /* Force a reschedule if it is the currently running task that has just
2317 #if ( configNUMBER_OF_CORES == 1 )
2319 if( xSchedulerRunning != pdFALSE )
2321 if( pxTCB == pxCurrentTCB )
2323 configASSERT( uxSchedulerSuspended == 0 );
2324 taskYIELD_WITHIN_API();
2328 mtCOVERAGE_TEST_MARKER();
2332 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2334 traceRETURN_vTaskDelete();
2337 #endif /* INCLUDE_vTaskDelete */
2338 /*-----------------------------------------------------------*/
2340 #if ( INCLUDE_xTaskDelayUntil == 1 )
2342 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2343 const TickType_t xTimeIncrement )
2345 TickType_t xTimeToWake;
2346 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2348 traceENTER_xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
2350 configASSERT( pxPreviousWakeTime );
2351 configASSERT( ( xTimeIncrement > 0U ) );
2355 /* Minor optimisation. The tick count cannot change in this
2357 const TickType_t xConstTickCount = xTickCount;
2359 configASSERT( uxSchedulerSuspended == 1U );
2361 /* Generate the tick time at which the task wants to wake. */
2362 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2364 if( xConstTickCount < *pxPreviousWakeTime )
2366 /* The tick count has overflowed since this function was
2367 * lasted called. In this case the only time we should ever
2368 * actually delay is if the wake time has also overflowed,
2369 * and the wake time is greater than the tick time. When this
2370 * is the case it is as if neither time had overflowed. */
2371 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2373 xShouldDelay = pdTRUE;
2377 mtCOVERAGE_TEST_MARKER();
2382 /* The tick time has not overflowed. In this case we will
2383 * delay if either the wake time has overflowed, and/or the
2384 * tick time is less than the wake time. */
2385 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2387 xShouldDelay = pdTRUE;
2391 mtCOVERAGE_TEST_MARKER();
2395 /* Update the wake time ready for the next call. */
2396 *pxPreviousWakeTime = xTimeToWake;
2398 if( xShouldDelay != pdFALSE )
2400 traceTASK_DELAY_UNTIL( xTimeToWake );
2402 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2403 * the time to wake, so subtract the current tick count. */
2404 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2408 mtCOVERAGE_TEST_MARKER();
2411 xAlreadyYielded = xTaskResumeAll();
2413 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2414 * have put ourselves to sleep. */
2415 if( xAlreadyYielded == pdFALSE )
2417 taskYIELD_WITHIN_API();
2421 mtCOVERAGE_TEST_MARKER();
2424 traceRETURN_xTaskDelayUntil( xShouldDelay );
2426 return xShouldDelay;
2429 #endif /* INCLUDE_xTaskDelayUntil */
2430 /*-----------------------------------------------------------*/
2432 #if ( INCLUDE_vTaskDelay == 1 )
2434 void vTaskDelay( const TickType_t xTicksToDelay )
2436 BaseType_t xAlreadyYielded = pdFALSE;
2438 traceENTER_vTaskDelay( xTicksToDelay );
2440 /* A delay time of zero just forces a reschedule. */
2441 if( xTicksToDelay > ( TickType_t ) 0U )
2445 configASSERT( uxSchedulerSuspended == 1U );
2449 /* A task that is removed from the event list while the
2450 * scheduler is suspended will not get placed in the ready
2451 * list or removed from the blocked list until the scheduler
2454 * This task cannot be in an event list as it is the currently
2455 * executing task. */
2456 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2458 xAlreadyYielded = xTaskResumeAll();
2462 mtCOVERAGE_TEST_MARKER();
2465 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2466 * have put ourselves to sleep. */
2467 if( xAlreadyYielded == pdFALSE )
2469 taskYIELD_WITHIN_API();
2473 mtCOVERAGE_TEST_MARKER();
2476 traceRETURN_vTaskDelay();
2479 #endif /* INCLUDE_vTaskDelay */
2480 /*-----------------------------------------------------------*/
2482 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2484 eTaskState eTaskGetState( TaskHandle_t xTask )
2487 List_t const * pxStateList;
2488 List_t const * pxEventList;
2489 List_t const * pxDelayedList;
2490 List_t const * pxOverflowedDelayedList;
2491 const TCB_t * const pxTCB = xTask;
2493 traceENTER_eTaskGetState( xTask );
2495 configASSERT( pxTCB );
2497 #if ( configNUMBER_OF_CORES == 1 )
2498 if( pxTCB == pxCurrentTCB )
2500 /* The task calling this function is querying its own state. */
2506 taskENTER_CRITICAL();
2508 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2509 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2510 pxDelayedList = pxDelayedTaskList;
2511 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2513 taskEXIT_CRITICAL();
2515 if( pxEventList == &xPendingReadyList )
2517 /* The task has been placed on the pending ready list, so its
2518 * state is eReady regardless of what list the task's state list
2519 * item is currently placed on. */
2522 else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2524 /* The task being queried is referenced from one of the Blocked
2529 #if ( INCLUDE_vTaskSuspend == 1 )
2530 else if( pxStateList == &xSuspendedTaskList )
2532 /* The task being queried is referenced from the suspended
2533 * list. Is it genuinely suspended or is it blocked
2535 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2537 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2541 /* The task does not appear on the event list item of
2542 * and of the RTOS objects, but could still be in the
2543 * blocked state if it is waiting on its notification
2544 * rather than waiting on an object. If not, is
2546 eReturn = eSuspended;
2548 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2550 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2557 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2559 eReturn = eSuspended;
2561 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2568 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2570 #if ( INCLUDE_vTaskDelete == 1 )
2571 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2573 /* The task being queried is referenced from the deleted
2574 * tasks list, or it is not referenced from any lists at
2582 #if ( configNUMBER_OF_CORES == 1 )
2584 /* If the task is not in any other state, it must be in the
2585 * Ready (including pending ready) state. */
2588 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2590 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2592 /* Is it actively running on a core? */
2597 /* If the task is not in any other state, it must be in the
2598 * Ready (including pending ready) state. */
2602 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2606 traceRETURN_eTaskGetState( eReturn );
2611 #endif /* INCLUDE_eTaskGetState */
2612 /*-----------------------------------------------------------*/
2614 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2616 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2618 TCB_t const * pxTCB;
2619 UBaseType_t uxReturn;
2621 traceENTER_uxTaskPriorityGet( xTask );
2623 taskENTER_CRITICAL();
2625 /* If null is passed in here then it is the priority of the task
2626 * that called uxTaskPriorityGet() that is being queried. */
2627 pxTCB = prvGetTCBFromHandle( xTask );
2628 uxReturn = pxTCB->uxPriority;
2630 taskEXIT_CRITICAL();
2632 traceRETURN_uxTaskPriorityGet( uxReturn );
2637 #endif /* INCLUDE_uxTaskPriorityGet */
2638 /*-----------------------------------------------------------*/
2640 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2642 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2644 TCB_t const * pxTCB;
2645 UBaseType_t uxReturn;
2646 UBaseType_t uxSavedInterruptStatus;
2648 traceENTER_uxTaskPriorityGetFromISR( xTask );
2650 /* RTOS ports that support interrupt nesting have the concept of a
2651 * maximum system call (or maximum API call) interrupt priority.
2652 * Interrupts that are above the maximum system call priority are keep
2653 * permanently enabled, even when the RTOS kernel is in a critical section,
2654 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2655 * is defined in FreeRTOSConfig.h then
2656 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2657 * failure if a FreeRTOS API function is called from an interrupt that has
2658 * been assigned a priority above the configured maximum system call
2659 * priority. Only FreeRTOS functions that end in FromISR can be called
2660 * from interrupts that have been assigned a priority at or (logically)
2661 * below the maximum system call interrupt priority. FreeRTOS maintains a
2662 * separate interrupt safe API to ensure interrupt entry is as fast and as
2663 * simple as possible. More information (albeit Cortex-M specific) is
2664 * provided on the following link:
2665 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2666 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2668 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
2670 /* If null is passed in here then it is the priority of the calling
2671 * task that is being queried. */
2672 pxTCB = prvGetTCBFromHandle( xTask );
2673 uxReturn = pxTCB->uxPriority;
2675 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2677 traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
2682 #endif /* INCLUDE_uxTaskPriorityGet */
2683 /*-----------------------------------------------------------*/
2685 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2687 UBaseType_t uxTaskBasePriorityGet( const TaskHandle_t xTask )
2689 TCB_t const * pxTCB;
2690 UBaseType_t uxReturn;
2692 traceENTER_uxTaskBasePriorityGet( xTask );
2694 taskENTER_CRITICAL();
2696 /* If null is passed in here then it is the base priority of the task
2697 * that called uxTaskBasePriorityGet() that is being queried. */
2698 pxTCB = prvGetTCBFromHandle( xTask );
2699 uxReturn = pxTCB->uxBasePriority;
2701 taskEXIT_CRITICAL();
2703 traceRETURN_uxTaskBasePriorityGet( uxReturn );
2708 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2709 /*-----------------------------------------------------------*/
2711 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2713 UBaseType_t uxTaskBasePriorityGetFromISR( const TaskHandle_t xTask )
2715 TCB_t const * pxTCB;
2716 UBaseType_t uxReturn;
2717 UBaseType_t uxSavedInterruptStatus;
2719 traceENTER_uxTaskBasePriorityGetFromISR( xTask );
2721 /* RTOS ports that support interrupt nesting have the concept of a
2722 * maximum system call (or maximum API call) interrupt priority.
2723 * Interrupts that are above the maximum system call priority are keep
2724 * permanently enabled, even when the RTOS kernel is in a critical section,
2725 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2726 * is defined in FreeRTOSConfig.h then
2727 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2728 * failure if a FreeRTOS API function is called from an interrupt that has
2729 * been assigned a priority above the configured maximum system call
2730 * priority. Only FreeRTOS functions that end in FromISR can be called
2731 * from interrupts that have been assigned a priority at or (logically)
2732 * below the maximum system call interrupt priority. FreeRTOS maintains a
2733 * separate interrupt safe API to ensure interrupt entry is as fast and as
2734 * simple as possible. More information (albeit Cortex-M specific) is
2735 * provided on the following link:
2736 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2737 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2739 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
2741 /* If null is passed in here then it is the base priority of the calling
2742 * task that is being queried. */
2743 pxTCB = prvGetTCBFromHandle( xTask );
2744 uxReturn = pxTCB->uxBasePriority;
2746 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2748 traceRETURN_uxTaskBasePriorityGetFromISR( uxReturn );
2753 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2754 /*-----------------------------------------------------------*/
2756 #if ( INCLUDE_vTaskPrioritySet == 1 )
2758 void vTaskPrioritySet( TaskHandle_t xTask,
2759 UBaseType_t uxNewPriority )
2762 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2763 BaseType_t xYieldRequired = pdFALSE;
2765 #if ( configNUMBER_OF_CORES > 1 )
2766 BaseType_t xYieldForTask = pdFALSE;
2769 traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
2771 configASSERT( uxNewPriority < configMAX_PRIORITIES );
2773 /* Ensure the new priority is valid. */
2774 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2776 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2780 mtCOVERAGE_TEST_MARKER();
2783 taskENTER_CRITICAL();
2785 /* If null is passed in here then it is the priority of the calling
2786 * task that is being changed. */
2787 pxTCB = prvGetTCBFromHandle( xTask );
2789 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2791 #if ( configUSE_MUTEXES == 1 )
2793 uxCurrentBasePriority = pxTCB->uxBasePriority;
2797 uxCurrentBasePriority = pxTCB->uxPriority;
2801 if( uxCurrentBasePriority != uxNewPriority )
2803 /* The priority change may have readied a task of higher
2804 * priority than a running task. */
2805 if( uxNewPriority > uxCurrentBasePriority )
2807 #if ( configNUMBER_OF_CORES == 1 )
2809 if( pxTCB != pxCurrentTCB )
2811 /* The priority of a task other than the currently
2812 * running task is being raised. Is the priority being
2813 * raised above that of the running task? */
2814 if( uxNewPriority > pxCurrentTCB->uxPriority )
2816 xYieldRequired = pdTRUE;
2820 mtCOVERAGE_TEST_MARKER();
2825 /* The priority of the running task is being raised,
2826 * but the running task must already be the highest
2827 * priority task able to run so no yield is required. */
2830 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2832 /* The priority of a task is being raised so
2833 * perform a yield for this task later. */
2834 xYieldForTask = pdTRUE;
2836 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2838 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2840 /* Setting the priority of a running task down means
2841 * there may now be another task of higher priority that
2842 * is ready to execute. */
2843 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2844 if( pxTCB->xPreemptionDisable == pdFALSE )
2847 xYieldRequired = pdTRUE;
2852 /* Setting the priority of any other task down does not
2853 * require a yield as the running task must be above the
2854 * new priority of the task being modified. */
2857 /* Remember the ready list the task might be referenced from
2858 * before its uxPriority member is changed so the
2859 * taskRESET_READY_PRIORITY() macro can function correctly. */
2860 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2862 #if ( configUSE_MUTEXES == 1 )
2864 /* Only change the priority being used if the task is not
2865 * currently using an inherited priority or the new priority
2866 * is bigger than the inherited priority. */
2867 if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2869 pxTCB->uxPriority = uxNewPriority;
2873 mtCOVERAGE_TEST_MARKER();
2876 /* The base priority gets set whatever. */
2877 pxTCB->uxBasePriority = uxNewPriority;
2879 #else /* if ( configUSE_MUTEXES == 1 ) */
2881 pxTCB->uxPriority = uxNewPriority;
2883 #endif /* if ( configUSE_MUTEXES == 1 ) */
2885 /* Only reset the event list item value if the value is not
2886 * being used for anything else. */
2887 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0UL ) )
2889 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) );
2893 mtCOVERAGE_TEST_MARKER();
2896 /* If the task is in the blocked or suspended list we need do
2897 * nothing more than change its priority variable. However, if
2898 * the task is in a ready list it needs to be removed and placed
2899 * in the list appropriate to its new priority. */
2900 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2902 /* The task is currently in its ready list - remove before
2903 * adding it to its new ready list. As we are in a critical
2904 * section we can do this even if the scheduler is suspended. */
2905 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2907 /* It is known that the task is in its ready list so
2908 * there is no need to check again and the port level
2909 * reset macro can be called directly. */
2910 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2914 mtCOVERAGE_TEST_MARKER();
2917 prvAddTaskToReadyList( pxTCB );
2921 #if ( configNUMBER_OF_CORES == 1 )
2923 mtCOVERAGE_TEST_MARKER();
2927 /* It's possible that xYieldForTask was already set to pdTRUE because
2928 * its priority is being raised. However, since it is not in a ready list
2929 * we don't actually need to yield for it. */
2930 xYieldForTask = pdFALSE;
2935 if( xYieldRequired != pdFALSE )
2937 /* The running task priority is set down. Request the task to yield. */
2938 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2942 #if ( configNUMBER_OF_CORES > 1 )
2943 if( xYieldForTask != pdFALSE )
2945 /* The priority of the task is being raised. If a running
2946 * task has priority lower than this task, it should yield
2948 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2951 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2953 mtCOVERAGE_TEST_MARKER();
2957 /* Remove compiler warning about unused variables when the port
2958 * optimised task selection is not being used. */
2959 ( void ) uxPriorityUsedOnEntry;
2962 taskEXIT_CRITICAL();
2964 traceRETURN_vTaskPrioritySet();
2967 #endif /* INCLUDE_vTaskPrioritySet */
2968 /*-----------------------------------------------------------*/
2970 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2971 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2972 UBaseType_t uxCoreAffinityMask )
2976 UBaseType_t uxPrevCoreAffinityMask;
2978 #if ( configUSE_PREEMPTION == 1 )
2979 UBaseType_t uxPrevNotAllowedCores;
2982 traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
2984 taskENTER_CRITICAL();
2986 pxTCB = prvGetTCBFromHandle( xTask );
2988 uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2989 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2991 if( xSchedulerRunning != pdFALSE )
2993 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2995 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2997 /* If the task can no longer run on the core it was running,
2998 * request the core to yield. */
2999 if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
3001 prvYieldCore( xCoreID );
3006 #if ( configUSE_PREEMPTION == 1 )
3008 /* Calculate the cores on which this task was not allowed to
3009 * run previously. */
3010 uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
3012 /* Does the new core mask enables this task to run on any of the
3013 * previously not allowed cores? If yes, check if this task can be
3014 * scheduled on any of those cores. */
3015 if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
3017 prvYieldForTask( pxTCB );
3020 #else /* #if( configUSE_PREEMPTION == 1 ) */
3022 mtCOVERAGE_TEST_MARKER();
3024 #endif /* #if( configUSE_PREEMPTION == 1 ) */
3028 taskEXIT_CRITICAL();
3030 traceRETURN_vTaskCoreAffinitySet();
3032 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3033 /*-----------------------------------------------------------*/
3035 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
3036 UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
3038 const TCB_t * pxTCB;
3039 UBaseType_t uxCoreAffinityMask;
3041 traceENTER_vTaskCoreAffinityGet( xTask );
3043 taskENTER_CRITICAL();
3045 pxTCB = prvGetTCBFromHandle( xTask );
3046 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
3048 taskEXIT_CRITICAL();
3050 traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
3052 return uxCoreAffinityMask;
3054 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3056 /*-----------------------------------------------------------*/
3058 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3060 void vTaskPreemptionDisable( const TaskHandle_t xTask )
3064 traceENTER_vTaskPreemptionDisable( xTask );
3066 taskENTER_CRITICAL();
3068 pxTCB = prvGetTCBFromHandle( xTask );
3070 pxTCB->xPreemptionDisable = pdTRUE;
3072 taskEXIT_CRITICAL();
3074 traceRETURN_vTaskPreemptionDisable();
3077 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3078 /*-----------------------------------------------------------*/
3080 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3082 void vTaskPreemptionEnable( const TaskHandle_t xTask )
3087 traceENTER_vTaskPreemptionEnable( xTask );
3089 taskENTER_CRITICAL();
3091 pxTCB = prvGetTCBFromHandle( xTask );
3093 pxTCB->xPreemptionDisable = pdFALSE;
3095 if( xSchedulerRunning != pdFALSE )
3097 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3099 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
3100 prvYieldCore( xCoreID );
3104 taskEXIT_CRITICAL();
3106 traceRETURN_vTaskPreemptionEnable();
3109 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3110 /*-----------------------------------------------------------*/
3112 #if ( INCLUDE_vTaskSuspend == 1 )
3114 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
3118 traceENTER_vTaskSuspend( xTaskToSuspend );
3120 taskENTER_CRITICAL();
3122 /* If null is passed in here then it is the running task that is
3123 * being suspended. */
3124 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
3126 traceTASK_SUSPEND( pxTCB );
3128 /* Remove task from the ready/delayed list and place in the
3129 * suspended list. */
3130 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
3132 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
3136 mtCOVERAGE_TEST_MARKER();
3139 /* Is the task waiting on an event also? */
3140 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3142 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3146 mtCOVERAGE_TEST_MARKER();
3149 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
3151 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3155 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3157 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3159 /* The task was blocked to wait for a notification, but is
3160 * now suspended, so no notification was received. */
3161 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
3165 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3167 /* In the case of SMP, it is possible that the task being suspended
3168 * is running on another core. We must evict the task before
3169 * exiting the critical section to ensure that the task cannot
3170 * take an action which puts it back on ready/state/event list,
3171 * thereby nullifying the suspend operation. Once evicted, the
3172 * task won't be scheduled before it is resumed as it will no longer
3173 * be on the ready list. */
3174 #if ( configNUMBER_OF_CORES > 1 )
3176 if( xSchedulerRunning != pdFALSE )
3178 /* Reset the next expected unblock time in case it referred to the
3179 * task that is now in the Suspended state. */
3180 prvResetNextTaskUnblockTime();
3182 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3184 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
3186 /* The current task has just been suspended. */
3187 configASSERT( uxSchedulerSuspended == 0 );
3188 vTaskYieldWithinAPI();
3192 prvYieldCore( pxTCB->xTaskRunState );
3197 mtCOVERAGE_TEST_MARKER();
3202 mtCOVERAGE_TEST_MARKER();
3205 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
3207 taskEXIT_CRITICAL();
3209 #if ( configNUMBER_OF_CORES == 1 )
3211 UBaseType_t uxCurrentListLength;
3213 if( xSchedulerRunning != pdFALSE )
3215 /* Reset the next expected unblock time in case it referred to the
3216 * task that is now in the Suspended state. */
3217 taskENTER_CRITICAL();
3219 prvResetNextTaskUnblockTime();
3221 taskEXIT_CRITICAL();
3225 mtCOVERAGE_TEST_MARKER();
3228 if( pxTCB == pxCurrentTCB )
3230 if( xSchedulerRunning != pdFALSE )
3232 /* The current task has just been suspended. */
3233 configASSERT( uxSchedulerSuspended == 0 );
3234 portYIELD_WITHIN_API();
3238 /* The scheduler is not running, but the task that was pointed
3239 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
3240 * must be adjusted to point to a different task. */
3242 /* Use a temp variable as a distinct sequence point for reading
3243 * volatile variables prior to a comparison to ensure compliance
3244 * with MISRA C 2012 Rule 13.2. */
3245 uxCurrentListLength = listCURRENT_LIST_LENGTH( &xSuspendedTaskList );
3247 if( uxCurrentListLength == uxCurrentNumberOfTasks )
3249 /* No other tasks are ready, so set pxCurrentTCB back to
3250 * NULL so when the next task is created pxCurrentTCB will
3251 * be set to point to it no matter what its relative priority
3253 pxCurrentTCB = NULL;
3257 vTaskSwitchContext();
3263 mtCOVERAGE_TEST_MARKER();
3266 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3268 traceRETURN_vTaskSuspend();
3271 #endif /* INCLUDE_vTaskSuspend */
3272 /*-----------------------------------------------------------*/
3274 #if ( INCLUDE_vTaskSuspend == 1 )
3276 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
3278 BaseType_t xReturn = pdFALSE;
3279 const TCB_t * const pxTCB = xTask;
3281 /* Accesses xPendingReadyList so must be called from a critical
3284 /* It does not make sense to check if the calling task is suspended. */
3285 configASSERT( xTask );
3287 /* Is the task being resumed actually in the suspended list? */
3288 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
3290 /* Has the task already been resumed from within an ISR? */
3291 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
3293 /* Is it in the suspended list because it is in the Suspended
3294 * state, or because it is blocked with no timeout? */
3295 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE )
3297 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3301 /* The task does not appear on the event list item of
3302 * and of the RTOS objects, but could still be in the
3303 * blocked state if it is waiting on its notification
3304 * rather than waiting on an object. If not, is
3308 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3310 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3317 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3321 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3325 mtCOVERAGE_TEST_MARKER();
3330 mtCOVERAGE_TEST_MARKER();
3335 mtCOVERAGE_TEST_MARKER();
3341 #endif /* INCLUDE_vTaskSuspend */
3342 /*-----------------------------------------------------------*/
3344 #if ( INCLUDE_vTaskSuspend == 1 )
3346 void vTaskResume( TaskHandle_t xTaskToResume )
3348 TCB_t * const pxTCB = xTaskToResume;
3350 traceENTER_vTaskResume( xTaskToResume );
3352 /* It does not make sense to resume the calling task. */
3353 configASSERT( xTaskToResume );
3355 #if ( configNUMBER_OF_CORES == 1 )
3357 /* The parameter cannot be NULL as it is impossible to resume the
3358 * currently executing task. */
3359 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
3362 /* The parameter cannot be NULL as it is impossible to resume the
3363 * currently executing task. It is also impossible to resume a task
3364 * that is actively running on another core but it is not safe
3365 * to check their run state here. Therefore, we get into a critical
3366 * section and check if the task is actually suspended or not. */
3370 taskENTER_CRITICAL();
3372 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3374 traceTASK_RESUME( pxTCB );
3376 /* The ready list can be accessed even if the scheduler is
3377 * suspended because this is inside a critical section. */
3378 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3379 prvAddTaskToReadyList( pxTCB );
3381 /* This yield may not cause the task just resumed to run,
3382 * but will leave the lists in the correct state for the
3384 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
3388 mtCOVERAGE_TEST_MARKER();
3391 taskEXIT_CRITICAL();
3395 mtCOVERAGE_TEST_MARKER();
3398 traceRETURN_vTaskResume();
3401 #endif /* INCLUDE_vTaskSuspend */
3403 /*-----------------------------------------------------------*/
3405 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
3407 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
3409 BaseType_t xYieldRequired = pdFALSE;
3410 TCB_t * const pxTCB = xTaskToResume;
3411 UBaseType_t uxSavedInterruptStatus;
3413 traceENTER_xTaskResumeFromISR( xTaskToResume );
3415 configASSERT( xTaskToResume );
3417 /* RTOS ports that support interrupt nesting have the concept of a
3418 * maximum system call (or maximum API call) interrupt priority.
3419 * Interrupts that are above the maximum system call priority are keep
3420 * permanently enabled, even when the RTOS kernel is in a critical section,
3421 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
3422 * is defined in FreeRTOSConfig.h then
3423 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3424 * failure if a FreeRTOS API function is called from an interrupt that has
3425 * been assigned a priority above the configured maximum system call
3426 * priority. Only FreeRTOS functions that end in FromISR can be called
3427 * from interrupts that have been assigned a priority at or (logically)
3428 * below the maximum system call interrupt priority. FreeRTOS maintains a
3429 * separate interrupt safe API to ensure interrupt entry is as fast and as
3430 * simple as possible. More information (albeit Cortex-M specific) is
3431 * provided on the following link:
3432 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3433 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3435 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3437 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3439 traceTASK_RESUME_FROM_ISR( pxTCB );
3441 /* Check the ready lists can be accessed. */
3442 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3444 #if ( configNUMBER_OF_CORES == 1 )
3446 /* Ready lists can be accessed so move the task from the
3447 * suspended list to the ready list directly. */
3448 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3450 xYieldRequired = pdTRUE;
3452 /* Mark that a yield is pending in case the user is not
3453 * using the return value to initiate a context switch
3454 * from the ISR using the port specific portYIELD_FROM_ISR(). */
3455 xYieldPendings[ 0 ] = pdTRUE;
3459 mtCOVERAGE_TEST_MARKER();
3462 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3464 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3465 prvAddTaskToReadyList( pxTCB );
3469 /* The delayed or ready lists cannot be accessed so the task
3470 * is held in the pending ready list until the scheduler is
3472 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3475 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3477 prvYieldForTask( pxTCB );
3479 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3481 xYieldRequired = pdTRUE;
3484 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3488 mtCOVERAGE_TEST_MARKER();
3491 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3493 traceRETURN_xTaskResumeFromISR( xYieldRequired );
3495 return xYieldRequired;
3498 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3499 /*-----------------------------------------------------------*/
3501 static BaseType_t prvCreateIdleTasks( void )
3503 BaseType_t xReturn = pdPASS;
3505 char cIdleName[ configMAX_TASK_NAME_LEN ];
3506 TaskFunction_t pxIdleTaskFunction = NULL;
3507 BaseType_t xIdleTaskNameIndex;
3509 for( xIdleTaskNameIndex = ( BaseType_t ) 0; xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN; xIdleTaskNameIndex++ )
3511 cIdleName[ xIdleTaskNameIndex ] = configIDLE_TASK_NAME[ xIdleTaskNameIndex ];
3513 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3514 * configMAX_TASK_NAME_LEN characters just in case the memory after the
3515 * string is not accessible (extremely unlikely). */
3516 if( cIdleName[ xIdleTaskNameIndex ] == ( char ) 0x00 )
3522 mtCOVERAGE_TEST_MARKER();
3526 /* Add each idle task at the lowest priority. */
3527 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3529 #if ( configNUMBER_OF_CORES == 1 )
3531 pxIdleTaskFunction = prvIdleTask;
3533 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3535 /* In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks
3536 * are also created to ensure that each core has an idle task to
3537 * run when no other task is available to run. */
3540 pxIdleTaskFunction = prvIdleTask;
3544 pxIdleTaskFunction = prvPassiveIdleTask;
3547 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3549 /* Update the idle task name with suffix to differentiate the idle tasks.
3550 * This function is not required in single core FreeRTOS since there is
3551 * only one idle task. */
3552 #if ( configNUMBER_OF_CORES > 1 )
3554 /* Append the idle task number to the end of the name if there is space. */
3555 if( xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3557 cIdleName[ xIdleTaskNameIndex ] = ( char ) ( xCoreID + '0' );
3559 /* And append a null character if there is space. */
3560 if( ( xIdleTaskNameIndex + 1 ) < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3562 cIdleName[ xIdleTaskNameIndex + 1 ] = '\0';
3566 mtCOVERAGE_TEST_MARKER();
3571 mtCOVERAGE_TEST_MARKER();
3574 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
3576 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3578 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3579 StackType_t * pxIdleTaskStackBuffer = NULL;
3580 configSTACK_DEPTH_TYPE uxIdleTaskStackSize;
3582 /* The Idle task is created using user provided RAM - obtain the
3583 * address of the RAM then create the idle task. */
3584 #if ( configNUMBER_OF_CORES == 1 )
3586 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
3592 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
3596 vApplicationGetPassiveIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize, xCoreID - 1 );
3599 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
3600 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( pxIdleTaskFunction,
3602 uxIdleTaskStackSize,
3604 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3605 pxIdleTaskStackBuffer,
3606 pxIdleTaskTCBBuffer );
3608 if( xIdleTaskHandles[ xCoreID ] != NULL )
3617 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3619 /* The Idle task is being created using dynamically allocated RAM. */
3620 xReturn = xTaskCreate( pxIdleTaskFunction,
3622 configMINIMAL_STACK_SIZE,
3624 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3625 &xIdleTaskHandles[ xCoreID ] );
3627 #endif /* configSUPPORT_STATIC_ALLOCATION */
3629 /* Break the loop if any of the idle task is failed to be created. */
3630 if( xReturn == pdFAIL )
3636 #if ( configNUMBER_OF_CORES == 1 )
3638 mtCOVERAGE_TEST_MARKER();
3642 /* Assign idle task to each core before SMP scheduler is running. */
3643 xIdleTaskHandles[ xCoreID ]->xTaskRunState = xCoreID;
3644 pxCurrentTCBs[ xCoreID ] = xIdleTaskHandles[ xCoreID ];
3653 /*-----------------------------------------------------------*/
3655 void vTaskStartScheduler( void )
3659 traceENTER_vTaskStartScheduler();
3661 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3663 /* Sanity check that the UBaseType_t must have greater than or equal to
3664 * the number of bits as confNUMBER_OF_CORES. */
3665 configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3667 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3669 xReturn = prvCreateIdleTasks();
3671 #if ( configUSE_TIMERS == 1 )
3673 if( xReturn == pdPASS )
3675 xReturn = xTimerCreateTimerTask();
3679 mtCOVERAGE_TEST_MARKER();
3682 #endif /* configUSE_TIMERS */
3684 if( xReturn == pdPASS )
3686 /* freertos_tasks_c_additions_init() should only be called if the user
3687 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3688 * the only macro called by the function. */
3689 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3691 freertos_tasks_c_additions_init();
3695 /* Interrupts are turned off here, to ensure a tick does not occur
3696 * before or during the call to xPortStartScheduler(). The stacks of
3697 * the created tasks contain a status word with interrupts switched on
3698 * so interrupts will automatically get re-enabled when the first task
3700 portDISABLE_INTERRUPTS();
3702 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3704 /* Switch C-Runtime's TLS Block to point to the TLS
3705 * block specific to the task that will run first. */
3706 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3710 xNextTaskUnblockTime = portMAX_DELAY;
3711 xSchedulerRunning = pdTRUE;
3712 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3714 /* If configGENERATE_RUN_TIME_STATS is defined then the following
3715 * macro must be defined to configure the timer/counter used to generate
3716 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
3717 * is set to 0 and the following line fails to build then ensure you do not
3718 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3719 * FreeRTOSConfig.h file. */
3720 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3722 traceTASK_SWITCHED_IN();
3724 /* Setting up the timer tick is hardware specific and thus in the
3725 * portable interface. */
3727 /* The return value for xPortStartScheduler is not required
3728 * hence using a void datatype. */
3729 ( void ) xPortStartScheduler();
3731 /* In most cases, xPortStartScheduler() will not return. If it
3732 * returns pdTRUE then there was not enough heap memory available
3733 * to create either the Idle or the Timer task. If it returned
3734 * pdFALSE, then the application called xTaskEndScheduler().
3735 * Most ports don't implement xTaskEndScheduler() as there is
3736 * nothing to return to. */
3740 /* This line will only be reached if the kernel could not be started,
3741 * because there was not enough FreeRTOS heap to create the idle task
3742 * or the timer task. */
3743 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3746 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3747 * meaning xIdleTaskHandles are not used anywhere else. */
3748 ( void ) xIdleTaskHandles;
3750 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3751 * from getting optimized out as it is no longer used by the kernel. */
3752 ( void ) uxTopUsedPriority;
3754 traceRETURN_vTaskStartScheduler();
3756 /*-----------------------------------------------------------*/
3758 void vTaskEndScheduler( void )
3760 traceENTER_vTaskEndScheduler();
3762 #if ( INCLUDE_vTaskDelete == 1 )
3766 #if ( configUSE_TIMERS == 1 )
3768 /* Delete the timer task created by the kernel. */
3769 vTaskDelete( xTimerGetTimerDaemonTaskHandle() );
3771 #endif /* #if ( configUSE_TIMERS == 1 ) */
3773 /* Delete Idle tasks created by the kernel.*/
3774 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3776 vTaskDelete( xIdleTaskHandles[ xCoreID ] );
3779 /* Idle task is responsible for reclaiming the resources of the tasks in
3780 * xTasksWaitingTermination list. Since the idle task is now deleted and
3781 * no longer going to run, we need to reclaim resources of all the tasks
3782 * in the xTasksWaitingTermination list. */
3783 prvCheckTasksWaitingTermination();
3785 #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
3787 /* Stop the scheduler interrupts and call the portable scheduler end
3788 * routine so the original ISRs can be restored if necessary. The port
3789 * layer must ensure interrupts enable bit is left in the correct state. */
3790 portDISABLE_INTERRUPTS();
3791 xSchedulerRunning = pdFALSE;
3793 /* This function must be called from a task and the application is
3794 * responsible for deleting that task after the scheduler is stopped. */
3795 vPortEndScheduler();
3797 traceRETURN_vTaskEndScheduler();
3799 /*----------------------------------------------------------*/
3801 void vTaskSuspendAll( void )
3803 traceENTER_vTaskSuspendAll();
3805 #if ( configNUMBER_OF_CORES == 1 )
3807 /* A critical section is not required as the variable is of type
3808 * BaseType_t. Please read Richard Barry's reply in the following link to a
3809 * post in the FreeRTOS support forum before reporting this as a bug! -
3810 * https://goo.gl/wu4acr */
3812 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3813 * do not otherwise exhibit real time behaviour. */
3814 portSOFTWARE_BARRIER();
3816 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3817 * is used to allow calls to vTaskSuspendAll() to nest. */
3818 uxSchedulerSuspended += ( UBaseType_t ) 1U;
3820 /* Enforces ordering for ports and optimised compilers that may otherwise place
3821 * the above increment elsewhere. */
3822 portMEMORY_BARRIER();
3824 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3826 UBaseType_t ulState;
3828 /* This must only be called from within a task. */
3829 portASSERT_IF_IN_ISR();
3831 if( xSchedulerRunning != pdFALSE )
3833 /* This must never be called from inside a critical section. */
3834 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
3836 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3837 * We must disable interrupts before we grab the locks in the event that this task is
3838 * interrupted and switches context before incrementing uxSchedulerSuspended.
3839 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3840 * uxSchedulerSuspended since that will prevent context switches. */
3841 ulState = portSET_INTERRUPT_MASK();
3843 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3844 * do not otherwise exhibit real time behaviour. */
3845 portSOFTWARE_BARRIER();
3847 portGET_TASK_LOCK();
3849 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3850 * purpose is to prevent altering the variable when fromISR APIs are readying
3852 if( uxSchedulerSuspended == 0U )
3854 prvCheckForRunStateChange();
3858 mtCOVERAGE_TEST_MARKER();
3863 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3864 * is used to allow calls to vTaskSuspendAll() to nest. */
3865 ++uxSchedulerSuspended;
3866 portRELEASE_ISR_LOCK();
3868 portCLEAR_INTERRUPT_MASK( ulState );
3872 mtCOVERAGE_TEST_MARKER();
3875 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3877 traceRETURN_vTaskSuspendAll();
3880 /*----------------------------------------------------------*/
3882 #if ( configUSE_TICKLESS_IDLE != 0 )
3884 static TickType_t prvGetExpectedIdleTime( void )
3887 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
3889 /* uxHigherPriorityReadyTasks takes care of the case where
3890 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3891 * task that are in the Ready state, even though the idle task is
3893 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3895 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3897 uxHigherPriorityReadyTasks = pdTRUE;
3902 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3904 /* When port optimised task selection is used the uxTopReadyPriority
3905 * variable is used as a bit map. If bits other than the least
3906 * significant bit are set then there are tasks that have a priority
3907 * above the idle priority that are in the Ready state. This takes
3908 * care of the case where the co-operative scheduler is in use. */
3909 if( uxTopReadyPriority > uxLeastSignificantBit )
3911 uxHigherPriorityReadyTasks = pdTRUE;
3914 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3916 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3920 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1U )
3922 /* There are other idle priority tasks in the ready state. If
3923 * time slicing is used then the very next tick interrupt must be
3927 else if( uxHigherPriorityReadyTasks != pdFALSE )
3929 /* There are tasks in the Ready state that have a priority above the
3930 * idle priority. This path can only be reached if
3931 * configUSE_PREEMPTION is 0. */
3936 xReturn = xNextTaskUnblockTime;
3937 xReturn -= xTickCount;
3943 #endif /* configUSE_TICKLESS_IDLE */
3944 /*----------------------------------------------------------*/
3946 BaseType_t xTaskResumeAll( void )
3948 TCB_t * pxTCB = NULL;
3949 BaseType_t xAlreadyYielded = pdFALSE;
3951 traceENTER_xTaskResumeAll();
3953 #if ( configNUMBER_OF_CORES > 1 )
3954 if( xSchedulerRunning != pdFALSE )
3957 /* It is possible that an ISR caused a task to be removed from an event
3958 * list while the scheduler was suspended. If this was the case then the
3959 * removed task will have been added to the xPendingReadyList. Once the
3960 * scheduler has been resumed it is safe to move all the pending ready
3961 * tasks from this list into their appropriate ready list. */
3962 taskENTER_CRITICAL();
3965 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3967 /* If uxSchedulerSuspended is zero then this function does not match a
3968 * previous call to vTaskSuspendAll(). */
3969 configASSERT( uxSchedulerSuspended != 0U );
3971 uxSchedulerSuspended -= ( UBaseType_t ) 1U;
3972 portRELEASE_TASK_LOCK();
3974 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3976 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3978 /* Move any readied tasks from the pending list into the
3979 * appropriate ready list. */
3980 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3982 /* MISRA Ref 11.5.3 [Void pointer assignment] */
3983 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
3984 /* coverity[misra_c_2012_rule_11_5_violation] */
3985 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) );
3986 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
3987 portMEMORY_BARRIER();
3988 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
3989 prvAddTaskToReadyList( pxTCB );
3991 #if ( configNUMBER_OF_CORES == 1 )
3993 /* If the moved task has a priority higher than the current
3994 * task then a yield must be performed. */
3995 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3997 xYieldPendings[ xCoreID ] = pdTRUE;
4001 mtCOVERAGE_TEST_MARKER();
4004 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4006 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
4007 * If the current core yielded then vTaskSwitchContext() has already been called
4008 * which sets xYieldPendings for the current core to pdTRUE. */
4010 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4015 /* A task was unblocked while the scheduler was suspended,
4016 * which may have prevented the next unblock time from being
4017 * re-calculated, in which case re-calculate it now. Mainly
4018 * important for low power tickless implementations, where
4019 * this can prevent an unnecessary exit from low power
4021 prvResetNextTaskUnblockTime();
4024 /* If any ticks occurred while the scheduler was suspended then
4025 * they should be processed now. This ensures the tick count does
4026 * not slip, and that any delayed tasks are resumed at the correct
4029 * It should be safe to call xTaskIncrementTick here from any core
4030 * since we are in a critical section and xTaskIncrementTick itself
4031 * protects itself within a critical section. Suspending the scheduler
4032 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
4034 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
4036 if( xPendedCounts > ( TickType_t ) 0U )
4040 if( xTaskIncrementTick() != pdFALSE )
4042 /* Other cores are interrupted from
4043 * within xTaskIncrementTick(). */
4044 xYieldPendings[ xCoreID ] = pdTRUE;
4048 mtCOVERAGE_TEST_MARKER();
4052 } while( xPendedCounts > ( TickType_t ) 0U );
4058 mtCOVERAGE_TEST_MARKER();
4062 if( xYieldPendings[ xCoreID ] != pdFALSE )
4064 #if ( configUSE_PREEMPTION != 0 )
4066 xAlreadyYielded = pdTRUE;
4068 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
4070 #if ( configNUMBER_OF_CORES == 1 )
4072 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
4074 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4078 mtCOVERAGE_TEST_MARKER();
4084 mtCOVERAGE_TEST_MARKER();
4087 taskEXIT_CRITICAL();
4090 traceRETURN_xTaskResumeAll( xAlreadyYielded );
4092 return xAlreadyYielded;
4094 /*-----------------------------------------------------------*/
4096 TickType_t xTaskGetTickCount( void )
4100 traceENTER_xTaskGetTickCount();
4102 /* Critical section required if running on a 16 bit processor. */
4103 portTICK_TYPE_ENTER_CRITICAL();
4105 xTicks = xTickCount;
4107 portTICK_TYPE_EXIT_CRITICAL();
4109 traceRETURN_xTaskGetTickCount( xTicks );
4113 /*-----------------------------------------------------------*/
4115 TickType_t xTaskGetTickCountFromISR( void )
4118 UBaseType_t uxSavedInterruptStatus;
4120 traceENTER_xTaskGetTickCountFromISR();
4122 /* RTOS ports that support interrupt nesting have the concept of a maximum
4123 * system call (or maximum API call) interrupt priority. Interrupts that are
4124 * above the maximum system call priority are kept permanently enabled, even
4125 * when the RTOS kernel is in a critical section, but cannot make any calls to
4126 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
4127 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
4128 * failure if a FreeRTOS API function is called from an interrupt that has been
4129 * assigned a priority above the configured maximum system call priority.
4130 * Only FreeRTOS functions that end in FromISR can be called from interrupts
4131 * that have been assigned a priority at or (logically) below the maximum
4132 * system call interrupt priority. FreeRTOS maintains a separate interrupt
4133 * safe API to ensure interrupt entry is as fast and as simple as possible.
4134 * More information (albeit Cortex-M specific) is provided on the following
4135 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
4136 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
4138 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
4140 xReturn = xTickCount;
4142 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
4144 traceRETURN_xTaskGetTickCountFromISR( xReturn );
4148 /*-----------------------------------------------------------*/
4150 UBaseType_t uxTaskGetNumberOfTasks( void )
4152 traceENTER_uxTaskGetNumberOfTasks();
4154 /* A critical section is not required because the variables are of type
4156 traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
4158 return uxCurrentNumberOfTasks;
4160 /*-----------------------------------------------------------*/
4162 char * pcTaskGetName( TaskHandle_t xTaskToQuery )
4166 traceENTER_pcTaskGetName( xTaskToQuery );
4168 /* If null is passed in here then the name of the calling task is being
4170 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4171 configASSERT( pxTCB );
4173 traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
4175 return &( pxTCB->pcTaskName[ 0 ] );
4177 /*-----------------------------------------------------------*/
4179 #if ( INCLUDE_xTaskGetHandle == 1 )
4181 #if ( configNUMBER_OF_CORES == 1 )
4182 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4183 const char pcNameToQuery[] )
4187 TCB_t * pxReturn = NULL;
4190 BaseType_t xBreakLoop;
4192 /* This function is called with the scheduler suspended. */
4194 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4196 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4197 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4198 /* coverity[misra_c_2012_rule_11_5_violation] */
4199 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
4203 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4204 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4205 /* coverity[misra_c_2012_rule_11_5_violation] */
4206 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
4208 /* Check each character in the name looking for a match or
4210 xBreakLoop = pdFALSE;
4212 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4214 cNextChar = pxNextTCB->pcTaskName[ x ];
4216 if( cNextChar != pcNameToQuery[ x ] )
4218 /* Characters didn't match. */
4219 xBreakLoop = pdTRUE;
4221 else if( cNextChar == ( char ) 0x00 )
4223 /* Both strings terminated, a match must have been
4225 pxReturn = pxNextTCB;
4226 xBreakLoop = pdTRUE;
4230 mtCOVERAGE_TEST_MARKER();
4233 if( xBreakLoop != pdFALSE )
4239 if( pxReturn != NULL )
4241 /* The handle has been found. */
4244 } while( pxNextTCB != pxFirstTCB );
4248 mtCOVERAGE_TEST_MARKER();
4253 #else /* if ( configNUMBER_OF_CORES == 1 ) */
4254 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4255 const char pcNameToQuery[] )
4257 TCB_t * pxReturn = NULL;
4260 BaseType_t xBreakLoop;
4261 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
4262 ListItem_t * pxIterator;
4264 /* This function is called with the scheduler suspended. */
4266 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4268 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
4270 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4271 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4272 /* coverity[misra_c_2012_rule_11_5_violation] */
4273 TCB_t * pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
4275 /* Check each character in the name looking for a match or
4277 xBreakLoop = pdFALSE;
4279 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4281 cNextChar = pxTCB->pcTaskName[ x ];
4283 if( cNextChar != pcNameToQuery[ x ] )
4285 /* Characters didn't match. */
4286 xBreakLoop = pdTRUE;
4288 else if( cNextChar == ( char ) 0x00 )
4290 /* Both strings terminated, a match must have been
4293 xBreakLoop = pdTRUE;
4297 mtCOVERAGE_TEST_MARKER();
4300 if( xBreakLoop != pdFALSE )
4306 if( pxReturn != NULL )
4308 /* The handle has been found. */
4315 mtCOVERAGE_TEST_MARKER();
4320 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4322 #endif /* INCLUDE_xTaskGetHandle */
4323 /*-----------------------------------------------------------*/
4325 #if ( INCLUDE_xTaskGetHandle == 1 )
4327 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery )
4329 UBaseType_t uxQueue = configMAX_PRIORITIES;
4332 traceENTER_xTaskGetHandle( pcNameToQuery );
4334 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
4335 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
4339 /* Search the ready lists. */
4343 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
4347 /* Found the handle. */
4350 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4352 /* Search the delayed lists. */
4355 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
4360 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
4363 #if ( INCLUDE_vTaskSuspend == 1 )
4367 /* Search the suspended list. */
4368 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
4373 #if ( INCLUDE_vTaskDelete == 1 )
4377 /* Search the deleted list. */
4378 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
4383 ( void ) xTaskResumeAll();
4385 traceRETURN_xTaskGetHandle( pxTCB );
4390 #endif /* INCLUDE_xTaskGetHandle */
4391 /*-----------------------------------------------------------*/
4393 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4395 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4396 StackType_t ** ppuxStackBuffer,
4397 StaticTask_t ** ppxTaskBuffer )
4402 traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4404 configASSERT( ppuxStackBuffer != NULL );
4405 configASSERT( ppxTaskBuffer != NULL );
4407 pxTCB = prvGetTCBFromHandle( xTask );
4409 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4411 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4413 *ppuxStackBuffer = pxTCB->pxStack;
4414 /* MISRA Ref 11.3.1 [Misaligned access] */
4415 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
4416 /* coverity[misra_c_2012_rule_11_3_violation] */
4417 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4420 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4422 *ppuxStackBuffer = pxTCB->pxStack;
4423 *ppxTaskBuffer = NULL;
4431 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4433 *ppuxStackBuffer = pxTCB->pxStack;
4434 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4437 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4439 traceRETURN_xTaskGetStaticBuffers( xReturn );
4444 #endif /* configSUPPORT_STATIC_ALLOCATION */
4445 /*-----------------------------------------------------------*/
4447 #if ( configUSE_TRACE_FACILITY == 1 )
4449 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4450 const UBaseType_t uxArraySize,
4451 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4453 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4455 traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4459 /* Is there a space in the array for each task in the system? */
4460 if( uxArraySize >= uxCurrentNumberOfTasks )
4462 /* Fill in an TaskStatus_t structure with information on each
4463 * task in the Ready state. */
4467 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4468 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
4470 /* Fill in an TaskStatus_t structure with information on each
4471 * task in the Blocked state. */
4472 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4473 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4475 #if ( INCLUDE_vTaskDelete == 1 )
4477 /* Fill in an TaskStatus_t structure with information on
4478 * each task that has been deleted but not yet cleaned up. */
4479 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4483 #if ( INCLUDE_vTaskSuspend == 1 )
4485 /* Fill in an TaskStatus_t structure with information on
4486 * each task in the Suspended state. */
4487 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4491 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4493 if( pulTotalRunTime != NULL )
4495 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4496 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4498 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4502 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4504 if( pulTotalRunTime != NULL )
4506 *pulTotalRunTime = 0;
4509 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4513 mtCOVERAGE_TEST_MARKER();
4516 ( void ) xTaskResumeAll();
4518 traceRETURN_uxTaskGetSystemState( uxTask );
4523 #endif /* configUSE_TRACE_FACILITY */
4524 /*----------------------------------------------------------*/
4526 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4528 #if ( configNUMBER_OF_CORES == 1 )
4529 TaskHandle_t xTaskGetIdleTaskHandle( void )
4531 traceENTER_xTaskGetIdleTaskHandle();
4533 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4534 * started, then xIdleTaskHandles will be NULL. */
4535 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4537 traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4539 return xIdleTaskHandles[ 0 ];
4541 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
4543 TaskHandle_t xTaskGetIdleTaskHandleForCore( BaseType_t xCoreID )
4545 traceENTER_xTaskGetIdleTaskHandleForCore( xCoreID );
4547 /* Ensure the core ID is valid. */
4548 configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE );
4550 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4551 * started, then xIdleTaskHandles will be NULL. */
4552 configASSERT( ( xIdleTaskHandles[ xCoreID ] != NULL ) );
4554 traceRETURN_xTaskGetIdleTaskHandleForCore( xIdleTaskHandles[ xCoreID ] );
4556 return xIdleTaskHandles[ xCoreID ];
4559 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4560 /*----------------------------------------------------------*/
4562 /* This conditional compilation should use inequality to 0, not equality to 1.
4563 * This is to ensure vTaskStepTick() is available when user defined low power mode
4564 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4566 #if ( configUSE_TICKLESS_IDLE != 0 )
4568 void vTaskStepTick( TickType_t xTicksToJump )
4570 TickType_t xUpdatedTickCount;
4572 traceENTER_vTaskStepTick( xTicksToJump );
4574 /* Correct the tick count value after a period during which the tick
4575 * was suppressed. Note this does *not* call the tick hook function for
4576 * each stepped tick. */
4577 xUpdatedTickCount = xTickCount + xTicksToJump;
4578 configASSERT( xUpdatedTickCount <= xNextTaskUnblockTime );
4580 if( xUpdatedTickCount == xNextTaskUnblockTime )
4582 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4583 * xTaskIncrementTick() when the scheduler resumes. This ensures
4584 * that any delayed tasks are resumed at the correct time. */
4585 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4586 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4588 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4589 taskENTER_CRITICAL();
4593 taskEXIT_CRITICAL();
4598 mtCOVERAGE_TEST_MARKER();
4601 xTickCount += xTicksToJump;
4603 traceINCREASE_TICK_COUNT( xTicksToJump );
4604 traceRETURN_vTaskStepTick();
4607 #endif /* configUSE_TICKLESS_IDLE */
4608 /*----------------------------------------------------------*/
4610 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4612 BaseType_t xYieldOccurred;
4614 traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4616 /* Must not be called with the scheduler suspended as the implementation
4617 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4618 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4620 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4621 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4624 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4625 taskENTER_CRITICAL();
4627 xPendedTicks += xTicksToCatchUp;
4629 taskEXIT_CRITICAL();
4630 xYieldOccurred = xTaskResumeAll();
4632 traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4634 return xYieldOccurred;
4636 /*----------------------------------------------------------*/
4638 #if ( INCLUDE_xTaskAbortDelay == 1 )
4640 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4642 TCB_t * pxTCB = xTask;
4645 traceENTER_xTaskAbortDelay( xTask );
4647 configASSERT( pxTCB );
4651 /* A task can only be prematurely removed from the Blocked state if
4652 * it is actually in the Blocked state. */
4653 if( eTaskGetState( xTask ) == eBlocked )
4657 /* Remove the reference to the task from the blocked list. An
4658 * interrupt won't touch the xStateListItem because the
4659 * scheduler is suspended. */
4660 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4662 /* Is the task waiting on an event also? If so remove it from
4663 * the event list too. Interrupts can touch the event list item,
4664 * even though the scheduler is suspended, so a critical section
4666 taskENTER_CRITICAL();
4668 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4670 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4672 /* This lets the task know it was forcibly removed from the
4673 * blocked state so it should not re-evaluate its block time and
4674 * then block again. */
4675 pxTCB->ucDelayAborted = ( uint8_t ) pdTRUE;
4679 mtCOVERAGE_TEST_MARKER();
4682 taskEXIT_CRITICAL();
4684 /* Place the unblocked task into the appropriate ready list. */
4685 prvAddTaskToReadyList( pxTCB );
4687 /* A task being unblocked cannot cause an immediate context
4688 * switch if preemption is turned off. */
4689 #if ( configUSE_PREEMPTION == 1 )
4691 #if ( configNUMBER_OF_CORES == 1 )
4693 /* Preemption is on, but a context switch should only be
4694 * performed if the unblocked task has a priority that is
4695 * higher than the currently executing task. */
4696 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4698 /* Pend the yield to be performed when the scheduler
4699 * is unsuspended. */
4700 xYieldPendings[ 0 ] = pdTRUE;
4704 mtCOVERAGE_TEST_MARKER();
4707 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4709 taskENTER_CRITICAL();
4711 prvYieldForTask( pxTCB );
4713 taskEXIT_CRITICAL();
4715 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4717 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4724 ( void ) xTaskResumeAll();
4726 traceRETURN_xTaskAbortDelay( xReturn );
4731 #endif /* INCLUDE_xTaskAbortDelay */
4732 /*----------------------------------------------------------*/
4734 BaseType_t xTaskIncrementTick( void )
4737 TickType_t xItemValue;
4738 BaseType_t xSwitchRequired = pdFALSE;
4740 #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4741 BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4742 #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4744 traceENTER_xTaskIncrementTick();
4746 /* Called by the portable layer each time a tick interrupt occurs.
4747 * Increments the tick then checks to see if the new tick value will cause any
4748 * tasks to be unblocked. */
4749 traceTASK_INCREMENT_TICK( xTickCount );
4751 /* Tick increment should occur on every kernel timer event. Core 0 has the
4752 * responsibility to increment the tick, or increment the pended ticks if the
4753 * scheduler is suspended. If pended ticks is greater than zero, the core that
4754 * calls xTaskResumeAll has the responsibility to increment the tick. */
4755 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4757 /* Minor optimisation. The tick count cannot change in this
4759 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4761 /* Increment the RTOS tick, switching the delayed and overflowed
4762 * delayed lists if it wraps to 0. */
4763 xTickCount = xConstTickCount;
4765 if( xConstTickCount == ( TickType_t ) 0U )
4767 taskSWITCH_DELAYED_LISTS();
4771 mtCOVERAGE_TEST_MARKER();
4774 /* See if this tick has made a timeout expire. Tasks are stored in
4775 * the queue in the order of their wake time - meaning once one task
4776 * has been found whose block time has not expired there is no need to
4777 * look any further down the list. */
4778 if( xConstTickCount >= xNextTaskUnblockTime )
4782 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4784 /* The delayed list is empty. Set xNextTaskUnblockTime
4785 * to the maximum possible value so it is extremely
4787 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4788 * next time through. */
4789 xNextTaskUnblockTime = portMAX_DELAY;
4794 /* The delayed list is not empty, get the value of the
4795 * item at the head of the delayed list. This is the time
4796 * at which the task at the head of the delayed list must
4797 * be removed from the Blocked state. */
4798 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4799 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4800 /* coverity[misra_c_2012_rule_11_5_violation] */
4801 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
4802 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4804 if( xConstTickCount < xItemValue )
4806 /* It is not time to unblock this item yet, but the
4807 * item value is the time at which the task at the head
4808 * of the blocked list must be removed from the Blocked
4809 * state - so record the item value in
4810 * xNextTaskUnblockTime. */
4811 xNextTaskUnblockTime = xItemValue;
4816 mtCOVERAGE_TEST_MARKER();
4819 /* It is time to remove the item from the Blocked state. */
4820 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4822 /* Is the task waiting on an event also? If so remove
4823 * it from the event list. */
4824 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4826 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4830 mtCOVERAGE_TEST_MARKER();
4833 /* Place the unblocked task into the appropriate ready
4835 prvAddTaskToReadyList( pxTCB );
4837 /* A task being unblocked cannot cause an immediate
4838 * context switch if preemption is turned off. */
4839 #if ( configUSE_PREEMPTION == 1 )
4841 #if ( configNUMBER_OF_CORES == 1 )
4843 /* Preemption is on, but a context switch should
4844 * only be performed if the unblocked task's
4845 * priority is higher than the currently executing
4847 * The case of equal priority tasks sharing
4848 * processing time (which happens when both
4849 * preemption and time slicing are on) is
4851 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4853 xSwitchRequired = pdTRUE;
4857 mtCOVERAGE_TEST_MARKER();
4860 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4862 prvYieldForTask( pxTCB );
4864 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4866 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4871 /* Tasks of equal priority to the currently running task will share
4872 * processing time (time slice) if preemption is on, and the application
4873 * writer has not explicitly turned time slicing off. */
4874 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4876 #if ( configNUMBER_OF_CORES == 1 )
4878 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > 1U )
4880 xSwitchRequired = pdTRUE;
4884 mtCOVERAGE_TEST_MARKER();
4887 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4891 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4893 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1U )
4895 xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4899 mtCOVERAGE_TEST_MARKER();
4903 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4905 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4907 #if ( configUSE_TICK_HOOK == 1 )
4909 /* Guard against the tick hook being called when the pended tick
4910 * count is being unwound (when the scheduler is being unlocked). */
4911 if( xPendedTicks == ( TickType_t ) 0 )
4913 vApplicationTickHook();
4917 mtCOVERAGE_TEST_MARKER();
4920 #endif /* configUSE_TICK_HOOK */
4922 #if ( configUSE_PREEMPTION == 1 )
4924 #if ( configNUMBER_OF_CORES == 1 )
4926 /* For single core the core ID is always 0. */
4927 if( xYieldPendings[ 0 ] != pdFALSE )
4929 xSwitchRequired = pdTRUE;
4933 mtCOVERAGE_TEST_MARKER();
4936 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4938 BaseType_t xCoreID, xCurrentCoreID;
4939 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4941 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4943 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4944 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4947 if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4949 if( xCoreID == xCurrentCoreID )
4951 xSwitchRequired = pdTRUE;
4955 prvYieldCore( xCoreID );
4960 mtCOVERAGE_TEST_MARKER();
4965 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4967 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4973 /* The tick hook gets called at regular intervals, even if the
4974 * scheduler is locked. */
4975 #if ( configUSE_TICK_HOOK == 1 )
4977 vApplicationTickHook();
4982 traceRETURN_xTaskIncrementTick( xSwitchRequired );
4984 return xSwitchRequired;
4986 /*-----------------------------------------------------------*/
4988 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4990 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4991 TaskHookFunction_t pxHookFunction )
4995 traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4997 /* If xTask is NULL then it is the task hook of the calling task that is
5001 xTCB = ( TCB_t * ) pxCurrentTCB;
5008 /* Save the hook function in the TCB. A critical section is required as
5009 * the value can be accessed from an interrupt. */
5010 taskENTER_CRITICAL();
5012 xTCB->pxTaskTag = pxHookFunction;
5014 taskEXIT_CRITICAL();
5016 traceRETURN_vTaskSetApplicationTaskTag();
5019 #endif /* configUSE_APPLICATION_TASK_TAG */
5020 /*-----------------------------------------------------------*/
5022 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5024 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
5027 TaskHookFunction_t xReturn;
5029 traceENTER_xTaskGetApplicationTaskTag( xTask );
5031 /* If xTask is NULL then set the calling task's hook. */
5032 pxTCB = prvGetTCBFromHandle( xTask );
5034 /* Save the hook function in the TCB. A critical section is required as
5035 * the value can be accessed from an interrupt. */
5036 taskENTER_CRITICAL();
5038 xReturn = pxTCB->pxTaskTag;
5040 taskEXIT_CRITICAL();
5042 traceRETURN_xTaskGetApplicationTaskTag( xReturn );
5047 #endif /* configUSE_APPLICATION_TASK_TAG */
5048 /*-----------------------------------------------------------*/
5050 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5052 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
5055 TaskHookFunction_t xReturn;
5056 UBaseType_t uxSavedInterruptStatus;
5058 traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
5060 /* If xTask is NULL then set the calling task's hook. */
5061 pxTCB = prvGetTCBFromHandle( xTask );
5063 /* Save the hook function in the TCB. A critical section is required as
5064 * the value can be accessed from an interrupt. */
5065 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
5067 xReturn = pxTCB->pxTaskTag;
5069 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
5071 traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
5076 #endif /* configUSE_APPLICATION_TASK_TAG */
5077 /*-----------------------------------------------------------*/
5079 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5081 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
5082 void * pvParameter )
5087 traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
5089 /* If xTask is NULL then we are calling our own task hook. */
5092 xTCB = pxCurrentTCB;
5099 if( xTCB->pxTaskTag != NULL )
5101 xReturn = xTCB->pxTaskTag( pvParameter );
5108 traceRETURN_xTaskCallApplicationTaskHook( xReturn );
5113 #endif /* configUSE_APPLICATION_TASK_TAG */
5114 /*-----------------------------------------------------------*/
5116 #if ( configNUMBER_OF_CORES == 1 )
5117 void vTaskSwitchContext( void )
5119 traceENTER_vTaskSwitchContext();
5121 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5123 /* The scheduler is currently suspended - do not allow a context
5125 xYieldPendings[ 0 ] = pdTRUE;
5129 xYieldPendings[ 0 ] = pdFALSE;
5130 traceTASK_SWITCHED_OUT();
5132 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5134 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5135 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
5137 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
5140 /* Add the amount of time the task has been running to the
5141 * accumulated time so far. The time the task started running was
5142 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5143 * protection here so count values are only valid until the timer
5144 * overflows. The guard against negative values is to protect
5145 * against suspect run time stat counter implementations - which
5146 * are provided by the application, not the kernel. */
5147 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
5149 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
5153 mtCOVERAGE_TEST_MARKER();
5156 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
5158 #endif /* configGENERATE_RUN_TIME_STATS */
5160 /* Check for stack overflow, if configured. */
5161 taskCHECK_FOR_STACK_OVERFLOW();
5163 /* Before the currently running task is switched out, save its errno. */
5164 #if ( configUSE_POSIX_ERRNO == 1 )
5166 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
5170 /* Select a new task to run using either the generic C or port
5171 * optimised asm code. */
5172 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5173 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5174 /* coverity[misra_c_2012_rule_11_5_violation] */
5175 taskSELECT_HIGHEST_PRIORITY_TASK();
5176 traceTASK_SWITCHED_IN();
5178 /* Macro to inject port specific behaviour immediately after
5179 * switching tasks, such as setting an end of stack watchpoint
5180 * or reconfiguring the MPU. */
5181 portTASK_SWITCH_HOOK( pxCurrentTCB );
5183 /* After the new task is switched in, update the global errno. */
5184 #if ( configUSE_POSIX_ERRNO == 1 )
5186 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
5190 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5192 /* Switch C-Runtime's TLS Block to point to the TLS
5193 * Block specific to this task. */
5194 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
5199 traceRETURN_vTaskSwitchContext();
5201 #else /* if ( configNUMBER_OF_CORES == 1 ) */
5202 void vTaskSwitchContext( BaseType_t xCoreID )
5204 traceENTER_vTaskSwitchContext();
5206 /* Acquire both locks:
5207 * - The ISR lock protects the ready list from simultaneous access by
5208 * both other ISRs and tasks.
5209 * - We also take the task lock to pause here in case another core has
5210 * suspended the scheduler. We don't want to simply set xYieldPending
5211 * and move on if another core suspended the scheduler. We should only
5212 * do that if the current core has suspended the scheduler. */
5214 portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
5217 /* vTaskSwitchContext() must never be called from within a critical section.
5218 * This is not necessarily true for single core FreeRTOS, but it is for this
5220 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
5222 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5224 /* The scheduler is currently suspended - do not allow a context
5226 xYieldPendings[ xCoreID ] = pdTRUE;
5230 xYieldPendings[ xCoreID ] = pdFALSE;
5231 traceTASK_SWITCHED_OUT();
5233 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5235 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5236 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
5238 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
5241 /* Add the amount of time the task has been running to the
5242 * accumulated time so far. The time the task started running was
5243 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5244 * protection here so count values are only valid until the timer
5245 * overflows. The guard against negative values is to protect
5246 * against suspect run time stat counter implementations - which
5247 * are provided by the application, not the kernel. */
5248 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
5250 pxCurrentTCBs[ xCoreID ]->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
5254 mtCOVERAGE_TEST_MARKER();
5257 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
5259 #endif /* configGENERATE_RUN_TIME_STATS */
5261 /* Check for stack overflow, if configured. */
5262 taskCHECK_FOR_STACK_OVERFLOW();
5264 /* Before the currently running task is switched out, save its errno. */
5265 #if ( configUSE_POSIX_ERRNO == 1 )
5267 pxCurrentTCBs[ xCoreID ]->iTaskErrno = FreeRTOS_errno;
5271 /* Select a new task to run. */
5272 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
5273 traceTASK_SWITCHED_IN();
5275 /* Macro to inject port specific behaviour immediately after
5276 * switching tasks, such as setting an end of stack watchpoint
5277 * or reconfiguring the MPU. */
5278 portTASK_SWITCH_HOOK( pxCurrentTCBs[ portGET_CORE_ID() ] );
5280 /* After the new task is switched in, update the global errno. */
5281 #if ( configUSE_POSIX_ERRNO == 1 )
5283 FreeRTOS_errno = pxCurrentTCBs[ xCoreID ]->iTaskErrno;
5287 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5289 /* Switch C-Runtime's TLS Block to point to the TLS
5290 * Block specific to this task. */
5291 configSET_TLS_BLOCK( pxCurrentTCBs[ xCoreID ]->xTLSBlock );
5296 portRELEASE_ISR_LOCK();
5297 portRELEASE_TASK_LOCK();
5299 traceRETURN_vTaskSwitchContext();
5301 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
5302 /*-----------------------------------------------------------*/
5304 void vTaskPlaceOnEventList( List_t * const pxEventList,
5305 const TickType_t xTicksToWait )
5307 traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
5309 configASSERT( pxEventList );
5311 /* THIS FUNCTION MUST BE CALLED WITH THE
5312 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
5314 /* Place the event list item of the TCB in the appropriate event list.
5315 * This is placed in the list in priority order so the highest priority task
5316 * is the first to be woken by the event.
5318 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
5319 * Normally, the xItemValue of a TCB's ListItem_t members is:
5320 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
5321 * Therefore, the event list is sorted in descending priority order.
5323 * The queue that contains the event list is locked, preventing
5324 * simultaneous access from interrupts. */
5325 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5327 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5329 traceRETURN_vTaskPlaceOnEventList();
5331 /*-----------------------------------------------------------*/
5333 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
5334 const TickType_t xItemValue,
5335 const TickType_t xTicksToWait )
5337 traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
5339 configASSERT( pxEventList );
5341 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5342 * the event groups implementation. */
5343 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5345 /* Store the item value in the event list item. It is safe to access the
5346 * event list item here as interrupts won't access the event list item of a
5347 * task that is not in the Blocked state. */
5348 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5350 /* Place the event list item of the TCB at the end of the appropriate event
5351 * list. It is safe to access the event list here because it is part of an
5352 * event group implementation - and interrupts don't access event groups
5353 * directly (instead they access them indirectly by pending function calls to
5354 * the task level). */
5355 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5357 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5359 traceRETURN_vTaskPlaceOnUnorderedEventList();
5361 /*-----------------------------------------------------------*/
5363 #if ( configUSE_TIMERS == 1 )
5365 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
5366 TickType_t xTicksToWait,
5367 const BaseType_t xWaitIndefinitely )
5369 traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
5371 configASSERT( pxEventList );
5373 /* This function should not be called by application code hence the
5374 * 'Restricted' in its name. It is not part of the public API. It is
5375 * designed for use by kernel code, and has special calling requirements -
5376 * it should be called with the scheduler suspended. */
5379 /* Place the event list item of the TCB in the appropriate event list.
5380 * In this case it is assume that this is the only task that is going to
5381 * be waiting on this event list, so the faster vListInsertEnd() function
5382 * can be used in place of vListInsert. */
5383 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5385 /* If the task should block indefinitely then set the block time to a
5386 * value that will be recognised as an indefinite delay inside the
5387 * prvAddCurrentTaskToDelayedList() function. */
5388 if( xWaitIndefinitely != pdFALSE )
5390 xTicksToWait = portMAX_DELAY;
5393 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
5394 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
5396 traceRETURN_vTaskPlaceOnEventListRestricted();
5399 #endif /* configUSE_TIMERS */
5400 /*-----------------------------------------------------------*/
5402 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
5404 TCB_t * pxUnblockedTCB;
5407 traceENTER_xTaskRemoveFromEventList( pxEventList );
5409 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
5410 * called from a critical section within an ISR. */
5412 /* The event list is sorted in priority order, so the first in the list can
5413 * be removed as it is known to be the highest priority. Remove the TCB from
5414 * the delayed list, and add it to the ready list.
5416 * If an event is for a queue that is locked then this function will never
5417 * get called - the lock count on the queue will get modified instead. This
5418 * means exclusive access to the event list is guaranteed here.
5420 * This function assumes that a check has already been made to ensure that
5421 * pxEventList is not empty. */
5422 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5423 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5424 /* coverity[misra_c_2012_rule_11_5_violation] */
5425 pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
5426 configASSERT( pxUnblockedTCB );
5427 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5429 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5431 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5432 prvAddTaskToReadyList( pxUnblockedTCB );
5434 #if ( configUSE_TICKLESS_IDLE != 0 )
5436 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5437 * might be set to the blocked task's time out time. If the task is
5438 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5439 * normally left unchanged, because it is automatically reset to a new
5440 * value when the tick count equals xNextTaskUnblockTime. However if
5441 * tickless idling is used it might be more important to enter sleep mode
5442 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5443 * ensure it is updated at the earliest possible time. */
5444 prvResetNextTaskUnblockTime();
5450 /* The delayed and ready lists cannot be accessed, so hold this task
5451 * pending until the scheduler is resumed. */
5452 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5455 #if ( configNUMBER_OF_CORES == 1 )
5457 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5459 /* Return true if the task removed from the event list has a higher
5460 * priority than the calling task. This allows the calling task to know if
5461 * it should force a context switch now. */
5464 /* Mark that a yield is pending in case the user is not using the
5465 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5466 xYieldPendings[ 0 ] = pdTRUE;
5473 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5477 #if ( configUSE_PREEMPTION == 1 )
5479 prvYieldForTask( pxUnblockedTCB );
5481 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5486 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5488 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5490 traceRETURN_xTaskRemoveFromEventList( xReturn );
5493 /*-----------------------------------------------------------*/
5495 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5496 const TickType_t xItemValue )
5498 TCB_t * pxUnblockedTCB;
5500 traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5502 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5503 * the event flags implementation. */
5504 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5506 /* Store the new item value in the event list. */
5507 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5509 /* Remove the event list form the event flag. Interrupts do not access
5511 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5512 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5513 /* coverity[misra_c_2012_rule_11_5_violation] */
5514 pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem );
5515 configASSERT( pxUnblockedTCB );
5516 listREMOVE_ITEM( pxEventListItem );
5518 #if ( configUSE_TICKLESS_IDLE != 0 )
5520 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5521 * might be set to the blocked task's time out time. If the task is
5522 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5523 * normally left unchanged, because it is automatically reset to a new
5524 * value when the tick count equals xNextTaskUnblockTime. However if
5525 * tickless idling is used it might be more important to enter sleep mode
5526 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5527 * ensure it is updated at the earliest possible time. */
5528 prvResetNextTaskUnblockTime();
5532 /* Remove the task from the delayed list and add it to the ready list. The
5533 * scheduler is suspended so interrupts will not be accessing the ready
5535 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5536 prvAddTaskToReadyList( pxUnblockedTCB );
5538 #if ( configNUMBER_OF_CORES == 1 )
5540 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5542 /* The unblocked task has a priority above that of the calling task, so
5543 * a context switch is required. This function is called with the
5544 * scheduler suspended so xYieldPending is set so the context switch
5545 * occurs immediately that the scheduler is resumed (unsuspended). */
5546 xYieldPendings[ 0 ] = pdTRUE;
5549 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5551 #if ( configUSE_PREEMPTION == 1 )
5553 taskENTER_CRITICAL();
5555 prvYieldForTask( pxUnblockedTCB );
5557 taskEXIT_CRITICAL();
5561 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5563 traceRETURN_vTaskRemoveFromUnorderedEventList();
5565 /*-----------------------------------------------------------*/
5567 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5569 traceENTER_vTaskSetTimeOutState( pxTimeOut );
5571 configASSERT( pxTimeOut );
5572 taskENTER_CRITICAL();
5574 pxTimeOut->xOverflowCount = xNumOfOverflows;
5575 pxTimeOut->xTimeOnEntering = xTickCount;
5577 taskEXIT_CRITICAL();
5579 traceRETURN_vTaskSetTimeOutState();
5581 /*-----------------------------------------------------------*/
5583 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5585 traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5587 /* For internal use only as it does not use a critical section. */
5588 pxTimeOut->xOverflowCount = xNumOfOverflows;
5589 pxTimeOut->xTimeOnEntering = xTickCount;
5591 traceRETURN_vTaskInternalSetTimeOutState();
5593 /*-----------------------------------------------------------*/
5595 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5596 TickType_t * const pxTicksToWait )
5600 traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5602 configASSERT( pxTimeOut );
5603 configASSERT( pxTicksToWait );
5605 taskENTER_CRITICAL();
5607 /* Minor optimisation. The tick count cannot change in this block. */
5608 const TickType_t xConstTickCount = xTickCount;
5609 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5611 #if ( INCLUDE_xTaskAbortDelay == 1 )
5612 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5614 /* The delay was aborted, which is not the same as a time out,
5615 * but has the same result. */
5616 pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
5622 #if ( INCLUDE_vTaskSuspend == 1 )
5623 if( *pxTicksToWait == portMAX_DELAY )
5625 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5626 * specified is the maximum block time then the task should block
5627 * indefinitely, and therefore never time out. */
5633 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) )
5635 /* The tick count is greater than the time at which
5636 * vTaskSetTimeout() was called, but has also overflowed since
5637 * vTaskSetTimeOut() was called. It must have wrapped all the way
5638 * around and gone past again. This passed since vTaskSetTimeout()
5641 *pxTicksToWait = ( TickType_t ) 0;
5643 else if( xElapsedTime < *pxTicksToWait )
5645 /* Not a genuine timeout. Adjust parameters for time remaining. */
5646 *pxTicksToWait -= xElapsedTime;
5647 vTaskInternalSetTimeOutState( pxTimeOut );
5652 *pxTicksToWait = ( TickType_t ) 0;
5656 taskEXIT_CRITICAL();
5658 traceRETURN_xTaskCheckForTimeOut( xReturn );
5662 /*-----------------------------------------------------------*/
5664 void vTaskMissedYield( void )
5666 traceENTER_vTaskMissedYield();
5668 /* Must be called from within a critical section. */
5669 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5671 traceRETURN_vTaskMissedYield();
5673 /*-----------------------------------------------------------*/
5675 #if ( configUSE_TRACE_FACILITY == 1 )
5677 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5679 UBaseType_t uxReturn;
5680 TCB_t const * pxTCB;
5682 traceENTER_uxTaskGetTaskNumber( xTask );
5687 uxReturn = pxTCB->uxTaskNumber;
5694 traceRETURN_uxTaskGetTaskNumber( uxReturn );
5699 #endif /* configUSE_TRACE_FACILITY */
5700 /*-----------------------------------------------------------*/
5702 #if ( configUSE_TRACE_FACILITY == 1 )
5704 void vTaskSetTaskNumber( TaskHandle_t xTask,
5705 const UBaseType_t uxHandle )
5709 traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5714 pxTCB->uxTaskNumber = uxHandle;
5717 traceRETURN_vTaskSetTaskNumber();
5720 #endif /* configUSE_TRACE_FACILITY */
5721 /*-----------------------------------------------------------*/
5724 * -----------------------------------------------------------
5725 * The passive idle task.
5726 * ----------------------------------------------------------
5728 * The passive idle task is used for all the additional cores in a SMP
5729 * system. There must be only 1 active idle task and the rest are passive
5732 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5733 * language extensions. The equivalent prototype for this function is:
5735 * void prvPassiveIdleTask( void *pvParameters );
5738 #if ( configNUMBER_OF_CORES > 1 )
5739 static portTASK_FUNCTION( prvPassiveIdleTask, pvParameters )
5741 ( void ) pvParameters;
5745 for( ; configCONTROL_INFINITE_LOOP(); )
5747 #if ( configUSE_PREEMPTION == 0 )
5749 /* If we are not using preemption we keep forcing a task switch to
5750 * see if any other task has become available. If we are using
5751 * preemption we don't need to do this as any task becoming available
5752 * will automatically get the processor anyway. */
5755 #endif /* configUSE_PREEMPTION */
5757 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5759 /* When using preemption tasks of equal priority will be
5760 * timesliced. If a task that is sharing the idle priority is ready
5761 * to run then the idle task should yield before the end of the
5764 * A critical region is not required here as we are just reading from
5765 * the list, and an occasional incorrect value will not matter. If
5766 * the ready list at the idle priority contains one more task than the
5767 * number of idle tasks, which is equal to the configured numbers of cores
5768 * then a task other than the idle task is ready to execute. */
5769 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5775 mtCOVERAGE_TEST_MARKER();
5778 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5780 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
5782 /* Call the user defined function from within the idle task. This
5783 * allows the application designer to add background functionality
5784 * without the overhead of a separate task.
5786 * This hook is intended to manage core activity such as disabling cores that go idle.
5788 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5789 * CALL A FUNCTION THAT MIGHT BLOCK. */
5790 vApplicationPassiveIdleHook();
5792 #endif /* configUSE_PASSIVE_IDLE_HOOK */
5795 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5798 * -----------------------------------------------------------
5800 * ----------------------------------------------------------
5802 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5803 * language extensions. The equivalent prototype for this function is:
5805 * void prvIdleTask( void *pvParameters );
5809 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5811 /* Stop warnings. */
5812 ( void ) pvParameters;
5814 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5815 * SCHEDULER IS STARTED. **/
5817 /* In case a task that has a secure context deletes itself, in which case
5818 * the idle task is responsible for deleting the task's secure context, if
5820 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5822 #if ( configNUMBER_OF_CORES > 1 )
5824 /* SMP all cores start up in the idle task. This initial yield gets the application
5828 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5830 for( ; configCONTROL_INFINITE_LOOP(); )
5832 /* See if any tasks have deleted themselves - if so then the idle task
5833 * is responsible for freeing the deleted task's TCB and stack. */
5834 prvCheckTasksWaitingTermination();
5836 #if ( configUSE_PREEMPTION == 0 )
5838 /* If we are not using preemption we keep forcing a task switch to
5839 * see if any other task has become available. If we are using
5840 * preemption we don't need to do this as any task becoming available
5841 * will automatically get the processor anyway. */
5844 #endif /* configUSE_PREEMPTION */
5846 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5848 /* When using preemption tasks of equal priority will be
5849 * timesliced. If a task that is sharing the idle priority is ready
5850 * to run then the idle task should yield before the end of the
5853 * A critical region is not required here as we are just reading from
5854 * the list, and an occasional incorrect value will not matter. If
5855 * the ready list at the idle priority contains one more task than the
5856 * number of idle tasks, which is equal to the configured numbers of cores
5857 * then a task other than the idle task is ready to execute. */
5858 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5864 mtCOVERAGE_TEST_MARKER();
5867 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5869 #if ( configUSE_IDLE_HOOK == 1 )
5871 /* Call the user defined function from within the idle task. */
5872 vApplicationIdleHook();
5874 #endif /* configUSE_IDLE_HOOK */
5876 /* This conditional compilation should use inequality to 0, not equality
5877 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5878 * user defined low power mode implementations require
5879 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5880 #if ( configUSE_TICKLESS_IDLE != 0 )
5882 TickType_t xExpectedIdleTime;
5884 /* It is not desirable to suspend then resume the scheduler on
5885 * each iteration of the idle task. Therefore, a preliminary
5886 * test of the expected idle time is performed without the
5887 * scheduler suspended. The result here is not necessarily
5889 xExpectedIdleTime = prvGetExpectedIdleTime();
5891 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5895 /* Now the scheduler is suspended, the expected idle
5896 * time can be sampled again, and this time its value can
5898 configASSERT( xNextTaskUnblockTime >= xTickCount );
5899 xExpectedIdleTime = prvGetExpectedIdleTime();
5901 /* Define the following macro to set xExpectedIdleTime to 0
5902 * if the application does not want
5903 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5904 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5906 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5908 traceLOW_POWER_IDLE_BEGIN();
5909 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5910 traceLOW_POWER_IDLE_END();
5914 mtCOVERAGE_TEST_MARKER();
5917 ( void ) xTaskResumeAll();
5921 mtCOVERAGE_TEST_MARKER();
5924 #endif /* configUSE_TICKLESS_IDLE */
5926 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) )
5928 /* Call the user defined function from within the idle task. This
5929 * allows the application designer to add background functionality
5930 * without the overhead of a separate task.
5932 * This hook is intended to manage core activity such as disabling cores that go idle.
5934 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5935 * CALL A FUNCTION THAT MIGHT BLOCK. */
5936 vApplicationPassiveIdleHook();
5938 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) ) */
5941 /*-----------------------------------------------------------*/
5943 #if ( configUSE_TICKLESS_IDLE != 0 )
5945 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5947 #if ( INCLUDE_vTaskSuspend == 1 )
5948 /* The idle task exists in addition to the application tasks. */
5949 const UBaseType_t uxNonApplicationTasks = configNUMBER_OF_CORES;
5950 #endif /* INCLUDE_vTaskSuspend */
5952 eSleepModeStatus eReturn = eStandardSleep;
5954 traceENTER_eTaskConfirmSleepModeStatus();
5956 /* This function must be called from a critical section. */
5958 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0U )
5960 /* A task was made ready while the scheduler was suspended. */
5961 eReturn = eAbortSleep;
5963 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5965 /* A yield was pended while the scheduler was suspended. */
5966 eReturn = eAbortSleep;
5968 else if( xPendedTicks != 0U )
5970 /* A tick interrupt has already occurred but was held pending
5971 * because the scheduler is suspended. */
5972 eReturn = eAbortSleep;
5975 #if ( INCLUDE_vTaskSuspend == 1 )
5976 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5978 /* If all the tasks are in the suspended list (which might mean they
5979 * have an infinite block time rather than actually being suspended)
5980 * then it is safe to turn all clocks off and just wait for external
5982 eReturn = eNoTasksWaitingTimeout;
5984 #endif /* INCLUDE_vTaskSuspend */
5987 mtCOVERAGE_TEST_MARKER();
5990 traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5995 #endif /* configUSE_TICKLESS_IDLE */
5996 /*-----------------------------------------------------------*/
5998 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
6000 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
6006 traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
6008 if( ( xIndex >= 0 ) &&
6009 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
6011 pxTCB = prvGetTCBFromHandle( xTaskToSet );
6012 configASSERT( pxTCB != NULL );
6013 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
6016 traceRETURN_vTaskSetThreadLocalStoragePointer();
6019 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
6020 /*-----------------------------------------------------------*/
6022 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
6024 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
6027 void * pvReturn = NULL;
6030 traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
6032 if( ( xIndex >= 0 ) &&
6033 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
6035 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
6036 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
6043 traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
6048 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
6049 /*-----------------------------------------------------------*/
6051 #if ( portUSING_MPU_WRAPPERS == 1 )
6053 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
6054 const MemoryRegion_t * const pxRegions )
6058 traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
6060 /* If null is passed in here then we are modifying the MPU settings of
6061 * the calling task. */
6062 pxTCB = prvGetTCBFromHandle( xTaskToModify );
6064 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
6066 traceRETURN_vTaskAllocateMPURegions();
6069 #endif /* portUSING_MPU_WRAPPERS */
6070 /*-----------------------------------------------------------*/
6072 static void prvInitialiseTaskLists( void )
6074 UBaseType_t uxPriority;
6076 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
6078 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
6081 vListInitialise( &xDelayedTaskList1 );
6082 vListInitialise( &xDelayedTaskList2 );
6083 vListInitialise( &xPendingReadyList );
6085 #if ( INCLUDE_vTaskDelete == 1 )
6087 vListInitialise( &xTasksWaitingTermination );
6089 #endif /* INCLUDE_vTaskDelete */
6091 #if ( INCLUDE_vTaskSuspend == 1 )
6093 vListInitialise( &xSuspendedTaskList );
6095 #endif /* INCLUDE_vTaskSuspend */
6097 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
6099 pxDelayedTaskList = &xDelayedTaskList1;
6100 pxOverflowDelayedTaskList = &xDelayedTaskList2;
6102 /*-----------------------------------------------------------*/
6104 static void prvCheckTasksWaitingTermination( void )
6106 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
6108 #if ( INCLUDE_vTaskDelete == 1 )
6112 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
6113 * being called too often in the idle task. */
6114 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6116 #if ( configNUMBER_OF_CORES == 1 )
6118 taskENTER_CRITICAL();
6121 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6122 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6123 /* coverity[misra_c_2012_rule_11_5_violation] */
6124 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6125 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6126 --uxCurrentNumberOfTasks;
6127 --uxDeletedTasksWaitingCleanUp;
6130 taskEXIT_CRITICAL();
6132 prvDeleteTCB( pxTCB );
6134 #else /* #if( configNUMBER_OF_CORES == 1 ) */
6138 taskENTER_CRITICAL();
6140 /* For SMP, multiple idles can be running simultaneously
6141 * and we need to check that other idles did not cleanup while we were
6142 * waiting to enter the critical section. */
6143 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6145 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6146 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6147 /* coverity[misra_c_2012_rule_11_5_violation] */
6148 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6150 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
6152 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6153 --uxCurrentNumberOfTasks;
6154 --uxDeletedTasksWaitingCleanUp;
6158 /* The TCB to be deleted still has not yet been switched out
6159 * by the scheduler, so we will just exit this loop early and
6160 * try again next time. */
6161 taskEXIT_CRITICAL();
6166 taskEXIT_CRITICAL();
6170 prvDeleteTCB( pxTCB );
6173 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
6176 #endif /* INCLUDE_vTaskDelete */
6178 /*-----------------------------------------------------------*/
6180 #if ( configUSE_TRACE_FACILITY == 1 )
6182 void vTaskGetInfo( TaskHandle_t xTask,
6183 TaskStatus_t * pxTaskStatus,
6184 BaseType_t xGetFreeStackSpace,
6189 traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
6191 /* xTask is NULL then get the state of the calling task. */
6192 pxTCB = prvGetTCBFromHandle( xTask );
6194 pxTaskStatus->xHandle = pxTCB;
6195 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
6196 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
6197 pxTaskStatus->pxStackBase = pxTCB->pxStack;
6198 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
6199 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
6200 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
6202 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
6204 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
6206 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
6210 #if ( configUSE_MUTEXES == 1 )
6212 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
6216 pxTaskStatus->uxBasePriority = 0;
6220 #if ( configGENERATE_RUN_TIME_STATS == 1 )
6222 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
6226 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
6230 /* Obtaining the task state is a little fiddly, so is only done if the
6231 * value of eState passed into this function is eInvalid - otherwise the
6232 * state is just set to whatever is passed in. */
6233 if( eState != eInvalid )
6235 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6237 pxTaskStatus->eCurrentState = eRunning;
6241 pxTaskStatus->eCurrentState = eState;
6243 #if ( INCLUDE_vTaskSuspend == 1 )
6245 /* If the task is in the suspended list then there is a
6246 * chance it is actually just blocked indefinitely - so really
6247 * it should be reported as being in the Blocked state. */
6248 if( eState == eSuspended )
6252 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
6254 pxTaskStatus->eCurrentState = eBlocked;
6258 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6262 /* The task does not appear on the event list item of
6263 * and of the RTOS objects, but could still be in the
6264 * blocked state if it is waiting on its notification
6265 * rather than waiting on an object. If not, is
6267 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
6269 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
6271 pxTaskStatus->eCurrentState = eBlocked;
6276 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
6279 ( void ) xTaskResumeAll();
6282 #endif /* INCLUDE_vTaskSuspend */
6284 /* Tasks can be in pending ready list and other state list at the
6285 * same time. These tasks are in ready state no matter what state
6286 * list the task is in. */
6287 taskENTER_CRITICAL();
6289 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
6291 pxTaskStatus->eCurrentState = eReady;
6294 taskEXIT_CRITICAL();
6299 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
6302 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
6303 * parameter is provided to allow it to be skipped. */
6304 if( xGetFreeStackSpace != pdFALSE )
6306 #if ( portSTACK_GROWTH > 0 )
6308 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
6312 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
6318 pxTaskStatus->usStackHighWaterMark = 0;
6321 traceRETURN_vTaskGetInfo();
6324 #endif /* configUSE_TRACE_FACILITY */
6325 /*-----------------------------------------------------------*/
6327 #if ( configUSE_TRACE_FACILITY == 1 )
6329 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
6333 configLIST_VOLATILE TCB_t * pxNextTCB;
6334 configLIST_VOLATILE TCB_t * pxFirstTCB;
6335 UBaseType_t uxTask = 0;
6337 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
6339 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6340 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6341 /* coverity[misra_c_2012_rule_11_5_violation] */
6342 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
6344 /* Populate an TaskStatus_t structure within the
6345 * pxTaskStatusArray array for each task that is referenced from
6346 * pxList. See the definition of TaskStatus_t in task.h for the
6347 * meaning of each TaskStatus_t structure member. */
6350 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6351 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6352 /* coverity[misra_c_2012_rule_11_5_violation] */
6353 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
6354 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
6356 } while( pxNextTCB != pxFirstTCB );
6360 mtCOVERAGE_TEST_MARKER();
6366 #endif /* configUSE_TRACE_FACILITY */
6367 /*-----------------------------------------------------------*/
6369 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
6371 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
6373 configSTACK_DEPTH_TYPE uxCount = 0U;
6375 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
6377 pucStackByte -= portSTACK_GROWTH;
6381 uxCount /= ( configSTACK_DEPTH_TYPE ) sizeof( StackType_t );
6386 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
6387 /*-----------------------------------------------------------*/
6389 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
6391 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
6392 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
6393 * user to determine the return type. It gets around the problem of the value
6394 * overflowing on 8-bit types without breaking backward compatibility for
6395 * applications that expect an 8-bit return type. */
6396 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
6399 uint8_t * pucEndOfStack;
6400 configSTACK_DEPTH_TYPE uxReturn;
6402 traceENTER_uxTaskGetStackHighWaterMark2( xTask );
6404 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
6405 * the same except for their return type. Using configSTACK_DEPTH_TYPE
6406 * allows the user to determine the return type. It gets around the
6407 * problem of the value overflowing on 8-bit types without breaking
6408 * backward compatibility for applications that expect an 8-bit return
6411 pxTCB = prvGetTCBFromHandle( xTask );
6413 #if portSTACK_GROWTH < 0
6415 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6419 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6423 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
6425 traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
6430 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
6431 /*-----------------------------------------------------------*/
6433 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
6435 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
6438 uint8_t * pucEndOfStack;
6439 UBaseType_t uxReturn;
6441 traceENTER_uxTaskGetStackHighWaterMark( xTask );
6443 pxTCB = prvGetTCBFromHandle( xTask );
6445 #if portSTACK_GROWTH < 0
6447 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6451 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6455 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
6457 traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6462 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6463 /*-----------------------------------------------------------*/
6465 #if ( INCLUDE_vTaskDelete == 1 )
6467 static void prvDeleteTCB( TCB_t * pxTCB )
6469 /* This call is required specifically for the TriCore port. It must be
6470 * above the vPortFree() calls. The call is also used by ports/demos that
6471 * want to allocate and clean RAM statically. */
6472 portCLEAN_UP_TCB( pxTCB );
6474 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6476 /* Free up the memory allocated for the task's TLS Block. */
6477 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6481 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6483 /* The task can only have been allocated dynamically - free both
6484 * the stack and TCB. */
6485 vPortFreeStack( pxTCB->pxStack );
6488 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
6490 /* The task could have been allocated statically or dynamically, so
6491 * check what was statically allocated before trying to free the
6493 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6495 /* Both the stack and TCB were allocated dynamically, so both
6497 vPortFreeStack( pxTCB->pxStack );
6500 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6502 /* Only the stack was statically allocated, so the TCB is the
6503 * only memory that must be freed. */
6508 /* Neither the stack nor the TCB were allocated dynamically, so
6509 * nothing needs to be freed. */
6510 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6511 mtCOVERAGE_TEST_MARKER();
6514 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6517 #endif /* INCLUDE_vTaskDelete */
6518 /*-----------------------------------------------------------*/
6520 static void prvResetNextTaskUnblockTime( void )
6522 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6524 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
6525 * the maximum possible value so it is extremely unlikely that the
6526 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6527 * there is an item in the delayed list. */
6528 xNextTaskUnblockTime = portMAX_DELAY;
6532 /* The new current delayed list is not empty, get the value of
6533 * the item at the head of the delayed list. This is the time at
6534 * which the task at the head of the delayed list should be removed
6535 * from the Blocked state. */
6536 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6539 /*-----------------------------------------------------------*/
6541 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6543 #if ( configNUMBER_OF_CORES == 1 )
6544 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6546 TaskHandle_t xReturn;
6548 traceENTER_xTaskGetCurrentTaskHandle();
6550 /* A critical section is not required as this is not called from
6551 * an interrupt and the current TCB will always be the same for any
6552 * individual execution thread. */
6553 xReturn = pxCurrentTCB;
6555 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6559 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6560 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6562 TaskHandle_t xReturn;
6563 UBaseType_t uxSavedInterruptStatus;
6565 traceENTER_xTaskGetCurrentTaskHandle();
6567 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6569 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6571 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6573 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6577 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6579 TaskHandle_t xTaskGetCurrentTaskHandleForCore( BaseType_t xCoreID )
6581 TaskHandle_t xReturn = NULL;
6583 traceENTER_xTaskGetCurrentTaskHandleForCore( xCoreID );
6585 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6587 #if ( configNUMBER_OF_CORES == 1 )
6588 xReturn = pxCurrentTCB;
6589 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6590 xReturn = pxCurrentTCBs[ xCoreID ];
6591 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6594 traceRETURN_xTaskGetCurrentTaskHandleForCore( xReturn );
6599 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
6600 /*-----------------------------------------------------------*/
6602 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6604 BaseType_t xTaskGetSchedulerState( void )
6608 traceENTER_xTaskGetSchedulerState();
6610 if( xSchedulerRunning == pdFALSE )
6612 xReturn = taskSCHEDULER_NOT_STARTED;
6616 #if ( configNUMBER_OF_CORES > 1 )
6617 taskENTER_CRITICAL();
6620 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6622 xReturn = taskSCHEDULER_RUNNING;
6626 xReturn = taskSCHEDULER_SUSPENDED;
6629 #if ( configNUMBER_OF_CORES > 1 )
6630 taskEXIT_CRITICAL();
6634 traceRETURN_xTaskGetSchedulerState( xReturn );
6639 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6640 /*-----------------------------------------------------------*/
6642 #if ( configUSE_MUTEXES == 1 )
6644 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6646 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6647 BaseType_t xReturn = pdFALSE;
6649 traceENTER_xTaskPriorityInherit( pxMutexHolder );
6651 /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6652 * inheritance is not applied in this scenario. */
6653 if( pxMutexHolder != NULL )
6655 /* If the holder of the mutex has a priority below the priority of
6656 * the task attempting to obtain the mutex then it will temporarily
6657 * inherit the priority of the task attempting to obtain the mutex. */
6658 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6660 /* Adjust the mutex holder state to account for its new
6661 * priority. Only reset the event list item value if the value is
6662 * not being used for anything else. */
6663 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0UL ) )
6665 listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority );
6669 mtCOVERAGE_TEST_MARKER();
6672 /* If the task being modified is in the ready state it will need
6673 * to be moved into a new list. */
6674 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6676 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6678 /* It is known that the task is in its ready list so
6679 * there is no need to check again and the port level
6680 * reset macro can be called directly. */
6681 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6685 mtCOVERAGE_TEST_MARKER();
6688 /* Inherit the priority before being moved into the new list. */
6689 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6690 prvAddTaskToReadyList( pxMutexHolderTCB );
6691 #if ( configNUMBER_OF_CORES > 1 )
6693 /* The priority of the task is raised. Yield for this task
6694 * if it is not running. */
6695 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6697 prvYieldForTask( pxMutexHolderTCB );
6700 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6704 /* Just inherit the priority. */
6705 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6708 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6710 /* Inheritance occurred. */
6715 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6717 /* The base priority of the mutex holder is lower than the
6718 * priority of the task attempting to take the mutex, but the
6719 * current priority of the mutex holder is not lower than the
6720 * priority of the task attempting to take the mutex.
6721 * Therefore the mutex holder must have already inherited a
6722 * priority, but inheritance would have occurred if that had
6723 * not been the case. */
6728 mtCOVERAGE_TEST_MARKER();
6734 mtCOVERAGE_TEST_MARKER();
6737 traceRETURN_xTaskPriorityInherit( xReturn );
6742 #endif /* configUSE_MUTEXES */
6743 /*-----------------------------------------------------------*/
6745 #if ( configUSE_MUTEXES == 1 )
6747 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6749 TCB_t * const pxTCB = pxMutexHolder;
6750 BaseType_t xReturn = pdFALSE;
6752 traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6754 if( pxMutexHolder != NULL )
6756 /* A task can only have an inherited priority if it holds the mutex.
6757 * If the mutex is held by a task then it cannot be given from an
6758 * interrupt, and if a mutex is given by the holding task then it must
6759 * be the running state task. */
6760 configASSERT( pxTCB == pxCurrentTCB );
6761 configASSERT( pxTCB->uxMutexesHeld );
6762 ( pxTCB->uxMutexesHeld )--;
6764 /* Has the holder of the mutex inherited the priority of another
6766 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6768 /* Only disinherit if no other mutexes are held. */
6769 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6771 /* A task can only have an inherited priority if it holds
6772 * the mutex. If the mutex is held by a task then it cannot be
6773 * given from an interrupt, and if a mutex is given by the
6774 * holding task then it must be the running state task. Remove
6775 * the holding task from the ready list. */
6776 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6778 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6782 mtCOVERAGE_TEST_MARKER();
6785 /* Disinherit the priority before adding the task into the
6786 * new ready list. */
6787 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6788 pxTCB->uxPriority = pxTCB->uxBasePriority;
6790 /* Reset the event list item value. It cannot be in use for
6791 * any other purpose if this task is running, and it must be
6792 * running to give back the mutex. */
6793 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority );
6794 prvAddTaskToReadyList( pxTCB );
6795 #if ( configNUMBER_OF_CORES > 1 )
6797 /* The priority of the task is dropped. Yield the core on
6798 * which the task is running. */
6799 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6801 prvYieldCore( pxTCB->xTaskRunState );
6804 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6806 /* Return true to indicate that a context switch is required.
6807 * This is only actually required in the corner case whereby
6808 * multiple mutexes were held and the mutexes were given back
6809 * in an order different to that in which they were taken.
6810 * If a context switch did not occur when the first mutex was
6811 * returned, even if a task was waiting on it, then a context
6812 * switch should occur when the last mutex is returned whether
6813 * a task is waiting on it or not. */
6818 mtCOVERAGE_TEST_MARKER();
6823 mtCOVERAGE_TEST_MARKER();
6828 mtCOVERAGE_TEST_MARKER();
6831 traceRETURN_xTaskPriorityDisinherit( xReturn );
6836 #endif /* configUSE_MUTEXES */
6837 /*-----------------------------------------------------------*/
6839 #if ( configUSE_MUTEXES == 1 )
6841 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6842 UBaseType_t uxHighestPriorityWaitingTask )
6844 TCB_t * const pxTCB = pxMutexHolder;
6845 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6846 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6848 traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6850 if( pxMutexHolder != NULL )
6852 /* If pxMutexHolder is not NULL then the holder must hold at least
6854 configASSERT( pxTCB->uxMutexesHeld );
6856 /* Determine the priority to which the priority of the task that
6857 * holds the mutex should be set. This will be the greater of the
6858 * holding task's base priority and the priority of the highest
6859 * priority task that is waiting to obtain the mutex. */
6860 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6862 uxPriorityToUse = uxHighestPriorityWaitingTask;
6866 uxPriorityToUse = pxTCB->uxBasePriority;
6869 /* Does the priority need to change? */
6870 if( pxTCB->uxPriority != uxPriorityToUse )
6872 /* Only disinherit if no other mutexes are held. This is a
6873 * simplification in the priority inheritance implementation. If
6874 * the task that holds the mutex is also holding other mutexes then
6875 * the other mutexes may have caused the priority inheritance. */
6876 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6878 /* If a task has timed out because it already holds the
6879 * mutex it was trying to obtain then it cannot of inherited
6880 * its own priority. */
6881 configASSERT( pxTCB != pxCurrentTCB );
6883 /* Disinherit the priority, remembering the previous
6884 * priority to facilitate determining the subject task's
6886 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6887 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6888 pxTCB->uxPriority = uxPriorityToUse;
6890 /* Only reset the event list item value if the value is not
6891 * being used for anything else. */
6892 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0UL ) )
6894 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse );
6898 mtCOVERAGE_TEST_MARKER();
6901 /* If the running task is not the task that holds the mutex
6902 * then the task that holds the mutex could be in either the
6903 * Ready, Blocked or Suspended states. Only remove the task
6904 * from its current state list if it is in the Ready state as
6905 * the task's priority is going to change and there is one
6906 * Ready list per priority. */
6907 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6909 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6911 /* It is known that the task is in its ready list so
6912 * there is no need to check again and the port level
6913 * reset macro can be called directly. */
6914 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6918 mtCOVERAGE_TEST_MARKER();
6921 prvAddTaskToReadyList( pxTCB );
6922 #if ( configNUMBER_OF_CORES > 1 )
6924 /* The priority of the task is dropped. Yield the core on
6925 * which the task is running. */
6926 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6928 prvYieldCore( pxTCB->xTaskRunState );
6931 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6935 mtCOVERAGE_TEST_MARKER();
6940 mtCOVERAGE_TEST_MARKER();
6945 mtCOVERAGE_TEST_MARKER();
6950 mtCOVERAGE_TEST_MARKER();
6953 traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6956 #endif /* configUSE_MUTEXES */
6957 /*-----------------------------------------------------------*/
6959 #if ( configNUMBER_OF_CORES > 1 )
6961 /* If not in a critical section then yield immediately.
6962 * Otherwise set xYieldPendings to true to wait to
6963 * yield until exiting the critical section.
6965 void vTaskYieldWithinAPI( void )
6967 traceENTER_vTaskYieldWithinAPI();
6969 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6975 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6978 traceRETURN_vTaskYieldWithinAPI();
6980 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6982 /*-----------------------------------------------------------*/
6984 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6986 void vTaskEnterCritical( void )
6988 traceENTER_vTaskEnterCritical();
6990 portDISABLE_INTERRUPTS();
6992 if( xSchedulerRunning != pdFALSE )
6994 ( pxCurrentTCB->uxCriticalNesting )++;
6996 /* This is not the interrupt safe version of the enter critical
6997 * function so assert() if it is being called from an interrupt
6998 * context. Only API functions that end in "FromISR" can be used in an
6999 * interrupt. Only assert if the critical nesting count is 1 to
7000 * protect against recursive calls if the assert function also uses a
7001 * critical section. */
7002 if( pxCurrentTCB->uxCriticalNesting == 1U )
7004 portASSERT_IF_IN_ISR();
7009 mtCOVERAGE_TEST_MARKER();
7012 traceRETURN_vTaskEnterCritical();
7015 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
7016 /*-----------------------------------------------------------*/
7018 #if ( configNUMBER_OF_CORES > 1 )
7020 void vTaskEnterCritical( void )
7022 traceENTER_vTaskEnterCritical();
7024 portDISABLE_INTERRUPTS();
7026 if( xSchedulerRunning != pdFALSE )
7028 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7030 portGET_TASK_LOCK();
7034 portINCREMENT_CRITICAL_NESTING_COUNT();
7036 /* This is not the interrupt safe version of the enter critical
7037 * function so assert() if it is being called from an interrupt
7038 * context. Only API functions that end in "FromISR" can be used in an
7039 * interrupt. Only assert if the critical nesting count is 1 to
7040 * protect against recursive calls if the assert function also uses a
7041 * critical section. */
7042 if( portGET_CRITICAL_NESTING_COUNT() == 1U )
7044 portASSERT_IF_IN_ISR();
7046 if( uxSchedulerSuspended == 0U )
7048 /* The only time there would be a problem is if this is called
7049 * before a context switch and vTaskExitCritical() is called
7050 * after pxCurrentTCB changes. Therefore this should not be
7051 * used within vTaskSwitchContext(). */
7052 prvCheckForRunStateChange();
7058 mtCOVERAGE_TEST_MARKER();
7061 traceRETURN_vTaskEnterCritical();
7064 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7066 /*-----------------------------------------------------------*/
7068 #if ( configNUMBER_OF_CORES > 1 )
7070 UBaseType_t vTaskEnterCriticalFromISR( void )
7072 UBaseType_t uxSavedInterruptStatus = 0;
7074 traceENTER_vTaskEnterCriticalFromISR();
7076 if( xSchedulerRunning != pdFALSE )
7078 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
7080 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7085 portINCREMENT_CRITICAL_NESTING_COUNT();
7089 mtCOVERAGE_TEST_MARKER();
7092 traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
7094 return uxSavedInterruptStatus;
7097 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7098 /*-----------------------------------------------------------*/
7100 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
7102 void vTaskExitCritical( void )
7104 traceENTER_vTaskExitCritical();
7106 if( xSchedulerRunning != pdFALSE )
7108 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
7109 * does not match a previous call to vTaskEnterCritical(). */
7110 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
7112 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7113 * to exit critical section from ISR. */
7114 portASSERT_IF_IN_ISR();
7116 if( pxCurrentTCB->uxCriticalNesting > 0U )
7118 ( pxCurrentTCB->uxCriticalNesting )--;
7120 if( pxCurrentTCB->uxCriticalNesting == 0U )
7122 portENABLE_INTERRUPTS();
7126 mtCOVERAGE_TEST_MARKER();
7131 mtCOVERAGE_TEST_MARKER();
7136 mtCOVERAGE_TEST_MARKER();
7139 traceRETURN_vTaskExitCritical();
7142 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
7143 /*-----------------------------------------------------------*/
7145 #if ( configNUMBER_OF_CORES > 1 )
7147 void vTaskExitCritical( void )
7149 traceENTER_vTaskExitCritical();
7151 if( xSchedulerRunning != pdFALSE )
7153 /* If critical nesting count is zero then this function
7154 * does not match a previous call to vTaskEnterCritical(). */
7155 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7157 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7158 * to exit critical section from ISR. */
7159 portASSERT_IF_IN_ISR();
7161 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7163 portDECREMENT_CRITICAL_NESTING_COUNT();
7165 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7167 BaseType_t xYieldCurrentTask;
7169 /* Get the xYieldPending stats inside the critical section. */
7170 xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
7172 portRELEASE_ISR_LOCK();
7173 portRELEASE_TASK_LOCK();
7174 portENABLE_INTERRUPTS();
7176 /* When a task yields in a critical section it just sets
7177 * xYieldPending to true. So now that we have exited the
7178 * critical section check if xYieldPending is true, and
7180 if( xYieldCurrentTask != pdFALSE )
7187 mtCOVERAGE_TEST_MARKER();
7192 mtCOVERAGE_TEST_MARKER();
7197 mtCOVERAGE_TEST_MARKER();
7200 traceRETURN_vTaskExitCritical();
7203 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7204 /*-----------------------------------------------------------*/
7206 #if ( configNUMBER_OF_CORES > 1 )
7208 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
7210 traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
7212 if( xSchedulerRunning != pdFALSE )
7214 /* If critical nesting count is zero then this function
7215 * does not match a previous call to vTaskEnterCritical(). */
7216 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7218 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7220 portDECREMENT_CRITICAL_NESTING_COUNT();
7222 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7224 portRELEASE_ISR_LOCK();
7225 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
7229 mtCOVERAGE_TEST_MARKER();
7234 mtCOVERAGE_TEST_MARKER();
7239 mtCOVERAGE_TEST_MARKER();
7242 traceRETURN_vTaskExitCriticalFromISR();
7245 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7246 /*-----------------------------------------------------------*/
7248 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
7250 static char * prvWriteNameToBuffer( char * pcBuffer,
7251 const char * pcTaskName )
7255 /* Start by copying the entire string. */
7256 ( void ) strcpy( pcBuffer, pcTaskName );
7258 /* Pad the end of the string with spaces to ensure columns line up when
7260 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
7262 pcBuffer[ x ] = ' ';
7266 pcBuffer[ x ] = ( char ) 0x00;
7268 /* Return the new end of string. */
7269 return &( pcBuffer[ x ] );
7272 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
7273 /*-----------------------------------------------------------*/
7275 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
7277 void vTaskListTasks( char * pcWriteBuffer,
7278 size_t uxBufferLength )
7280 TaskStatus_t * pxTaskStatusArray;
7281 size_t uxConsumedBufferLength = 0;
7282 size_t uxCharsWrittenBySnprintf;
7283 int iSnprintfReturnValue;
7284 BaseType_t xOutputBufferFull = pdFALSE;
7285 UBaseType_t uxArraySize, x;
7288 traceENTER_vTaskListTasks( pcWriteBuffer, uxBufferLength );
7293 * This function is provided for convenience only, and is used by many
7294 * of the demo applications. Do not consider it to be part of the
7297 * vTaskListTasks() calls uxTaskGetSystemState(), then formats part of the
7298 * uxTaskGetSystemState() output into a human readable table that
7299 * displays task: names, states, priority, stack usage and task number.
7300 * Stack usage specified as the number of unused StackType_t words stack can hold
7301 * on top of stack - not the number of bytes.
7303 * vTaskListTasks() has a dependency on the snprintf() C library function that
7304 * might bloat the code size, use a lot of stack, and provide different
7305 * results on different platforms. An alternative, tiny, third party,
7306 * and limited functionality implementation of snprintf() is provided in
7307 * many of the FreeRTOS/Demo sub-directories in a file called
7308 * printf-stdarg.c (note printf-stdarg.c does not provide a full
7309 * snprintf() implementation!).
7311 * It is recommended that production systems call uxTaskGetSystemState()
7312 * directly to get access to raw stats data, rather than indirectly
7313 * through a call to vTaskListTasks().
7317 /* Make sure the write buffer does not contain a string. */
7318 *pcWriteBuffer = ( char ) 0x00;
7320 /* Take a snapshot of the number of tasks in case it changes while this
7321 * function is executing. */
7322 uxArraySize = uxCurrentNumberOfTasks;
7324 /* Allocate an array index for each task. NOTE! if
7325 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7326 * equate to NULL. */
7327 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7328 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7329 /* coverity[misra_c_2012_rule_11_5_violation] */
7330 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7332 if( pxTaskStatusArray != NULL )
7334 /* Generate the (binary) data. */
7335 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
7337 /* Create a human readable table from the binary data. */
7338 for( x = 0; x < uxArraySize; x++ )
7340 switch( pxTaskStatusArray[ x ].eCurrentState )
7343 cStatus = tskRUNNING_CHAR;
7347 cStatus = tskREADY_CHAR;
7351 cStatus = tskBLOCKED_CHAR;
7355 cStatus = tskSUSPENDED_CHAR;
7359 cStatus = tskDELETED_CHAR;
7362 case eInvalid: /* Fall through. */
7363 default: /* Should not get here, but it is included
7364 * to prevent static checking errors. */
7365 cStatus = ( char ) 0x00;
7369 /* Is there enough space in the buffer to hold task name? */
7370 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7372 /* Write the task name to the string, padding with spaces so it
7373 * can be printed in tabular form more easily. */
7374 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7375 /* Do not count the terminating null character. */
7376 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7378 /* Is there space left in the buffer? -1 is done because snprintf
7379 * writes a terminating null character. So we are essentially
7380 * checking if the buffer has space to write at least one non-null
7382 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7384 /* Write the rest of the string. */
7385 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
7386 /* MISRA Ref 21.6.1 [snprintf for utility] */
7387 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7388 /* coverity[misra_c_2012_rule_21_6_violation] */
7389 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7390 uxBufferLength - uxConsumedBufferLength,
7391 "\t%c\t%u\t%u\t%u\t0x%x\r\n",
7393 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7394 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7395 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber,
7396 ( unsigned int ) pxTaskStatusArray[ x ].uxCoreAffinityMask );
7397 #else /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7398 /* MISRA Ref 21.6.1 [snprintf for utility] */
7399 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7400 /* coverity[misra_c_2012_rule_21_6_violation] */
7401 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7402 uxBufferLength - uxConsumedBufferLength,
7403 "\t%c\t%u\t%u\t%u\r\n",
7405 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7406 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7407 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber );
7408 #endif /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7409 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7411 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7412 pcWriteBuffer += uxCharsWrittenBySnprintf;
7416 xOutputBufferFull = pdTRUE;
7421 xOutputBufferFull = pdTRUE;
7424 if( xOutputBufferFull == pdTRUE )
7430 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7431 * is 0 then vPortFree() will be #defined to nothing. */
7432 vPortFree( pxTaskStatusArray );
7436 mtCOVERAGE_TEST_MARKER();
7439 traceRETURN_vTaskListTasks();
7442 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7443 /*----------------------------------------------------------*/
7445 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
7447 void vTaskGetRunTimeStatistics( char * pcWriteBuffer,
7448 size_t uxBufferLength )
7450 TaskStatus_t * pxTaskStatusArray;
7451 size_t uxConsumedBufferLength = 0;
7452 size_t uxCharsWrittenBySnprintf;
7453 int iSnprintfReturnValue;
7454 BaseType_t xOutputBufferFull = pdFALSE;
7455 UBaseType_t uxArraySize, x;
7456 configRUN_TIME_COUNTER_TYPE ulTotalTime = 0;
7457 configRUN_TIME_COUNTER_TYPE ulStatsAsPercentage;
7459 traceENTER_vTaskGetRunTimeStatistics( pcWriteBuffer, uxBufferLength );
7464 * This function is provided for convenience only, and is used by many
7465 * of the demo applications. Do not consider it to be part of the
7468 * vTaskGetRunTimeStatistics() calls uxTaskGetSystemState(), then formats part
7469 * of the uxTaskGetSystemState() output into a human readable table that
7470 * displays the amount of time each task has spent in the Running state
7471 * in both absolute and percentage terms.
7473 * vTaskGetRunTimeStatistics() has a dependency on the snprintf() C library
7474 * function that might bloat the code size, use a lot of stack, and
7475 * provide different results on different platforms. An alternative,
7476 * tiny, third party, and limited functionality implementation of
7477 * snprintf() is provided in many of the FreeRTOS/Demo sub-directories in
7478 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
7479 * a full snprintf() implementation!).
7481 * It is recommended that production systems call uxTaskGetSystemState()
7482 * directly to get access to raw stats data, rather than indirectly
7483 * through a call to vTaskGetRunTimeStatistics().
7486 /* Make sure the write buffer does not contain a string. */
7487 *pcWriteBuffer = ( char ) 0x00;
7489 /* Take a snapshot of the number of tasks in case it changes while this
7490 * function is executing. */
7491 uxArraySize = uxCurrentNumberOfTasks;
7493 /* Allocate an array index for each task. NOTE! If
7494 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7495 * equate to NULL. */
7496 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7497 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7498 /* coverity[misra_c_2012_rule_11_5_violation] */
7499 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7501 if( pxTaskStatusArray != NULL )
7503 /* Generate the (binary) data. */
7504 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
7506 /* For percentage calculations. */
7507 ulTotalTime /= ( ( configRUN_TIME_COUNTER_TYPE ) 100UL );
7509 /* Avoid divide by zero errors. */
7510 if( ulTotalTime > 0UL )
7512 /* Create a human readable table from the binary data. */
7513 for( x = 0; x < uxArraySize; x++ )
7515 /* What percentage of the total run time has the task used?
7516 * This will always be rounded down to the nearest integer.
7517 * ulTotalRunTime has already been divided by 100. */
7518 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
7520 /* Is there enough space in the buffer to hold task name? */
7521 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7523 /* Write the task name to the string, padding with
7524 * spaces so it can be printed in tabular form more
7526 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7527 /* Do not count the terminating null character. */
7528 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7530 /* Is there space left in the buffer? -1 is done because snprintf
7531 * writes a terminating null character. So we are essentially
7532 * checking if the buffer has space to write at least one non-null
7534 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7536 if( ulStatsAsPercentage > 0UL )
7538 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7540 /* MISRA Ref 21.6.1 [snprintf for utility] */
7541 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7542 /* coverity[misra_c_2012_rule_21_6_violation] */
7543 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7544 uxBufferLength - uxConsumedBufferLength,
7545 "\t%lu\t\t%lu%%\r\n",
7546 pxTaskStatusArray[ x ].ulRunTimeCounter,
7547 ulStatsAsPercentage );
7549 #else /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7551 /* sizeof( int ) == sizeof( long ) so a smaller
7552 * printf() library can be used. */
7553 /* MISRA Ref 21.6.1 [snprintf for utility] */
7554 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7555 /* coverity[misra_c_2012_rule_21_6_violation] */
7556 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7557 uxBufferLength - uxConsumedBufferLength,
7559 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter,
7560 ( unsigned int ) ulStatsAsPercentage );
7562 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7566 /* If the percentage is zero here then the task has
7567 * consumed less than 1% of the total run time. */
7568 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7570 /* MISRA Ref 21.6.1 [snprintf for utility] */
7571 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7572 /* coverity[misra_c_2012_rule_21_6_violation] */
7573 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7574 uxBufferLength - uxConsumedBufferLength,
7575 "\t%lu\t\t<1%%\r\n",
7576 pxTaskStatusArray[ x ].ulRunTimeCounter );
7580 /* sizeof( int ) == sizeof( long ) so a smaller
7581 * printf() library can be used. */
7582 /* MISRA Ref 21.6.1 [snprintf for utility] */
7583 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
7584 /* coverity[misra_c_2012_rule_21_6_violation] */
7585 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7586 uxBufferLength - uxConsumedBufferLength,
7588 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter );
7590 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7593 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7594 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7595 pcWriteBuffer += uxCharsWrittenBySnprintf;
7599 xOutputBufferFull = pdTRUE;
7604 xOutputBufferFull = pdTRUE;
7607 if( xOutputBufferFull == pdTRUE )
7615 mtCOVERAGE_TEST_MARKER();
7618 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7619 * is 0 then vPortFree() will be #defined to nothing. */
7620 vPortFree( pxTaskStatusArray );
7624 mtCOVERAGE_TEST_MARKER();
7627 traceRETURN_vTaskGetRunTimeStatistics();
7630 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7631 /*-----------------------------------------------------------*/
7633 TickType_t uxTaskResetEventItemValue( void )
7635 TickType_t uxReturn;
7637 traceENTER_uxTaskResetEventItemValue();
7639 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7641 /* Reset the event list item to its normal value - so it can be used with
7642 * queues and semaphores. */
7643 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) );
7645 traceRETURN_uxTaskResetEventItemValue( uxReturn );
7649 /*-----------------------------------------------------------*/
7651 #if ( configUSE_MUTEXES == 1 )
7653 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7657 traceENTER_pvTaskIncrementMutexHeldCount();
7659 pxTCB = pxCurrentTCB;
7661 /* If xSemaphoreCreateMutex() is called before any tasks have been created
7662 * then pxCurrentTCB will be NULL. */
7665 ( pxTCB->uxMutexesHeld )++;
7668 traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7673 #endif /* configUSE_MUTEXES */
7674 /*-----------------------------------------------------------*/
7676 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7678 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7679 BaseType_t xClearCountOnExit,
7680 TickType_t xTicksToWait )
7683 BaseType_t xAlreadyYielded, xShouldBlock = pdFALSE;
7685 traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7687 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7689 /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
7690 * non-deterministic operation. */
7693 /* We MUST enter a critical section to atomically check if a notification
7694 * has occurred and set the flag to indicate that we are waiting for
7695 * a notification. If we do not do so, a notification sent from an ISR
7697 taskENTER_CRITICAL();
7699 /* Only block if the notification count is not already non-zero. */
7700 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0UL )
7702 /* Mark this task as waiting for a notification. */
7703 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7705 if( xTicksToWait > ( TickType_t ) 0 )
7707 xShouldBlock = pdTRUE;
7711 mtCOVERAGE_TEST_MARKER();
7716 mtCOVERAGE_TEST_MARKER();
7719 taskEXIT_CRITICAL();
7721 /* We are now out of the critical section but the scheduler is still
7722 * suspended, so we are safe to do non-deterministic operations such
7723 * as prvAddCurrentTaskToDelayedList. */
7724 if( xShouldBlock == pdTRUE )
7726 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7727 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7731 mtCOVERAGE_TEST_MARKER();
7734 xAlreadyYielded = xTaskResumeAll();
7736 /* Force a reschedule if xTaskResumeAll has not already done so. */
7737 if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
7739 taskYIELD_WITHIN_API();
7743 mtCOVERAGE_TEST_MARKER();
7746 taskENTER_CRITICAL();
7748 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7749 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7751 if( ulReturn != 0UL )
7753 if( xClearCountOnExit != pdFALSE )
7755 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ( uint32_t ) 0UL;
7759 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7764 mtCOVERAGE_TEST_MARKER();
7767 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7769 taskEXIT_CRITICAL();
7771 traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7776 #endif /* configUSE_TASK_NOTIFICATIONS */
7777 /*-----------------------------------------------------------*/
7779 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7781 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7782 uint32_t ulBitsToClearOnEntry,
7783 uint32_t ulBitsToClearOnExit,
7784 uint32_t * pulNotificationValue,
7785 TickType_t xTicksToWait )
7787 BaseType_t xReturn, xAlreadyYielded, xShouldBlock = pdFALSE;
7789 traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7791 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7793 /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
7794 * non-deterministic operation. */
7797 /* We MUST enter a critical section to atomically check and update the
7798 * task notification value. If we do not do so, a notification from
7799 * an ISR will get lost. */
7800 taskENTER_CRITICAL();
7802 /* Only block if a notification is not already pending. */
7803 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7805 /* Clear bits in the task's notification value as bits may get
7806 * set by the notifying task or interrupt. This can be used
7807 * to clear the value to zero. */
7808 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7810 /* Mark this task as waiting for a notification. */
7811 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7813 if( xTicksToWait > ( TickType_t ) 0 )
7815 xShouldBlock = pdTRUE;
7819 mtCOVERAGE_TEST_MARKER();
7824 mtCOVERAGE_TEST_MARKER();
7827 taskEXIT_CRITICAL();
7829 /* We are now out of the critical section but the scheduler is still
7830 * suspended, so we are safe to do non-deterministic operations such
7831 * as prvAddCurrentTaskToDelayedList. */
7832 if( xShouldBlock == pdTRUE )
7834 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7835 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7839 mtCOVERAGE_TEST_MARKER();
7842 xAlreadyYielded = xTaskResumeAll();
7844 /* Force a reschedule if xTaskResumeAll has not already done so. */
7845 if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
7847 taskYIELD_WITHIN_API();
7851 mtCOVERAGE_TEST_MARKER();
7854 taskENTER_CRITICAL();
7856 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7858 if( pulNotificationValue != NULL )
7860 /* Output the current notification value, which may or may not
7862 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7865 /* If ucNotifyValue is set then either the task never entered the
7866 * blocked state (because a notification was already pending) or the
7867 * task unblocked because of a notification. Otherwise the task
7868 * unblocked because of a timeout. */
7869 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7871 /* A notification was not received. */
7876 /* A notification was already pending or a notification was
7877 * received while the task was waiting. */
7878 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7882 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7884 taskEXIT_CRITICAL();
7886 traceRETURN_xTaskGenericNotifyWait( xReturn );
7891 #endif /* configUSE_TASK_NOTIFICATIONS */
7892 /*-----------------------------------------------------------*/
7894 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7896 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7897 UBaseType_t uxIndexToNotify,
7899 eNotifyAction eAction,
7900 uint32_t * pulPreviousNotificationValue )
7903 BaseType_t xReturn = pdPASS;
7904 uint8_t ucOriginalNotifyState;
7906 traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7908 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7909 configASSERT( xTaskToNotify );
7910 pxTCB = xTaskToNotify;
7912 taskENTER_CRITICAL();
7914 if( pulPreviousNotificationValue != NULL )
7916 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7919 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7921 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7926 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7930 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7933 case eSetValueWithOverwrite:
7934 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7937 case eSetValueWithoutOverwrite:
7939 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7941 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7945 /* The value could not be written to the task. */
7953 /* The task is being notified without its notify value being
7959 /* Should not get here if all enums are handled.
7960 * Artificially force an assert by testing a value the
7961 * compiler can't assume is const. */
7962 configASSERT( xTickCount == ( TickType_t ) 0 );
7967 traceTASK_NOTIFY( uxIndexToNotify );
7969 /* If the task is in the blocked state specifically to wait for a
7970 * notification then unblock it now. */
7971 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7973 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7974 prvAddTaskToReadyList( pxTCB );
7976 /* The task should not have been on an event list. */
7977 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7979 #if ( configUSE_TICKLESS_IDLE != 0 )
7981 /* If a task is blocked waiting for a notification then
7982 * xNextTaskUnblockTime might be set to the blocked task's time
7983 * out time. If the task is unblocked for a reason other than
7984 * a timeout xNextTaskUnblockTime is normally left unchanged,
7985 * because it will automatically get reset to a new value when
7986 * the tick count equals xNextTaskUnblockTime. However if
7987 * tickless idling is used it might be more important to enter
7988 * sleep mode at the earliest possible time - so reset
7989 * xNextTaskUnblockTime here to ensure it is updated at the
7990 * earliest possible time. */
7991 prvResetNextTaskUnblockTime();
7995 /* Check if the notified task has a priority above the currently
7996 * executing task. */
7997 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
8001 mtCOVERAGE_TEST_MARKER();
8004 taskEXIT_CRITICAL();
8006 traceRETURN_xTaskGenericNotify( xReturn );
8011 #endif /* configUSE_TASK_NOTIFICATIONS */
8012 /*-----------------------------------------------------------*/
8014 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8016 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
8017 UBaseType_t uxIndexToNotify,
8019 eNotifyAction eAction,
8020 uint32_t * pulPreviousNotificationValue,
8021 BaseType_t * pxHigherPriorityTaskWoken )
8024 uint8_t ucOriginalNotifyState;
8025 BaseType_t xReturn = pdPASS;
8026 UBaseType_t uxSavedInterruptStatus;
8028 traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
8030 configASSERT( xTaskToNotify );
8031 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8033 /* RTOS ports that support interrupt nesting have the concept of a
8034 * maximum system call (or maximum API call) interrupt priority.
8035 * Interrupts that are above the maximum system call priority are keep
8036 * permanently enabled, even when the RTOS kernel is in a critical section,
8037 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
8038 * is defined in FreeRTOSConfig.h then
8039 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
8040 * failure if a FreeRTOS API function is called from an interrupt that has
8041 * been assigned a priority above the configured maximum system call
8042 * priority. Only FreeRTOS functions that end in FromISR can be called
8043 * from interrupts that have been assigned a priority at or (logically)
8044 * below the maximum system call interrupt priority. FreeRTOS maintains a
8045 * separate interrupt safe API to ensure interrupt entry is as fast and as
8046 * simple as possible. More information (albeit Cortex-M specific) is
8047 * provided on the following link:
8048 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
8049 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
8051 pxTCB = xTaskToNotify;
8053 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
8055 if( pulPreviousNotificationValue != NULL )
8057 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
8060 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8061 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8066 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
8070 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8073 case eSetValueWithOverwrite:
8074 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8077 case eSetValueWithoutOverwrite:
8079 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
8081 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8085 /* The value could not be written to the task. */
8093 /* The task is being notified without its notify value being
8099 /* Should not get here if all enums are handled.
8100 * Artificially force an assert by testing a value the
8101 * compiler can't assume is const. */
8102 configASSERT( xTickCount == ( TickType_t ) 0 );
8106 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
8108 /* If the task is in the blocked state specifically to wait for a
8109 * notification then unblock it now. */
8110 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8112 /* The task should not have been on an event list. */
8113 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8115 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8117 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8118 prvAddTaskToReadyList( pxTCB );
8122 /* The delayed and ready lists cannot be accessed, so hold
8123 * this task pending until the scheduler is resumed. */
8124 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8127 #if ( configNUMBER_OF_CORES == 1 )
8129 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8131 /* The notified task has a priority above the currently
8132 * executing task so a yield is required. */
8133 if( pxHigherPriorityTaskWoken != NULL )
8135 *pxHigherPriorityTaskWoken = pdTRUE;
8138 /* Mark that a yield is pending in case the user is not
8139 * using the "xHigherPriorityTaskWoken" parameter to an ISR
8140 * safe FreeRTOS function. */
8141 xYieldPendings[ 0 ] = pdTRUE;
8145 mtCOVERAGE_TEST_MARKER();
8148 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8150 #if ( configUSE_PREEMPTION == 1 )
8152 prvYieldForTask( pxTCB );
8154 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8156 if( pxHigherPriorityTaskWoken != NULL )
8158 *pxHigherPriorityTaskWoken = pdTRUE;
8162 #endif /* if ( configUSE_PREEMPTION == 1 ) */
8164 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8167 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8169 traceRETURN_xTaskGenericNotifyFromISR( xReturn );
8174 #endif /* configUSE_TASK_NOTIFICATIONS */
8175 /*-----------------------------------------------------------*/
8177 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8179 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
8180 UBaseType_t uxIndexToNotify,
8181 BaseType_t * pxHigherPriorityTaskWoken )
8184 uint8_t ucOriginalNotifyState;
8185 UBaseType_t uxSavedInterruptStatus;
8187 traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
8189 configASSERT( xTaskToNotify );
8190 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8192 /* RTOS ports that support interrupt nesting have the concept of a
8193 * maximum system call (or maximum API call) interrupt priority.
8194 * Interrupts that are above the maximum system call priority are keep
8195 * permanently enabled, even when the RTOS kernel is in a critical section,
8196 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
8197 * is defined in FreeRTOSConfig.h then
8198 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
8199 * failure if a FreeRTOS API function is called from an interrupt that has
8200 * been assigned a priority above the configured maximum system call
8201 * priority. Only FreeRTOS functions that end in FromISR can be called
8202 * from interrupts that have been assigned a priority at or (logically)
8203 * below the maximum system call interrupt priority. FreeRTOS maintains a
8204 * separate interrupt safe API to ensure interrupt entry is as fast and as
8205 * simple as possible. More information (albeit Cortex-M specific) is
8206 * provided on the following link:
8207 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
8208 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
8210 pxTCB = xTaskToNotify;
8212 uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
8214 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8215 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8217 /* 'Giving' is equivalent to incrementing a count in a counting
8219 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8221 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
8223 /* If the task is in the blocked state specifically to wait for a
8224 * notification then unblock it now. */
8225 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8227 /* The task should not have been on an event list. */
8228 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8230 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8232 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8233 prvAddTaskToReadyList( pxTCB );
8237 /* The delayed and ready lists cannot be accessed, so hold
8238 * this task pending until the scheduler is resumed. */
8239 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8242 #if ( configNUMBER_OF_CORES == 1 )
8244 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8246 /* The notified task has a priority above the currently
8247 * executing task so a yield is required. */
8248 if( pxHigherPriorityTaskWoken != NULL )
8250 *pxHigherPriorityTaskWoken = pdTRUE;
8253 /* Mark that a yield is pending in case the user is not
8254 * using the "xHigherPriorityTaskWoken" parameter in an ISR
8255 * safe FreeRTOS function. */
8256 xYieldPendings[ 0 ] = pdTRUE;
8260 mtCOVERAGE_TEST_MARKER();
8263 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8265 #if ( configUSE_PREEMPTION == 1 )
8267 prvYieldForTask( pxTCB );
8269 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8271 if( pxHigherPriorityTaskWoken != NULL )
8273 *pxHigherPriorityTaskWoken = pdTRUE;
8277 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
8279 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8282 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8284 traceRETURN_vTaskGenericNotifyGiveFromISR();
8287 #endif /* configUSE_TASK_NOTIFICATIONS */
8288 /*-----------------------------------------------------------*/
8290 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8292 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
8293 UBaseType_t uxIndexToClear )
8298 traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
8300 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8302 /* If null is passed in here then it is the calling task that is having
8303 * its notification state cleared. */
8304 pxTCB = prvGetTCBFromHandle( xTask );
8306 taskENTER_CRITICAL();
8308 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
8310 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
8318 taskEXIT_CRITICAL();
8320 traceRETURN_xTaskGenericNotifyStateClear( xReturn );
8325 #endif /* configUSE_TASK_NOTIFICATIONS */
8326 /*-----------------------------------------------------------*/
8328 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8330 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
8331 UBaseType_t uxIndexToClear,
8332 uint32_t ulBitsToClear )
8337 traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
8339 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8341 /* If null is passed in here then it is the calling task that is having
8342 * its notification state cleared. */
8343 pxTCB = prvGetTCBFromHandle( xTask );
8345 taskENTER_CRITICAL();
8347 /* Return the notification as it was before the bits were cleared,
8348 * then clear the bit mask. */
8349 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
8350 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
8352 taskEXIT_CRITICAL();
8354 traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
8359 #endif /* configUSE_TASK_NOTIFICATIONS */
8360 /*-----------------------------------------------------------*/
8362 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8364 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
8368 traceENTER_ulTaskGetRunTimeCounter( xTask );
8370 pxTCB = prvGetTCBFromHandle( xTask );
8372 traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
8374 return pxTCB->ulRunTimeCounter;
8377 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8378 /*-----------------------------------------------------------*/
8380 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8382 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
8385 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8387 traceENTER_ulTaskGetRunTimePercent( xTask );
8389 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
8391 /* For percentage calculations. */
8392 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8394 /* Avoid divide by zero errors. */
8395 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8397 pxTCB = prvGetTCBFromHandle( xTask );
8398 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
8405 traceRETURN_ulTaskGetRunTimePercent( ulReturn );
8410 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8411 /*-----------------------------------------------------------*/
8413 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8415 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
8417 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
8420 traceENTER_ulTaskGetIdleRunTimeCounter();
8422 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8424 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8427 traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
8432 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8433 /*-----------------------------------------------------------*/
8435 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8437 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
8439 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8440 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
8443 traceENTER_ulTaskGetIdleRunTimePercent();
8445 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
8447 /* For percentage calculations. */
8448 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8450 /* Avoid divide by zero errors. */
8451 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8453 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8455 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8458 ulReturn = ulRunTimeCounter / ulTotalTime;
8465 traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
8470 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8471 /*-----------------------------------------------------------*/
8473 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
8474 const BaseType_t xCanBlockIndefinitely )
8476 TickType_t xTimeToWake;
8477 const TickType_t xConstTickCount = xTickCount;
8478 List_t * const pxDelayedList = pxDelayedTaskList;
8479 List_t * const pxOverflowDelayedList = pxOverflowDelayedTaskList;
8481 #if ( INCLUDE_xTaskAbortDelay == 1 )
8483 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
8484 * reset to pdFALSE so it can be detected as having been set to pdTRUE
8485 * when the task leaves the Blocked state. */
8486 pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
8490 /* Remove the task from the ready list before adding it to the blocked list
8491 * as the same list item is used for both lists. */
8492 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
8494 /* The current task must be in a ready list, so there is no need to
8495 * check, and the port reset macro can be called directly. */
8496 portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
8500 mtCOVERAGE_TEST_MARKER();
8503 #if ( INCLUDE_vTaskSuspend == 1 )
8505 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
8507 /* Add the task to the suspended task list instead of a delayed task
8508 * list to ensure it is not woken by a timing event. It will block
8510 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
8514 /* Calculate the time at which the task should be woken if the event
8515 * does not occur. This may overflow but this doesn't matter, the
8516 * kernel will manage it correctly. */
8517 xTimeToWake = xConstTickCount + xTicksToWait;
8519 /* The list item will be inserted in wake time order. */
8520 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8522 if( xTimeToWake < xConstTickCount )
8524 /* Wake time has overflowed. Place this item in the overflow
8526 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8527 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8531 /* The wake time has not overflowed, so the current block list
8533 traceMOVED_TASK_TO_DELAYED_LIST();
8534 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8536 /* If the task entering the blocked state was placed at the
8537 * head of the list of blocked tasks then xNextTaskUnblockTime
8538 * needs to be updated too. */
8539 if( xTimeToWake < xNextTaskUnblockTime )
8541 xNextTaskUnblockTime = xTimeToWake;
8545 mtCOVERAGE_TEST_MARKER();
8550 #else /* INCLUDE_vTaskSuspend */
8552 /* Calculate the time at which the task should be woken if the event
8553 * does not occur. This may overflow but this doesn't matter, the kernel
8554 * will manage it correctly. */
8555 xTimeToWake = xConstTickCount + xTicksToWait;
8557 /* The list item will be inserted in wake time order. */
8558 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8560 if( xTimeToWake < xConstTickCount )
8562 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8563 /* Wake time has overflowed. Place this item in the overflow list. */
8564 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8568 traceMOVED_TASK_TO_DELAYED_LIST();
8569 /* The wake time has not overflowed, so the current block list is used. */
8570 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8572 /* If the task entering the blocked state was placed at the head of the
8573 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
8575 if( xTimeToWake < xNextTaskUnblockTime )
8577 xNextTaskUnblockTime = xTimeToWake;
8581 mtCOVERAGE_TEST_MARKER();
8585 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
8586 ( void ) xCanBlockIndefinitely;
8588 #endif /* INCLUDE_vTaskSuspend */
8590 /*-----------------------------------------------------------*/
8592 #if ( portUSING_MPU_WRAPPERS == 1 )
8594 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
8598 traceENTER_xTaskGetMPUSettings( xTask );
8600 pxTCB = prvGetTCBFromHandle( xTask );
8602 traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
8604 return &( pxTCB->xMPUSettings );
8607 #endif /* portUSING_MPU_WRAPPERS */
8608 /*-----------------------------------------------------------*/
8610 /* Code below here allows additional code to be inserted into this source file,
8611 * especially where access to file scope functions and data is needed (for example
8612 * when performing module tests). */
8614 #ifdef FREERTOS_MODULE_TEST
8615 #include "tasks_test_access_functions.h"
8619 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
8621 #include "freertos_tasks_c_additions.h"
8623 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8624 static void freertos_tasks_c_additions_init( void )
8626 FREERTOS_TASKS_C_ADDITIONS_INIT();
8630 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8631 /*-----------------------------------------------------------*/
8633 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8636 * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8637 * to provide the memory that is used by the Idle task. It is used when
8638 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8639 * it's own implementation of vApplicationGetIdleTaskMemory by setting
8640 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8642 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8643 StackType_t ** ppxIdleTaskStackBuffer,
8644 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize )
8646 static StaticTask_t xIdleTaskTCB;
8647 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8649 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8650 *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8651 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8654 #if ( configNUMBER_OF_CORES > 1 )
8656 void vApplicationGetPassiveIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8657 StackType_t ** ppxIdleTaskStackBuffer,
8658 configSTACK_DEPTH_TYPE * puxIdleTaskStackSize,
8659 BaseType_t xPassiveIdleTaskIndex )
8661 static StaticTask_t xIdleTaskTCBs[ configNUMBER_OF_CORES - 1 ];
8662 static StackType_t uxIdleTaskStacks[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
8664 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCBs[ xPassiveIdleTaskIndex ] );
8665 *ppxIdleTaskStackBuffer = &( uxIdleTaskStacks[ xPassiveIdleTaskIndex ][ 0 ] );
8666 *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8669 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
8671 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8672 /*-----------------------------------------------------------*/
8674 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8677 * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8678 * to provide the memory that is used by the Timer service task. It is used when
8679 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8680 * it's own implementation of vApplicationGetTimerTaskMemory by setting
8681 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8683 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8684 StackType_t ** ppxTimerTaskStackBuffer,
8685 configSTACK_DEPTH_TYPE * puxTimerTaskStackSize )
8687 static StaticTask_t xTimerTaskTCB;
8688 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8690 *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8691 *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8692 *puxTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8695 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8696 /*-----------------------------------------------------------*/
8699 * Reset the state in this file. This state is normally initialized at start up.
8700 * This function must be called by the application before restarting the
8703 void vTaskResetState( void )
8707 /* Task control block. */
8708 #if ( configNUMBER_OF_CORES == 1 )
8710 pxCurrentTCB = NULL;
8712 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8714 #if ( INCLUDE_vTaskDelete == 1 )
8716 uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
8718 #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
8720 #if ( configUSE_POSIX_ERRNO == 1 )
8724 #endif /* #if ( configUSE_POSIX_ERRNO == 1 ) */
8726 /* Other file private variables. */
8727 uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
8728 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
8729 uxTopReadyPriority = tskIDLE_PRIORITY;
8730 xSchedulerRunning = pdFALSE;
8731 xPendedTicks = ( TickType_t ) 0U;
8733 for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
8735 xYieldPendings[ xCoreID ] = pdFALSE;
8738 xNumOfOverflows = ( BaseType_t ) 0;
8739 uxTaskNumber = ( UBaseType_t ) 0U;
8740 xNextTaskUnblockTime = ( TickType_t ) 0U;
8742 uxSchedulerSuspended = ( UBaseType_t ) 0U;
8744 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8746 for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
8748 ulTaskSwitchedInTime[ xCoreID ] = 0U;
8749 ulTotalRunTime[ xCoreID ] = 0U;
8752 #endif /* #if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8754 /*-----------------------------------------------------------*/