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 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
45 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
46 * for the header files above, but not in this file, in order to generate the
47 * correct privileged Vs unprivileged linkage and placement. */
48 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
50 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
51 * functions but without including stdio.h here. */
52 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
54 /* At the bottom of this file are two optional functions that can be used
55 * to generate human readable text from the raw data generated by the
56 * uxTaskGetSystemState() function. Note the formatting functions are provided
57 * for convenience only, and are NOT considered part of the kernel. */
59 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
61 #if ( configUSE_PREEMPTION == 0 )
63 /* If the cooperative scheduler is being used then a yield should not be
64 * performed just because a higher priority task has been woken. */
65 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB )
66 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB )
69 #if ( configNUMBER_OF_CORES == 1 )
71 /* This macro requests the running task pxTCB to yield. In single core
72 * scheduler, a running task always runs on core 0 and portYIELD_WITHIN_API()
73 * can be used to request the task running on core 0 to yield. Therefore, pxTCB
74 * is not used in this macro. */
75 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) \
78 portYIELD_WITHIN_API(); \
81 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \
83 if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \
85 portYIELD_WITHIN_API(); \
89 mtCOVERAGE_TEST_MARKER(); \
93 #else /* if ( configNUMBER_OF_CORES == 1 ) */
95 /* Yield the core on which this task is running. */
96 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldCore( ( pxTCB )->xTaskRunState )
98 /* Yield for the task if a running task has priority lower than this task. */
99 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldForTask( pxTCB )
101 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
103 #endif /* if ( configUSE_PREEMPTION == 0 ) */
105 /* Values that can be assigned to the ucNotifyState member of the TCB. */
106 #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
107 #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
108 #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
111 * The value used to fill the stack of a task when the task is created. This
112 * is used purely for checking the high water mark for tasks.
114 #define tskSTACK_FILL_BYTE ( 0xa5U )
116 /* Bits used to record how a task's stack and TCB were allocated. */
117 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
118 #define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
119 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
121 /* If any of the following are set then task stacks are filled with a known
122 * value so the high water mark can be determined. If none of the following are
123 * set then don't fill the stack so there is no unnecessary dependency on memset. */
124 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
125 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
127 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
131 * Macros used by vListTask to indicate which state a task is in.
133 #define tskRUNNING_CHAR ( 'X' )
134 #define tskBLOCKED_CHAR ( 'B' )
135 #define tskREADY_CHAR ( 'R' )
136 #define tskDELETED_CHAR ( 'D' )
137 #define tskSUSPENDED_CHAR ( 'S' )
140 * Some kernel aware debuggers require the data the debugger needs access to to
141 * be global, rather than file scope.
143 #ifdef portREMOVE_STATIC_QUALIFIER
147 /* The name allocated to the Idle task. This can be overridden by defining
148 * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
149 #ifndef configIDLE_TASK_NAME
150 #define configIDLE_TASK_NAME "IDLE"
153 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
155 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
156 * performed in a generic way that is not optimised to any particular
157 * microcontroller architecture. */
159 /* uxTopReadyPriority holds the priority of the highest priority ready
161 #define taskRECORD_READY_PRIORITY( uxPriority ) \
163 if( ( uxPriority ) > uxTopReadyPriority ) \
165 uxTopReadyPriority = ( uxPriority ); \
167 } while( 0 ) /* taskRECORD_READY_PRIORITY */
169 /*-----------------------------------------------------------*/
171 #if ( configNUMBER_OF_CORES == 1 )
172 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
174 UBaseType_t uxTopPriority = uxTopReadyPriority; \
176 /* Find the highest priority queue that contains ready tasks. */ \
177 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
179 configASSERT( uxTopPriority ); \
183 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
184 * the same priority get an equal share of the processor time. */ \
185 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
186 uxTopReadyPriority = uxTopPriority; \
187 } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */
188 #else /* if ( configNUMBER_OF_CORES == 1 ) */
190 #define taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID ) prvSelectHighestPriorityTask( xCoreID )
192 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
194 /*-----------------------------------------------------------*/
196 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
197 * they are only required when a port optimised method of task selection is
199 #define taskRESET_READY_PRIORITY( uxPriority )
200 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
202 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
204 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
205 * performed in a way that is tailored to the particular microcontroller
206 * architecture being used. */
208 /* A port optimised version is provided. Call the port defined macros. */
209 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( ( uxPriority ), uxTopReadyPriority )
211 /*-----------------------------------------------------------*/
213 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
215 UBaseType_t uxTopPriority; \
217 /* Find the highest priority list that contains ready tasks. */ \
218 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
219 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
220 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
223 /*-----------------------------------------------------------*/
225 /* A port optimised version is provided, call it only if the TCB being reset
226 * is being referenced from a ready list. If it is referenced from a delayed
227 * or suspended list then it won't be in a ready list. */
228 #define taskRESET_READY_PRIORITY( uxPriority ) \
230 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
232 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
236 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
238 /*-----------------------------------------------------------*/
240 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
241 * count overflows. */
242 #define taskSWITCH_DELAYED_LISTS() \
246 /* The delayed tasks list should be empty when the lists are switched. */ \
247 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
249 pxTemp = pxDelayedTaskList; \
250 pxDelayedTaskList = pxOverflowDelayedTaskList; \
251 pxOverflowDelayedTaskList = pxTemp; \
253 prvResetNextTaskUnblockTime(); \
256 /*-----------------------------------------------------------*/
259 * Place the task represented by pxTCB into the appropriate ready list for
260 * the task. It is inserted at the end of the list.
262 #define prvAddTaskToReadyList( pxTCB ) \
264 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
265 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
266 listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
267 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB ); \
269 /*-----------------------------------------------------------*/
272 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
273 * where NULL is used to indicate that the handle of the currently executing
274 * task should be used in place of the parameter. This macro simply checks to
275 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
277 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
279 /* The item value of the event list item is normally used to hold the priority
280 * of the task to which it belongs (coded to allow it to be held in reverse
281 * priority order). However, it is occasionally borrowed for other purposes. It
282 * is important its value is not updated due to a task priority change while it is
283 * being used for another purpose. The following bit definition is used to inform
284 * the scheduler that the value should not be changed - in which case it is the
285 * responsibility of whichever module is using the value to ensure it gets set back
286 * to its original value when it is released. */
287 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
288 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint16_t ) 0x8000U )
289 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
290 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint32_t ) 0x80000000UL )
291 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
292 #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint64_t ) 0x8000000000000000ULL )
295 /* Indicates that the task is not actively running on any core. */
296 #define taskTASK_NOT_RUNNING ( ( BaseType_t ) ( -1 ) )
298 /* Indicates that the task is actively running but scheduled to yield. */
299 #define taskTASK_SCHEDULED_TO_YIELD ( ( BaseType_t ) ( -2 ) )
301 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
302 #if ( configNUMBER_OF_CORES == 1 )
303 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
304 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
306 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) )
307 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB )->xTaskRunState != taskTASK_NOT_RUNNING ) ? ( pdTRUE ) : ( pdFALSE ) )
310 /* Indicates that the task is an Idle task. */
311 #define taskATTRIBUTE_IS_IDLE ( UBaseType_t ) ( 1UL << 0UL )
313 #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) )
314 #define portGET_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting )
315 #define portSET_CRITICAL_NESTING_COUNT( x ) ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting = ( x ) )
316 #define portINCREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting++ )
317 #define portDECREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting-- )
318 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) */
320 #define taskBITS_PER_BYTE ( ( size_t ) 8 )
322 #if ( configNUMBER_OF_CORES > 1 )
324 /* Yields the given core. This must be called from a critical section and xCoreID
325 * must be valid. This macro is not required in single core since there is only
326 * one core to yield. */
327 #define prvYieldCore( xCoreID ) \
329 if( ( xCoreID ) == ( BaseType_t ) portGET_CORE_ID() ) \
331 /* Pending a yield for this core since it is in the critical section. */ \
332 xYieldPendings[ ( xCoreID ) ] = pdTRUE; \
336 /* Request other core to yield if it is not requested before. */ \
337 if( pxCurrentTCBs[ ( xCoreID ) ]->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD ) \
339 portYIELD_CORE( xCoreID ); \
340 pxCurrentTCBs[ ( xCoreID ) ]->xTaskRunState = taskTASK_SCHEDULED_TO_YIELD; \
344 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
345 /*-----------------------------------------------------------*/
348 * Task control block. A task control block (TCB) is allocated for each task,
349 * and stores task state information, including a pointer to the task's context
350 * (the task's run time environment, including register values)
352 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
354 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. */
356 #if ( portUSING_MPU_WRAPPERS == 1 )
357 xMPU_SETTINGS xMPUSettings; /**< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
360 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
361 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. */
364 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 ). */
365 ListItem_t xEventListItem; /**< Used to reference a task from an event list. */
366 UBaseType_t uxPriority; /**< The priority of the task. 0 is the lowest priority. */
367 StackType_t * pxStack; /**< Points to the start of the stack. */
368 #if ( configNUMBER_OF_CORES > 1 )
369 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. */
370 UBaseType_t uxTaskAttributes; /**< Task's attributes - currently used to identify the idle tasks. */
372 char pcTaskName[ configMAX_TASK_NAME_LEN ]; /**< Descriptive name given to the task when created. Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
374 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
375 BaseType_t xPreemptionDisable; /**< Used to prevent the task from being preempted. */
378 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
379 StackType_t * pxEndOfStack; /**< Points to the highest valid address for the stack. */
382 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
383 UBaseType_t uxCriticalNesting; /**< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
386 #if ( configUSE_TRACE_FACILITY == 1 )
387 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. */
388 UBaseType_t uxTaskNumber; /**< Stores a number specifically for use by third party trace code. */
391 #if ( configUSE_MUTEXES == 1 )
392 UBaseType_t uxBasePriority; /**< The priority last assigned to the task - used by the priority inheritance mechanism. */
393 UBaseType_t uxMutexesHeld;
396 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
397 TaskHookFunction_t pxTaskTag;
400 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
401 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
404 #if ( configGENERATE_RUN_TIME_STATS == 1 )
405 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /**< Stores the amount of time the task has spent in the Running state. */
408 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
409 configTLS_BLOCK_TYPE xTLSBlock; /**< Memory block used as Thread Local Storage (TLS) Block for the task. */
412 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
413 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
414 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
417 /* See the comments in FreeRTOS.h with the definition of
418 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
419 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
420 uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
423 #if ( INCLUDE_xTaskAbortDelay == 1 )
424 uint8_t ucDelayAborted;
427 #if ( configUSE_POSIX_ERRNO == 1 )
432 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
433 * below to enable the use of older kernel aware debuggers. */
434 typedef tskTCB TCB_t;
436 /*lint -save -e956 A manual analysis and inspection has been used to determine
437 * which static variables must be declared volatile. */
438 #if ( configNUMBER_OF_CORES == 1 )
439 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
440 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
441 /* coverity[misra_c_2012_rule_8_4_violation] */
442 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
444 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
445 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
446 /* coverity[misra_c_2012_rule_8_4_violation] */
447 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ];
448 #define pxCurrentTCB xTaskGetCurrentTaskHandle()
451 /* Lists for ready and blocked tasks. --------------------
452 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
453 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
454 * the static qualifier. */
455 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
456 PRIVILEGED_DATA static List_t xDelayedTaskList1; /**< Delayed tasks. */
457 PRIVILEGED_DATA static List_t xDelayedTaskList2; /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
458 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /**< Points to the delayed task list currently being used. */
459 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. */
460 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. */
462 #if ( INCLUDE_vTaskDelete == 1 )
464 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
465 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
469 #if ( INCLUDE_vTaskSuspend == 1 )
471 PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
475 /* Global POSIX errno. Its value is changed upon context switching to match
476 * the errno of the currently running task. */
477 #if ( configUSE_POSIX_ERRNO == 1 )
478 int FreeRTOS_errno = 0;
481 /* Other file private variables. --------------------------------*/
482 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
483 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
484 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
485 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
486 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
487 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
488 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
489 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
490 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
491 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. */
493 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
494 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
495 * to determine the number of priority lists to read back from the remote target. */
496 static const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
498 /* Context switches are held pending while the scheduler is suspended. Also,
499 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
500 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
501 * If an interrupt needs to unblock a task while the scheduler is suspended then it
502 * moves the task's event list item into the xPendingReadyList, ready for the
503 * kernel to move the task from the pending ready list into the real ready list
504 * when the scheduler is unsuspended. The pending ready list itself can only be
505 * accessed from a critical section.
507 * Updates to uxSchedulerSuspended must be protected by both the task lock and the ISR lock
508 * and must not be done from an ISR. Reads must be protected by either lock and may be done
509 * from either an ISR or a task. */
510 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
512 #if ( configGENERATE_RUN_TIME_STATS == 1 )
514 /* Do not move these variables to function scope as doing so prevents the
515 * code working with debuggers that need to remove the static qualifier. */
516 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. */
517 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. */
523 /*-----------------------------------------------------------*/
525 /* File private functions. --------------------------------*/
528 * Creates the idle tasks during scheduler start.
530 static BaseType_t prvCreateIdleTasks( void );
532 #if ( configNUMBER_OF_CORES > 1 )
535 * Checks to see if another task moved the current task out of the ready
536 * list while it was waiting to enter a critical section and yields, if so.
538 static void prvCheckForRunStateChange( void );
539 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
541 #if ( configNUMBER_OF_CORES > 1 )
544 * Yields a core, or cores if multiple priorities are not allowed to run
545 * simultaneously, to allow the task pxTCB to run.
547 static void prvYieldForTask( const TCB_t * pxTCB );
548 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
550 #if ( configNUMBER_OF_CORES > 1 )
553 * Selects the highest priority available task for the given core.
555 static void prvSelectHighestPriorityTask( BaseType_t xCoreID );
556 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
559 * Utility task that simply returns pdTRUE if the task referenced by xTask is
560 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
561 * is in any other state.
563 #if ( INCLUDE_vTaskSuspend == 1 )
565 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
567 #endif /* INCLUDE_vTaskSuspend */
570 * Utility to ready all the lists used by the scheduler. This is called
571 * automatically upon the creation of the first task.
573 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
576 * The idle task, which as all tasks is implemented as a never ending loop.
577 * The idle task is automatically created and added to the ready lists upon
578 * creation of the first user task.
580 * In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks are also
581 * created to ensure that each core has an idle task to run when no other
582 * task is available to run.
584 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
585 * language extensions. The equivalent prototype for these functions are:
587 * void prvIdleTask( void *pvParameters );
588 * void prvPassiveIdleTask( void *pvParameters );
591 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
592 #if ( configNUMBER_OF_CORES > 1 )
593 static portTASK_FUNCTION_PROTO( prvPassiveIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
597 * Utility to free all memory allocated by the scheduler to hold a TCB,
598 * including the stack pointed to by the TCB.
600 * This does not free memory allocated by the task itself (i.e. memory
601 * allocated by calls to pvPortMalloc from within the tasks application code).
603 #if ( INCLUDE_vTaskDelete == 1 )
605 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
610 * Used only by the idle task. This checks to see if anything has been placed
611 * in the list of tasks waiting to be deleted. If so the task is cleaned up
612 * and its TCB deleted.
614 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
617 * The currently executing task is entering the Blocked state. Add the task to
618 * either the current or the overflow delayed task list.
620 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
621 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
624 * Fills an TaskStatus_t structure with information on each task that is
625 * referenced from the pxList list (which may be a ready list, a delayed list,
626 * a suspended list, etc.).
628 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
629 * NORMAL APPLICATION CODE.
631 #if ( configUSE_TRACE_FACILITY == 1 )
633 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
635 eTaskState eState ) PRIVILEGED_FUNCTION;
640 * Searches pxList for a task with name pcNameToQuery - returning a handle to
641 * the task if it is found, or NULL if the task is not found.
643 #if ( INCLUDE_xTaskGetHandle == 1 )
645 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
646 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
651 * When a task is created, the stack of the task is filled with a known value.
652 * This function determines the 'high water mark' of the task stack by
653 * determining how much of the stack remains at the original preset value.
655 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
657 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
662 * Return the amount of time, in ticks, that will pass before the kernel will
663 * next move a task from the Blocked state to the Running state.
665 * This conditional compilation should use inequality to 0, not equality to 1.
666 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
667 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
668 * set to a value other than 1.
670 #if ( configUSE_TICKLESS_IDLE != 0 )
672 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
677 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
678 * will exit the Blocked state.
680 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
682 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
685 * Helper function used to pad task names with spaces when printing out
686 * human readable tables of task information.
688 static char * prvWriteNameToBuffer( char * pcBuffer,
689 const char * pcTaskName ) PRIVILEGED_FUNCTION;
694 * Called after a Task_t structure has been allocated either statically or
695 * dynamically to fill in the structure's members.
697 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
698 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
699 const uint32_t ulStackDepth,
700 void * const pvParameters,
701 UBaseType_t uxPriority,
702 TaskHandle_t * const pxCreatedTask,
704 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
707 * Called after a new task has been created and initialised to place the task
708 * under the control of the scheduler.
710 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
713 * Create a task with static buffer for both TCB and stack. Returns a handle to
714 * the task if it is created successfully. Otherwise, returns NULL.
716 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
717 static TCB_t * prvCreateStaticTask( TaskFunction_t pxTaskCode,
718 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
719 const uint32_t ulStackDepth,
720 void * const pvParameters,
721 UBaseType_t uxPriority,
722 StackType_t * const puxStackBuffer,
723 StaticTask_t * const pxTaskBuffer,
724 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
725 #endif /* #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
728 * Create a restricted task with static buffer for both TCB and stack. Returns
729 * a handle to the task if it is created successfully. Otherwise, returns NULL.
731 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
732 static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
733 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
734 #endif /* #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
737 * Create a restricted task with static buffer for task stack and allocated buffer
738 * for TCB. Returns a handle to the task if it is created successfully. Otherwise,
741 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
742 static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
743 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
744 #endif /* #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
747 * Create a task with allocated buffer for both TCB and stack. Returns a handle to
748 * the task if it is created successfully. Otherwise, returns NULL.
750 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
751 static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
752 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
753 const configSTACK_DEPTH_TYPE usStackDepth,
754 void * const pvParameters,
755 UBaseType_t uxPriority,
756 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
757 #endif /* #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) */
760 * freertos_tasks_c_additions_init() should only be called if the user definable
761 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
762 * called by the function.
764 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
766 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
770 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
771 extern void vApplicationPassiveIdleHook( void );
772 #endif /* #if ( configUSE_PASSIVE_IDLE_HOOK == 1 ) */
774 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
777 * Convert the snprintf return value to the number of characters
778 * written. The following are the possible cases:
780 * 1. The buffer supplied to snprintf is large enough to hold the
781 * generated string. The return value in this case is the number
782 * of characters actually written, not counting the terminating
784 * 2. The buffer supplied to snprintf is NOT large enough to hold
785 * the generated string. The return value in this case is the
786 * number of characters that would have been written if the
787 * buffer had been sufficiently large, not counting the
788 * terminating null character.
789 * 3. Encoding error. The return value in this case is a negative
792 * From 1 and 2 above ==> Only when the return value is non-negative
793 * and less than the supplied buffer length, the string has been
794 * completely written.
796 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
799 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
800 /*-----------------------------------------------------------*/
802 #if ( configNUMBER_OF_CORES > 1 )
803 static void prvCheckForRunStateChange( void )
805 UBaseType_t uxPrevCriticalNesting;
806 const TCB_t * pxThisTCB;
808 /* This must only be called from within a task. */
809 portASSERT_IF_IN_ISR();
811 /* This function is always called with interrupts disabled
812 * so this is safe. */
813 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
815 while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD )
817 /* We are only here if we just entered a critical section
818 * or if we just suspended the scheduler, and another task
819 * has requested that we yield.
821 * This is slightly complicated since we need to save and restore
822 * the suspension and critical nesting counts, as well as release
823 * and reacquire the correct locks. And then, do it all over again
824 * if our state changed again during the reacquisition. */
825 uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
827 if( uxPrevCriticalNesting > 0U )
829 portSET_CRITICAL_NESTING_COUNT( 0U );
830 portRELEASE_ISR_LOCK();
834 /* The scheduler is suspended. uxSchedulerSuspended is updated
835 * only when the task is not requested to yield. */
836 mtCOVERAGE_TEST_MARKER();
839 portRELEASE_TASK_LOCK();
840 portMEMORY_BARRIER();
841 configASSERT( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD );
843 portENABLE_INTERRUPTS();
845 /* Enabling interrupts should cause this core to immediately
846 * service the pending interrupt and yield. If the run state is still
847 * yielding here then that is a problem. */
848 configASSERT( pxThisTCB->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD );
850 portDISABLE_INTERRUPTS();
854 portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
856 if( uxPrevCriticalNesting == 0U )
858 portRELEASE_ISR_LOCK();
862 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
864 /*-----------------------------------------------------------*/
866 #if ( configNUMBER_OF_CORES > 1 )
867 static void prvYieldForTask( const TCB_t * pxTCB )
869 BaseType_t xLowestPriorityToPreempt;
870 BaseType_t xCurrentCoreTaskPriority;
871 BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
874 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
875 BaseType_t xYieldCount = 0;
876 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
878 /* This must be called from a critical section. */
879 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
881 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
883 /* No task should yield for this one if it is a lower priority
884 * than priority level of currently ready tasks. */
885 if( pxTCB->uxPriority >= uxTopReadyPriority )
887 /* Yield is not required for a task which is already running. */
888 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
891 xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
893 /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
894 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
895 --xLowestPriorityToPreempt;
897 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
899 xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
901 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
902 if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
904 xCurrentCoreTaskPriority = xCurrentCoreTaskPriority - 1;
907 if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
909 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
910 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
913 if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
915 #if ( configUSE_CORE_AFFINITY == 1 )
916 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
919 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
920 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
923 xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
924 xLowestPriorityCore = xCoreID;
930 mtCOVERAGE_TEST_MARKER();
934 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
936 /* Yield all currently running non-idle tasks with a priority lower than
937 * the task that needs to run. */
938 if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
939 ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
941 prvYieldCore( xCoreID );
946 mtCOVERAGE_TEST_MARKER();
949 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
953 mtCOVERAGE_TEST_MARKER();
957 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
958 if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
959 #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
960 if( xLowestPriorityCore >= 0 )
961 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
963 prvYieldCore( xLowestPriorityCore );
966 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
967 /* Verify that the calling core always yields to higher priority tasks. */
968 if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) &&
969 ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
971 configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) ||
972 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) );
977 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
978 /*-----------------------------------------------------------*/
980 #if ( configNUMBER_OF_CORES > 1 )
981 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
983 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
984 BaseType_t xTaskScheduled = pdFALSE;
985 BaseType_t xDecrementTopPriority = pdTRUE;
987 #if ( configUSE_CORE_AFFINITY == 1 )
988 const TCB_t * pxPreviousTCB = NULL;
990 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
991 BaseType_t xPriorityDropped = pdFALSE;
994 /* This function should be called when scheduler is running. */
995 configASSERT( xSchedulerRunning == pdTRUE );
997 /* A new task is created and a running task with the same priority yields
998 * itself to run the new task. When a running task yields itself, it is still
999 * in the ready list. This running task will be selected before the new task
1000 * since the new task is always added to the end of the ready list.
1001 * The other problem is that the running task still in the same position of
1002 * the ready list when it yields itself. It is possible that it will be selected
1003 * earlier then other tasks which waits longer than this task.
1005 * To fix these problems, the running task should be put to the end of the
1006 * ready list before searching for the ready task in the ready list. */
1007 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
1008 &pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
1010 ( void ) uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
1011 vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
1012 &pxCurrentTCBs[ xCoreID ]->xStateListItem );
1015 while( xTaskScheduled == pdFALSE )
1017 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1019 if( uxCurrentPriority < uxTopReadyPriority )
1021 /* We can't schedule any tasks, other than idle, that have a
1022 * priority lower than the priority of a task currently running
1023 * on another core. */
1024 uxCurrentPriority = tskIDLE_PRIORITY;
1029 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
1031 const List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
1032 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxReadyList );
1033 ListItem_t * pxIterator;
1035 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
1036 * must not be decremented any further. */
1037 xDecrementTopPriority = pdFALSE;
1039 for( pxIterator = listGET_HEAD_ENTRY( pxReadyList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
1041 /* MISRA Ref 11.5.3 [Void pointer assignment] */
1042 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1043 /* coverity[misra_c_2012_rule_11_5_violation] */
1044 TCB_t * pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
1046 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1048 /* When falling back to the idle priority because only one priority
1049 * level is allowed to run at a time, we should ONLY schedule the true
1050 * idle tasks, not user tasks at the idle priority. */
1051 if( uxCurrentPriority < uxTopReadyPriority )
1053 if( ( pxTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U )
1059 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1061 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
1063 #if ( configUSE_CORE_AFFINITY == 1 )
1064 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1067 /* If the task is not being executed by any core swap it in. */
1068 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
1069 #if ( configUSE_CORE_AFFINITY == 1 )
1070 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
1072 pxTCB->xTaskRunState = xCoreID;
1073 pxCurrentTCBs[ xCoreID ] = pxTCB;
1074 xTaskScheduled = pdTRUE;
1077 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
1079 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) );
1081 #if ( configUSE_CORE_AFFINITY == 1 )
1082 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1085 /* The task is already running on this core, mark it as scheduled. */
1086 pxTCB->xTaskRunState = xCoreID;
1087 xTaskScheduled = pdTRUE;
1092 /* This task is running on the core other than xCoreID. */
1093 mtCOVERAGE_TEST_MARKER();
1096 if( xTaskScheduled != pdFALSE )
1098 /* A task has been selected to run on this core. */
1105 if( xDecrementTopPriority != pdFALSE )
1107 uxTopReadyPriority--;
1108 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1110 xPriorityDropped = pdTRUE;
1116 /* There are configNUMBER_OF_CORES Idle tasks created when scheduler started.
1117 * The scheduler should be able to select a task to run when uxCurrentPriority
1118 * is tskIDLE_PRIORITY. uxCurrentPriority is never decreased to value blow
1119 * tskIDLE_PRIORITY. */
1120 if( uxCurrentPriority > tskIDLE_PRIORITY )
1122 uxCurrentPriority--;
1126 /* This function is called when idle task is not created. Break the
1127 * loop to prevent uxCurrentPriority overrun. */
1132 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1134 if( xTaskScheduled == pdTRUE )
1136 if( xPriorityDropped != pdFALSE )
1138 /* There may be several ready tasks that were being prevented from running because there was
1139 * a higher priority task running. Now that the last of the higher priority tasks is no longer
1140 * running, make sure all the other idle tasks yield. */
1143 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUMBER_OF_CORES; x++ )
1145 if( ( pxCurrentTCBs[ x ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1153 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1155 #if ( configUSE_CORE_AFFINITY == 1 )
1157 if( xTaskScheduled == pdTRUE )
1159 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
1161 /* A ready task was just evicted from this core. See if it can be
1162 * scheduled on any other core. */
1163 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
1164 BaseType_t xLowestPriority = ( BaseType_t ) pxPreviousTCB->uxPriority;
1165 BaseType_t xLowestPriorityCore = -1;
1168 if( ( pxPreviousTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1170 xLowestPriority = xLowestPriority - 1;
1173 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1175 /* pxPreviousTCB was removed from this core and this core is not excluded
1176 * from it's core affinity mask.
1178 * pxPreviousTCB is preempted by the new higher priority task
1179 * pxCurrentTCBs[ xCoreID ]. When searching a new core for pxPreviousTCB,
1180 * we do not need to look at the cores on which pxCurrentTCBs[ xCoreID ]
1181 * is allowed to run. The reason is - when more than one cores are
1182 * eligible for an incoming task, we preempt the core with the minimum
1183 * priority task. Because this core (i.e. xCoreID) was preempted for
1184 * pxCurrentTCBs[ xCoreID ], this means that all the others cores
1185 * where pxCurrentTCBs[ xCoreID ] can run, are running tasks with priority
1186 * no lower than pxPreviousTCB's priority. Therefore, the only cores where
1187 * which can be preempted for pxPreviousTCB are the ones where
1188 * pxCurrentTCBs[ xCoreID ] is not allowed to run (and obviously,
1189 * pxPreviousTCB is allowed to run).
1191 * This is an optimization which reduces the number of cores needed to be
1192 * searched for pxPreviousTCB to run. */
1193 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1197 /* pxPreviousTCB's core affinity mask is changed and it is no longer
1198 * allowed to run on this core. Searching all the cores in pxPreviousTCB's
1199 * new core affinity mask to find a core on which it can run. */
1202 uxCoreMap &= ( ( 1U << configNUMBER_OF_CORES ) - 1U );
1204 for( x = ( ( BaseType_t ) configNUMBER_OF_CORES - 1 ); x >= ( BaseType_t ) 0; x-- )
1206 UBaseType_t uxCore = ( UBaseType_t ) x;
1207 BaseType_t xTaskPriority;
1209 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << uxCore ) ) != 0U )
1211 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority;
1213 if( ( pxCurrentTCBs[ uxCore ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1215 xTaskPriority = xTaskPriority - ( BaseType_t ) 1;
1218 uxCoreMap &= ~( ( UBaseType_t ) 1U << uxCore );
1220 if( ( xTaskPriority < xLowestPriority ) &&
1221 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ] ) != pdFALSE ) &&
1222 ( xYieldPendings[ uxCore ] == pdFALSE ) )
1224 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1225 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1228 xLowestPriority = xTaskPriority;
1229 xLowestPriorityCore = ( BaseType_t ) uxCore;
1235 if( xLowestPriorityCore >= 0 )
1237 prvYieldCore( xLowestPriorityCore );
1242 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) */
1245 #endif /* ( configNUMBER_OF_CORES > 1 ) */
1247 /*-----------------------------------------------------------*/
1249 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1251 static TCB_t * prvCreateStaticTask( TaskFunction_t pxTaskCode,
1252 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1253 const uint32_t ulStackDepth,
1254 void * const pvParameters,
1255 UBaseType_t uxPriority,
1256 StackType_t * const puxStackBuffer,
1257 StaticTask_t * const pxTaskBuffer,
1258 TaskHandle_t * const pxCreatedTask )
1262 configASSERT( puxStackBuffer != NULL );
1263 configASSERT( pxTaskBuffer != NULL );
1265 #if ( configASSERT_DEFINED == 1 )
1267 /* Sanity check that the size of the structure used to declare a
1268 * variable of type StaticTask_t equals the size of the real task
1270 volatile size_t xSize = sizeof( StaticTask_t );
1271 configASSERT( xSize == sizeof( TCB_t ) );
1272 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1274 #endif /* configASSERT_DEFINED */
1276 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1278 /* The memory used for the task's TCB and stack are passed into this
1279 * function - use them. */
1280 /* MISRA Ref 11.3.1 [Misaligned access] */
1281 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
1282 /* coverity[misra_c_2012_rule_11_3_violation] */
1283 pxNewTCB = ( TCB_t * ) pxTaskBuffer;
1284 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1285 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1287 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1289 /* Tasks can be created statically or dynamically, so note this
1290 * task was created statically in case the task is later deleted. */
1291 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1293 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1295 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1304 /*-----------------------------------------------------------*/
1306 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1307 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1308 const uint32_t ulStackDepth,
1309 void * const pvParameters,
1310 UBaseType_t uxPriority,
1311 StackType_t * const puxStackBuffer,
1312 StaticTask_t * const pxTaskBuffer )
1314 TaskHandle_t xReturn = NULL;
1317 traceENTER_xTaskCreateStatic( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer );
1319 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1321 if( pxNewTCB != NULL )
1323 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1325 /* Set the task's affinity before scheduling it. */
1326 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1330 prvAddNewTaskToReadyList( pxNewTCB );
1334 mtCOVERAGE_TEST_MARKER();
1337 traceRETURN_xTaskCreateStatic( xReturn );
1341 /*-----------------------------------------------------------*/
1343 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1344 TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1345 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1346 const uint32_t ulStackDepth,
1347 void * const pvParameters,
1348 UBaseType_t uxPriority,
1349 StackType_t * const puxStackBuffer,
1350 StaticTask_t * const pxTaskBuffer,
1351 UBaseType_t uxCoreAffinityMask )
1353 TaskHandle_t xReturn = NULL;
1356 traceENTER_xTaskCreateStaticAffinitySet( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, uxCoreAffinityMask );
1358 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1360 if( pxNewTCB != NULL )
1362 /* Set the task's affinity before scheduling it. */
1363 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1365 prvAddNewTaskToReadyList( pxNewTCB );
1369 mtCOVERAGE_TEST_MARKER();
1372 traceRETURN_xTaskCreateStaticAffinitySet( xReturn );
1376 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1378 #endif /* SUPPORT_STATIC_ALLOCATION */
1379 /*-----------------------------------------------------------*/
1381 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1382 static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
1383 TaskHandle_t * const pxCreatedTask )
1387 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1388 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1390 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1392 /* Allocate space for the TCB. Where the memory comes from depends
1393 * on the implementation of the port malloc function and whether or
1394 * not static allocation is being used. */
1395 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1396 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1398 /* Store the stack location in the TCB. */
1399 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1401 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1403 /* Tasks can be created statically or dynamically, so note this
1404 * task was created statically in case the task is later deleted. */
1405 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1407 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1409 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1410 pxTaskDefinition->pcName,
1411 ( uint32_t ) pxTaskDefinition->usStackDepth,
1412 pxTaskDefinition->pvParameters,
1413 pxTaskDefinition->uxPriority,
1414 pxCreatedTask, pxNewTCB,
1415 pxTaskDefinition->xRegions );
1424 /*-----------------------------------------------------------*/
1426 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1427 TaskHandle_t * pxCreatedTask )
1432 traceENTER_xTaskCreateRestrictedStatic( pxTaskDefinition, pxCreatedTask );
1434 configASSERT( pxTaskDefinition != NULL );
1436 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1438 if( pxNewTCB != NULL )
1440 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1442 /* Set the task's affinity before scheduling it. */
1443 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1447 prvAddNewTaskToReadyList( pxNewTCB );
1452 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1455 traceRETURN_xTaskCreateRestrictedStatic( xReturn );
1459 /*-----------------------------------------------------------*/
1461 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1462 BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1463 UBaseType_t uxCoreAffinityMask,
1464 TaskHandle_t * pxCreatedTask )
1469 traceENTER_xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1471 configASSERT( pxTaskDefinition != NULL );
1473 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1475 if( pxNewTCB != NULL )
1477 /* Set the task's affinity before scheduling it. */
1478 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1480 prvAddNewTaskToReadyList( pxNewTCB );
1485 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1488 traceRETURN_xTaskCreateRestrictedStaticAffinitySet( xReturn );
1492 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1494 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1495 /*-----------------------------------------------------------*/
1497 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1498 static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
1499 TaskHandle_t * const pxCreatedTask )
1503 configASSERT( pxTaskDefinition->puxStackBuffer );
1505 if( pxTaskDefinition->puxStackBuffer != NULL )
1507 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1508 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1509 /* coverity[misra_c_2012_rule_11_5_violation] */
1510 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1512 if( pxNewTCB != NULL )
1514 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1516 /* Store the stack location in the TCB. */
1517 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1519 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1521 /* Tasks can be created statically or dynamically, so note
1522 * this task had a statically allocated stack in case it is
1523 * later deleted. The TCB was allocated dynamically. */
1524 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1526 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1528 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1529 pxTaskDefinition->pcName,
1530 ( uint32_t ) pxTaskDefinition->usStackDepth,
1531 pxTaskDefinition->pvParameters,
1532 pxTaskDefinition->uxPriority,
1533 pxCreatedTask, pxNewTCB,
1534 pxTaskDefinition->xRegions );
1544 /*-----------------------------------------------------------*/
1546 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1547 TaskHandle_t * pxCreatedTask )
1552 traceENTER_xTaskCreateRestricted( pxTaskDefinition, pxCreatedTask );
1554 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1556 if( pxNewTCB != NULL )
1558 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1560 /* Set the task's affinity before scheduling it. */
1561 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1563 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1565 prvAddNewTaskToReadyList( pxNewTCB );
1571 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1574 traceRETURN_xTaskCreateRestricted( xReturn );
1578 /*-----------------------------------------------------------*/
1580 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1581 BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1582 UBaseType_t uxCoreAffinityMask,
1583 TaskHandle_t * pxCreatedTask )
1588 traceENTER_xTaskCreateRestrictedAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1590 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1592 if( pxNewTCB != NULL )
1594 /* Set the task's affinity before scheduling it. */
1595 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1597 prvAddNewTaskToReadyList( pxNewTCB );
1603 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1606 traceRETURN_xTaskCreateRestrictedAffinitySet( xReturn );
1610 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1613 #endif /* portUSING_MPU_WRAPPERS */
1614 /*-----------------------------------------------------------*/
1616 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1617 static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
1618 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1619 const configSTACK_DEPTH_TYPE usStackDepth,
1620 void * const pvParameters,
1621 UBaseType_t uxPriority,
1622 TaskHandle_t * const pxCreatedTask )
1626 /* If the stack grows down then allocate the stack then the TCB so the stack
1627 * does not grow into the TCB. Likewise if the stack grows up then allocate
1628 * the TCB then the stack. */
1629 #if ( portSTACK_GROWTH > 0 )
1631 /* Allocate space for the TCB. Where the memory comes from depends on
1632 * the implementation of the port malloc function and whether or not static
1633 * allocation is being used. */
1634 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1635 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1636 /* coverity[misra_c_2012_rule_11_5_violation] */
1637 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1639 if( pxNewTCB != NULL )
1641 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1643 /* Allocate space for the stack used by the task being created.
1644 * The base of the stack memory stored in the TCB so the task can
1645 * be deleted later if required. */
1646 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1647 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1648 /* coverity[misra_c_2012_rule_11_5_violation] */
1649 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) );
1651 if( pxNewTCB->pxStack == NULL )
1653 /* Could not allocate the stack. Delete the allocated TCB. */
1654 vPortFree( pxNewTCB );
1659 #else /* portSTACK_GROWTH */
1661 StackType_t * pxStack;
1663 /* Allocate space for the stack used by the task being created. */
1664 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1665 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1666 /* coverity[misra_c_2012_rule_11_5_violation] */
1667 pxStack = pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) );
1669 if( pxStack != NULL )
1671 /* Allocate space for the TCB. */
1672 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
1673 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1674 /* coverity[misra_c_2012_rule_11_5_violation] */
1675 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1677 if( pxNewTCB != NULL )
1679 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1681 /* Store the stack location in the TCB. */
1682 pxNewTCB->pxStack = pxStack;
1686 /* The stack cannot be used as the TCB was not created. Free
1688 vPortFreeStack( pxStack );
1696 #endif /* portSTACK_GROWTH */
1698 if( pxNewTCB != NULL )
1700 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1702 /* Tasks can be created statically or dynamically, so note this
1703 * task was created dynamically in case it is later deleted. */
1704 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1706 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1708 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1713 /*-----------------------------------------------------------*/
1715 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1716 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1717 const configSTACK_DEPTH_TYPE usStackDepth,
1718 void * const pvParameters,
1719 UBaseType_t uxPriority,
1720 TaskHandle_t * const pxCreatedTask )
1725 traceENTER_xTaskCreate( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask );
1727 pxNewTCB = prvCreateTask( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask );
1729 if( pxNewTCB != NULL )
1731 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1733 /* Set the task's affinity before scheduling it. */
1734 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1738 prvAddNewTaskToReadyList( pxNewTCB );
1743 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1746 traceRETURN_xTaskCreate( xReturn );
1750 /*-----------------------------------------------------------*/
1752 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1753 BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1754 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1755 const configSTACK_DEPTH_TYPE usStackDepth,
1756 void * const pvParameters,
1757 UBaseType_t uxPriority,
1758 UBaseType_t uxCoreAffinityMask,
1759 TaskHandle_t * const pxCreatedTask )
1764 traceENTER_xTaskCreateAffinitySet( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, uxCoreAffinityMask, pxCreatedTask );
1766 pxNewTCB = prvCreateTask( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask );
1768 if( pxNewTCB != NULL )
1770 /* Set the task's affinity before scheduling it. */
1771 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1773 prvAddNewTaskToReadyList( pxNewTCB );
1778 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1781 traceRETURN_xTaskCreateAffinitySet( xReturn );
1785 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1787 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1788 /*-----------------------------------------------------------*/
1790 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1791 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1792 const uint32_t ulStackDepth,
1793 void * const pvParameters,
1794 UBaseType_t uxPriority,
1795 TaskHandle_t * const pxCreatedTask,
1797 const MemoryRegion_t * const xRegions )
1799 StackType_t * pxTopOfStack;
1802 #if ( portUSING_MPU_WRAPPERS == 1 )
1803 /* Should the task be created in privileged mode? */
1804 BaseType_t xRunPrivileged;
1806 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1808 xRunPrivileged = pdTRUE;
1812 xRunPrivileged = pdFALSE;
1814 uxPriority &= ~portPRIVILEGE_BIT;
1815 #endif /* portUSING_MPU_WRAPPERS == 1 */
1817 /* Avoid dependency on memset() if it is not required. */
1818 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1820 /* Fill the stack with a known value to assist debugging. */
1821 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1823 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1825 /* Calculate the top of stack address. This depends on whether the stack
1826 * grows from high memory to low (as per the 80x86) or vice versa.
1827 * portSTACK_GROWTH is used to make the result positive or negative as required
1829 #if ( portSTACK_GROWTH < 0 )
1831 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1832 pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /*lint !e923 !e9033 !e9078 MISRA exception. Avoiding casts between pointers and integers is not practical. Size differences accounted for using portPOINTER_SIZE_TYPE type. Checked by assert(). */
1834 /* Check the alignment of the calculated top of stack is correct. */
1835 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1837 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1839 /* Also record the stack's high address, which may assist
1841 pxNewTCB->pxEndOfStack = pxTopOfStack;
1843 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1845 #else /* portSTACK_GROWTH */
1847 pxTopOfStack = pxNewTCB->pxStack;
1848 pxTopOfStack = ( StackType_t * ) ( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) + portBYTE_ALIGNMENT_MASK ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /*lint !e923 !e9033 !e9078 MISRA exception. Avoiding casts between pointers and integers is not practical. Size differences accounted for using portPOINTER_SIZE_TYPE type. Checked by assert(). */
1850 /* Check the alignment of the calculated top of stack is correct. */
1851 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1853 /* The other extreme of the stack space is required if stack checking is
1855 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1857 #endif /* portSTACK_GROWTH */
1859 /* Store the task name in the TCB. */
1860 if( pcName != NULL )
1862 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1864 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1866 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1867 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1868 * string is not accessible (extremely unlikely). */
1869 if( pcName[ x ] == ( char ) 0x00 )
1875 mtCOVERAGE_TEST_MARKER();
1879 /* Ensure the name string is terminated in the case that the string length
1880 * was greater or equal to configMAX_TASK_NAME_LEN. */
1881 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1U ] = '\0';
1885 mtCOVERAGE_TEST_MARKER();
1888 /* This is used as an array index so must ensure it's not too large. */
1889 configASSERT( uxPriority < configMAX_PRIORITIES );
1891 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1893 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1897 mtCOVERAGE_TEST_MARKER();
1900 pxNewTCB->uxPriority = uxPriority;
1901 #if ( configUSE_MUTEXES == 1 )
1903 pxNewTCB->uxBasePriority = uxPriority;
1905 #endif /* configUSE_MUTEXES */
1907 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1908 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1910 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1911 * back to the containing TCB from a generic item in a list. */
1912 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1914 /* Event lists are always in priority order. */
1915 listSET_LIST_ITEM_VALUE( &( pxNewTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1916 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1918 #if ( portUSING_MPU_WRAPPERS == 1 )
1920 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1924 /* Avoid compiler warning about unreferenced parameter. */
1929 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
1931 /* Allocate and initialize memory for the task's TLS Block. */
1932 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
1936 /* Initialize the TCB stack to look as if the task was already running,
1937 * but had been interrupted by the scheduler. The return address is set
1938 * to the start of the task function. Once the stack has been initialised
1939 * the top of stack variable is updated. */
1940 #if ( portUSING_MPU_WRAPPERS == 1 )
1942 /* If the port has capability to detect stack overflow,
1943 * pass the stack end address to the stack initialization
1944 * function as well. */
1945 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1947 #if ( portSTACK_GROWTH < 0 )
1949 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1951 #else /* portSTACK_GROWTH */
1953 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1955 #endif /* portSTACK_GROWTH */
1957 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1959 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1961 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1963 #else /* portUSING_MPU_WRAPPERS */
1965 /* If the port has capability to detect stack overflow,
1966 * pass the stack end address to the stack initialization
1967 * function as well. */
1968 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1970 #if ( portSTACK_GROWTH < 0 )
1972 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1974 #else /* portSTACK_GROWTH */
1976 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1978 #endif /* portSTACK_GROWTH */
1980 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1982 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1984 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1986 #endif /* portUSING_MPU_WRAPPERS */
1988 /* Initialize task state and task attributes. */
1989 #if ( configNUMBER_OF_CORES > 1 )
1991 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1993 /* Is this an idle task? */
1994 if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvPassiveIdleTask ) )
1996 pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
1999 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
2001 if( pxCreatedTask != NULL )
2003 /* Pass the handle out in an anonymous way. The handle can be used to
2004 * change the created task's priority, delete the created task, etc.*/
2005 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
2009 mtCOVERAGE_TEST_MARKER();
2012 /*-----------------------------------------------------------*/
2014 #if ( configNUMBER_OF_CORES == 1 )
2016 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
2018 /* Ensure interrupts don't access the task lists while the lists are being
2020 taskENTER_CRITICAL();
2022 uxCurrentNumberOfTasks++;
2024 if( pxCurrentTCB == NULL )
2026 /* There are no other tasks, or all the other tasks are in
2027 * the suspended state - make this the current task. */
2028 pxCurrentTCB = pxNewTCB;
2030 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2032 /* This is the first task to be created so do the preliminary
2033 * initialisation required. We will not recover if this call
2034 * fails, but we will report the failure. */
2035 prvInitialiseTaskLists();
2039 mtCOVERAGE_TEST_MARKER();
2044 /* If the scheduler is not already running, make this task the
2045 * current task if it is the highest priority task to be created
2047 if( xSchedulerRunning == pdFALSE )
2049 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
2051 pxCurrentTCB = pxNewTCB;
2055 mtCOVERAGE_TEST_MARKER();
2060 mtCOVERAGE_TEST_MARKER();
2066 #if ( configUSE_TRACE_FACILITY == 1 )
2068 /* Add a counter into the TCB for tracing only. */
2069 pxNewTCB->uxTCBNumber = uxTaskNumber;
2071 #endif /* configUSE_TRACE_FACILITY */
2072 traceTASK_CREATE( pxNewTCB );
2074 prvAddTaskToReadyList( pxNewTCB );
2076 portSETUP_TCB( pxNewTCB );
2078 taskEXIT_CRITICAL();
2080 if( xSchedulerRunning != pdFALSE )
2082 /* If the created task is of a higher priority than the current task
2083 * then it should run now. */
2084 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2088 mtCOVERAGE_TEST_MARKER();
2092 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2094 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
2096 /* Ensure interrupts don't access the task lists while the lists are being
2098 taskENTER_CRITICAL();
2100 uxCurrentNumberOfTasks++;
2102 if( xSchedulerRunning == pdFALSE )
2104 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2106 /* This is the first task to be created so do the preliminary
2107 * initialisation required. We will not recover if this call
2108 * fails, but we will report the failure. */
2109 prvInitialiseTaskLists();
2113 mtCOVERAGE_TEST_MARKER();
2116 if( ( pxNewTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
2120 /* Check if a core is free. */
2121 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
2123 if( pxCurrentTCBs[ xCoreID ] == NULL )
2125 pxNewTCB->xTaskRunState = xCoreID;
2126 pxCurrentTCBs[ xCoreID ] = pxNewTCB;
2131 mtCOVERAGE_TEST_MARKER();
2137 mtCOVERAGE_TEST_MARKER();
2143 #if ( configUSE_TRACE_FACILITY == 1 )
2145 /* Add a counter into the TCB for tracing only. */
2146 pxNewTCB->uxTCBNumber = uxTaskNumber;
2148 #endif /* configUSE_TRACE_FACILITY */
2149 traceTASK_CREATE( pxNewTCB );
2151 prvAddTaskToReadyList( pxNewTCB );
2153 portSETUP_TCB( pxNewTCB );
2155 if( xSchedulerRunning != pdFALSE )
2157 /* If the created task is of a higher priority than another
2158 * currently running task and preemption is on then it should
2160 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2164 mtCOVERAGE_TEST_MARKER();
2167 taskEXIT_CRITICAL();
2170 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2171 /*-----------------------------------------------------------*/
2173 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
2175 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
2178 size_t uxCharsWritten;
2180 if( iSnprintfReturnValue < 0 )
2182 /* Encoding error - Return 0 to indicate that nothing
2183 * was written to the buffer. */
2186 else if( iSnprintfReturnValue >= ( int ) n )
2188 /* This is the case when the supplied buffer is not
2189 * large to hold the generated string. Return the
2190 * number of characters actually written without
2191 * counting the terminating NULL character. */
2192 uxCharsWritten = n - 1U;
2196 /* Complete string was written to the buffer. */
2197 uxCharsWritten = ( size_t ) iSnprintfReturnValue;
2200 return uxCharsWritten;
2203 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
2204 /*-----------------------------------------------------------*/
2206 #if ( INCLUDE_vTaskDelete == 1 )
2208 void vTaskDelete( TaskHandle_t xTaskToDelete )
2212 traceENTER_vTaskDelete( xTaskToDelete );
2214 taskENTER_CRITICAL();
2216 /* If null is passed in here then it is the calling task that is
2218 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
2220 /* Remove task from the ready/delayed list. */
2221 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2223 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2227 mtCOVERAGE_TEST_MARKER();
2230 /* Is the task waiting on an event also? */
2231 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2233 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2237 mtCOVERAGE_TEST_MARKER();
2240 /* Increment the uxTaskNumber also so kernel aware debuggers can
2241 * detect that the task lists need re-generating. This is done before
2242 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
2246 /* If the task is running (or yielding), we must add it to the
2247 * termination list so that an idle task can delete it when it is
2248 * no longer running. */
2249 if( taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) != pdFALSE )
2251 /* A running task or a task which is scheduled to yield is being
2252 * deleted. This cannot complete when the task is still running
2253 * on a core, as a context switch to another task is required.
2254 * Place the task in the termination list. The idle task will check
2255 * the termination list and free up any memory allocated by the
2256 * scheduler for the TCB and stack of the deleted task. */
2257 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
2259 /* Increment the ucTasksDeleted variable so the idle task knows
2260 * there is a task that has been deleted and that it should therefore
2261 * check the xTasksWaitingTermination list. */
2262 ++uxDeletedTasksWaitingCleanUp;
2264 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
2265 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
2266 traceTASK_DELETE( pxTCB );
2268 /* The pre-delete hook is primarily for the Windows simulator,
2269 * in which Windows specific clean up operations are performed,
2270 * after which it is not possible to yield away from this task -
2271 * hence xYieldPending is used to latch that a context switch is
2273 #if ( configNUMBER_OF_CORES == 1 )
2274 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
2276 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
2281 --uxCurrentNumberOfTasks;
2282 traceTASK_DELETE( pxTCB );
2284 /* Reset the next expected unblock time in case it referred to
2285 * the task that has just been deleted. */
2286 prvResetNextTaskUnblockTime();
2290 #if ( configNUMBER_OF_CORES == 1 )
2292 taskEXIT_CRITICAL();
2294 /* If the task is not deleting itself, call prvDeleteTCB from outside of
2295 * critical section. If a task deletes itself, prvDeleteTCB is called
2296 * from prvCheckTasksWaitingTermination which is called from Idle task. */
2297 if( pxTCB != pxCurrentTCB )
2299 prvDeleteTCB( pxTCB );
2302 /* Force a reschedule if it is the currently running task that has just
2304 if( xSchedulerRunning != pdFALSE )
2306 if( pxTCB == pxCurrentTCB )
2308 configASSERT( uxSchedulerSuspended == 0 );
2309 portYIELD_WITHIN_API();
2313 mtCOVERAGE_TEST_MARKER();
2317 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2319 /* If a running task is not deleting itself, call prvDeleteTCB. If a running
2320 * task deletes itself, prvDeleteTCB is called from prvCheckTasksWaitingTermination
2321 * which is called from Idle task. */
2322 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
2324 prvDeleteTCB( pxTCB );
2327 /* Force a reschedule if the task that has just been deleted was running. */
2328 if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) )
2330 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2332 configASSERT( uxSchedulerSuspended == 0 );
2333 vTaskYieldWithinAPI();
2337 prvYieldCore( pxTCB->xTaskRunState );
2341 taskEXIT_CRITICAL();
2343 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2345 traceRETURN_vTaskDelete();
2348 #endif /* INCLUDE_vTaskDelete */
2349 /*-----------------------------------------------------------*/
2351 #if ( INCLUDE_xTaskDelayUntil == 1 )
2353 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2354 const TickType_t xTimeIncrement )
2356 TickType_t xTimeToWake;
2357 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2359 traceENTER_xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
2361 configASSERT( pxPreviousWakeTime );
2362 configASSERT( ( xTimeIncrement > 0U ) );
2366 /* Minor optimisation. The tick count cannot change in this
2368 const TickType_t xConstTickCount = xTickCount;
2370 configASSERT( uxSchedulerSuspended == 1U );
2372 /* Generate the tick time at which the task wants to wake. */
2373 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2375 if( xConstTickCount < *pxPreviousWakeTime )
2377 /* The tick count has overflowed since this function was
2378 * lasted called. In this case the only time we should ever
2379 * actually delay is if the wake time has also overflowed,
2380 * and the wake time is greater than the tick time. When this
2381 * is the case it is as if neither time had overflowed. */
2382 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2384 xShouldDelay = pdTRUE;
2388 mtCOVERAGE_TEST_MARKER();
2393 /* The tick time has not overflowed. In this case we will
2394 * delay if either the wake time has overflowed, and/or the
2395 * tick time is less than the wake time. */
2396 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2398 xShouldDelay = pdTRUE;
2402 mtCOVERAGE_TEST_MARKER();
2406 /* Update the wake time ready for the next call. */
2407 *pxPreviousWakeTime = xTimeToWake;
2409 if( xShouldDelay != pdFALSE )
2411 traceTASK_DELAY_UNTIL( xTimeToWake );
2413 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2414 * the time to wake, so subtract the current tick count. */
2415 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2419 mtCOVERAGE_TEST_MARKER();
2422 xAlreadyYielded = xTaskResumeAll();
2424 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2425 * have put ourselves to sleep. */
2426 if( xAlreadyYielded == pdFALSE )
2428 taskYIELD_WITHIN_API();
2432 mtCOVERAGE_TEST_MARKER();
2435 traceRETURN_xTaskDelayUntil( xShouldDelay );
2437 return xShouldDelay;
2440 #endif /* INCLUDE_xTaskDelayUntil */
2441 /*-----------------------------------------------------------*/
2443 #if ( INCLUDE_vTaskDelay == 1 )
2445 void vTaskDelay( const TickType_t xTicksToDelay )
2447 BaseType_t xAlreadyYielded = pdFALSE;
2449 traceENTER_vTaskDelay( xTicksToDelay );
2451 /* A delay time of zero just forces a reschedule. */
2452 if( xTicksToDelay > ( TickType_t ) 0U )
2456 configASSERT( uxSchedulerSuspended == 1U );
2460 /* A task that is removed from the event list while the
2461 * scheduler is suspended will not get placed in the ready
2462 * list or removed from the blocked list until the scheduler
2465 * This task cannot be in an event list as it is the currently
2466 * executing task. */
2467 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2469 xAlreadyYielded = xTaskResumeAll();
2473 mtCOVERAGE_TEST_MARKER();
2476 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2477 * have put ourselves to sleep. */
2478 if( xAlreadyYielded == pdFALSE )
2480 taskYIELD_WITHIN_API();
2484 mtCOVERAGE_TEST_MARKER();
2487 traceRETURN_vTaskDelay();
2490 #endif /* INCLUDE_vTaskDelay */
2491 /*-----------------------------------------------------------*/
2493 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2495 eTaskState eTaskGetState( TaskHandle_t xTask )
2498 List_t const * pxStateList;
2499 List_t const * pxEventList;
2500 List_t const * pxDelayedList;
2501 List_t const * pxOverflowedDelayedList;
2502 const TCB_t * const pxTCB = xTask;
2504 traceENTER_eTaskGetState( xTask );
2506 configASSERT( pxTCB );
2508 #if ( configNUMBER_OF_CORES == 1 )
2509 if( pxTCB == pxCurrentTCB )
2511 /* The task calling this function is querying its own state. */
2517 taskENTER_CRITICAL();
2519 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2520 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2521 pxDelayedList = pxDelayedTaskList;
2522 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2524 taskEXIT_CRITICAL();
2526 if( pxEventList == &xPendingReadyList )
2528 /* The task has been placed on the pending ready list, so its
2529 * state is eReady regardless of what list the task's state list
2530 * item is currently placed on. */
2533 else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2535 /* The task being queried is referenced from one of the Blocked
2540 #if ( INCLUDE_vTaskSuspend == 1 )
2541 else if( pxStateList == &xSuspendedTaskList )
2543 /* The task being queried is referenced from the suspended
2544 * list. Is it genuinely suspended or is it blocked
2546 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2548 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2552 /* The task does not appear on the event list item of
2553 * and of the RTOS objects, but could still be in the
2554 * blocked state if it is waiting on its notification
2555 * rather than waiting on an object. If not, is
2557 eReturn = eSuspended;
2559 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2561 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2568 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2570 eReturn = eSuspended;
2572 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2579 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2581 #if ( INCLUDE_vTaskDelete == 1 )
2582 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2584 /* The task being queried is referenced from the deleted
2585 * tasks list, or it is not referenced from any lists at
2591 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
2593 #if ( configNUMBER_OF_CORES == 1 )
2595 /* If the task is not in any other state, it must be in the
2596 * Ready (including pending ready) state. */
2599 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2601 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2603 /* Is it actively running on a core? */
2608 /* If the task is not in any other state, it must be in the
2609 * Ready (including pending ready) state. */
2613 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2617 traceRETURN_eTaskGetState( eReturn );
2620 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2622 #endif /* INCLUDE_eTaskGetState */
2623 /*-----------------------------------------------------------*/
2625 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2627 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2629 TCB_t const * pxTCB;
2630 UBaseType_t uxReturn;
2632 traceENTER_uxTaskPriorityGet( xTask );
2634 taskENTER_CRITICAL();
2636 /* If null is passed in here then it is the priority of the task
2637 * that called uxTaskPriorityGet() that is being queried. */
2638 pxTCB = prvGetTCBFromHandle( xTask );
2639 uxReturn = pxTCB->uxPriority;
2641 taskEXIT_CRITICAL();
2643 traceRETURN_uxTaskPriorityGet( uxReturn );
2648 #endif /* INCLUDE_uxTaskPriorityGet */
2649 /*-----------------------------------------------------------*/
2651 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2653 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2655 TCB_t const * pxTCB;
2656 UBaseType_t uxReturn;
2657 UBaseType_t uxSavedInterruptStatus;
2659 traceENTER_uxTaskPriorityGetFromISR( xTask );
2661 /* RTOS ports that support interrupt nesting have the concept of a
2662 * maximum system call (or maximum API call) interrupt priority.
2663 * Interrupts that are above the maximum system call priority are keep
2664 * permanently enabled, even when the RTOS kernel is in a critical section,
2665 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2666 * is defined in FreeRTOSConfig.h then
2667 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2668 * failure if a FreeRTOS API function is called from an interrupt that has
2669 * been assigned a priority above the configured maximum system call
2670 * priority. Only FreeRTOS functions that end in FromISR can be called
2671 * from interrupts that have been assigned a priority at or (logically)
2672 * below the maximum system call interrupt priority. FreeRTOS maintains a
2673 * separate interrupt safe API to ensure interrupt entry is as fast and as
2674 * simple as possible. More information (albeit Cortex-M specific) is
2675 * provided on the following link:
2676 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2677 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2679 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
2681 /* If null is passed in here then it is the priority of the calling
2682 * task that is being queried. */
2683 pxTCB = prvGetTCBFromHandle( xTask );
2684 uxReturn = pxTCB->uxPriority;
2686 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2688 traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
2693 #endif /* INCLUDE_uxTaskPriorityGet */
2694 /*-----------------------------------------------------------*/
2696 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2698 UBaseType_t uxTaskBasePriorityGet( const TaskHandle_t xTask )
2700 TCB_t const * pxTCB;
2701 UBaseType_t uxReturn;
2703 traceENTER_uxTaskBasePriorityGet( xTask );
2705 taskENTER_CRITICAL();
2707 /* If null is passed in here then it is the base priority of the task
2708 * that called uxTaskBasePriorityGet() that is being queried. */
2709 pxTCB = prvGetTCBFromHandle( xTask );
2710 uxReturn = pxTCB->uxBasePriority;
2712 taskEXIT_CRITICAL();
2714 traceRETURN_uxTaskBasePriorityGet( uxReturn );
2719 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2720 /*-----------------------------------------------------------*/
2722 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2724 UBaseType_t uxTaskBasePriorityGetFromISR( const TaskHandle_t xTask )
2726 TCB_t const * pxTCB;
2727 UBaseType_t uxReturn;
2728 UBaseType_t uxSavedInterruptStatus;
2730 traceENTER_uxTaskBasePriorityGetFromISR( xTask );
2732 /* RTOS ports that support interrupt nesting have the concept of a
2733 * maximum system call (or maximum API call) interrupt priority.
2734 * Interrupts that are above the maximum system call priority are keep
2735 * permanently enabled, even when the RTOS kernel is in a critical section,
2736 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2737 * is defined in FreeRTOSConfig.h then
2738 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2739 * failure if a FreeRTOS API function is called from an interrupt that has
2740 * been assigned a priority above the configured maximum system call
2741 * priority. Only FreeRTOS functions that end in FromISR can be called
2742 * from interrupts that have been assigned a priority at or (logically)
2743 * below the maximum system call interrupt priority. FreeRTOS maintains a
2744 * separate interrupt safe API to ensure interrupt entry is as fast and as
2745 * simple as possible. More information (albeit Cortex-M specific) is
2746 * provided on the following link:
2747 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2748 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2750 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
2752 /* If null is passed in here then it is the base priority of the calling
2753 * task that is being queried. */
2754 pxTCB = prvGetTCBFromHandle( xTask );
2755 uxReturn = pxTCB->uxBasePriority;
2757 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2759 traceRETURN_uxTaskBasePriorityGetFromISR( uxReturn );
2764 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2765 /*-----------------------------------------------------------*/
2767 #if ( INCLUDE_vTaskPrioritySet == 1 )
2769 void vTaskPrioritySet( TaskHandle_t xTask,
2770 UBaseType_t uxNewPriority )
2773 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2774 BaseType_t xYieldRequired = pdFALSE;
2776 #if ( configNUMBER_OF_CORES > 1 )
2777 BaseType_t xYieldForTask = pdFALSE;
2780 traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
2782 configASSERT( uxNewPriority < configMAX_PRIORITIES );
2784 /* Ensure the new priority is valid. */
2785 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2787 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2791 mtCOVERAGE_TEST_MARKER();
2794 taskENTER_CRITICAL();
2796 /* If null is passed in here then it is the priority of the calling
2797 * task that is being changed. */
2798 pxTCB = prvGetTCBFromHandle( xTask );
2800 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2802 #if ( configUSE_MUTEXES == 1 )
2804 uxCurrentBasePriority = pxTCB->uxBasePriority;
2808 uxCurrentBasePriority = pxTCB->uxPriority;
2812 if( uxCurrentBasePriority != uxNewPriority )
2814 /* The priority change may have readied a task of higher
2815 * priority than a running task. */
2816 if( uxNewPriority > uxCurrentBasePriority )
2818 #if ( configNUMBER_OF_CORES == 1 )
2820 if( pxTCB != pxCurrentTCB )
2822 /* The priority of a task other than the currently
2823 * running task is being raised. Is the priority being
2824 * raised above that of the running task? */
2825 if( uxNewPriority > pxCurrentTCB->uxPriority )
2827 xYieldRequired = pdTRUE;
2831 mtCOVERAGE_TEST_MARKER();
2836 /* The priority of the running task is being raised,
2837 * but the running task must already be the highest
2838 * priority task able to run so no yield is required. */
2841 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2843 /* The priority of a task is being raised so
2844 * perform a yield for this task later. */
2845 xYieldForTask = pdTRUE;
2847 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2849 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2851 /* Setting the priority of a running task down means
2852 * there may now be another task of higher priority that
2853 * is ready to execute. */
2854 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2855 if( pxTCB->xPreemptionDisable == pdFALSE )
2858 xYieldRequired = pdTRUE;
2863 /* Setting the priority of any other task down does not
2864 * require a yield as the running task must be above the
2865 * new priority of the task being modified. */
2868 /* Remember the ready list the task might be referenced from
2869 * before its uxPriority member is changed so the
2870 * taskRESET_READY_PRIORITY() macro can function correctly. */
2871 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2873 #if ( configUSE_MUTEXES == 1 )
2875 /* Only change the priority being used if the task is not
2876 * currently using an inherited priority or the new priority
2877 * is bigger than the inherited priority. */
2878 if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2880 pxTCB->uxPriority = uxNewPriority;
2884 mtCOVERAGE_TEST_MARKER();
2887 /* The base priority gets set whatever. */
2888 pxTCB->uxBasePriority = uxNewPriority;
2890 #else /* if ( configUSE_MUTEXES == 1 ) */
2892 pxTCB->uxPriority = uxNewPriority;
2894 #endif /* if ( configUSE_MUTEXES == 1 ) */
2896 /* Only reset the event list item value if the value is not
2897 * being used for anything else. */
2898 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0UL ) )
2900 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2904 mtCOVERAGE_TEST_MARKER();
2907 /* If the task is in the blocked or suspended list we need do
2908 * nothing more than change its priority variable. However, if
2909 * the task is in a ready list it needs to be removed and placed
2910 * in the list appropriate to its new priority. */
2911 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2913 /* The task is currently in its ready list - remove before
2914 * adding it to its new ready list. As we are in a critical
2915 * section we can do this even if the scheduler is suspended. */
2916 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2918 /* It is known that the task is in its ready list so
2919 * there is no need to check again and the port level
2920 * reset macro can be called directly. */
2921 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2925 mtCOVERAGE_TEST_MARKER();
2928 prvAddTaskToReadyList( pxTCB );
2932 #if ( configNUMBER_OF_CORES == 1 )
2934 mtCOVERAGE_TEST_MARKER();
2938 /* It's possible that xYieldForTask was already set to pdTRUE because
2939 * its priority is being raised. However, since it is not in a ready list
2940 * we don't actually need to yield for it. */
2941 xYieldForTask = pdFALSE;
2946 if( xYieldRequired != pdFALSE )
2948 /* The running task priority is set down. Request the task to yield. */
2949 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2953 #if ( configNUMBER_OF_CORES > 1 )
2954 if( xYieldForTask != pdFALSE )
2956 /* The priority of the task is being raised. If a running
2957 * task has priority lower than this task, it should yield
2959 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2962 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2964 mtCOVERAGE_TEST_MARKER();
2968 /* Remove compiler warning about unused variables when the port
2969 * optimised task selection is not being used. */
2970 ( void ) uxPriorityUsedOnEntry;
2973 taskEXIT_CRITICAL();
2975 traceRETURN_vTaskPrioritySet();
2978 #endif /* INCLUDE_vTaskPrioritySet */
2979 /*-----------------------------------------------------------*/
2981 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2982 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2983 UBaseType_t uxCoreAffinityMask )
2987 UBaseType_t uxPrevCoreAffinityMask;
2989 #if ( configUSE_PREEMPTION == 1 )
2990 UBaseType_t uxPrevNotAllowedCores;
2993 traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
2995 taskENTER_CRITICAL();
2997 pxTCB = prvGetTCBFromHandle( xTask );
2999 uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
3000 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
3002 if( xSchedulerRunning != pdFALSE )
3004 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3006 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
3008 /* If the task can no longer run on the core it was running,
3009 * request the core to yield. */
3010 if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
3012 prvYieldCore( xCoreID );
3017 #if ( configUSE_PREEMPTION == 1 )
3019 /* Calculate the cores on which this task was not allowed to
3020 * run previously. */
3021 uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
3023 /* Does the new core mask enables this task to run on any of the
3024 * previously not allowed cores? If yes, check if this task can be
3025 * scheduled on any of those cores. */
3026 if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
3028 prvYieldForTask( pxTCB );
3031 #else /* #if( configUSE_PREEMPTION == 1 ) */
3033 mtCOVERAGE_TEST_MARKER();
3035 #endif /* #if( configUSE_PREEMPTION == 1 ) */
3039 taskEXIT_CRITICAL();
3041 traceRETURN_vTaskCoreAffinitySet();
3043 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3044 /*-----------------------------------------------------------*/
3046 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
3047 UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
3049 const TCB_t * pxTCB;
3050 UBaseType_t uxCoreAffinityMask;
3052 traceENTER_vTaskCoreAffinityGet( xTask );
3054 taskENTER_CRITICAL();
3056 pxTCB = prvGetTCBFromHandle( xTask );
3057 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
3059 taskEXIT_CRITICAL();
3061 traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
3063 return uxCoreAffinityMask;
3065 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3067 /*-----------------------------------------------------------*/
3069 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3071 void vTaskPreemptionDisable( const TaskHandle_t xTask )
3075 traceENTER_vTaskPreemptionDisable( xTask );
3077 taskENTER_CRITICAL();
3079 pxTCB = prvGetTCBFromHandle( xTask );
3081 pxTCB->xPreemptionDisable = pdTRUE;
3083 taskEXIT_CRITICAL();
3085 traceRETURN_vTaskPreemptionDisable();
3088 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3089 /*-----------------------------------------------------------*/
3091 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3093 void vTaskPreemptionEnable( const TaskHandle_t xTask )
3098 traceENTER_vTaskPreemptionEnable( xTask );
3100 taskENTER_CRITICAL();
3102 pxTCB = prvGetTCBFromHandle( xTask );
3104 pxTCB->xPreemptionDisable = pdFALSE;
3106 if( xSchedulerRunning != pdFALSE )
3108 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3110 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
3111 prvYieldCore( xCoreID );
3115 taskEXIT_CRITICAL();
3117 traceRETURN_vTaskPreemptionEnable();
3120 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3121 /*-----------------------------------------------------------*/
3123 #if ( INCLUDE_vTaskSuspend == 1 )
3125 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
3129 #if ( configNUMBER_OF_CORES > 1 )
3130 BaseType_t xTaskRunningOnCore;
3133 traceENTER_vTaskSuspend( xTaskToSuspend );
3135 taskENTER_CRITICAL();
3137 /* If null is passed in here then it is the running task that is
3138 * being suspended. */
3139 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
3141 traceTASK_SUSPEND( pxTCB );
3143 #if ( configNUMBER_OF_CORES > 1 )
3144 xTaskRunningOnCore = pxTCB->xTaskRunState;
3147 /* Remove task from the ready/delayed list and place in the
3148 * suspended list. */
3149 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
3151 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
3155 mtCOVERAGE_TEST_MARKER();
3158 /* Is the task waiting on an event also? */
3159 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3161 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3165 mtCOVERAGE_TEST_MARKER();
3168 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
3170 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3174 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3176 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3178 /* The task was blocked to wait for a notification, but is
3179 * now suspended, so no notification was received. */
3180 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
3184 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3187 #if ( configNUMBER_OF_CORES == 1 )
3189 taskEXIT_CRITICAL();
3191 if( xSchedulerRunning != pdFALSE )
3193 /* Reset the next expected unblock time in case it referred to the
3194 * task that is now in the Suspended state. */
3195 taskENTER_CRITICAL();
3197 prvResetNextTaskUnblockTime();
3199 taskEXIT_CRITICAL();
3203 mtCOVERAGE_TEST_MARKER();
3206 if( pxTCB == pxCurrentTCB )
3208 if( xSchedulerRunning != pdFALSE )
3210 /* The current task has just been suspended. */
3211 configASSERT( uxSchedulerSuspended == 0 );
3212 portYIELD_WITHIN_API();
3216 /* The scheduler is not running, but the task that was pointed
3217 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
3218 * must be adjusted to point to a different task. */
3219 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
3221 /* No other tasks are ready, so set pxCurrentTCB back to
3222 * NULL so when the next task is created pxCurrentTCB will
3223 * be set to point to it no matter what its relative priority
3225 pxCurrentTCB = NULL;
3229 vTaskSwitchContext();
3235 mtCOVERAGE_TEST_MARKER();
3238 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3240 if( xSchedulerRunning != pdFALSE )
3242 /* Reset the next expected unblock time in case it referred to the
3243 * task that is now in the Suspended state. */
3244 prvResetNextTaskUnblockTime();
3248 mtCOVERAGE_TEST_MARKER();
3251 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3253 if( xSchedulerRunning != pdFALSE )
3255 if( xTaskRunningOnCore == ( BaseType_t ) portGET_CORE_ID() )
3257 /* The current task has just been suspended. */
3258 configASSERT( uxSchedulerSuspended == 0 );
3259 vTaskYieldWithinAPI();
3263 prvYieldCore( xTaskRunningOnCore );
3268 /* This code path is not possible because only Idle tasks are
3269 * assigned a core before the scheduler is started ( i.e.
3270 * taskTASK_IS_RUNNING is only true for idle tasks before
3271 * the scheduler is started ) and idle tasks cannot be
3273 mtCOVERAGE_TEST_MARKER();
3278 mtCOVERAGE_TEST_MARKER();
3281 taskEXIT_CRITICAL();
3283 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3285 traceRETURN_vTaskSuspend();
3288 #endif /* INCLUDE_vTaskSuspend */
3289 /*-----------------------------------------------------------*/
3291 #if ( INCLUDE_vTaskSuspend == 1 )
3293 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
3295 BaseType_t xReturn = pdFALSE;
3296 const TCB_t * const pxTCB = xTask;
3298 /* Accesses xPendingReadyList so must be called from a critical
3301 /* It does not make sense to check if the calling task is suspended. */
3302 configASSERT( xTask );
3304 /* Is the task being resumed actually in the suspended list? */
3305 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
3307 /* Has the task already been resumed from within an ISR? */
3308 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
3310 /* Is it in the suspended list because it is in the Suspended
3311 * state, or because it is blocked with no timeout? */
3312 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
3314 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3318 /* The task does not appear on the event list item of
3319 * and of the RTOS objects, but could still be in the
3320 * blocked state if it is waiting on its notification
3321 * rather than waiting on an object. If not, is
3325 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3327 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3334 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3338 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3342 mtCOVERAGE_TEST_MARKER();
3347 mtCOVERAGE_TEST_MARKER();
3352 mtCOVERAGE_TEST_MARKER();
3356 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
3358 #endif /* INCLUDE_vTaskSuspend */
3359 /*-----------------------------------------------------------*/
3361 #if ( INCLUDE_vTaskSuspend == 1 )
3363 void vTaskResume( TaskHandle_t xTaskToResume )
3365 TCB_t * const pxTCB = xTaskToResume;
3367 traceENTER_vTaskResume( xTaskToResume );
3369 /* It does not make sense to resume the calling task. */
3370 configASSERT( xTaskToResume );
3372 #if ( configNUMBER_OF_CORES == 1 )
3374 /* The parameter cannot be NULL as it is impossible to resume the
3375 * currently executing task. */
3376 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
3379 /* The parameter cannot be NULL as it is impossible to resume the
3380 * currently executing task. It is also impossible to resume a task
3381 * that is actively running on another core but it is not safe
3382 * to check their run state here. Therefore, we get into a critical
3383 * section and check if the task is actually suspended or not. */
3387 taskENTER_CRITICAL();
3389 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3391 traceTASK_RESUME( pxTCB );
3393 /* The ready list can be accessed even if the scheduler is
3394 * suspended because this is inside a critical section. */
3395 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3396 prvAddTaskToReadyList( pxTCB );
3398 /* This yield may not cause the task just resumed to run,
3399 * but will leave the lists in the correct state for the
3401 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
3405 mtCOVERAGE_TEST_MARKER();
3408 taskEXIT_CRITICAL();
3412 mtCOVERAGE_TEST_MARKER();
3415 traceRETURN_vTaskResume();
3418 #endif /* INCLUDE_vTaskSuspend */
3420 /*-----------------------------------------------------------*/
3422 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
3424 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
3426 BaseType_t xYieldRequired = pdFALSE;
3427 TCB_t * const pxTCB = xTaskToResume;
3428 UBaseType_t uxSavedInterruptStatus;
3430 traceENTER_xTaskResumeFromISR( xTaskToResume );
3432 configASSERT( xTaskToResume );
3434 /* RTOS ports that support interrupt nesting have the concept of a
3435 * maximum system call (or maximum API call) interrupt priority.
3436 * Interrupts that are above the maximum system call priority are keep
3437 * permanently enabled, even when the RTOS kernel is in a critical section,
3438 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
3439 * is defined in FreeRTOSConfig.h then
3440 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3441 * failure if a FreeRTOS API function is called from an interrupt that has
3442 * been assigned a priority above the configured maximum system call
3443 * priority. Only FreeRTOS functions that end in FromISR can be called
3444 * from interrupts that have been assigned a priority at or (logically)
3445 * below the maximum system call interrupt priority. FreeRTOS maintains a
3446 * separate interrupt safe API to ensure interrupt entry is as fast and as
3447 * simple as possible. More information (albeit Cortex-M specific) is
3448 * provided on the following link:
3449 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3450 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3452 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3454 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3456 traceTASK_RESUME_FROM_ISR( pxTCB );
3458 /* Check the ready lists can be accessed. */
3459 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3461 #if ( configNUMBER_OF_CORES == 1 )
3463 /* Ready lists can be accessed so move the task from the
3464 * suspended list to the ready list directly. */
3465 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3467 xYieldRequired = pdTRUE;
3469 /* Mark that a yield is pending in case the user is not
3470 * using the return value to initiate a context switch
3471 * from the ISR using portYIELD_FROM_ISR. */
3472 xYieldPendings[ 0 ] = pdTRUE;
3476 mtCOVERAGE_TEST_MARKER();
3479 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3481 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3482 prvAddTaskToReadyList( pxTCB );
3486 /* The delayed or ready lists cannot be accessed so the task
3487 * is held in the pending ready list until the scheduler is
3489 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3492 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3494 prvYieldForTask( pxTCB );
3496 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3498 xYieldRequired = pdTRUE;
3501 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3505 mtCOVERAGE_TEST_MARKER();
3508 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3510 traceRETURN_xTaskResumeFromISR( xYieldRequired );
3512 return xYieldRequired;
3515 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3516 /*-----------------------------------------------------------*/
3518 static BaseType_t prvCreateIdleTasks( void )
3520 BaseType_t xReturn = pdPASS;
3522 char cIdleName[ configMAX_TASK_NAME_LEN ];
3523 TaskFunction_t pxIdleTaskFunction = NULL;
3524 BaseType_t xIdleTaskNameIndex;
3526 for( xIdleTaskNameIndex = ( BaseType_t ) 0; xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN; xIdleTaskNameIndex++ )
3528 cIdleName[ xIdleTaskNameIndex ] = configIDLE_TASK_NAME[ xIdleTaskNameIndex ];
3530 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3531 * configMAX_TASK_NAME_LEN characters just in case the memory after the
3532 * string is not accessible (extremely unlikely). */
3533 if( cIdleName[ xIdleTaskNameIndex ] == ( char ) 0x00 )
3539 mtCOVERAGE_TEST_MARKER();
3543 /* Add each idle task at the lowest priority. */
3544 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3546 #if ( configNUMBER_OF_CORES == 1 )
3548 pxIdleTaskFunction = prvIdleTask;
3550 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3552 /* In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks
3553 * are also created to ensure that each core has an idle task to
3554 * run when no other task is available to run. */
3557 pxIdleTaskFunction = prvIdleTask;
3561 pxIdleTaskFunction = prvPassiveIdleTask;
3564 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3566 /* Update the idle task name with suffix to differentiate the idle tasks.
3567 * This function is not required in single core FreeRTOS since there is
3568 * only one idle task. */
3569 #if ( configNUMBER_OF_CORES > 1 )
3571 /* Append the idle task number to the end of the name if there is space. */
3572 if( xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3574 cIdleName[ xIdleTaskNameIndex ] = ( char ) ( xCoreID + '0' );
3576 /* And append a null character if there is space. */
3577 if( ( xIdleTaskNameIndex + 1 ) < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3579 cIdleName[ xIdleTaskNameIndex + 1 ] = '\0';
3583 mtCOVERAGE_TEST_MARKER();
3588 mtCOVERAGE_TEST_MARKER();
3591 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
3593 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3595 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3596 StackType_t * pxIdleTaskStackBuffer = NULL;
3597 uint32_t ulIdleTaskStackSize;
3599 /* The Idle task is created using user provided RAM - obtain the
3600 * address of the RAM then create the idle task. */
3601 #if ( configNUMBER_OF_CORES == 1 )
3603 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3609 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3613 vApplicationGetPassiveIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize, xCoreID - 1 );
3616 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
3617 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( pxIdleTaskFunction,
3619 ulIdleTaskStackSize,
3620 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3621 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3622 pxIdleTaskStackBuffer,
3623 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3625 if( xIdleTaskHandles[ xCoreID ] != NULL )
3634 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3636 /* The Idle task is being created using dynamically allocated RAM. */
3637 xReturn = xTaskCreate( pxIdleTaskFunction,
3639 configMINIMAL_STACK_SIZE,
3641 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3642 &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3644 #endif /* configSUPPORT_STATIC_ALLOCATION */
3646 /* Break the loop if any of the idle task is failed to be created. */
3647 if( xReturn == pdFAIL )
3653 mtCOVERAGE_TEST_MARKER();
3660 /*-----------------------------------------------------------*/
3662 void vTaskStartScheduler( void )
3666 traceENTER_vTaskStartScheduler();
3668 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3670 /* Sanity check that the UBaseType_t must have greater than or equal to
3671 * the number of bits as confNUMBER_OF_CORES. */
3672 configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3674 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3676 xReturn = prvCreateIdleTasks();
3678 #if ( configUSE_TIMERS == 1 )
3680 if( xReturn == pdPASS )
3682 xReturn = xTimerCreateTimerTask();
3686 mtCOVERAGE_TEST_MARKER();
3689 #endif /* configUSE_TIMERS */
3691 if( xReturn == pdPASS )
3693 /* freertos_tasks_c_additions_init() should only be called if the user
3694 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3695 * the only macro called by the function. */
3696 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3698 freertos_tasks_c_additions_init();
3702 /* Interrupts are turned off here, to ensure a tick does not occur
3703 * before or during the call to xPortStartScheduler(). The stacks of
3704 * the created tasks contain a status word with interrupts switched on
3705 * so interrupts will automatically get re-enabled when the first task
3707 portDISABLE_INTERRUPTS();
3709 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3711 /* Switch C-Runtime's TLS Block to point to the TLS
3712 * block specific to the task that will run first. */
3713 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3717 xNextTaskUnblockTime = portMAX_DELAY;
3718 xSchedulerRunning = pdTRUE;
3719 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3721 /* If configGENERATE_RUN_TIME_STATS is defined then the following
3722 * macro must be defined to configure the timer/counter used to generate
3723 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
3724 * is set to 0 and the following line fails to build then ensure you do not
3725 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3726 * FreeRTOSConfig.h file. */
3727 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3729 traceTASK_SWITCHED_IN();
3731 /* Setting up the timer tick is hardware specific and thus in the
3732 * portable interface. */
3734 /* The return value for xPortStartScheduler is not required
3735 * hence using a void datatype. */
3736 ( void ) xPortStartScheduler();
3738 /* In most cases, xPortStartScheduler() will not return. If it
3739 * returns pdTRUE then there was not enough heap memory available
3740 * to create either the Idle or the Timer task. If it returned
3741 * pdFALSE, then the application called xTaskEndScheduler().
3742 * Most ports don't implement xTaskEndScheduler() as there is
3743 * nothing to return to. */
3747 /* This line will only be reached if the kernel could not be started,
3748 * because there was not enough FreeRTOS heap to create the idle task
3749 * or the timer task. */
3750 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3753 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3754 * meaning xIdleTaskHandles are not used anywhere else. */
3755 ( void ) xIdleTaskHandles;
3757 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3758 * from getting optimized out as it is no longer used by the kernel. */
3759 ( void ) uxTopUsedPriority;
3761 traceRETURN_vTaskStartScheduler();
3763 /*-----------------------------------------------------------*/
3765 void vTaskEndScheduler( void )
3767 traceENTER_vTaskEndScheduler();
3769 /* Stop the scheduler interrupts and call the portable scheduler end
3770 * routine so the original ISRs can be restored if necessary. The port
3771 * layer must ensure interrupts enable bit is left in the correct state. */
3772 portDISABLE_INTERRUPTS();
3773 xSchedulerRunning = pdFALSE;
3774 vPortEndScheduler();
3776 traceRETURN_vTaskEndScheduler();
3778 /*----------------------------------------------------------*/
3780 void vTaskSuspendAll( void )
3782 traceENTER_vTaskSuspendAll();
3784 #if ( configNUMBER_OF_CORES == 1 )
3786 /* A critical section is not required as the variable is of type
3787 * BaseType_t. Please read Richard Barry's reply in the following link to a
3788 * post in the FreeRTOS support forum before reporting this as a bug! -
3789 * https://goo.gl/wu4acr */
3791 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3792 * do not otherwise exhibit real time behaviour. */
3793 portSOFTWARE_BARRIER();
3795 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3796 * is used to allow calls to vTaskSuspendAll() to nest. */
3797 ++uxSchedulerSuspended;
3799 /* Enforces ordering for ports and optimised compilers that may otherwise place
3800 * the above increment elsewhere. */
3801 portMEMORY_BARRIER();
3803 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3805 UBaseType_t ulState;
3807 /* This must only be called from within a task. */
3808 portASSERT_IF_IN_ISR();
3810 if( xSchedulerRunning != pdFALSE )
3812 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3813 * We must disable interrupts before we grab the locks in the event that this task is
3814 * interrupted and switches context before incrementing uxSchedulerSuspended.
3815 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3816 * uxSchedulerSuspended since that will prevent context switches. */
3817 ulState = portSET_INTERRUPT_MASK();
3819 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3820 * do not otherwise exhibit real time behaviour. */
3821 portSOFTWARE_BARRIER();
3823 portGET_TASK_LOCK();
3825 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3826 * purpose is to prevent altering the variable when fromISR APIs are readying
3828 if( uxSchedulerSuspended == 0U )
3830 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
3832 prvCheckForRunStateChange();
3836 mtCOVERAGE_TEST_MARKER();
3841 mtCOVERAGE_TEST_MARKER();
3846 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3847 * is used to allow calls to vTaskSuspendAll() to nest. */
3848 ++uxSchedulerSuspended;
3849 portRELEASE_ISR_LOCK();
3851 portCLEAR_INTERRUPT_MASK( ulState );
3855 mtCOVERAGE_TEST_MARKER();
3858 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3860 traceRETURN_vTaskSuspendAll();
3863 /*----------------------------------------------------------*/
3865 #if ( configUSE_TICKLESS_IDLE != 0 )
3867 static TickType_t prvGetExpectedIdleTime( void )
3870 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
3872 /* uxHigherPriorityReadyTasks takes care of the case where
3873 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3874 * task that are in the Ready state, even though the idle task is
3876 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3878 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3880 uxHigherPriorityReadyTasks = pdTRUE;
3885 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3887 /* When port optimised task selection is used the uxTopReadyPriority
3888 * variable is used as a bit map. If bits other than the least
3889 * significant bit are set then there are tasks that have a priority
3890 * above the idle priority that are in the Ready state. This takes
3891 * care of the case where the co-operative scheduler is in use. */
3892 if( uxTopReadyPriority > uxLeastSignificantBit )
3894 uxHigherPriorityReadyTasks = pdTRUE;
3897 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3899 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3903 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1U )
3905 /* There are other idle priority tasks in the ready state. If
3906 * time slicing is used then the very next tick interrupt must be
3910 else if( uxHigherPriorityReadyTasks != pdFALSE )
3912 /* There are tasks in the Ready state that have a priority above the
3913 * idle priority. This path can only be reached if
3914 * configUSE_PREEMPTION is 0. */
3919 xReturn = xNextTaskUnblockTime;
3920 xReturn -= xTickCount;
3926 #endif /* configUSE_TICKLESS_IDLE */
3927 /*----------------------------------------------------------*/
3929 BaseType_t xTaskResumeAll( void )
3931 TCB_t * pxTCB = NULL;
3932 BaseType_t xAlreadyYielded = pdFALSE;
3934 traceENTER_xTaskResumeAll();
3936 #if ( configNUMBER_OF_CORES > 1 )
3937 if( xSchedulerRunning != pdFALSE )
3940 /* It is possible that an ISR caused a task to be removed from an event
3941 * list while the scheduler was suspended. If this was the case then the
3942 * removed task will have been added to the xPendingReadyList. Once the
3943 * scheduler has been resumed it is safe to move all the pending ready
3944 * tasks from this list into their appropriate ready list. */
3945 taskENTER_CRITICAL();
3948 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3950 /* If uxSchedulerSuspended is zero then this function does not match a
3951 * previous call to vTaskSuspendAll(). */
3952 configASSERT( uxSchedulerSuspended != 0U );
3954 --uxSchedulerSuspended;
3955 portRELEASE_TASK_LOCK();
3957 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3959 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3961 /* Move any readied tasks from the pending list into the
3962 * appropriate ready list. */
3963 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3965 /* MISRA Ref 11.5.3 [Void pointer assignment] */
3966 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
3967 /* coverity[misra_c_2012_rule_11_5_violation] */
3968 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) );
3969 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
3970 portMEMORY_BARRIER();
3971 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
3972 prvAddTaskToReadyList( pxTCB );
3974 #if ( configNUMBER_OF_CORES == 1 )
3976 /* If the moved task has a priority higher than the current
3977 * task then a yield must be performed. */
3978 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3980 xYieldPendings[ xCoreID ] = pdTRUE;
3984 mtCOVERAGE_TEST_MARKER();
3987 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3989 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
3990 * If the current core yielded then vTaskSwitchContext() has already been called
3991 * which sets xYieldPendings for the current core to pdTRUE. */
3993 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3998 /* A task was unblocked while the scheduler was suspended,
3999 * which may have prevented the next unblock time from being
4000 * re-calculated, in which case re-calculate it now. Mainly
4001 * important for low power tickless implementations, where
4002 * this can prevent an unnecessary exit from low power
4004 prvResetNextTaskUnblockTime();
4007 /* If any ticks occurred while the scheduler was suspended then
4008 * they should be processed now. This ensures the tick count does
4009 * not slip, and that any delayed tasks are resumed at the correct
4012 * It should be safe to call xTaskIncrementTick here from any core
4013 * since we are in a critical section and xTaskIncrementTick itself
4014 * protects itself within a critical section. Suspending the scheduler
4015 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
4017 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
4019 if( xPendedCounts > ( TickType_t ) 0U )
4023 if( xTaskIncrementTick() != pdFALSE )
4025 /* Other cores are interrupted from
4026 * within xTaskIncrementTick(). */
4027 xYieldPendings[ xCoreID ] = pdTRUE;
4031 mtCOVERAGE_TEST_MARKER();
4035 } while( xPendedCounts > ( TickType_t ) 0U );
4041 mtCOVERAGE_TEST_MARKER();
4045 if( xYieldPendings[ xCoreID ] != pdFALSE )
4047 #if ( configUSE_PREEMPTION != 0 )
4049 xAlreadyYielded = pdTRUE;
4051 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
4053 #if ( configNUMBER_OF_CORES == 1 )
4055 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
4057 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4061 mtCOVERAGE_TEST_MARKER();
4067 mtCOVERAGE_TEST_MARKER();
4070 taskEXIT_CRITICAL();
4073 traceRETURN_xTaskResumeAll( xAlreadyYielded );
4075 return xAlreadyYielded;
4077 /*-----------------------------------------------------------*/
4079 TickType_t xTaskGetTickCount( void )
4083 traceENTER_xTaskGetTickCount();
4085 /* Critical section required if running on a 16 bit processor. */
4086 portTICK_TYPE_ENTER_CRITICAL();
4088 xTicks = xTickCount;
4090 portTICK_TYPE_EXIT_CRITICAL();
4092 traceRETURN_xTaskGetTickCount( xTicks );
4096 /*-----------------------------------------------------------*/
4098 TickType_t xTaskGetTickCountFromISR( void )
4101 UBaseType_t uxSavedInterruptStatus;
4103 traceENTER_xTaskGetTickCountFromISR();
4105 /* RTOS ports that support interrupt nesting have the concept of a maximum
4106 * system call (or maximum API call) interrupt priority. Interrupts that are
4107 * above the maximum system call priority are kept permanently enabled, even
4108 * when the RTOS kernel is in a critical section, but cannot make any calls to
4109 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
4110 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
4111 * failure if a FreeRTOS API function is called from an interrupt that has been
4112 * assigned a priority above the configured maximum system call priority.
4113 * Only FreeRTOS functions that end in FromISR can be called from interrupts
4114 * that have been assigned a priority at or (logically) below the maximum
4115 * system call interrupt priority. FreeRTOS maintains a separate interrupt
4116 * safe API to ensure interrupt entry is as fast and as simple as possible.
4117 * More information (albeit Cortex-M specific) is provided on the following
4118 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
4119 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
4121 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
4123 xReturn = xTickCount;
4125 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
4127 traceRETURN_xTaskGetTickCountFromISR( xReturn );
4131 /*-----------------------------------------------------------*/
4133 UBaseType_t uxTaskGetNumberOfTasks( void )
4135 traceENTER_uxTaskGetNumberOfTasks();
4137 /* A critical section is not required because the variables are of type
4139 traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
4141 return uxCurrentNumberOfTasks;
4143 /*-----------------------------------------------------------*/
4145 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
4149 traceENTER_pcTaskGetName( xTaskToQuery );
4151 /* If null is passed in here then the name of the calling task is being
4153 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4154 configASSERT( pxTCB );
4156 traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
4158 return &( pxTCB->pcTaskName[ 0 ] );
4160 /*-----------------------------------------------------------*/
4162 #if ( INCLUDE_xTaskGetHandle == 1 )
4164 #if ( configNUMBER_OF_CORES == 1 )
4165 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4166 const char pcNameToQuery[] )
4170 TCB_t * pxReturn = NULL;
4173 BaseType_t xBreakLoop;
4175 /* This function is called with the scheduler suspended. */
4177 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4179 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4180 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4181 /* coverity[misra_c_2012_rule_11_5_violation] */
4182 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
4186 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4187 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4188 /* coverity[misra_c_2012_rule_11_5_violation] */
4189 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
4191 /* Check each character in the name looking for a match or
4193 xBreakLoop = pdFALSE;
4195 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4197 cNextChar = pxNextTCB->pcTaskName[ x ];
4199 if( cNextChar != pcNameToQuery[ x ] )
4201 /* Characters didn't match. */
4202 xBreakLoop = pdTRUE;
4204 else if( cNextChar == ( char ) 0x00 )
4206 /* Both strings terminated, a match must have been
4208 pxReturn = pxNextTCB;
4209 xBreakLoop = pdTRUE;
4213 mtCOVERAGE_TEST_MARKER();
4216 if( xBreakLoop != pdFALSE )
4222 if( pxReturn != NULL )
4224 /* The handle has been found. */
4227 } while( pxNextTCB != pxFirstTCB );
4231 mtCOVERAGE_TEST_MARKER();
4236 #else /* if ( configNUMBER_OF_CORES == 1 ) */
4237 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4238 const char pcNameToQuery[] )
4240 TCB_t * pxReturn = NULL;
4243 BaseType_t xBreakLoop;
4244 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
4245 ListItem_t * pxIterator;
4247 /* This function is called with the scheduler suspended. */
4249 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4251 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
4253 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4254 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4255 /* coverity[misra_c_2012_rule_11_5_violation] */
4256 TCB_t * pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
4258 /* Check each character in the name looking for a match or
4260 xBreakLoop = pdFALSE;
4262 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4264 cNextChar = pxTCB->pcTaskName[ x ];
4266 if( cNextChar != pcNameToQuery[ x ] )
4268 /* Characters didn't match. */
4269 xBreakLoop = pdTRUE;
4271 else if( cNextChar == ( char ) 0x00 )
4273 /* Both strings terminated, a match must have been
4276 xBreakLoop = pdTRUE;
4280 mtCOVERAGE_TEST_MARKER();
4283 if( xBreakLoop != pdFALSE )
4289 if( pxReturn != NULL )
4291 /* The handle has been found. */
4298 mtCOVERAGE_TEST_MARKER();
4303 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4305 #endif /* INCLUDE_xTaskGetHandle */
4306 /*-----------------------------------------------------------*/
4308 #if ( INCLUDE_xTaskGetHandle == 1 )
4310 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
4312 UBaseType_t uxQueue = configMAX_PRIORITIES;
4315 traceENTER_xTaskGetHandle( pcNameToQuery );
4317 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
4318 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
4322 /* Search the ready lists. */
4326 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
4330 /* Found the handle. */
4333 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4335 /* Search the delayed lists. */
4338 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
4343 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
4346 #if ( INCLUDE_vTaskSuspend == 1 )
4350 /* Search the suspended list. */
4351 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
4356 #if ( INCLUDE_vTaskDelete == 1 )
4360 /* Search the deleted list. */
4361 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
4366 ( void ) xTaskResumeAll();
4368 traceRETURN_xTaskGetHandle( pxTCB );
4373 #endif /* INCLUDE_xTaskGetHandle */
4374 /*-----------------------------------------------------------*/
4376 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4378 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4379 StackType_t ** ppuxStackBuffer,
4380 StaticTask_t ** ppxTaskBuffer )
4385 traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4387 configASSERT( ppuxStackBuffer != NULL );
4388 configASSERT( ppxTaskBuffer != NULL );
4390 pxTCB = prvGetTCBFromHandle( xTask );
4392 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4394 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4396 *ppuxStackBuffer = pxTCB->pxStack;
4397 /* MISRA Ref 11.3.1 [Misaligned access] */
4398 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
4399 /* coverity[misra_c_2012_rule_11_3_violation] */
4400 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4403 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4405 *ppuxStackBuffer = pxTCB->pxStack;
4406 *ppxTaskBuffer = NULL;
4414 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4416 *ppuxStackBuffer = pxTCB->pxStack;
4417 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4420 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4422 traceRETURN_xTaskGetStaticBuffers( xReturn );
4427 #endif /* configSUPPORT_STATIC_ALLOCATION */
4428 /*-----------------------------------------------------------*/
4430 #if ( configUSE_TRACE_FACILITY == 1 )
4432 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4433 const UBaseType_t uxArraySize,
4434 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4436 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4438 traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4442 /* Is there a space in the array for each task in the system? */
4443 if( uxArraySize >= uxCurrentNumberOfTasks )
4445 /* Fill in an TaskStatus_t structure with information on each
4446 * task in the Ready state. */
4450 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4451 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4453 /* Fill in an TaskStatus_t structure with information on each
4454 * task in the Blocked state. */
4455 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4456 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4458 #if ( INCLUDE_vTaskDelete == 1 )
4460 /* Fill in an TaskStatus_t structure with information on
4461 * each task that has been deleted but not yet cleaned up. */
4462 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4466 #if ( INCLUDE_vTaskSuspend == 1 )
4468 /* Fill in an TaskStatus_t structure with information on
4469 * each task in the Suspended state. */
4470 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4474 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4476 if( pulTotalRunTime != NULL )
4478 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4479 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4481 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4485 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4487 if( pulTotalRunTime != NULL )
4489 *pulTotalRunTime = 0;
4492 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4496 mtCOVERAGE_TEST_MARKER();
4499 ( void ) xTaskResumeAll();
4501 traceRETURN_uxTaskGetSystemState( uxTask );
4506 #endif /* configUSE_TRACE_FACILITY */
4507 /*----------------------------------------------------------*/
4509 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4511 #if ( configNUMBER_OF_CORES == 1 )
4512 TaskHandle_t xTaskGetIdleTaskHandle( void )
4514 traceENTER_xTaskGetIdleTaskHandle();
4516 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4517 * started, then xIdleTaskHandles will be NULL. */
4518 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4520 traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4522 return xIdleTaskHandles[ 0 ];
4524 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
4526 TaskHandle_t xTaskGetIdleTaskHandleForCore( BaseType_t xCoreID )
4528 traceENTER_xTaskGetIdleTaskHandleForCore( xCoreID );
4530 /* Ensure the core ID is valid. */
4531 configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE );
4533 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4534 * started, then xIdleTaskHandles will be NULL. */
4535 configASSERT( ( xIdleTaskHandles[ xCoreID ] != NULL ) );
4537 traceRETURN_xTaskGetIdleTaskHandleForCore( xIdleTaskHandles[ xCoreID ] );
4539 return xIdleTaskHandles[ xCoreID ];
4542 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4543 /*----------------------------------------------------------*/
4545 /* This conditional compilation should use inequality to 0, not equality to 1.
4546 * This is to ensure vTaskStepTick() is available when user defined low power mode
4547 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4549 #if ( configUSE_TICKLESS_IDLE != 0 )
4551 void vTaskStepTick( TickType_t xTicksToJump )
4553 TickType_t xUpdatedTickCount;
4555 traceENTER_vTaskStepTick( xTicksToJump );
4557 /* Correct the tick count value after a period during which the tick
4558 * was suppressed. Note this does *not* call the tick hook function for
4559 * each stepped tick. */
4560 xUpdatedTickCount = xTickCount + xTicksToJump;
4561 configASSERT( xUpdatedTickCount <= xNextTaskUnblockTime );
4563 if( xUpdatedTickCount == xNextTaskUnblockTime )
4565 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4566 * xTaskIncrementTick() when the scheduler resumes. This ensures
4567 * that any delayed tasks are resumed at the correct time. */
4568 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4569 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4571 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4572 taskENTER_CRITICAL();
4576 taskEXIT_CRITICAL();
4581 mtCOVERAGE_TEST_MARKER();
4584 xTickCount += xTicksToJump;
4586 traceINCREASE_TICK_COUNT( xTicksToJump );
4587 traceRETURN_vTaskStepTick();
4590 #endif /* configUSE_TICKLESS_IDLE */
4591 /*----------------------------------------------------------*/
4593 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4595 BaseType_t xYieldOccurred;
4597 traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4599 /* Must not be called with the scheduler suspended as the implementation
4600 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4601 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4603 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4604 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4607 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4608 taskENTER_CRITICAL();
4610 xPendedTicks += xTicksToCatchUp;
4612 taskEXIT_CRITICAL();
4613 xYieldOccurred = xTaskResumeAll();
4615 traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4617 return xYieldOccurred;
4619 /*----------------------------------------------------------*/
4621 #if ( INCLUDE_xTaskAbortDelay == 1 )
4623 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4625 TCB_t * pxTCB = xTask;
4628 traceENTER_xTaskAbortDelay( xTask );
4630 configASSERT( pxTCB );
4634 /* A task can only be prematurely removed from the Blocked state if
4635 * it is actually in the Blocked state. */
4636 if( eTaskGetState( xTask ) == eBlocked )
4640 /* Remove the reference to the task from the blocked list. An
4641 * interrupt won't touch the xStateListItem because the
4642 * scheduler is suspended. */
4643 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4645 /* Is the task waiting on an event also? If so remove it from
4646 * the event list too. Interrupts can touch the event list item,
4647 * even though the scheduler is suspended, so a critical section
4649 taskENTER_CRITICAL();
4651 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4653 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4655 /* This lets the task know it was forcibly removed from the
4656 * blocked state so it should not re-evaluate its block time and
4657 * then block again. */
4658 pxTCB->ucDelayAborted = pdTRUE;
4662 mtCOVERAGE_TEST_MARKER();
4665 taskEXIT_CRITICAL();
4667 /* Place the unblocked task into the appropriate ready list. */
4668 prvAddTaskToReadyList( pxTCB );
4670 /* A task being unblocked cannot cause an immediate context
4671 * switch if preemption is turned off. */
4672 #if ( configUSE_PREEMPTION == 1 )
4674 #if ( configNUMBER_OF_CORES == 1 )
4676 /* Preemption is on, but a context switch should only be
4677 * performed if the unblocked task has a priority that is
4678 * higher than the currently executing task. */
4679 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4681 /* Pend the yield to be performed when the scheduler
4682 * is unsuspended. */
4683 xYieldPendings[ 0 ] = pdTRUE;
4687 mtCOVERAGE_TEST_MARKER();
4690 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4692 taskENTER_CRITICAL();
4694 prvYieldForTask( pxTCB );
4696 taskEXIT_CRITICAL();
4698 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4700 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4707 ( void ) xTaskResumeAll();
4709 traceRETURN_xTaskAbortDelay( xReturn );
4714 #endif /* INCLUDE_xTaskAbortDelay */
4715 /*----------------------------------------------------------*/
4717 BaseType_t xTaskIncrementTick( void )
4720 TickType_t xItemValue;
4721 BaseType_t xSwitchRequired = pdFALSE;
4723 #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4724 BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4725 #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4727 traceENTER_xTaskIncrementTick();
4729 /* Called by the portable layer each time a tick interrupt occurs.
4730 * Increments the tick then checks to see if the new tick value will cause any
4731 * tasks to be unblocked. */
4732 traceTASK_INCREMENT_TICK( xTickCount );
4734 /* Tick increment should occur on every kernel timer event. Core 0 has the
4735 * responsibility to increment the tick, or increment the pended ticks if the
4736 * scheduler is suspended. If pended ticks is greater than zero, the core that
4737 * calls xTaskResumeAll has the responsibility to increment the tick. */
4738 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4740 /* Minor optimisation. The tick count cannot change in this
4742 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4744 /* Increment the RTOS tick, switching the delayed and overflowed
4745 * delayed lists if it wraps to 0. */
4746 xTickCount = xConstTickCount;
4748 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
4750 taskSWITCH_DELAYED_LISTS();
4754 mtCOVERAGE_TEST_MARKER();
4757 /* See if this tick has made a timeout expire. Tasks are stored in
4758 * the queue in the order of their wake time - meaning once one task
4759 * has been found whose block time has not expired there is no need to
4760 * look any further down the list. */
4761 if( xConstTickCount >= xNextTaskUnblockTime )
4765 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4767 /* The delayed list is empty. Set xNextTaskUnblockTime
4768 * to the maximum possible value so it is extremely
4770 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4771 * next time through. */
4772 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4777 /* The delayed list is not empty, get the value of the
4778 * item at the head of the delayed list. This is the time
4779 * at which the task at the head of the delayed list must
4780 * be removed from the Blocked state. */
4781 /* MISRA Ref 11.5.3 [Void pointer assignment] */
4782 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
4783 /* coverity[misra_c_2012_rule_11_5_violation] */
4784 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
4785 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4787 if( xConstTickCount < xItemValue )
4789 /* It is not time to unblock this item yet, but the
4790 * item value is the time at which the task at the head
4791 * of the blocked list must be removed from the Blocked
4792 * state - so record the item value in
4793 * xNextTaskUnblockTime. */
4794 xNextTaskUnblockTime = xItemValue;
4795 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
4799 mtCOVERAGE_TEST_MARKER();
4802 /* It is time to remove the item from the Blocked state. */
4803 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4805 /* Is the task waiting on an event also? If so remove
4806 * it from the event list. */
4807 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4809 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4813 mtCOVERAGE_TEST_MARKER();
4816 /* Place the unblocked task into the appropriate ready
4818 prvAddTaskToReadyList( pxTCB );
4820 /* A task being unblocked cannot cause an immediate
4821 * context switch if preemption is turned off. */
4822 #if ( configUSE_PREEMPTION == 1 )
4824 #if ( configNUMBER_OF_CORES == 1 )
4826 /* Preemption is on, but a context switch should
4827 * only be performed if the unblocked task's
4828 * priority is higher than the currently executing
4830 * The case of equal priority tasks sharing
4831 * processing time (which happens when both
4832 * preemption and time slicing are on) is
4834 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4836 xSwitchRequired = pdTRUE;
4840 mtCOVERAGE_TEST_MARKER();
4843 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4845 prvYieldForTask( pxTCB );
4847 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4849 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4854 /* Tasks of equal priority to the currently running task will share
4855 * processing time (time slice) if preemption is on, and the application
4856 * writer has not explicitly turned time slicing off. */
4857 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4859 #if ( configNUMBER_OF_CORES == 1 )
4861 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > 1U )
4863 xSwitchRequired = pdTRUE;
4867 mtCOVERAGE_TEST_MARKER();
4870 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4874 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4876 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1U )
4878 xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4882 mtCOVERAGE_TEST_MARKER();
4886 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4888 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4890 #if ( configUSE_TICK_HOOK == 1 )
4892 /* Guard against the tick hook being called when the pended tick
4893 * count is being unwound (when the scheduler is being unlocked). */
4894 if( xPendedTicks == ( TickType_t ) 0 )
4896 vApplicationTickHook();
4900 mtCOVERAGE_TEST_MARKER();
4903 #endif /* configUSE_TICK_HOOK */
4905 #if ( configUSE_PREEMPTION == 1 )
4907 #if ( configNUMBER_OF_CORES == 1 )
4909 /* For single core the core ID is always 0. */
4910 if( xYieldPendings[ 0 ] != pdFALSE )
4912 xSwitchRequired = pdTRUE;
4916 mtCOVERAGE_TEST_MARKER();
4919 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4921 BaseType_t xCoreID, xCurrentCoreID;
4922 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4924 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4926 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4927 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4930 if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4932 if( xCoreID == xCurrentCoreID )
4934 xSwitchRequired = pdTRUE;
4938 prvYieldCore( xCoreID );
4943 mtCOVERAGE_TEST_MARKER();
4948 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4950 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4956 /* The tick hook gets called at regular intervals, even if the
4957 * scheduler is locked. */
4958 #if ( configUSE_TICK_HOOK == 1 )
4960 vApplicationTickHook();
4965 traceRETURN_xTaskIncrementTick( xSwitchRequired );
4967 return xSwitchRequired;
4969 /*-----------------------------------------------------------*/
4971 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4973 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4974 TaskHookFunction_t pxHookFunction )
4978 traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4980 /* If xTask is NULL then it is the task hook of the calling task that is
4984 xTCB = ( TCB_t * ) pxCurrentTCB;
4991 /* Save the hook function in the TCB. A critical section is required as
4992 * the value can be accessed from an interrupt. */
4993 taskENTER_CRITICAL();
4995 xTCB->pxTaskTag = pxHookFunction;
4997 taskEXIT_CRITICAL();
4999 traceRETURN_vTaskSetApplicationTaskTag();
5002 #endif /* configUSE_APPLICATION_TASK_TAG */
5003 /*-----------------------------------------------------------*/
5005 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5007 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
5010 TaskHookFunction_t xReturn;
5012 traceENTER_xTaskGetApplicationTaskTag( xTask );
5014 /* If xTask is NULL then set the calling task's hook. */
5015 pxTCB = prvGetTCBFromHandle( xTask );
5017 /* Save the hook function in the TCB. A critical section is required as
5018 * the value can be accessed from an interrupt. */
5019 taskENTER_CRITICAL();
5021 xReturn = pxTCB->pxTaskTag;
5023 taskEXIT_CRITICAL();
5025 traceRETURN_xTaskGetApplicationTaskTag( xReturn );
5030 #endif /* configUSE_APPLICATION_TASK_TAG */
5031 /*-----------------------------------------------------------*/
5033 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5035 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
5038 TaskHookFunction_t xReturn;
5039 UBaseType_t uxSavedInterruptStatus;
5041 traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
5043 /* If xTask is NULL then set the calling task's hook. */
5044 pxTCB = prvGetTCBFromHandle( xTask );
5046 /* Save the hook function in the TCB. A critical section is required as
5047 * the value can be accessed from an interrupt. */
5048 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
5050 xReturn = pxTCB->pxTaskTag;
5052 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
5054 traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
5059 #endif /* configUSE_APPLICATION_TASK_TAG */
5060 /*-----------------------------------------------------------*/
5062 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5064 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
5065 void * pvParameter )
5070 traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
5072 /* If xTask is NULL then we are calling our own task hook. */
5075 xTCB = pxCurrentTCB;
5082 if( xTCB->pxTaskTag != NULL )
5084 xReturn = xTCB->pxTaskTag( pvParameter );
5091 traceRETURN_xTaskCallApplicationTaskHook( xReturn );
5096 #endif /* configUSE_APPLICATION_TASK_TAG */
5097 /*-----------------------------------------------------------*/
5099 #if ( configNUMBER_OF_CORES == 1 )
5100 void vTaskSwitchContext( void )
5102 traceENTER_vTaskSwitchContext();
5104 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5106 /* The scheduler is currently suspended - do not allow a context
5108 xYieldPendings[ 0 ] = pdTRUE;
5112 xYieldPendings[ 0 ] = pdFALSE;
5113 traceTASK_SWITCHED_OUT();
5115 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5117 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5118 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
5120 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
5123 /* Add the amount of time the task has been running to the
5124 * accumulated time so far. The time the task started running was
5125 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5126 * protection here so count values are only valid until the timer
5127 * overflows. The guard against negative values is to protect
5128 * against suspect run time stat counter implementations - which
5129 * are provided by the application, not the kernel. */
5130 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
5132 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
5136 mtCOVERAGE_TEST_MARKER();
5139 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
5141 #endif /* configGENERATE_RUN_TIME_STATS */
5143 /* Check for stack overflow, if configured. */
5144 taskCHECK_FOR_STACK_OVERFLOW();
5146 /* Before the currently running task is switched out, save its errno. */
5147 #if ( configUSE_POSIX_ERRNO == 1 )
5149 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
5153 /* Select a new task to run using either the generic C or port
5154 * optimised asm code. */
5155 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5156 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5157 /* coverity[misra_c_2012_rule_11_5_violation] */
5158 taskSELECT_HIGHEST_PRIORITY_TASK();
5159 traceTASK_SWITCHED_IN();
5161 /* After the new task is switched in, update the global errno. */
5162 #if ( configUSE_POSIX_ERRNO == 1 )
5164 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
5168 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5170 /* Switch C-Runtime's TLS Block to point to the TLS
5171 * Block specific to this task. */
5172 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
5177 traceRETURN_vTaskSwitchContext();
5179 #else /* if ( configNUMBER_OF_CORES == 1 ) */
5180 void vTaskSwitchContext( BaseType_t xCoreID )
5182 traceENTER_vTaskSwitchContext();
5184 /* Acquire both locks:
5185 * - The ISR lock protects the ready list from simultaneous access by
5186 * both other ISRs and tasks.
5187 * - We also take the task lock to pause here in case another core has
5188 * suspended the scheduler. We don't want to simply set xYieldPending
5189 * and move on if another core suspended the scheduler. We should only
5190 * do that if the current core has suspended the scheduler. */
5192 portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
5195 /* vTaskSwitchContext() must never be called from within a critical section.
5196 * This is not necessarily true for single core FreeRTOS, but it is for this
5198 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
5200 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5202 /* The scheduler is currently suspended - do not allow a context
5204 xYieldPendings[ xCoreID ] = pdTRUE;
5208 xYieldPendings[ xCoreID ] = pdFALSE;
5209 traceTASK_SWITCHED_OUT();
5211 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5213 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5214 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
5216 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
5219 /* Add the amount of time the task has been running to the
5220 * accumulated time so far. The time the task started running was
5221 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5222 * protection here so count values are only valid until the timer
5223 * overflows. The guard against negative values is to protect
5224 * against suspect run time stat counter implementations - which
5225 * are provided by the application, not the kernel. */
5226 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
5228 pxCurrentTCBs[ xCoreID ]->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
5232 mtCOVERAGE_TEST_MARKER();
5235 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
5237 #endif /* configGENERATE_RUN_TIME_STATS */
5239 /* Check for stack overflow, if configured. */
5240 taskCHECK_FOR_STACK_OVERFLOW();
5242 /* Before the currently running task is switched out, save its errno. */
5243 #if ( configUSE_POSIX_ERRNO == 1 )
5245 pxCurrentTCBs[ xCoreID ]->iTaskErrno = FreeRTOS_errno;
5249 /* Select a new task to run. */
5250 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
5251 traceTASK_SWITCHED_IN();
5253 /* After the new task is switched in, update the global errno. */
5254 #if ( configUSE_POSIX_ERRNO == 1 )
5256 FreeRTOS_errno = pxCurrentTCBs[ xCoreID ]->iTaskErrno;
5260 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5262 /* Switch C-Runtime's TLS Block to point to the TLS
5263 * Block specific to this task. */
5264 configSET_TLS_BLOCK( pxCurrentTCBs[ xCoreID ]->xTLSBlock );
5269 portRELEASE_ISR_LOCK();
5270 portRELEASE_TASK_LOCK();
5272 traceRETURN_vTaskSwitchContext();
5274 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
5275 /*-----------------------------------------------------------*/
5277 void vTaskPlaceOnEventList( List_t * const pxEventList,
5278 const TickType_t xTicksToWait )
5280 traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
5282 configASSERT( pxEventList );
5284 /* THIS FUNCTION MUST BE CALLED WITH THE
5285 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
5287 /* Place the event list item of the TCB in the appropriate event list.
5288 * This is placed in the list in priority order so the highest priority task
5289 * is the first to be woken by the event.
5291 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
5292 * Normally, the xItemValue of a TCB's ListItem_t members is:
5293 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
5294 * Therefore, the event list is sorted in descending priority order.
5296 * The queue that contains the event list is locked, preventing
5297 * simultaneous access from interrupts. */
5298 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5300 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5302 traceRETURN_vTaskPlaceOnEventList();
5304 /*-----------------------------------------------------------*/
5306 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
5307 const TickType_t xItemValue,
5308 const TickType_t xTicksToWait )
5310 traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
5312 configASSERT( pxEventList );
5314 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5315 * the event groups implementation. */
5316 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5318 /* Store the item value in the event list item. It is safe to access the
5319 * event list item here as interrupts won't access the event list item of a
5320 * task that is not in the Blocked state. */
5321 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5323 /* Place the event list item of the TCB at the end of the appropriate event
5324 * list. It is safe to access the event list here because it is part of an
5325 * event group implementation - and interrupts don't access event groups
5326 * directly (instead they access them indirectly by pending function calls to
5327 * the task level). */
5328 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5330 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5332 traceRETURN_vTaskPlaceOnUnorderedEventList();
5334 /*-----------------------------------------------------------*/
5336 #if ( configUSE_TIMERS == 1 )
5338 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
5339 TickType_t xTicksToWait,
5340 const BaseType_t xWaitIndefinitely )
5342 traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
5344 configASSERT( pxEventList );
5346 /* This function should not be called by application code hence the
5347 * 'Restricted' in its name. It is not part of the public API. It is
5348 * designed for use by kernel code, and has special calling requirements -
5349 * it should be called with the scheduler suspended. */
5352 /* Place the event list item of the TCB in the appropriate event list.
5353 * In this case it is assume that this is the only task that is going to
5354 * be waiting on this event list, so the faster vListInsertEnd() function
5355 * can be used in place of vListInsert. */
5356 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5358 /* If the task should block indefinitely then set the block time to a
5359 * value that will be recognised as an indefinite delay inside the
5360 * prvAddCurrentTaskToDelayedList() function. */
5361 if( xWaitIndefinitely != pdFALSE )
5363 xTicksToWait = portMAX_DELAY;
5366 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
5367 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
5369 traceRETURN_vTaskPlaceOnEventListRestricted();
5372 #endif /* configUSE_TIMERS */
5373 /*-----------------------------------------------------------*/
5375 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
5377 TCB_t * pxUnblockedTCB;
5380 traceENTER_xTaskRemoveFromEventList( pxEventList );
5382 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
5383 * called from a critical section within an ISR. */
5385 /* The event list is sorted in priority order, so the first in the list can
5386 * be removed as it is known to be the highest priority. Remove the TCB from
5387 * the delayed list, and add it to the ready list.
5389 * If an event is for a queue that is locked then this function will never
5390 * get called - the lock count on the queue will get modified instead. This
5391 * means exclusive access to the event list is guaranteed here.
5393 * This function assumes that a check has already been made to ensure that
5394 * pxEventList is not empty. */
5395 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5396 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5397 /* coverity[misra_c_2012_rule_11_5_violation] */
5398 pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
5399 configASSERT( pxUnblockedTCB );
5400 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5402 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5404 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5405 prvAddTaskToReadyList( pxUnblockedTCB );
5407 #if ( configUSE_TICKLESS_IDLE != 0 )
5409 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5410 * might be set to the blocked task's time out time. If the task is
5411 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5412 * normally left unchanged, because it is automatically reset to a new
5413 * value when the tick count equals xNextTaskUnblockTime. However if
5414 * tickless idling is used it might be more important to enter sleep mode
5415 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5416 * ensure it is updated at the earliest possible time. */
5417 prvResetNextTaskUnblockTime();
5423 /* The delayed and ready lists cannot be accessed, so hold this task
5424 * pending until the scheduler is resumed. */
5425 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5428 #if ( configNUMBER_OF_CORES == 1 )
5430 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5432 /* Return true if the task removed from the event list has a higher
5433 * priority than the calling task. This allows the calling task to know if
5434 * it should force a context switch now. */
5437 /* Mark that a yield is pending in case the user is not using the
5438 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5439 xYieldPendings[ 0 ] = pdTRUE;
5446 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5450 #if ( configUSE_PREEMPTION == 1 )
5452 prvYieldForTask( pxUnblockedTCB );
5454 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5459 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5461 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5463 traceRETURN_xTaskRemoveFromEventList( xReturn );
5466 /*-----------------------------------------------------------*/
5468 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5469 const TickType_t xItemValue )
5471 TCB_t * pxUnblockedTCB;
5473 traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5475 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5476 * the event flags implementation. */
5477 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5479 /* Store the new item value in the event list. */
5480 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5482 /* Remove the event list form the event flag. Interrupts do not access
5484 /* MISRA Ref 11.5.3 [Void pointer assignment] */
5485 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
5486 /* coverity[misra_c_2012_rule_11_5_violation] */
5487 pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem );
5488 configASSERT( pxUnblockedTCB );
5489 listREMOVE_ITEM( pxEventListItem );
5491 #if ( configUSE_TICKLESS_IDLE != 0 )
5493 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5494 * might be set to the blocked task's time out time. If the task is
5495 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5496 * normally left unchanged, because it is automatically reset to a new
5497 * value when the tick count equals xNextTaskUnblockTime. However if
5498 * tickless idling is used it might be more important to enter sleep mode
5499 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5500 * ensure it is updated at the earliest possible time. */
5501 prvResetNextTaskUnblockTime();
5505 /* Remove the task from the delayed list and add it to the ready list. The
5506 * scheduler is suspended so interrupts will not be accessing the ready
5508 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5509 prvAddTaskToReadyList( pxUnblockedTCB );
5511 #if ( configNUMBER_OF_CORES == 1 )
5513 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5515 /* The unblocked task has a priority above that of the calling task, so
5516 * a context switch is required. This function is called with the
5517 * scheduler suspended so xYieldPending is set so the context switch
5518 * occurs immediately that the scheduler is resumed (unsuspended). */
5519 xYieldPendings[ 0 ] = pdTRUE;
5522 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5524 #if ( configUSE_PREEMPTION == 1 )
5526 taskENTER_CRITICAL();
5528 prvYieldForTask( pxUnblockedTCB );
5530 taskEXIT_CRITICAL();
5534 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5536 traceRETURN_vTaskRemoveFromUnorderedEventList();
5538 /*-----------------------------------------------------------*/
5540 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5542 traceENTER_vTaskSetTimeOutState( pxTimeOut );
5544 configASSERT( pxTimeOut );
5545 taskENTER_CRITICAL();
5547 pxTimeOut->xOverflowCount = xNumOfOverflows;
5548 pxTimeOut->xTimeOnEntering = xTickCount;
5550 taskEXIT_CRITICAL();
5552 traceRETURN_vTaskSetTimeOutState();
5554 /*-----------------------------------------------------------*/
5556 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5558 traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5560 /* For internal use only as it does not use a critical section. */
5561 pxTimeOut->xOverflowCount = xNumOfOverflows;
5562 pxTimeOut->xTimeOnEntering = xTickCount;
5564 traceRETURN_vTaskInternalSetTimeOutState();
5566 /*-----------------------------------------------------------*/
5568 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5569 TickType_t * const pxTicksToWait )
5573 traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5575 configASSERT( pxTimeOut );
5576 configASSERT( pxTicksToWait );
5578 taskENTER_CRITICAL();
5580 /* Minor optimisation. The tick count cannot change in this block. */
5581 const TickType_t xConstTickCount = xTickCount;
5582 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5584 #if ( INCLUDE_xTaskAbortDelay == 1 )
5585 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5587 /* The delay was aborted, which is not the same as a time out,
5588 * but has the same result. */
5589 pxCurrentTCB->ucDelayAborted = pdFALSE;
5595 #if ( INCLUDE_vTaskSuspend == 1 )
5596 if( *pxTicksToWait == portMAX_DELAY )
5598 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5599 * specified is the maximum block time then the task should block
5600 * indefinitely, and therefore never time out. */
5606 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
5608 /* The tick count is greater than the time at which
5609 * vTaskSetTimeout() was called, but has also overflowed since
5610 * vTaskSetTimeOut() was called. It must have wrapped all the way
5611 * around and gone past again. This passed since vTaskSetTimeout()
5614 *pxTicksToWait = ( TickType_t ) 0;
5616 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
5618 /* Not a genuine timeout. Adjust parameters for time remaining. */
5619 *pxTicksToWait -= xElapsedTime;
5620 vTaskInternalSetTimeOutState( pxTimeOut );
5625 *pxTicksToWait = ( TickType_t ) 0;
5629 taskEXIT_CRITICAL();
5631 traceRETURN_xTaskCheckForTimeOut( xReturn );
5635 /*-----------------------------------------------------------*/
5637 void vTaskMissedYield( void )
5639 traceENTER_vTaskMissedYield();
5641 /* Must be called from within a critical section. */
5642 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5644 traceRETURN_vTaskMissedYield();
5646 /*-----------------------------------------------------------*/
5648 #if ( configUSE_TRACE_FACILITY == 1 )
5650 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5652 UBaseType_t uxReturn;
5653 TCB_t const * pxTCB;
5655 traceENTER_uxTaskGetTaskNumber( xTask );
5660 uxReturn = pxTCB->uxTaskNumber;
5667 traceRETURN_uxTaskGetTaskNumber( uxReturn );
5672 #endif /* configUSE_TRACE_FACILITY */
5673 /*-----------------------------------------------------------*/
5675 #if ( configUSE_TRACE_FACILITY == 1 )
5677 void vTaskSetTaskNumber( TaskHandle_t xTask,
5678 const UBaseType_t uxHandle )
5682 traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5687 pxTCB->uxTaskNumber = uxHandle;
5690 traceRETURN_vTaskSetTaskNumber();
5693 #endif /* configUSE_TRACE_FACILITY */
5694 /*-----------------------------------------------------------*/
5697 * -----------------------------------------------------------
5698 * The passive idle task.
5699 * ----------------------------------------------------------
5701 * The passive idle task is used for all the additional cores in a SMP
5702 * system. There must be only 1 active idle task and the rest are passive
5705 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5706 * language extensions. The equivalent prototype for this function is:
5708 * void prvPassiveIdleTask( void *pvParameters );
5711 #if ( configNUMBER_OF_CORES > 1 )
5712 static portTASK_FUNCTION( prvPassiveIdleTask, pvParameters )
5714 ( void ) pvParameters;
5718 for( ; configCONTROL_INFINITE_LOOP(); )
5720 #if ( configUSE_PREEMPTION == 0 )
5722 /* If we are not using preemption we keep forcing a task switch to
5723 * see if any other task has become available. If we are using
5724 * preemption we don't need to do this as any task becoming available
5725 * will automatically get the processor anyway. */
5728 #endif /* configUSE_PREEMPTION */
5730 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5732 /* When using preemption tasks of equal priority will be
5733 * timesliced. If a task that is sharing the idle priority is ready
5734 * to run then the idle task should yield before the end of the
5737 * A critical region is not required here as we are just reading from
5738 * the list, and an occasional incorrect value will not matter. If
5739 * the ready list at the idle priority contains one more task than the
5740 * number of idle tasks, which is equal to the configured numbers of cores
5741 * then a task other than the idle task is ready to execute. */
5742 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5748 mtCOVERAGE_TEST_MARKER();
5751 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5753 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
5755 /* Call the user defined function from within the idle task. This
5756 * allows the application designer to add background functionality
5757 * without the overhead of a separate task.
5759 * This hook is intended to manage core activity such as disabling cores that go idle.
5761 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5762 * CALL A FUNCTION THAT MIGHT BLOCK. */
5763 vApplicationPassiveIdleHook();
5765 #endif /* configUSE_PASSIVE_IDLE_HOOK */
5768 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5771 * -----------------------------------------------------------
5773 * ----------------------------------------------------------
5775 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5776 * language extensions. The equivalent prototype for this function is:
5778 * void prvIdleTask( void *pvParameters );
5782 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5784 /* Stop warnings. */
5785 ( void ) pvParameters;
5787 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5788 * SCHEDULER IS STARTED. **/
5790 /* In case a task that has a secure context deletes itself, in which case
5791 * the idle task is responsible for deleting the task's secure context, if
5793 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5795 #if ( configNUMBER_OF_CORES > 1 )
5797 /* SMP all cores start up in the idle task. This initial yield gets the application
5801 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5803 for( ; configCONTROL_INFINITE_LOOP(); )
5805 /* See if any tasks have deleted themselves - if so then the idle task
5806 * is responsible for freeing the deleted task's TCB and stack. */
5807 prvCheckTasksWaitingTermination();
5809 #if ( configUSE_PREEMPTION == 0 )
5811 /* If we are not using preemption we keep forcing a task switch to
5812 * see if any other task has become available. If we are using
5813 * preemption we don't need to do this as any task becoming available
5814 * will automatically get the processor anyway. */
5817 #endif /* configUSE_PREEMPTION */
5819 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5821 /* When using preemption tasks of equal priority will be
5822 * timesliced. If a task that is sharing the idle priority is ready
5823 * to run then the idle task should yield before the end of the
5826 * A critical region is not required here as we are just reading from
5827 * the list, and an occasional incorrect value will not matter. If
5828 * the ready list at the idle priority contains one more task than the
5829 * number of idle tasks, which is equal to the configured numbers of cores
5830 * then a task other than the idle task is ready to execute. */
5831 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5837 mtCOVERAGE_TEST_MARKER();
5840 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5842 #if ( configUSE_IDLE_HOOK == 1 )
5844 /* Call the user defined function from within the idle task. */
5845 vApplicationIdleHook();
5847 #endif /* configUSE_IDLE_HOOK */
5849 /* This conditional compilation should use inequality to 0, not equality
5850 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5851 * user defined low power mode implementations require
5852 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5853 #if ( configUSE_TICKLESS_IDLE != 0 )
5855 TickType_t xExpectedIdleTime;
5857 /* It is not desirable to suspend then resume the scheduler on
5858 * each iteration of the idle task. Therefore, a preliminary
5859 * test of the expected idle time is performed without the
5860 * scheduler suspended. The result here is not necessarily
5862 xExpectedIdleTime = prvGetExpectedIdleTime();
5864 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5868 /* Now the scheduler is suspended, the expected idle
5869 * time can be sampled again, and this time its value can
5871 configASSERT( xNextTaskUnblockTime >= xTickCount );
5872 xExpectedIdleTime = prvGetExpectedIdleTime();
5874 /* Define the following macro to set xExpectedIdleTime to 0
5875 * if the application does not want
5876 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5877 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5879 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5881 traceLOW_POWER_IDLE_BEGIN();
5882 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5883 traceLOW_POWER_IDLE_END();
5887 mtCOVERAGE_TEST_MARKER();
5890 ( void ) xTaskResumeAll();
5894 mtCOVERAGE_TEST_MARKER();
5897 #endif /* configUSE_TICKLESS_IDLE */
5899 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) )
5901 /* Call the user defined function from within the idle task. This
5902 * allows the application designer to add background functionality
5903 * without the overhead of a separate task.
5905 * This hook is intended to manage core activity such as disabling cores that go idle.
5907 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5908 * CALL A FUNCTION THAT MIGHT BLOCK. */
5909 vApplicationPassiveIdleHook();
5911 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) ) */
5914 /*-----------------------------------------------------------*/
5916 #if ( configUSE_TICKLESS_IDLE != 0 )
5918 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5920 #if ( INCLUDE_vTaskSuspend == 1 )
5921 /* The idle task exists in addition to the application tasks. */
5922 const UBaseType_t uxNonApplicationTasks = configNUMBER_OF_CORES;
5923 #endif /* INCLUDE_vTaskSuspend */
5925 eSleepModeStatus eReturn = eStandardSleep;
5927 traceENTER_eTaskConfirmSleepModeStatus();
5929 /* This function must be called from a critical section. */
5931 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0U )
5933 /* A task was made ready while the scheduler was suspended. */
5934 eReturn = eAbortSleep;
5936 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5938 /* A yield was pended while the scheduler was suspended. */
5939 eReturn = eAbortSleep;
5941 else if( xPendedTicks != 0U )
5943 /* A tick interrupt has already occurred but was held pending
5944 * because the scheduler is suspended. */
5945 eReturn = eAbortSleep;
5948 #if ( INCLUDE_vTaskSuspend == 1 )
5949 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5951 /* If all the tasks are in the suspended list (which might mean they
5952 * have an infinite block time rather than actually being suspended)
5953 * then it is safe to turn all clocks off and just wait for external
5955 eReturn = eNoTasksWaitingTimeout;
5957 #endif /* INCLUDE_vTaskSuspend */
5960 mtCOVERAGE_TEST_MARKER();
5963 traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5968 #endif /* configUSE_TICKLESS_IDLE */
5969 /*-----------------------------------------------------------*/
5971 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5973 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5979 traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
5981 if( ( xIndex >= 0 ) &&
5982 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5984 pxTCB = prvGetTCBFromHandle( xTaskToSet );
5985 configASSERT( pxTCB != NULL );
5986 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5989 traceRETURN_vTaskSetThreadLocalStoragePointer();
5992 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5993 /*-----------------------------------------------------------*/
5995 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5997 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
6000 void * pvReturn = NULL;
6003 traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
6005 if( ( xIndex >= 0 ) &&
6006 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
6008 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
6009 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
6016 traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
6021 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
6022 /*-----------------------------------------------------------*/
6024 #if ( portUSING_MPU_WRAPPERS == 1 )
6026 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
6027 const MemoryRegion_t * const pxRegions )
6031 traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
6033 /* If null is passed in here then we are modifying the MPU settings of
6034 * the calling task. */
6035 pxTCB = prvGetTCBFromHandle( xTaskToModify );
6037 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
6039 traceRETURN_vTaskAllocateMPURegions();
6042 #endif /* portUSING_MPU_WRAPPERS */
6043 /*-----------------------------------------------------------*/
6045 static void prvInitialiseTaskLists( void )
6047 UBaseType_t uxPriority;
6049 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
6051 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
6054 vListInitialise( &xDelayedTaskList1 );
6055 vListInitialise( &xDelayedTaskList2 );
6056 vListInitialise( &xPendingReadyList );
6058 #if ( INCLUDE_vTaskDelete == 1 )
6060 vListInitialise( &xTasksWaitingTermination );
6062 #endif /* INCLUDE_vTaskDelete */
6064 #if ( INCLUDE_vTaskSuspend == 1 )
6066 vListInitialise( &xSuspendedTaskList );
6068 #endif /* INCLUDE_vTaskSuspend */
6070 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
6072 pxDelayedTaskList = &xDelayedTaskList1;
6073 pxOverflowDelayedTaskList = &xDelayedTaskList2;
6075 /*-----------------------------------------------------------*/
6077 static void prvCheckTasksWaitingTermination( void )
6079 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
6081 #if ( INCLUDE_vTaskDelete == 1 )
6085 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
6086 * being called too often in the idle task. */
6087 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6089 #if ( configNUMBER_OF_CORES == 1 )
6091 taskENTER_CRITICAL();
6094 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6095 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6096 /* coverity[misra_c_2012_rule_11_5_violation] */
6097 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6098 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6099 --uxCurrentNumberOfTasks;
6100 --uxDeletedTasksWaitingCleanUp;
6103 taskEXIT_CRITICAL();
6105 prvDeleteTCB( pxTCB );
6107 #else /* #if( configNUMBER_OF_CORES == 1 ) */
6111 taskENTER_CRITICAL();
6113 /* For SMP, multiple idles can be running simultaneously
6114 * and we need to check that other idles did not cleanup while we were
6115 * waiting to enter the critical section. */
6116 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6118 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6119 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6120 /* coverity[misra_c_2012_rule_11_5_violation] */
6121 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
6123 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
6125 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6126 --uxCurrentNumberOfTasks;
6127 --uxDeletedTasksWaitingCleanUp;
6131 /* The TCB to be deleted still has not yet been switched out
6132 * by the scheduler, so we will just exit this loop early and
6133 * try again next time. */
6134 taskEXIT_CRITICAL();
6139 taskEXIT_CRITICAL();
6143 prvDeleteTCB( pxTCB );
6146 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
6149 #endif /* INCLUDE_vTaskDelete */
6151 /*-----------------------------------------------------------*/
6153 #if ( configUSE_TRACE_FACILITY == 1 )
6155 void vTaskGetInfo( TaskHandle_t xTask,
6156 TaskStatus_t * pxTaskStatus,
6157 BaseType_t xGetFreeStackSpace,
6162 traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
6164 /* xTask is NULL then get the state of the calling task. */
6165 pxTCB = prvGetTCBFromHandle( xTask );
6167 pxTaskStatus->xHandle = pxTCB;
6168 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
6169 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
6170 pxTaskStatus->pxStackBase = pxTCB->pxStack;
6171 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
6172 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
6173 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
6175 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
6177 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
6179 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
6183 #if ( configUSE_MUTEXES == 1 )
6185 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
6189 pxTaskStatus->uxBasePriority = 0;
6193 #if ( configGENERATE_RUN_TIME_STATS == 1 )
6195 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
6199 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
6203 /* Obtaining the task state is a little fiddly, so is only done if the
6204 * value of eState passed into this function is eInvalid - otherwise the
6205 * state is just set to whatever is passed in. */
6206 if( eState != eInvalid )
6208 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6210 pxTaskStatus->eCurrentState = eRunning;
6214 pxTaskStatus->eCurrentState = eState;
6216 #if ( INCLUDE_vTaskSuspend == 1 )
6218 /* If the task is in the suspended list then there is a
6219 * chance it is actually just blocked indefinitely - so really
6220 * it should be reported as being in the Blocked state. */
6221 if( eState == eSuspended )
6225 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
6227 pxTaskStatus->eCurrentState = eBlocked;
6233 /* The task does not appear on the event list item of
6234 * and of the RTOS objects, but could still be in the
6235 * blocked state if it is waiting on its notification
6236 * rather than waiting on an object. If not, is
6238 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
6240 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
6242 pxTaskStatus->eCurrentState = eBlocked;
6248 ( void ) xTaskResumeAll();
6251 #endif /* INCLUDE_vTaskSuspend */
6253 /* Tasks can be in pending ready list and other state list at the
6254 * same time. These tasks are in ready state no matter what state
6255 * list the task is in. */
6256 taskENTER_CRITICAL();
6258 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
6260 pxTaskStatus->eCurrentState = eReady;
6263 taskEXIT_CRITICAL();
6268 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
6271 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
6272 * parameter is provided to allow it to be skipped. */
6273 if( xGetFreeStackSpace != pdFALSE )
6275 #if ( portSTACK_GROWTH > 0 )
6277 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
6281 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
6287 pxTaskStatus->usStackHighWaterMark = 0;
6290 traceRETURN_vTaskGetInfo();
6293 #endif /* configUSE_TRACE_FACILITY */
6294 /*-----------------------------------------------------------*/
6296 #if ( configUSE_TRACE_FACILITY == 1 )
6298 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
6302 configLIST_VOLATILE TCB_t * pxNextTCB;
6303 configLIST_VOLATILE TCB_t * pxFirstTCB;
6304 UBaseType_t uxTask = 0;
6306 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
6308 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6309 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6310 /* coverity[misra_c_2012_rule_11_5_violation] */
6311 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
6313 /* Populate an TaskStatus_t structure within the
6314 * pxTaskStatusArray array for each task that is referenced from
6315 * pxList. See the definition of TaskStatus_t in task.h for the
6316 * meaning of each TaskStatus_t structure member. */
6319 /* MISRA Ref 11.5.3 [Void pointer assignment] */
6320 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
6321 /* coverity[misra_c_2012_rule_11_5_violation] */
6322 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
6323 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
6325 } while( pxNextTCB != pxFirstTCB );
6329 mtCOVERAGE_TEST_MARKER();
6335 #endif /* configUSE_TRACE_FACILITY */
6336 /*-----------------------------------------------------------*/
6338 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
6340 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
6342 uint32_t ulCount = 0U;
6344 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
6346 pucStackByte -= portSTACK_GROWTH;
6350 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
6352 return ( configSTACK_DEPTH_TYPE ) ulCount;
6355 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
6356 /*-----------------------------------------------------------*/
6358 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
6360 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
6361 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
6362 * user to determine the return type. It gets around the problem of the value
6363 * overflowing on 8-bit types without breaking backward compatibility for
6364 * applications that expect an 8-bit return type. */
6365 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
6368 uint8_t * pucEndOfStack;
6369 configSTACK_DEPTH_TYPE uxReturn;
6371 traceENTER_uxTaskGetStackHighWaterMark2( xTask );
6373 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
6374 * the same except for their return type. Using configSTACK_DEPTH_TYPE
6375 * allows the user to determine the return type. It gets around the
6376 * problem of the value overflowing on 8-bit types without breaking
6377 * backward compatibility for applications that expect an 8-bit return
6380 pxTCB = prvGetTCBFromHandle( xTask );
6382 #if portSTACK_GROWTH < 0
6384 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6388 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6392 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
6394 traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
6399 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
6400 /*-----------------------------------------------------------*/
6402 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
6404 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
6407 uint8_t * pucEndOfStack;
6408 UBaseType_t uxReturn;
6410 traceENTER_uxTaskGetStackHighWaterMark( xTask );
6412 pxTCB = prvGetTCBFromHandle( xTask );
6414 #if portSTACK_GROWTH < 0
6416 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6420 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6424 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
6426 traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6431 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6432 /*-----------------------------------------------------------*/
6434 #if ( INCLUDE_vTaskDelete == 1 )
6436 static void prvDeleteTCB( TCB_t * pxTCB )
6438 /* This call is required specifically for the TriCore port. It must be
6439 * above the vPortFree() calls. The call is also used by ports/demos that
6440 * want to allocate and clean RAM statically. */
6441 portCLEAN_UP_TCB( pxTCB );
6443 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6445 /* Free up the memory allocated for the task's TLS Block. */
6446 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6450 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6452 /* The task can only have been allocated dynamically - free both
6453 * the stack and TCB. */
6454 vPortFreeStack( pxTCB->pxStack );
6457 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
6459 /* The task could have been allocated statically or dynamically, so
6460 * check what was statically allocated before trying to free the
6462 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6464 /* Both the stack and TCB were allocated dynamically, so both
6466 vPortFreeStack( pxTCB->pxStack );
6469 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6471 /* Only the stack was statically allocated, so the TCB is the
6472 * only memory that must be freed. */
6477 /* Neither the stack nor the TCB were allocated dynamically, so
6478 * nothing needs to be freed. */
6479 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6480 mtCOVERAGE_TEST_MARKER();
6483 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6486 #endif /* INCLUDE_vTaskDelete */
6487 /*-----------------------------------------------------------*/
6489 static void prvResetNextTaskUnblockTime( void )
6491 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6493 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
6494 * the maximum possible value so it is extremely unlikely that the
6495 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6496 * there is an item in the delayed list. */
6497 xNextTaskUnblockTime = portMAX_DELAY;
6501 /* The new current delayed list is not empty, get the value of
6502 * the item at the head of the delayed list. This is the time at
6503 * which the task at the head of the delayed list should be removed
6504 * from the Blocked state. */
6505 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6508 /*-----------------------------------------------------------*/
6510 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6512 #if ( configNUMBER_OF_CORES == 1 )
6513 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6515 TaskHandle_t xReturn;
6517 traceENTER_xTaskGetCurrentTaskHandle();
6519 /* A critical section is not required as this is not called from
6520 * an interrupt and the current TCB will always be the same for any
6521 * individual execution thread. */
6522 xReturn = pxCurrentTCB;
6524 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6528 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6529 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6531 TaskHandle_t xReturn;
6532 UBaseType_t uxSavedInterruptStatus;
6534 traceENTER_xTaskGetCurrentTaskHandle();
6536 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6538 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6540 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6542 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6547 TaskHandle_t xTaskGetCurrentTaskHandleForCore( BaseType_t xCoreID )
6549 TaskHandle_t xReturn = NULL;
6551 traceENTER_xTaskGetCurrentTaskHandleForCore( xCoreID );
6553 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6555 xReturn = pxCurrentTCBs[ xCoreID ];
6558 traceRETURN_xTaskGetCurrentTaskHandleForCore( xReturn );
6562 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6564 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
6565 /*-----------------------------------------------------------*/
6567 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6569 BaseType_t xTaskGetSchedulerState( void )
6573 traceENTER_xTaskGetSchedulerState();
6575 if( xSchedulerRunning == pdFALSE )
6577 xReturn = taskSCHEDULER_NOT_STARTED;
6581 #if ( configNUMBER_OF_CORES > 1 )
6582 taskENTER_CRITICAL();
6585 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6587 xReturn = taskSCHEDULER_RUNNING;
6591 xReturn = taskSCHEDULER_SUSPENDED;
6594 #if ( configNUMBER_OF_CORES > 1 )
6595 taskEXIT_CRITICAL();
6599 traceRETURN_xTaskGetSchedulerState( xReturn );
6604 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6605 /*-----------------------------------------------------------*/
6607 #if ( configUSE_MUTEXES == 1 )
6609 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6611 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6612 BaseType_t xReturn = pdFALSE;
6614 traceENTER_xTaskPriorityInherit( pxMutexHolder );
6616 /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6617 * inheritance is not applied in this scenario. */
6618 if( pxMutexHolder != NULL )
6620 /* If the holder of the mutex has a priority below the priority of
6621 * the task attempting to obtain the mutex then it will temporarily
6622 * inherit the priority of the task attempting to obtain the mutex. */
6623 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6625 /* Adjust the mutex holder state to account for its new
6626 * priority. Only reset the event list item value if the value is
6627 * not being used for anything else. */
6628 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0UL ) )
6630 listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
6634 mtCOVERAGE_TEST_MARKER();
6637 /* If the task being modified is in the ready state it will need
6638 * to be moved into a new list. */
6639 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6641 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6643 /* It is known that the task is in its ready list so
6644 * there is no need to check again and the port level
6645 * reset macro can be called directly. */
6646 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6650 mtCOVERAGE_TEST_MARKER();
6653 /* Inherit the priority before being moved into the new list. */
6654 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6655 prvAddTaskToReadyList( pxMutexHolderTCB );
6656 #if ( configNUMBER_OF_CORES > 1 )
6658 /* The priority of the task is raised. Yield for this task
6659 * if it is not running. */
6660 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6662 prvYieldForTask( pxMutexHolderTCB );
6665 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6669 /* Just inherit the priority. */
6670 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6673 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6675 /* Inheritance occurred. */
6680 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6682 /* The base priority of the mutex holder is lower than the
6683 * priority of the task attempting to take the mutex, but the
6684 * current priority of the mutex holder is not lower than the
6685 * priority of the task attempting to take the mutex.
6686 * Therefore the mutex holder must have already inherited a
6687 * priority, but inheritance would have occurred if that had
6688 * not been the case. */
6693 mtCOVERAGE_TEST_MARKER();
6699 mtCOVERAGE_TEST_MARKER();
6702 traceRETURN_xTaskPriorityInherit( xReturn );
6707 #endif /* configUSE_MUTEXES */
6708 /*-----------------------------------------------------------*/
6710 #if ( configUSE_MUTEXES == 1 )
6712 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6714 TCB_t * const pxTCB = pxMutexHolder;
6715 BaseType_t xReturn = pdFALSE;
6717 traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6719 if( pxMutexHolder != NULL )
6721 /* A task can only have an inherited priority if it holds the mutex.
6722 * If the mutex is held by a task then it cannot be given from an
6723 * interrupt, and if a mutex is given by the holding task then it must
6724 * be the running state task. */
6725 configASSERT( pxTCB == pxCurrentTCB );
6726 configASSERT( pxTCB->uxMutexesHeld );
6727 ( pxTCB->uxMutexesHeld )--;
6729 /* Has the holder of the mutex inherited the priority of another
6731 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6733 /* Only disinherit if no other mutexes are held. */
6734 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6736 /* A task can only have an inherited priority if it holds
6737 * the mutex. If the mutex is held by a task then it cannot be
6738 * given from an interrupt, and if a mutex is given by the
6739 * holding task then it must be the running state task. Remove
6740 * the holding task from the ready list. */
6741 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6743 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6747 mtCOVERAGE_TEST_MARKER();
6750 /* Disinherit the priority before adding the task into the
6751 * new ready list. */
6752 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6753 pxTCB->uxPriority = pxTCB->uxBasePriority;
6755 /* Reset the event list item value. It cannot be in use for
6756 * any other purpose if this task is running, and it must be
6757 * running to give back the mutex. */
6758 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
6759 prvAddTaskToReadyList( pxTCB );
6760 #if ( configNUMBER_OF_CORES > 1 )
6762 /* The priority of the task is dropped. Yield the core on
6763 * which the task is running. */
6764 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6766 prvYieldCore( pxTCB->xTaskRunState );
6769 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6771 /* Return true to indicate that a context switch is required.
6772 * This is only actually required in the corner case whereby
6773 * multiple mutexes were held and the mutexes were given back
6774 * in an order different to that in which they were taken.
6775 * If a context switch did not occur when the first mutex was
6776 * returned, even if a task was waiting on it, then a context
6777 * switch should occur when the last mutex is returned whether
6778 * a task is waiting on it or not. */
6783 mtCOVERAGE_TEST_MARKER();
6788 mtCOVERAGE_TEST_MARKER();
6793 mtCOVERAGE_TEST_MARKER();
6796 traceRETURN_xTaskPriorityDisinherit( xReturn );
6801 #endif /* configUSE_MUTEXES */
6802 /*-----------------------------------------------------------*/
6804 #if ( configUSE_MUTEXES == 1 )
6806 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6807 UBaseType_t uxHighestPriorityWaitingTask )
6809 TCB_t * const pxTCB = pxMutexHolder;
6810 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6811 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6813 traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6815 if( pxMutexHolder != NULL )
6817 /* If pxMutexHolder is not NULL then the holder must hold at least
6819 configASSERT( pxTCB->uxMutexesHeld );
6821 /* Determine the priority to which the priority of the task that
6822 * holds the mutex should be set. This will be the greater of the
6823 * holding task's base priority and the priority of the highest
6824 * priority task that is waiting to obtain the mutex. */
6825 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6827 uxPriorityToUse = uxHighestPriorityWaitingTask;
6831 uxPriorityToUse = pxTCB->uxBasePriority;
6834 /* Does the priority need to change? */
6835 if( pxTCB->uxPriority != uxPriorityToUse )
6837 /* Only disinherit if no other mutexes are held. This is a
6838 * simplification in the priority inheritance implementation. If
6839 * the task that holds the mutex is also holding other mutexes then
6840 * the other mutexes may have caused the priority inheritance. */
6841 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6843 /* If a task has timed out because it already holds the
6844 * mutex it was trying to obtain then it cannot of inherited
6845 * its own priority. */
6846 configASSERT( pxTCB != pxCurrentTCB );
6848 /* Disinherit the priority, remembering the previous
6849 * priority to facilitate determining the subject task's
6851 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6852 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6853 pxTCB->uxPriority = uxPriorityToUse;
6855 /* Only reset the event list item value if the value is not
6856 * being used for anything else. */
6857 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0UL ) )
6859 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
6863 mtCOVERAGE_TEST_MARKER();
6866 /* If the running task is not the task that holds the mutex
6867 * then the task that holds the mutex could be in either the
6868 * Ready, Blocked or Suspended states. Only remove the task
6869 * from its current state list if it is in the Ready state as
6870 * the task's priority is going to change and there is one
6871 * Ready list per priority. */
6872 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6874 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6876 /* It is known that the task is in its ready list so
6877 * there is no need to check again and the port level
6878 * reset macro can be called directly. */
6879 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6883 mtCOVERAGE_TEST_MARKER();
6886 prvAddTaskToReadyList( pxTCB );
6887 #if ( configNUMBER_OF_CORES > 1 )
6889 /* The priority of the task is dropped. Yield the core on
6890 * which the task is running. */
6891 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6893 prvYieldCore( pxTCB->xTaskRunState );
6896 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6900 mtCOVERAGE_TEST_MARKER();
6905 mtCOVERAGE_TEST_MARKER();
6910 mtCOVERAGE_TEST_MARKER();
6915 mtCOVERAGE_TEST_MARKER();
6918 traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6921 #endif /* configUSE_MUTEXES */
6922 /*-----------------------------------------------------------*/
6924 #if ( configNUMBER_OF_CORES > 1 )
6926 /* If not in a critical section then yield immediately.
6927 * Otherwise set xYieldPendings to true to wait to
6928 * yield until exiting the critical section.
6930 void vTaskYieldWithinAPI( void )
6932 traceENTER_vTaskYieldWithinAPI();
6934 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6940 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6943 traceRETURN_vTaskYieldWithinAPI();
6945 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6947 /*-----------------------------------------------------------*/
6949 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6951 void vTaskEnterCritical( void )
6953 traceENTER_vTaskEnterCritical();
6955 portDISABLE_INTERRUPTS();
6957 if( xSchedulerRunning != pdFALSE )
6959 ( pxCurrentTCB->uxCriticalNesting )++;
6961 /* This is not the interrupt safe version of the enter critical
6962 * function so assert() if it is being called from an interrupt
6963 * context. Only API functions that end in "FromISR" can be used in an
6964 * interrupt. Only assert if the critical nesting count is 1 to
6965 * protect against recursive calls if the assert function also uses a
6966 * critical section. */
6967 if( pxCurrentTCB->uxCriticalNesting == 1U )
6969 portASSERT_IF_IN_ISR();
6974 mtCOVERAGE_TEST_MARKER();
6977 traceRETURN_vTaskEnterCritical();
6980 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6981 /*-----------------------------------------------------------*/
6983 #if ( configNUMBER_OF_CORES > 1 )
6985 void vTaskEnterCritical( void )
6987 traceENTER_vTaskEnterCritical();
6989 portDISABLE_INTERRUPTS();
6991 if( xSchedulerRunning != pdFALSE )
6993 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6995 portGET_TASK_LOCK();
6999 portINCREMENT_CRITICAL_NESTING_COUNT();
7001 /* This is not the interrupt safe version of the enter critical
7002 * function so assert() if it is being called from an interrupt
7003 * context. Only API functions that end in "FromISR" can be used in an
7004 * interrupt. Only assert if the critical nesting count is 1 to
7005 * protect against recursive calls if the assert function also uses a
7006 * critical section. */
7007 if( portGET_CRITICAL_NESTING_COUNT() == 1U )
7009 portASSERT_IF_IN_ISR();
7011 if( uxSchedulerSuspended == 0U )
7013 /* The only time there would be a problem is if this is called
7014 * before a context switch and vTaskExitCritical() is called
7015 * after pxCurrentTCB changes. Therefore this should not be
7016 * used within vTaskSwitchContext(). */
7017 prvCheckForRunStateChange();
7023 mtCOVERAGE_TEST_MARKER();
7026 traceRETURN_vTaskEnterCritical();
7029 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7031 /*-----------------------------------------------------------*/
7033 #if ( configNUMBER_OF_CORES > 1 )
7035 UBaseType_t vTaskEnterCriticalFromISR( void )
7037 UBaseType_t uxSavedInterruptStatus = 0;
7039 traceENTER_vTaskEnterCriticalFromISR();
7041 if( xSchedulerRunning != pdFALSE )
7043 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
7045 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7050 portINCREMENT_CRITICAL_NESTING_COUNT();
7054 mtCOVERAGE_TEST_MARKER();
7057 traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
7059 return uxSavedInterruptStatus;
7062 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7063 /*-----------------------------------------------------------*/
7065 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
7067 void vTaskExitCritical( void )
7069 traceENTER_vTaskExitCritical();
7071 if( xSchedulerRunning != pdFALSE )
7073 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
7074 * does not match a previous call to vTaskEnterCritical(). */
7075 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
7077 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7078 * to exit critical section from ISR. */
7079 portASSERT_IF_IN_ISR();
7081 if( pxCurrentTCB->uxCriticalNesting > 0U )
7083 ( pxCurrentTCB->uxCriticalNesting )--;
7085 if( pxCurrentTCB->uxCriticalNesting == 0U )
7087 portENABLE_INTERRUPTS();
7091 mtCOVERAGE_TEST_MARKER();
7096 mtCOVERAGE_TEST_MARKER();
7101 mtCOVERAGE_TEST_MARKER();
7104 traceRETURN_vTaskExitCritical();
7107 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
7108 /*-----------------------------------------------------------*/
7110 #if ( configNUMBER_OF_CORES > 1 )
7112 void vTaskExitCritical( void )
7114 traceENTER_vTaskExitCritical();
7116 if( xSchedulerRunning != pdFALSE )
7118 /* If critical nesting count is zero then this function
7119 * does not match a previous call to vTaskEnterCritical(). */
7120 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7122 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7123 * to exit critical section from ISR. */
7124 portASSERT_IF_IN_ISR();
7126 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7128 portDECREMENT_CRITICAL_NESTING_COUNT();
7130 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7132 BaseType_t xYieldCurrentTask;
7134 /* Get the xYieldPending stats inside the critical section. */
7135 xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
7137 portRELEASE_ISR_LOCK();
7138 portRELEASE_TASK_LOCK();
7139 portENABLE_INTERRUPTS();
7141 /* When a task yields in a critical section it just sets
7142 * xYieldPending to true. So now that we have exited the
7143 * critical section check if xYieldPending is true, and
7145 if( xYieldCurrentTask != pdFALSE )
7152 mtCOVERAGE_TEST_MARKER();
7157 mtCOVERAGE_TEST_MARKER();
7162 mtCOVERAGE_TEST_MARKER();
7165 traceRETURN_vTaskExitCritical();
7168 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7169 /*-----------------------------------------------------------*/
7171 #if ( configNUMBER_OF_CORES > 1 )
7173 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
7175 traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
7177 if( xSchedulerRunning != pdFALSE )
7179 /* If critical nesting count is zero then this function
7180 * does not match a previous call to vTaskEnterCritical(). */
7181 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7183 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7185 portDECREMENT_CRITICAL_NESTING_COUNT();
7187 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7189 portRELEASE_ISR_LOCK();
7190 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
7194 mtCOVERAGE_TEST_MARKER();
7199 mtCOVERAGE_TEST_MARKER();
7204 mtCOVERAGE_TEST_MARKER();
7207 traceRETURN_vTaskExitCriticalFromISR();
7210 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7211 /*-----------------------------------------------------------*/
7213 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
7215 static char * prvWriteNameToBuffer( char * pcBuffer,
7216 const char * pcTaskName )
7220 /* Start by copying the entire string. */
7221 ( void ) strcpy( pcBuffer, pcTaskName );
7223 /* Pad the end of the string with spaces to ensure columns line up when
7225 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
7227 pcBuffer[ x ] = ' ';
7231 pcBuffer[ x ] = ( char ) 0x00;
7233 /* Return the new end of string. */
7234 return &( pcBuffer[ x ] );
7237 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
7238 /*-----------------------------------------------------------*/
7240 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
7242 void vTaskListTasks( char * pcWriteBuffer,
7243 size_t uxBufferLength )
7245 TaskStatus_t * pxTaskStatusArray;
7246 size_t uxConsumedBufferLength = 0;
7247 size_t uxCharsWrittenBySnprintf;
7248 int iSnprintfReturnValue;
7249 BaseType_t xOutputBufferFull = pdFALSE;
7250 UBaseType_t uxArraySize, x;
7253 traceENTER_vTaskListTasks( pcWriteBuffer, uxBufferLength );
7258 * This function is provided for convenience only, and is used by many
7259 * of the demo applications. Do not consider it to be part of the
7262 * vTaskListTasks() calls uxTaskGetSystemState(), then formats part of the
7263 * uxTaskGetSystemState() output into a human readable table that
7264 * displays task: names, states, priority, stack usage and task number.
7265 * Stack usage specified as the number of unused StackType_t words stack can hold
7266 * on top of stack - not the number of bytes.
7268 * vTaskListTasks() has a dependency on the snprintf() C library function that
7269 * might bloat the code size, use a lot of stack, and provide different
7270 * results on different platforms. An alternative, tiny, third party,
7271 * and limited functionality implementation of snprintf() is provided in
7272 * many of the FreeRTOS/Demo sub-directories in a file called
7273 * printf-stdarg.c (note printf-stdarg.c does not provide a full
7274 * snprintf() implementation!).
7276 * It is recommended that production systems call uxTaskGetSystemState()
7277 * directly to get access to raw stats data, rather than indirectly
7278 * through a call to vTaskListTasks().
7282 /* Make sure the write buffer does not contain a string. */
7283 *pcWriteBuffer = ( char ) 0x00;
7285 /* Take a snapshot of the number of tasks in case it changes while this
7286 * function is executing. */
7287 uxArraySize = uxCurrentNumberOfTasks;
7289 /* Allocate an array index for each task. NOTE! if
7290 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7291 * equate to NULL. */
7292 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7293 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7294 /* coverity[misra_c_2012_rule_11_5_violation] */
7295 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7297 if( pxTaskStatusArray != NULL )
7299 /* Generate the (binary) data. */
7300 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
7302 /* Create a human readable table from the binary data. */
7303 for( x = 0; x < uxArraySize; x++ )
7305 switch( pxTaskStatusArray[ x ].eCurrentState )
7308 cStatus = tskRUNNING_CHAR;
7312 cStatus = tskREADY_CHAR;
7316 cStatus = tskBLOCKED_CHAR;
7320 cStatus = tskSUSPENDED_CHAR;
7324 cStatus = tskDELETED_CHAR;
7327 case eInvalid: /* Fall through. */
7328 default: /* Should not get here, but it is included
7329 * to prevent static checking errors. */
7330 cStatus = ( char ) 0x00;
7334 /* Is there enough space in the buffer to hold task name? */
7335 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7337 /* Write the task name to the string, padding with spaces so it
7338 * can be printed in tabular form more easily. */
7339 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7340 /* Do not count the terminating null character. */
7341 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7343 /* Is there space left in the buffer? -1 is done because snprintf
7344 * writes a terminating null character. So we are essentially
7345 * checking if the buffer has space to write at least one non-null
7347 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7349 /* Write the rest of the string. */
7350 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
7351 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7352 uxBufferLength - uxConsumedBufferLength,
7353 "\t%c\t%u\t%u\t%u\t0x%x\r\n",
7355 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7356 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7357 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber,
7358 ( unsigned int ) pxTaskStatusArray[ x ].uxCoreAffinityMask ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
7359 #else /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7360 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7361 uxBufferLength - uxConsumedBufferLength,
7362 "\t%c\t%u\t%u\t%u\r\n",
7364 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7365 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7366 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
7367 #endif /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7368 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7370 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7371 pcWriteBuffer += uxCharsWrittenBySnprintf; /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
7375 xOutputBufferFull = pdTRUE;
7380 xOutputBufferFull = pdTRUE;
7383 if( xOutputBufferFull == pdTRUE )
7389 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7390 * is 0 then vPortFree() will be #defined to nothing. */
7391 vPortFree( pxTaskStatusArray );
7395 mtCOVERAGE_TEST_MARKER();
7398 traceRETURN_vTaskListTasks();
7401 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7402 /*----------------------------------------------------------*/
7404 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
7406 void vTaskGetRunTimeStatistics( char * pcWriteBuffer,
7407 size_t uxBufferLength )
7409 TaskStatus_t * pxTaskStatusArray;
7410 size_t uxConsumedBufferLength = 0;
7411 size_t uxCharsWrittenBySnprintf;
7412 int iSnprintfReturnValue;
7413 BaseType_t xOutputBufferFull = pdFALSE;
7414 UBaseType_t uxArraySize, x;
7415 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
7417 traceENTER_vTaskGetRunTimeStatistics( pcWriteBuffer, uxBufferLength );
7422 * This function is provided for convenience only, and is used by many
7423 * of the demo applications. Do not consider it to be part of the
7426 * vTaskGetRunTimeStatistics() calls uxTaskGetSystemState(), then formats part
7427 * of the uxTaskGetSystemState() output into a human readable table that
7428 * displays the amount of time each task has spent in the Running state
7429 * in both absolute and percentage terms.
7431 * vTaskGetRunTimeStatistics() has a dependency on the snprintf() C library
7432 * function that might bloat the code size, use a lot of stack, and
7433 * provide different results on different platforms. An alternative,
7434 * tiny, third party, and limited functionality implementation of
7435 * snprintf() is provided in many of the FreeRTOS/Demo sub-directories in
7436 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
7437 * a full snprintf() implementation!).
7439 * It is recommended that production systems call uxTaskGetSystemState()
7440 * directly to get access to raw stats data, rather than indirectly
7441 * through a call to vTaskGetRunTimeStatistics().
7444 /* Make sure the write buffer does not contain a string. */
7445 *pcWriteBuffer = ( char ) 0x00;
7447 /* Take a snapshot of the number of tasks in case it changes while this
7448 * function is executing. */
7449 uxArraySize = uxCurrentNumberOfTasks;
7451 /* Allocate an array index for each task. NOTE! If
7452 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7453 * equate to NULL. */
7454 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
7455 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
7456 /* coverity[misra_c_2012_rule_11_5_violation] */
7457 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
7459 if( pxTaskStatusArray != NULL )
7461 /* Generate the (binary) data. */
7462 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
7464 /* For percentage calculations. */
7465 ulTotalTime /= ( ( configRUN_TIME_COUNTER_TYPE ) 100UL );
7467 /* Avoid divide by zero errors. */
7468 if( ulTotalTime > 0UL )
7470 /* Create a human readable table from the binary data. */
7471 for( x = 0; x < uxArraySize; x++ )
7473 /* What percentage of the total run time has the task used?
7474 * This will always be rounded down to the nearest integer.
7475 * ulTotalRunTime has already been divided by 100. */
7476 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
7478 /* Is there enough space in the buffer to hold task name? */
7479 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7481 /* Write the task name to the string, padding with
7482 * spaces so it can be printed in tabular form more
7484 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7485 /* Do not count the terminating null character. */
7486 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7488 /* Is there space left in the buffer? -1 is done because snprintf
7489 * writes a terminating null character. So we are essentially
7490 * checking if the buffer has space to write at least one non-null
7492 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7494 if( ulStatsAsPercentage > 0UL )
7496 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7498 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7499 uxBufferLength - uxConsumedBufferLength,
7500 "\t%lu\t\t%lu%%\r\n",
7501 pxTaskStatusArray[ x ].ulRunTimeCounter,
7502 ulStatsAsPercentage );
7506 /* sizeof( int ) == sizeof( long ) so a smaller
7507 * printf() library can be used. */
7508 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7509 uxBufferLength - uxConsumedBufferLength,
7511 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter,
7512 ( unsigned int ) ulStatsAsPercentage ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
7514 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7518 /* If the percentage is zero here then the task has
7519 * consumed less than 1% of the total run time. */
7520 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7522 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7523 uxBufferLength - uxConsumedBufferLength,
7524 "\t%lu\t\t<1%%\r\n",
7525 pxTaskStatusArray[ x ].ulRunTimeCounter );
7529 /* sizeof( int ) == sizeof( long ) so a smaller
7530 * printf() library can be used. */
7531 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7532 uxBufferLength - uxConsumedBufferLength,
7534 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
7536 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7539 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7540 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7541 pcWriteBuffer += uxCharsWrittenBySnprintf; /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
7545 xOutputBufferFull = pdTRUE;
7550 xOutputBufferFull = pdTRUE;
7553 if( xOutputBufferFull == pdTRUE )
7561 mtCOVERAGE_TEST_MARKER();
7564 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7565 * is 0 then vPortFree() will be #defined to nothing. */
7566 vPortFree( pxTaskStatusArray );
7570 mtCOVERAGE_TEST_MARKER();
7573 traceRETURN_vTaskGetRunTimeStatistics();
7576 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7577 /*-----------------------------------------------------------*/
7579 TickType_t uxTaskResetEventItemValue( void )
7581 TickType_t uxReturn;
7583 traceENTER_uxTaskResetEventItemValue();
7585 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7587 /* Reset the event list item to its normal value - so it can be used with
7588 * queues and semaphores. */
7589 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
7591 traceRETURN_uxTaskResetEventItemValue( uxReturn );
7595 /*-----------------------------------------------------------*/
7597 #if ( configUSE_MUTEXES == 1 )
7599 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7603 traceENTER_pvTaskIncrementMutexHeldCount();
7605 pxTCB = pxCurrentTCB;
7607 /* If xSemaphoreCreateMutex() is called before any tasks have been created
7608 * then pxCurrentTCB will be NULL. */
7611 ( pxTCB->uxMutexesHeld )++;
7614 traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7619 #endif /* configUSE_MUTEXES */
7620 /*-----------------------------------------------------------*/
7622 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7624 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7625 BaseType_t xClearCountOnExit,
7626 TickType_t xTicksToWait )
7629 BaseType_t xAlreadyYielded;
7631 traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7633 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7635 taskENTER_CRITICAL();
7637 /* Only block if the notification count is not already non-zero. */
7638 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0UL )
7640 /* Mark this task as waiting for a notification. */
7641 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7643 if( xTicksToWait > ( TickType_t ) 0 )
7645 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7647 /* We MUST suspend the scheduler before exiting the critical
7648 * section (i.e. before enabling interrupts).
7650 * If we do not do so, a notification sent from an ISR, which
7651 * happens after exiting the critical section and before
7652 * suspending the scheduler, will get lost. The sequence of
7654 * 1. Exit critical section.
7655 * 2. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7656 * task to the Ready list.
7657 * 3. Suspend scheduler.
7658 * 4. prvAddCurrentTaskToDelayedList moves the task to the
7659 * delayed or suspended list.
7660 * 5. Resume scheduler does not touch the task (because it is
7661 * not on the pendingReady list), effectively losing the
7662 * notification from the ISR.
7664 * The same does not happen when we suspend the scheduler before
7665 * exiting the critical section. The sequence of events in this
7667 * 1. Suspend scheduler.
7668 * 2. Exit critical section.
7669 * 3. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7670 * task to the pendingReady list as the scheduler is
7672 * 4. prvAddCurrentTaskToDelayedList adds the task to delayed or
7673 * suspended list. Note that this operation does not nullify
7674 * the add to pendingReady list done in the above step because
7675 * a different list item, namely xEventListItem, is used for
7676 * adding the task to the pendingReady list. In other words,
7677 * the task still remains on the pendingReady list.
7678 * 5. Resume scheduler moves the task from pendingReady list to
7683 taskEXIT_CRITICAL();
7685 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7687 xAlreadyYielded = xTaskResumeAll();
7689 if( xAlreadyYielded == pdFALSE )
7691 taskYIELD_WITHIN_API();
7695 mtCOVERAGE_TEST_MARKER();
7700 taskEXIT_CRITICAL();
7705 taskEXIT_CRITICAL();
7708 taskENTER_CRITICAL();
7710 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7711 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7713 if( ulReturn != 0UL )
7715 if( xClearCountOnExit != pdFALSE )
7717 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ( uint32_t ) 0UL;
7721 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7726 mtCOVERAGE_TEST_MARKER();
7729 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7731 taskEXIT_CRITICAL();
7733 traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7738 #endif /* configUSE_TASK_NOTIFICATIONS */
7739 /*-----------------------------------------------------------*/
7741 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7743 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7744 uint32_t ulBitsToClearOnEntry,
7745 uint32_t ulBitsToClearOnExit,
7746 uint32_t * pulNotificationValue,
7747 TickType_t xTicksToWait )
7749 BaseType_t xReturn, xAlreadyYielded;
7751 traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7753 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7755 taskENTER_CRITICAL();
7757 /* Only block if a notification is not already pending. */
7758 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7760 /* Clear bits in the task's notification value as bits may get
7761 * set by the notifying task or interrupt. This can be used to
7762 * clear the value to zero. */
7763 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7765 /* Mark this task as waiting for a notification. */
7766 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7768 if( xTicksToWait > ( TickType_t ) 0 )
7770 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7772 /* We MUST suspend the scheduler before exiting the critical
7773 * section (i.e. before enabling interrupts).
7775 * If we do not do so, a notification sent from an ISR, which
7776 * happens after exiting the critical section and before
7777 * suspending the scheduler, will get lost. The sequence of
7779 * 1. Exit critical section.
7780 * 2. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7781 * task to the Ready list.
7782 * 3. Suspend scheduler.
7783 * 4. prvAddCurrentTaskToDelayedList moves the task to the
7784 * delayed or suspended list.
7785 * 5. Resume scheduler does not touch the task (because it is
7786 * not on the pendingReady list), effectively losing the
7787 * notification from the ISR.
7789 * The same does not happen when we suspend the scheduler before
7790 * exiting the critical section. The sequence of events in this
7792 * 1. Suspend scheduler.
7793 * 2. Exit critical section.
7794 * 3. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7795 * task to the pendingReady list as the scheduler is
7797 * 4. prvAddCurrentTaskToDelayedList adds the task to delayed or
7798 * suspended list. Note that this operation does not nullify
7799 * the add to pendingReady list done in the above step because
7800 * a different list item, namely xEventListItem, is used for
7801 * adding the task to the pendingReady list. In other words,
7802 * the task still remains on the pendingReady list.
7803 * 5. Resume scheduler moves the task from pendingReady list to
7808 taskEXIT_CRITICAL();
7810 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7812 xAlreadyYielded = xTaskResumeAll();
7814 if( xAlreadyYielded == pdFALSE )
7816 taskYIELD_WITHIN_API();
7820 mtCOVERAGE_TEST_MARKER();
7825 taskEXIT_CRITICAL();
7830 taskEXIT_CRITICAL();
7833 taskENTER_CRITICAL();
7835 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7837 if( pulNotificationValue != NULL )
7839 /* Output the current notification value, which may or may not
7841 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7844 /* If ucNotifyValue is set then either the task never entered the
7845 * blocked state (because a notification was already pending) or the
7846 * task unblocked because of a notification. Otherwise the task
7847 * unblocked because of a timeout. */
7848 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7850 /* A notification was not received. */
7855 /* A notification was already pending or a notification was
7856 * received while the task was waiting. */
7857 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7861 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7863 taskEXIT_CRITICAL();
7865 traceRETURN_xTaskGenericNotifyWait( xReturn );
7870 #endif /* configUSE_TASK_NOTIFICATIONS */
7871 /*-----------------------------------------------------------*/
7873 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7875 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7876 UBaseType_t uxIndexToNotify,
7878 eNotifyAction eAction,
7879 uint32_t * pulPreviousNotificationValue )
7882 BaseType_t xReturn = pdPASS;
7883 uint8_t ucOriginalNotifyState;
7885 traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7887 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7888 configASSERT( xTaskToNotify );
7889 pxTCB = xTaskToNotify;
7891 taskENTER_CRITICAL();
7893 if( pulPreviousNotificationValue != NULL )
7895 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7898 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7900 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7905 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7909 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7912 case eSetValueWithOverwrite:
7913 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7916 case eSetValueWithoutOverwrite:
7918 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7920 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7924 /* The value could not be written to the task. */
7932 /* The task is being notified without its notify value being
7938 /* Should not get here if all enums are handled.
7939 * Artificially force an assert by testing a value the
7940 * compiler can't assume is const. */
7941 configASSERT( xTickCount == ( TickType_t ) 0 );
7946 traceTASK_NOTIFY( uxIndexToNotify );
7948 /* If the task is in the blocked state specifically to wait for a
7949 * notification then unblock it now. */
7950 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7952 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7953 prvAddTaskToReadyList( pxTCB );
7955 /* The task should not have been on an event list. */
7956 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7958 #if ( configUSE_TICKLESS_IDLE != 0 )
7960 /* If a task is blocked waiting for a notification then
7961 * xNextTaskUnblockTime might be set to the blocked task's time
7962 * out time. If the task is unblocked for a reason other than
7963 * a timeout xNextTaskUnblockTime is normally left unchanged,
7964 * because it will automatically get reset to a new value when
7965 * the tick count equals xNextTaskUnblockTime. However if
7966 * tickless idling is used it might be more important to enter
7967 * sleep mode at the earliest possible time - so reset
7968 * xNextTaskUnblockTime here to ensure it is updated at the
7969 * earliest possible time. */
7970 prvResetNextTaskUnblockTime();
7974 /* Check if the notified task has a priority above the currently
7975 * executing task. */
7976 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7980 mtCOVERAGE_TEST_MARKER();
7983 taskEXIT_CRITICAL();
7985 traceRETURN_xTaskGenericNotify( xReturn );
7990 #endif /* configUSE_TASK_NOTIFICATIONS */
7991 /*-----------------------------------------------------------*/
7993 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7995 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
7996 UBaseType_t uxIndexToNotify,
7998 eNotifyAction eAction,
7999 uint32_t * pulPreviousNotificationValue,
8000 BaseType_t * pxHigherPriorityTaskWoken )
8003 uint8_t ucOriginalNotifyState;
8004 BaseType_t xReturn = pdPASS;
8005 UBaseType_t uxSavedInterruptStatus;
8007 traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
8009 configASSERT( xTaskToNotify );
8010 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8012 /* RTOS ports that support interrupt nesting have the concept of a
8013 * maximum system call (or maximum API call) interrupt priority.
8014 * Interrupts that are above the maximum system call priority are keep
8015 * permanently enabled, even when the RTOS kernel is in a critical section,
8016 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
8017 * is defined in FreeRTOSConfig.h then
8018 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
8019 * failure if a FreeRTOS API function is called from an interrupt that has
8020 * been assigned a priority above the configured maximum system call
8021 * priority. Only FreeRTOS functions that end in FromISR can be called
8022 * from interrupts that have been assigned a priority at or (logically)
8023 * below the maximum system call interrupt priority. FreeRTOS maintains a
8024 * separate interrupt safe API to ensure interrupt entry is as fast and as
8025 * simple as possible. More information (albeit Cortex-M specific) is
8026 * provided on the following link:
8027 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
8028 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
8030 pxTCB = xTaskToNotify;
8032 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
8034 if( pulPreviousNotificationValue != NULL )
8036 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
8039 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8040 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8045 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
8049 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8052 case eSetValueWithOverwrite:
8053 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8056 case eSetValueWithoutOverwrite:
8058 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
8060 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
8064 /* The value could not be written to the task. */
8072 /* The task is being notified without its notify value being
8078 /* Should not get here if all enums are handled.
8079 * Artificially force an assert by testing a value the
8080 * compiler can't assume is const. */
8081 configASSERT( xTickCount == ( TickType_t ) 0 );
8085 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
8087 /* If the task is in the blocked state specifically to wait for a
8088 * notification then unblock it now. */
8089 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8091 /* The task should not have been on an event list. */
8092 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8094 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8096 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8097 prvAddTaskToReadyList( pxTCB );
8101 /* The delayed and ready lists cannot be accessed, so hold
8102 * this task pending until the scheduler is resumed. */
8103 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8106 #if ( configNUMBER_OF_CORES == 1 )
8108 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8110 /* The notified task has a priority above the currently
8111 * executing task so a yield is required. */
8112 if( pxHigherPriorityTaskWoken != NULL )
8114 *pxHigherPriorityTaskWoken = pdTRUE;
8117 /* Mark that a yield is pending in case the user is not
8118 * using the "xHigherPriorityTaskWoken" parameter to an ISR
8119 * safe FreeRTOS function. */
8120 xYieldPendings[ 0 ] = pdTRUE;
8124 mtCOVERAGE_TEST_MARKER();
8127 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8129 #if ( configUSE_PREEMPTION == 1 )
8131 prvYieldForTask( pxTCB );
8133 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8135 if( pxHigherPriorityTaskWoken != NULL )
8137 *pxHigherPriorityTaskWoken = pdTRUE;
8141 #endif /* if ( configUSE_PREEMPTION == 1 ) */
8143 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8146 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8148 traceRETURN_xTaskGenericNotifyFromISR( xReturn );
8153 #endif /* configUSE_TASK_NOTIFICATIONS */
8154 /*-----------------------------------------------------------*/
8156 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8158 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
8159 UBaseType_t uxIndexToNotify,
8160 BaseType_t * pxHigherPriorityTaskWoken )
8163 uint8_t ucOriginalNotifyState;
8164 UBaseType_t uxSavedInterruptStatus;
8166 traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
8168 configASSERT( xTaskToNotify );
8169 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8171 /* RTOS ports that support interrupt nesting have the concept of a
8172 * maximum system call (or maximum API call) interrupt priority.
8173 * Interrupts that are above the maximum system call priority are keep
8174 * permanently enabled, even when the RTOS kernel is in a critical section,
8175 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
8176 * is defined in FreeRTOSConfig.h then
8177 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
8178 * failure if a FreeRTOS API function is called from an interrupt that has
8179 * been assigned a priority above the configured maximum system call
8180 * priority. Only FreeRTOS functions that end in FromISR can be called
8181 * from interrupts that have been assigned a priority at or (logically)
8182 * below the maximum system call interrupt priority. FreeRTOS maintains a
8183 * separate interrupt safe API to ensure interrupt entry is as fast and as
8184 * simple as possible. More information (albeit Cortex-M specific) is
8185 * provided on the following link:
8186 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
8187 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
8189 pxTCB = xTaskToNotify;
8191 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
8193 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8194 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8196 /* 'Giving' is equivalent to incrementing a count in a counting
8198 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8200 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
8202 /* If the task is in the blocked state specifically to wait for a
8203 * notification then unblock it now. */
8204 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8206 /* The task should not have been on an event list. */
8207 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8209 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8211 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8212 prvAddTaskToReadyList( pxTCB );
8216 /* The delayed and ready lists cannot be accessed, so hold
8217 * this task pending until the scheduler is resumed. */
8218 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8221 #if ( configNUMBER_OF_CORES == 1 )
8223 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8225 /* The notified task has a priority above the currently
8226 * executing task so a yield is required. */
8227 if( pxHigherPriorityTaskWoken != NULL )
8229 *pxHigherPriorityTaskWoken = pdTRUE;
8232 /* Mark that a yield is pending in case the user is not
8233 * using the "xHigherPriorityTaskWoken" parameter in an ISR
8234 * safe FreeRTOS function. */
8235 xYieldPendings[ 0 ] = pdTRUE;
8239 mtCOVERAGE_TEST_MARKER();
8242 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8244 #if ( configUSE_PREEMPTION == 1 )
8246 prvYieldForTask( pxTCB );
8248 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8250 if( pxHigherPriorityTaskWoken != NULL )
8252 *pxHigherPriorityTaskWoken = pdTRUE;
8256 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
8258 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8261 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8263 traceRETURN_vTaskGenericNotifyGiveFromISR();
8266 #endif /* configUSE_TASK_NOTIFICATIONS */
8267 /*-----------------------------------------------------------*/
8269 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8271 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
8272 UBaseType_t uxIndexToClear )
8277 traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
8279 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8281 /* If null is passed in here then it is the calling task that is having
8282 * its notification state cleared. */
8283 pxTCB = prvGetTCBFromHandle( xTask );
8285 taskENTER_CRITICAL();
8287 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
8289 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
8297 taskEXIT_CRITICAL();
8299 traceRETURN_xTaskGenericNotifyStateClear( xReturn );
8304 #endif /* configUSE_TASK_NOTIFICATIONS */
8305 /*-----------------------------------------------------------*/
8307 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8309 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
8310 UBaseType_t uxIndexToClear,
8311 uint32_t ulBitsToClear )
8316 traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
8318 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8320 /* If null is passed in here then it is the calling task that is having
8321 * its notification state cleared. */
8322 pxTCB = prvGetTCBFromHandle( xTask );
8324 taskENTER_CRITICAL();
8326 /* Return the notification as it was before the bits were cleared,
8327 * then clear the bit mask. */
8328 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
8329 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
8331 taskEXIT_CRITICAL();
8333 traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
8338 #endif /* configUSE_TASK_NOTIFICATIONS */
8339 /*-----------------------------------------------------------*/
8341 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8343 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
8347 traceENTER_ulTaskGetRunTimeCounter( xTask );
8349 pxTCB = prvGetTCBFromHandle( xTask );
8351 traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
8353 return pxTCB->ulRunTimeCounter;
8356 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8357 /*-----------------------------------------------------------*/
8359 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8361 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
8364 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8366 traceENTER_ulTaskGetRunTimePercent( xTask );
8368 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
8370 /* For percentage calculations. */
8371 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8373 /* Avoid divide by zero errors. */
8374 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8376 pxTCB = prvGetTCBFromHandle( xTask );
8377 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
8384 traceRETURN_ulTaskGetRunTimePercent( ulReturn );
8389 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8390 /*-----------------------------------------------------------*/
8392 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8394 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
8396 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
8399 traceENTER_ulTaskGetIdleRunTimeCounter();
8401 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8403 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8406 traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
8411 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8412 /*-----------------------------------------------------------*/
8414 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8416 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
8418 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8419 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
8422 traceENTER_ulTaskGetIdleRunTimePercent();
8424 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
8426 /* For percentage calculations. */
8427 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8429 /* Avoid divide by zero errors. */
8430 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8432 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8434 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8437 ulReturn = ulRunTimeCounter / ulTotalTime;
8444 traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
8449 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8450 /*-----------------------------------------------------------*/
8452 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
8453 const BaseType_t xCanBlockIndefinitely )
8455 TickType_t xTimeToWake;
8456 const TickType_t xConstTickCount = xTickCount;
8457 List_t * const pxDelayedList = pxDelayedTaskList;
8458 List_t * const pxOverflowDelayedList = pxOverflowDelayedTaskList;
8460 #if ( INCLUDE_xTaskAbortDelay == 1 )
8462 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
8463 * reset to pdFALSE so it can be detected as having been set to pdTRUE
8464 * when the task leaves the Blocked state. */
8465 pxCurrentTCB->ucDelayAborted = pdFALSE;
8469 /* Remove the task from the ready list before adding it to the blocked list
8470 * as the same list item is used for both lists. */
8471 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
8473 /* The current task must be in a ready list, so there is no need to
8474 * check, and the port reset macro can be called directly. */
8475 portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority ); /*lint !e931 pxCurrentTCB cannot change as it is the calling task. pxCurrentTCB->uxPriority and uxTopReadyPriority cannot change as called with scheduler suspended or in a critical section. */
8479 mtCOVERAGE_TEST_MARKER();
8482 #if ( INCLUDE_vTaskSuspend == 1 )
8484 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
8486 /* Add the task to the suspended task list instead of a delayed task
8487 * list to ensure it is not woken by a timing event. It will block
8489 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
8493 /* Calculate the time at which the task should be woken if the event
8494 * does not occur. This may overflow but this doesn't matter, the
8495 * kernel will manage it correctly. */
8496 xTimeToWake = xConstTickCount + xTicksToWait;
8498 /* The list item will be inserted in wake time order. */
8499 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8501 if( xTimeToWake < xConstTickCount )
8503 /* Wake time has overflowed. Place this item in the overflow
8505 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8506 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8510 /* The wake time has not overflowed, so the current block list
8512 traceMOVED_TASK_TO_DELAYED_LIST();
8513 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8515 /* If the task entering the blocked state was placed at the
8516 * head of the list of blocked tasks then xNextTaskUnblockTime
8517 * needs to be updated too. */
8518 if( xTimeToWake < xNextTaskUnblockTime )
8520 xNextTaskUnblockTime = xTimeToWake;
8524 mtCOVERAGE_TEST_MARKER();
8529 #else /* INCLUDE_vTaskSuspend */
8531 /* Calculate the time at which the task should be woken if the event
8532 * does not occur. This may overflow but this doesn't matter, the kernel
8533 * will manage it correctly. */
8534 xTimeToWake = xConstTickCount + xTicksToWait;
8536 /* The list item will be inserted in wake time order. */
8537 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8539 if( xTimeToWake < xConstTickCount )
8541 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8542 /* Wake time has overflowed. Place this item in the overflow list. */
8543 vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
8547 traceMOVED_TASK_TO_DELAYED_LIST();
8548 /* The wake time has not overflowed, so the current block list is used. */
8549 vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
8551 /* If the task entering the blocked state was placed at the head of the
8552 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
8554 if( xTimeToWake < xNextTaskUnblockTime )
8556 xNextTaskUnblockTime = xTimeToWake;
8560 mtCOVERAGE_TEST_MARKER();
8564 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
8565 ( void ) xCanBlockIndefinitely;
8567 #endif /* INCLUDE_vTaskSuspend */
8569 /*-----------------------------------------------------------*/
8571 #if ( portUSING_MPU_WRAPPERS == 1 )
8573 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
8577 traceENTER_xTaskGetMPUSettings( xTask );
8579 pxTCB = prvGetTCBFromHandle( xTask );
8581 traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
8583 return &( pxTCB->xMPUSettings );
8586 #endif /* portUSING_MPU_WRAPPERS */
8587 /*-----------------------------------------------------------*/
8589 /* Code below here allows additional code to be inserted into this source file,
8590 * especially where access to file scope functions and data is needed (for example
8591 * when performing module tests). */
8593 #ifdef FREERTOS_MODULE_TEST
8594 #include "tasks_test_access_functions.h"
8598 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
8600 #include "freertos_tasks_c_additions.h"
8602 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8603 static void freertos_tasks_c_additions_init( void )
8605 FREERTOS_TASKS_C_ADDITIONS_INIT();
8609 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8610 /*-----------------------------------------------------------*/
8612 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8615 * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8616 * to provide the memory that is used by the Idle task. It is used when
8617 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8618 * it's own implementation of vApplicationGetIdleTaskMemory by setting
8619 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8621 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8622 StackType_t ** ppxIdleTaskStackBuffer,
8623 uint32_t * pulIdleTaskStackSize )
8625 static StaticTask_t xIdleTaskTCB;
8626 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8628 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8629 *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8630 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8633 #if ( configNUMBER_OF_CORES > 1 )
8635 void vApplicationGetPassiveIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8636 StackType_t ** ppxIdleTaskStackBuffer,
8637 uint32_t * pulIdleTaskStackSize,
8638 BaseType_t xPassiveIdleTaskIndex )
8640 static StaticTask_t xIdleTaskTCBs[ configNUMBER_OF_CORES - 1 ];
8641 static StackType_t uxIdleTaskStacks[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
8643 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCBs[ xPassiveIdleTaskIndex ] );
8644 *ppxIdleTaskStackBuffer = &( uxIdleTaskStacks[ xPassiveIdleTaskIndex ][ 0 ] );
8645 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8648 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
8650 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8651 /*-----------------------------------------------------------*/
8653 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8656 * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8657 * to provide the memory that is used by the Timer service task. It is used when
8658 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8659 * it's own implementation of vApplicationGetTimerTaskMemory by setting
8660 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8662 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8663 StackType_t ** ppxTimerTaskStackBuffer,
8664 uint32_t * pulTimerTaskStackSize )
8666 static StaticTask_t xTimerTaskTCB;
8667 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8669 *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8670 *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8671 *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8674 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8675 /*-----------------------------------------------------------*/