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 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
441 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
442 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
443 /* coverity[misra_c_2012_rule_8_4_violation] */
444 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ];
445 #define pxCurrentTCB xTaskGetCurrentTaskHandle()
448 /* Lists for ready and blocked tasks. --------------------
449 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
450 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
451 * the static qualifier. */
452 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
453 PRIVILEGED_DATA static List_t xDelayedTaskList1; /**< Delayed tasks. */
454 PRIVILEGED_DATA static List_t xDelayedTaskList2; /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
455 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /**< Points to the delayed task list currently being used. */
456 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. */
457 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. */
459 #if ( INCLUDE_vTaskDelete == 1 )
461 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
462 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
466 #if ( INCLUDE_vTaskSuspend == 1 )
468 PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
472 /* Global POSIX errno. Its value is changed upon context switching to match
473 * the errno of the currently running task. */
474 #if ( configUSE_POSIX_ERRNO == 1 )
475 int FreeRTOS_errno = 0;
478 /* Other file private variables. --------------------------------*/
479 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
480 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
481 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
482 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
483 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
484 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
485 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
486 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
487 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
488 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. */
490 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
491 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
492 * to determine the number of priority lists to read back from the remote target. */
493 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
495 /* Context switches are held pending while the scheduler is suspended. Also,
496 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
497 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
498 * If an interrupt needs to unblock a task while the scheduler is suspended then it
499 * moves the task's event list item into the xPendingReadyList, ready for the
500 * kernel to move the task from the pending ready list into the real ready list
501 * when the scheduler is unsuspended. The pending ready list itself can only be
502 * accessed from a critical section.
504 * Updates to uxSchedulerSuspended must be protected by both the task lock and the ISR lock
505 * and must not be done from an ISR. Reads must be protected by either lock and may be done
506 * from either an ISR or a task. */
507 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
509 #if ( configGENERATE_RUN_TIME_STATS == 1 )
511 /* Do not move these variables to function scope as doing so prevents the
512 * code working with debuggers that need to remove the static qualifier. */
513 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. */
514 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. */
520 /*-----------------------------------------------------------*/
522 /* File private functions. --------------------------------*/
525 * Creates the idle tasks during scheduler start.
527 static BaseType_t prvCreateIdleTasks( void );
529 #if ( configNUMBER_OF_CORES > 1 )
532 * Checks to see if another task moved the current task out of the ready
533 * list while it was waiting to enter a critical section and yields, if so.
535 static void prvCheckForRunStateChange( void );
536 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
538 #if ( configNUMBER_OF_CORES > 1 )
541 * Yields a core, or cores if multiple priorities are not allowed to run
542 * simultaneously, to allow the task pxTCB to run.
544 static void prvYieldForTask( const TCB_t * pxTCB );
545 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
547 #if ( configNUMBER_OF_CORES > 1 )
550 * Selects the highest priority available task for the given core.
552 static void prvSelectHighestPriorityTask( BaseType_t xCoreID );
553 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
556 * Utility task that simply returns pdTRUE if the task referenced by xTask is
557 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
558 * is in any other state.
560 #if ( INCLUDE_vTaskSuspend == 1 )
562 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
564 #endif /* INCLUDE_vTaskSuspend */
567 * Utility to ready all the lists used by the scheduler. This is called
568 * automatically upon the creation of the first task.
570 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
573 * The idle task, which as all tasks is implemented as a never ending loop.
574 * The idle task is automatically created and added to the ready lists upon
575 * creation of the first user task.
577 * In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks are also
578 * created to ensure that each core has an idle task to run when no other
579 * task is available to run.
581 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
582 * language extensions. The equivalent prototype for these functions are:
584 * void prvIdleTask( void *pvParameters );
585 * void prvPassiveIdleTask( void *pvParameters );
588 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
589 #if ( configNUMBER_OF_CORES > 1 )
590 static portTASK_FUNCTION_PROTO( prvPassiveIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
594 * Utility to free all memory allocated by the scheduler to hold a TCB,
595 * including the stack pointed to by the TCB.
597 * This does not free memory allocated by the task itself (i.e. memory
598 * allocated by calls to pvPortMalloc from within the tasks application code).
600 #if ( INCLUDE_vTaskDelete == 1 )
602 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
607 * Used only by the idle task. This checks to see if anything has been placed
608 * in the list of tasks waiting to be deleted. If so the task is cleaned up
609 * and its TCB deleted.
611 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
614 * The currently executing task is entering the Blocked state. Add the task to
615 * either the current or the overflow delayed task list.
617 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
618 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
621 * Fills an TaskStatus_t structure with information on each task that is
622 * referenced from the pxList list (which may be a ready list, a delayed list,
623 * a suspended list, etc.).
625 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
626 * NORMAL APPLICATION CODE.
628 #if ( configUSE_TRACE_FACILITY == 1 )
630 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
632 eTaskState eState ) PRIVILEGED_FUNCTION;
637 * Searches pxList for a task with name pcNameToQuery - returning a handle to
638 * the task if it is found, or NULL if the task is not found.
640 #if ( INCLUDE_xTaskGetHandle == 1 )
642 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
643 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
648 * When a task is created, the stack of the task is filled with a known value.
649 * This function determines the 'high water mark' of the task stack by
650 * determining how much of the stack remains at the original preset value.
652 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
654 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
659 * Return the amount of time, in ticks, that will pass before the kernel will
660 * next move a task from the Blocked state to the Running state.
662 * This conditional compilation should use inequality to 0, not equality to 1.
663 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
664 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
665 * set to a value other than 1.
667 #if ( configUSE_TICKLESS_IDLE != 0 )
669 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
674 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
675 * will exit the Blocked state.
677 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
679 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
682 * Helper function used to pad task names with spaces when printing out
683 * human readable tables of task information.
685 static char * prvWriteNameToBuffer( char * pcBuffer,
686 const char * pcTaskName ) PRIVILEGED_FUNCTION;
691 * Called after a Task_t structure has been allocated either statically or
692 * dynamically to fill in the structure's members.
694 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
695 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
696 const uint32_t ulStackDepth,
697 void * const pvParameters,
698 UBaseType_t uxPriority,
699 TaskHandle_t * const pxCreatedTask,
701 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
704 * Called after a new task has been created and initialised to place the task
705 * under the control of the scheduler.
707 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
710 * Create a task with static buffer for both TCB and stack. Returns a handle to
711 * the task if it is created successfully. Otherwise, returns NULL.
713 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
714 static TCB_t * prvCreateStaticTask( TaskFunction_t pxTaskCode,
715 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
716 const uint32_t ulStackDepth,
717 void * const pvParameters,
718 UBaseType_t uxPriority,
719 StackType_t * const puxStackBuffer,
720 StaticTask_t * const pxTaskBuffer,
721 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
722 #endif /* #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
725 * Create a restricted task with static buffer for both TCB and stack. Returns
726 * a handle to the task if it is created successfully. Otherwise, returns NULL.
728 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
729 static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
730 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
731 #endif /* #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
734 * Create a restricted task with static buffer for task stack and allocated buffer
735 * for TCB. Returns a handle to the task if it is created successfully. Otherwise,
738 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
739 static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
740 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
741 #endif /* #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
744 * Create a task with allocated buffer for both TCB and stack. Returns a handle to
745 * the task if it is created successfully. Otherwise, returns NULL.
747 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
748 static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
749 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
750 const configSTACK_DEPTH_TYPE usStackDepth,
751 void * const pvParameters,
752 UBaseType_t uxPriority,
753 TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
754 #endif /* #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) */
757 * freertos_tasks_c_additions_init() should only be called if the user definable
758 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
759 * called by the function.
761 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
763 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
767 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
768 extern void vApplicationPassiveIdleHook( void );
769 #endif /* #if ( configUSE_PASSIVE_IDLE_HOOK == 1 ) */
771 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
774 * Convert the snprintf return value to the number of characters
775 * written. The following are the possible cases:
777 * 1. The buffer supplied to snprintf is large enough to hold the
778 * generated string. The return value in this case is the number
779 * of characters actually written, not counting the terminating
781 * 2. The buffer supplied to snprintf is NOT large enough to hold
782 * the generated string. The return value in this case is the
783 * number of characters that would have been written if the
784 * buffer had been sufficiently large, not counting the
785 * terminating null character.
786 * 3. Encoding error. The return value in this case is a negative
789 * From 1 and 2 above ==> Only when the return value is non-negative
790 * and less than the supplied buffer length, the string has been
791 * completely written.
793 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
796 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
797 /*-----------------------------------------------------------*/
799 #if ( configNUMBER_OF_CORES > 1 )
800 static void prvCheckForRunStateChange( void )
802 UBaseType_t uxPrevCriticalNesting;
803 const TCB_t * pxThisTCB;
805 /* This must only be called from within a task. */
806 portASSERT_IF_IN_ISR();
808 /* This function is always called with interrupts disabled
809 * so this is safe. */
810 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
812 while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD )
814 /* We are only here if we just entered a critical section
815 * or if we just suspended the scheduler, and another task
816 * has requested that we yield.
818 * This is slightly complicated since we need to save and restore
819 * the suspension and critical nesting counts, as well as release
820 * and reacquire the correct locks. And then, do it all over again
821 * if our state changed again during the reacquisition. */
822 uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
824 if( uxPrevCriticalNesting > 0U )
826 portSET_CRITICAL_NESTING_COUNT( 0U );
827 portRELEASE_ISR_LOCK();
831 /* The scheduler is suspended. uxSchedulerSuspended is updated
832 * only when the task is not requested to yield. */
833 mtCOVERAGE_TEST_MARKER();
836 portRELEASE_TASK_LOCK();
837 portMEMORY_BARRIER();
838 configASSERT( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD );
840 portENABLE_INTERRUPTS();
842 /* Enabling interrupts should cause this core to immediately
843 * service the pending interrupt and yield. If the run state is still
844 * yielding here then that is a problem. */
845 configASSERT( pxThisTCB->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD );
847 portDISABLE_INTERRUPTS();
851 portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
853 if( uxPrevCriticalNesting == 0U )
855 portRELEASE_ISR_LOCK();
859 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
861 /*-----------------------------------------------------------*/
863 #if ( configNUMBER_OF_CORES > 1 )
864 static void prvYieldForTask( const TCB_t * pxTCB )
866 BaseType_t xLowestPriorityToPreempt;
867 BaseType_t xCurrentCoreTaskPriority;
868 BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
871 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
872 BaseType_t xYieldCount = 0;
873 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
875 /* This must be called from a critical section. */
876 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
878 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
880 /* No task should yield for this one if it is a lower priority
881 * than priority level of currently ready tasks. */
882 if( pxTCB->uxPriority >= uxTopReadyPriority )
884 /* Yield is not required for a task which is already running. */
885 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
888 xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
890 /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
891 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
892 --xLowestPriorityToPreempt;
894 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
896 xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
898 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
899 if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
901 xCurrentCoreTaskPriority = xCurrentCoreTaskPriority - 1;
904 if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
906 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
907 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
910 if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
912 #if ( configUSE_CORE_AFFINITY == 1 )
913 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
916 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
917 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
920 xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
921 xLowestPriorityCore = xCoreID;
927 mtCOVERAGE_TEST_MARKER();
931 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
933 /* Yield all currently running non-idle tasks with a priority lower than
934 * the task that needs to run. */
935 if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
936 ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
938 prvYieldCore( xCoreID );
943 mtCOVERAGE_TEST_MARKER();
946 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
950 mtCOVERAGE_TEST_MARKER();
954 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
955 if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
956 #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
957 if( xLowestPriorityCore >= 0 )
958 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
960 prvYieldCore( xLowestPriorityCore );
963 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
964 /* Verify that the calling core always yields to higher priority tasks. */
965 if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) &&
966 ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
968 configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) ||
969 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) );
974 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
975 /*-----------------------------------------------------------*/
977 #if ( configNUMBER_OF_CORES > 1 )
978 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
980 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
981 BaseType_t xTaskScheduled = pdFALSE;
982 BaseType_t xDecrementTopPriority = pdTRUE;
984 #if ( configUSE_CORE_AFFINITY == 1 )
985 const TCB_t * pxPreviousTCB = NULL;
987 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
988 BaseType_t xPriorityDropped = pdFALSE;
991 /* This function should be called when scheduler is running. */
992 configASSERT( xSchedulerRunning == pdTRUE );
994 /* A new task is created and a running task with the same priority yields
995 * itself to run the new task. When a running task yields itself, it is still
996 * in the ready list. This running task will be selected before the new task
997 * since the new task is always added to the end of the ready list.
998 * The other problem is that the running task still in the same position of
999 * the ready list when it yields itself. It is possible that it will be selected
1000 * earlier then other tasks which waits longer than this task.
1002 * To fix these problems, the running task should be put to the end of the
1003 * ready list before searching for the ready task in the ready list. */
1004 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
1005 &pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
1007 ( void ) uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
1008 vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
1009 &pxCurrentTCBs[ xCoreID ]->xStateListItem );
1012 while( xTaskScheduled == pdFALSE )
1014 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1016 if( uxCurrentPriority < uxTopReadyPriority )
1018 /* We can't schedule any tasks, other than idle, that have a
1019 * priority lower than the priority of a task currently running
1020 * on another core. */
1021 uxCurrentPriority = tskIDLE_PRIORITY;
1026 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
1028 const List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
1029 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxReadyList );
1030 ListItem_t * pxIterator;
1032 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
1033 * must not be decremented any further. */
1034 xDecrementTopPriority = pdFALSE;
1036 for( pxIterator = listGET_HEAD_ENTRY( pxReadyList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
1038 TCB_t * pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
1040 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1042 /* When falling back to the idle priority because only one priority
1043 * level is allowed to run at a time, we should ONLY schedule the true
1044 * idle tasks, not user tasks at the idle priority. */
1045 if( uxCurrentPriority < uxTopReadyPriority )
1047 if( ( pxTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U )
1053 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1055 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
1057 #if ( configUSE_CORE_AFFINITY == 1 )
1058 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1061 /* If the task is not being executed by any core swap it in. */
1062 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
1063 #if ( configUSE_CORE_AFFINITY == 1 )
1064 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
1066 pxTCB->xTaskRunState = xCoreID;
1067 pxCurrentTCBs[ xCoreID ] = pxTCB;
1068 xTaskScheduled = pdTRUE;
1071 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
1073 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) );
1075 #if ( configUSE_CORE_AFFINITY == 1 )
1076 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1079 /* The task is already running on this core, mark it as scheduled. */
1080 pxTCB->xTaskRunState = xCoreID;
1081 xTaskScheduled = pdTRUE;
1086 /* This task is running on the core other than xCoreID. */
1087 mtCOVERAGE_TEST_MARKER();
1090 if( xTaskScheduled != pdFALSE )
1092 /* A task has been selected to run on this core. */
1099 if( xDecrementTopPriority != pdFALSE )
1101 uxTopReadyPriority--;
1102 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1104 xPriorityDropped = pdTRUE;
1110 /* There are configNUMBER_OF_CORES Idle tasks created when scheduler started.
1111 * The scheduler should be able to select a task to run when uxCurrentPriority
1112 * is tskIDLE_PRIORITY. uxCurrentPriority is never decreased to value blow
1113 * tskIDLE_PRIORITY. */
1114 if( uxCurrentPriority > tskIDLE_PRIORITY )
1116 uxCurrentPriority--;
1120 /* This function is called when idle task is not created. Break the
1121 * loop to prevent uxCurrentPriority overrun. */
1126 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1128 if( xTaskScheduled == pdTRUE )
1130 if( xPriorityDropped != pdFALSE )
1132 /* There may be several ready tasks that were being prevented from running because there was
1133 * a higher priority task running. Now that the last of the higher priority tasks is no longer
1134 * running, make sure all the other idle tasks yield. */
1137 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUMBER_OF_CORES; x++ )
1139 if( ( pxCurrentTCBs[ x ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1147 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1149 #if ( configUSE_CORE_AFFINITY == 1 )
1151 if( xTaskScheduled == pdTRUE )
1153 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
1155 /* A ready task was just evicted from this core. See if it can be
1156 * scheduled on any other core. */
1157 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
1158 BaseType_t xLowestPriority = ( BaseType_t ) pxPreviousTCB->uxPriority;
1159 BaseType_t xLowestPriorityCore = -1;
1162 if( ( pxPreviousTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1164 xLowestPriority = xLowestPriority - 1;
1167 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1169 /* pxPreviousTCB was removed from this core and this core is not excluded
1170 * from it's core affinity mask.
1172 * pxPreviousTCB is preempted by the new higher priority task
1173 * pxCurrentTCBs[ xCoreID ]. When searching a new core for pxPreviousTCB,
1174 * we do not need to look at the cores on which pxCurrentTCBs[ xCoreID ]
1175 * is allowed to run. The reason is - when more than one cores are
1176 * eligible for an incoming task, we preempt the core with the minimum
1177 * priority task. Because this core (i.e. xCoreID) was preempted for
1178 * pxCurrentTCBs[ xCoreID ], this means that all the others cores
1179 * where pxCurrentTCBs[ xCoreID ] can run, are running tasks with priority
1180 * no lower than pxPreviousTCB's priority. Therefore, the only cores where
1181 * which can be preempted for pxPreviousTCB are the ones where
1182 * pxCurrentTCBs[ xCoreID ] is not allowed to run (and obviously,
1183 * pxPreviousTCB is allowed to run).
1185 * This is an optimization which reduces the number of cores needed to be
1186 * searched for pxPreviousTCB to run. */
1187 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1191 /* pxPreviousTCB's core affinity mask is changed and it is no longer
1192 * allowed to run on this core. Searching all the cores in pxPreviousTCB's
1193 * new core affinity mask to find a core on which it can run. */
1196 uxCoreMap &= ( ( 1U << configNUMBER_OF_CORES ) - 1U );
1198 for( x = ( ( BaseType_t ) configNUMBER_OF_CORES - 1 ); x >= ( BaseType_t ) 0; x-- )
1200 UBaseType_t uxCore = ( UBaseType_t ) x;
1201 BaseType_t xTaskPriority;
1203 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << uxCore ) ) != 0U )
1205 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority;
1207 if( ( pxCurrentTCBs[ uxCore ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1209 xTaskPriority = xTaskPriority - ( BaseType_t ) 1;
1212 uxCoreMap &= ~( ( UBaseType_t ) 1U << uxCore );
1214 if( ( xTaskPriority < xLowestPriority ) &&
1215 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ] ) != pdFALSE ) &&
1216 ( xYieldPendings[ uxCore ] == pdFALSE ) )
1218 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1219 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1222 xLowestPriority = xTaskPriority;
1223 xLowestPriorityCore = ( BaseType_t ) uxCore;
1229 if( xLowestPriorityCore >= 0 )
1231 prvYieldCore( xLowestPriorityCore );
1236 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) */
1239 #endif /* ( configNUMBER_OF_CORES > 1 ) */
1241 /*-----------------------------------------------------------*/
1243 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1245 static TCB_t * prvCreateStaticTask( TaskFunction_t pxTaskCode,
1246 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1247 const uint32_t ulStackDepth,
1248 void * const pvParameters,
1249 UBaseType_t uxPriority,
1250 StackType_t * const puxStackBuffer,
1251 StaticTask_t * const pxTaskBuffer,
1252 TaskHandle_t * const pxCreatedTask )
1256 configASSERT( puxStackBuffer != NULL );
1257 configASSERT( pxTaskBuffer != NULL );
1259 #if ( configASSERT_DEFINED == 1 )
1261 /* Sanity check that the size of the structure used to declare a
1262 * variable of type StaticTask_t equals the size of the real task
1264 volatile size_t xSize = sizeof( StaticTask_t );
1265 configASSERT( xSize == sizeof( TCB_t ) );
1266 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1268 #endif /* configASSERT_DEFINED */
1270 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1272 /* The memory used for the task's TCB and stack are passed into this
1273 * function - use them. */
1274 /* MISRA Ref 11.3.1 [Misaligned access] */
1275 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
1276 /* coverity[misra_c_2012_rule_11_3_violation] */
1277 pxNewTCB = ( TCB_t * ) pxTaskBuffer;
1278 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1279 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1281 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1283 /* Tasks can be created statically or dynamically, so note this
1284 * task was created statically in case the task is later deleted. */
1285 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1287 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1289 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1298 /*-----------------------------------------------------------*/
1300 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1301 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1302 const uint32_t ulStackDepth,
1303 void * const pvParameters,
1304 UBaseType_t uxPriority,
1305 StackType_t * const puxStackBuffer,
1306 StaticTask_t * const pxTaskBuffer )
1308 TaskHandle_t xReturn = NULL;
1311 traceENTER_xTaskCreateStatic( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer );
1313 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1315 if( pxNewTCB != NULL )
1317 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1319 /* Set the task's affinity before scheduling it. */
1320 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1324 prvAddNewTaskToReadyList( pxNewTCB );
1328 mtCOVERAGE_TEST_MARKER();
1331 traceRETURN_xTaskCreateStatic( xReturn );
1335 /*-----------------------------------------------------------*/
1337 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1338 TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1339 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1340 const uint32_t ulStackDepth,
1341 void * const pvParameters,
1342 UBaseType_t uxPriority,
1343 StackType_t * const puxStackBuffer,
1344 StaticTask_t * const pxTaskBuffer,
1345 UBaseType_t uxCoreAffinityMask )
1347 TaskHandle_t xReturn = NULL;
1350 traceENTER_xTaskCreateStaticAffinitySet( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, uxCoreAffinityMask );
1352 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1354 if( pxNewTCB != NULL )
1356 /* Set the task's affinity before scheduling it. */
1357 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1359 prvAddNewTaskToReadyList( pxNewTCB );
1363 mtCOVERAGE_TEST_MARKER();
1366 traceRETURN_xTaskCreateStaticAffinitySet( xReturn );
1370 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1372 #endif /* SUPPORT_STATIC_ALLOCATION */
1373 /*-----------------------------------------------------------*/
1375 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1376 static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
1377 TaskHandle_t * const pxCreatedTask )
1381 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1382 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1384 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1386 /* Allocate space for the TCB. Where the memory comes from depends
1387 * on the implementation of the port malloc function and whether or
1388 * not static allocation is being used. */
1389 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1390 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1392 /* Store the stack location in the TCB. */
1393 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1395 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1397 /* Tasks can be created statically or dynamically, so note this
1398 * task was created statically in case the task is later deleted. */
1399 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1401 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1403 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1404 pxTaskDefinition->pcName,
1405 ( uint32_t ) pxTaskDefinition->usStackDepth,
1406 pxTaskDefinition->pvParameters,
1407 pxTaskDefinition->uxPriority,
1408 pxCreatedTask, pxNewTCB,
1409 pxTaskDefinition->xRegions );
1418 /*-----------------------------------------------------------*/
1420 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1421 TaskHandle_t * pxCreatedTask )
1426 traceENTER_xTaskCreateRestrictedStatic( pxTaskDefinition, pxCreatedTask );
1428 configASSERT( pxTaskDefinition != NULL );
1430 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1432 if( pxNewTCB != NULL )
1434 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1436 /* Set the task's affinity before scheduling it. */
1437 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1441 prvAddNewTaskToReadyList( pxNewTCB );
1446 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1449 traceRETURN_xTaskCreateRestrictedStatic( xReturn );
1453 /*-----------------------------------------------------------*/
1455 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1456 BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1457 UBaseType_t uxCoreAffinityMask,
1458 TaskHandle_t * pxCreatedTask )
1463 traceENTER_xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1465 configASSERT( pxTaskDefinition != NULL );
1467 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1469 if( pxNewTCB != NULL )
1471 /* Set the task's affinity before scheduling it. */
1472 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1474 prvAddNewTaskToReadyList( pxNewTCB );
1479 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1482 traceRETURN_xTaskCreateRestrictedStaticAffinitySet( xReturn );
1486 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1488 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1489 /*-----------------------------------------------------------*/
1491 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1492 static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
1493 TaskHandle_t * const pxCreatedTask )
1497 configASSERT( pxTaskDefinition->puxStackBuffer );
1499 if( pxTaskDefinition->puxStackBuffer != NULL )
1501 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1503 if( pxNewTCB != NULL )
1505 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1507 /* Store the stack location in the TCB. */
1508 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1510 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1512 /* Tasks can be created statically or dynamically, so note
1513 * this task had a statically allocated stack in case it is
1514 * later deleted. The TCB was allocated dynamically. */
1515 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1517 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1519 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1520 pxTaskDefinition->pcName,
1521 ( uint32_t ) pxTaskDefinition->usStackDepth,
1522 pxTaskDefinition->pvParameters,
1523 pxTaskDefinition->uxPriority,
1524 pxCreatedTask, pxNewTCB,
1525 pxTaskDefinition->xRegions );
1535 /*-----------------------------------------------------------*/
1537 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1538 TaskHandle_t * pxCreatedTask )
1543 traceENTER_xTaskCreateRestricted( pxTaskDefinition, pxCreatedTask );
1545 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1547 if( pxNewTCB != NULL )
1549 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1551 /* Set the task's affinity before scheduling it. */
1552 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1554 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1556 prvAddNewTaskToReadyList( pxNewTCB );
1562 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1565 traceRETURN_xTaskCreateRestricted( xReturn );
1569 /*-----------------------------------------------------------*/
1571 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1572 BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1573 UBaseType_t uxCoreAffinityMask,
1574 TaskHandle_t * pxCreatedTask )
1579 traceENTER_xTaskCreateRestrictedAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1581 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1583 if( pxNewTCB != NULL )
1585 /* Set the task's affinity before scheduling it. */
1586 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1588 prvAddNewTaskToReadyList( pxNewTCB );
1594 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1597 traceRETURN_xTaskCreateRestrictedAffinitySet( xReturn );
1601 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1604 #endif /* portUSING_MPU_WRAPPERS */
1605 /*-----------------------------------------------------------*/
1607 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1608 static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
1609 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1610 const configSTACK_DEPTH_TYPE usStackDepth,
1611 void * const pvParameters,
1612 UBaseType_t uxPriority,
1613 TaskHandle_t * const pxCreatedTask )
1617 /* If the stack grows down then allocate the stack then the TCB so the stack
1618 * does not grow into the TCB. Likewise if the stack grows up then allocate
1619 * the TCB then the stack. */
1620 #if ( portSTACK_GROWTH > 0 )
1622 /* Allocate space for the TCB. Where the memory comes from depends on
1623 * the implementation of the port malloc function and whether or not static
1624 * allocation is being used. */
1625 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1627 if( pxNewTCB != NULL )
1629 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1631 /* Allocate space for the stack used by the task being created.
1632 * The base of the stack memory stored in the TCB so the task can
1633 * be deleted later if required. */
1634 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1636 if( pxNewTCB->pxStack == NULL )
1638 /* Could not allocate the stack. Delete the allocated TCB. */
1639 vPortFree( pxNewTCB );
1644 #else /* portSTACK_GROWTH */
1646 StackType_t * pxStack;
1648 /* Allocate space for the stack used by the task being created. */
1649 pxStack = pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation is the stack. */
1651 if( pxStack != NULL )
1653 /* Allocate space for the TCB. */
1654 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) ); /*lint !e9087 !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack, and the first member of TCB_t is always a pointer to the task's stack. */
1656 if( pxNewTCB != NULL )
1658 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1660 /* Store the stack location in the TCB. */
1661 pxNewTCB->pxStack = pxStack;
1665 /* The stack cannot be used as the TCB was not created. Free
1667 vPortFreeStack( pxStack );
1675 #endif /* portSTACK_GROWTH */
1677 if( pxNewTCB != NULL )
1679 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1681 /* Tasks can be created statically or dynamically, so note this
1682 * task was created dynamically in case it is later deleted. */
1683 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1685 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1687 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1692 /*-----------------------------------------------------------*/
1694 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1695 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1696 const configSTACK_DEPTH_TYPE usStackDepth,
1697 void * const pvParameters,
1698 UBaseType_t uxPriority,
1699 TaskHandle_t * const pxCreatedTask )
1704 traceENTER_xTaskCreate( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask );
1706 pxNewTCB = prvCreateTask( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask );
1708 if( pxNewTCB != NULL )
1710 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1712 /* Set the task's affinity before scheduling it. */
1713 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1717 prvAddNewTaskToReadyList( pxNewTCB );
1722 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1725 traceRETURN_xTaskCreate( xReturn );
1729 /*-----------------------------------------------------------*/
1731 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1732 BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1733 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1734 const configSTACK_DEPTH_TYPE usStackDepth,
1735 void * const pvParameters,
1736 UBaseType_t uxPriority,
1737 UBaseType_t uxCoreAffinityMask,
1738 TaskHandle_t * const pxCreatedTask )
1743 traceENTER_xTaskCreateAffinitySet( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, uxCoreAffinityMask, pxCreatedTask );
1745 pxNewTCB = prvCreateTask( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask );
1747 if( pxNewTCB != NULL )
1749 /* Set the task's affinity before scheduling it. */
1750 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1752 prvAddNewTaskToReadyList( pxNewTCB );
1757 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1760 traceRETURN_xTaskCreateAffinitySet( xReturn );
1764 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1766 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1767 /*-----------------------------------------------------------*/
1769 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1770 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1771 const uint32_t ulStackDepth,
1772 void * const pvParameters,
1773 UBaseType_t uxPriority,
1774 TaskHandle_t * const pxCreatedTask,
1776 const MemoryRegion_t * const xRegions )
1778 StackType_t * pxTopOfStack;
1781 #if ( portUSING_MPU_WRAPPERS == 1 )
1782 /* Should the task be created in privileged mode? */
1783 BaseType_t xRunPrivileged;
1785 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1787 xRunPrivileged = pdTRUE;
1791 xRunPrivileged = pdFALSE;
1793 uxPriority &= ~portPRIVILEGE_BIT;
1794 #endif /* portUSING_MPU_WRAPPERS == 1 */
1796 /* Avoid dependency on memset() if it is not required. */
1797 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1799 /* Fill the stack with a known value to assist debugging. */
1800 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1802 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1804 /* Calculate the top of stack address. This depends on whether the stack
1805 * grows from high memory to low (as per the 80x86) or vice versa.
1806 * portSTACK_GROWTH is used to make the result positive or negative as required
1808 #if ( portSTACK_GROWTH < 0 )
1810 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1811 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(). */
1813 /* Check the alignment of the calculated top of stack is correct. */
1814 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1816 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1818 /* Also record the stack's high address, which may assist
1820 pxNewTCB->pxEndOfStack = pxTopOfStack;
1822 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1824 #else /* portSTACK_GROWTH */
1826 pxTopOfStack = pxNewTCB->pxStack;
1827 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(). */
1829 /* Check the alignment of the calculated top of stack is correct. */
1830 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1832 /* The other extreme of the stack space is required if stack checking is
1834 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1836 #endif /* portSTACK_GROWTH */
1838 /* Store the task name in the TCB. */
1839 if( pcName != NULL )
1841 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1843 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1845 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1846 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1847 * string is not accessible (extremely unlikely). */
1848 if( pcName[ x ] == ( char ) 0x00 )
1854 mtCOVERAGE_TEST_MARKER();
1858 /* Ensure the name string is terminated in the case that the string length
1859 * was greater or equal to configMAX_TASK_NAME_LEN. */
1860 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1U ] = '\0';
1864 mtCOVERAGE_TEST_MARKER();
1867 /* This is used as an array index so must ensure it's not too large. */
1868 configASSERT( uxPriority < configMAX_PRIORITIES );
1870 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1872 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1876 mtCOVERAGE_TEST_MARKER();
1879 pxNewTCB->uxPriority = uxPriority;
1880 #if ( configUSE_MUTEXES == 1 )
1882 pxNewTCB->uxBasePriority = uxPriority;
1884 #endif /* configUSE_MUTEXES */
1886 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1887 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1889 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1890 * back to the containing TCB from a generic item in a list. */
1891 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1893 /* Event lists are always in priority order. */
1894 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. */
1895 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1897 #if ( portUSING_MPU_WRAPPERS == 1 )
1899 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1903 /* Avoid compiler warning about unreferenced parameter. */
1908 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
1910 /* Allocate and initialize memory for the task's TLS Block. */
1911 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
1915 /* Initialize the TCB stack to look as if the task was already running,
1916 * but had been interrupted by the scheduler. The return address is set
1917 * to the start of the task function. Once the stack has been initialised
1918 * the top of stack variable is updated. */
1919 #if ( portUSING_MPU_WRAPPERS == 1 )
1921 /* If the port has capability to detect stack overflow,
1922 * pass the stack end address to the stack initialization
1923 * function as well. */
1924 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1926 #if ( portSTACK_GROWTH < 0 )
1928 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1930 #else /* portSTACK_GROWTH */
1932 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1934 #endif /* portSTACK_GROWTH */
1936 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1938 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1940 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1942 #else /* portUSING_MPU_WRAPPERS */
1944 /* If the port has capability to detect stack overflow,
1945 * pass the stack end address to the stack initialization
1946 * function as well. */
1947 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1949 #if ( portSTACK_GROWTH < 0 )
1951 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1953 #else /* portSTACK_GROWTH */
1955 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1957 #endif /* portSTACK_GROWTH */
1959 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1961 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1963 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1965 #endif /* portUSING_MPU_WRAPPERS */
1967 /* Initialize task state and task attributes. */
1968 #if ( configNUMBER_OF_CORES > 1 )
1970 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1972 /* Is this an idle task? */
1973 if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvPassiveIdleTask ) )
1975 pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
1978 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
1980 if( pxCreatedTask != NULL )
1982 /* Pass the handle out in an anonymous way. The handle can be used to
1983 * change the created task's priority, delete the created task, etc.*/
1984 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1988 mtCOVERAGE_TEST_MARKER();
1991 /*-----------------------------------------------------------*/
1993 #if ( configNUMBER_OF_CORES == 1 )
1995 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1997 /* Ensure interrupts don't access the task lists while the lists are being
1999 taskENTER_CRITICAL();
2001 uxCurrentNumberOfTasks++;
2003 if( pxCurrentTCB == NULL )
2005 /* There are no other tasks, or all the other tasks are in
2006 * the suspended state - make this the current task. */
2007 pxCurrentTCB = pxNewTCB;
2009 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2011 /* This is the first task to be created so do the preliminary
2012 * initialisation required. We will not recover if this call
2013 * fails, but we will report the failure. */
2014 prvInitialiseTaskLists();
2018 mtCOVERAGE_TEST_MARKER();
2023 /* If the scheduler is not already running, make this task the
2024 * current task if it is the highest priority task to be created
2026 if( xSchedulerRunning == pdFALSE )
2028 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
2030 pxCurrentTCB = pxNewTCB;
2034 mtCOVERAGE_TEST_MARKER();
2039 mtCOVERAGE_TEST_MARKER();
2045 #if ( configUSE_TRACE_FACILITY == 1 )
2047 /* Add a counter into the TCB for tracing only. */
2048 pxNewTCB->uxTCBNumber = uxTaskNumber;
2050 #endif /* configUSE_TRACE_FACILITY */
2051 traceTASK_CREATE( pxNewTCB );
2053 prvAddTaskToReadyList( pxNewTCB );
2055 portSETUP_TCB( pxNewTCB );
2057 taskEXIT_CRITICAL();
2059 if( xSchedulerRunning != pdFALSE )
2061 /* If the created task is of a higher priority than the current task
2062 * then it should run now. */
2063 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2067 mtCOVERAGE_TEST_MARKER();
2071 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2073 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
2075 /* Ensure interrupts don't access the task lists while the lists are being
2077 taskENTER_CRITICAL();
2079 uxCurrentNumberOfTasks++;
2081 if( xSchedulerRunning == pdFALSE )
2083 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2085 /* This is the first task to be created so do the preliminary
2086 * initialisation required. We will not recover if this call
2087 * fails, but we will report the failure. */
2088 prvInitialiseTaskLists();
2092 mtCOVERAGE_TEST_MARKER();
2095 if( ( pxNewTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
2099 /* Check if a core is free. */
2100 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
2102 if( pxCurrentTCBs[ xCoreID ] == NULL )
2104 pxNewTCB->xTaskRunState = xCoreID;
2105 pxCurrentTCBs[ xCoreID ] = pxNewTCB;
2110 mtCOVERAGE_TEST_MARKER();
2116 mtCOVERAGE_TEST_MARKER();
2122 #if ( configUSE_TRACE_FACILITY == 1 )
2124 /* Add a counter into the TCB for tracing only. */
2125 pxNewTCB->uxTCBNumber = uxTaskNumber;
2127 #endif /* configUSE_TRACE_FACILITY */
2128 traceTASK_CREATE( pxNewTCB );
2130 prvAddTaskToReadyList( pxNewTCB );
2132 portSETUP_TCB( pxNewTCB );
2134 if( xSchedulerRunning != pdFALSE )
2136 /* If the created task is of a higher priority than another
2137 * currently running task and preemption is on then it should
2139 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2143 mtCOVERAGE_TEST_MARKER();
2146 taskEXIT_CRITICAL();
2149 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2150 /*-----------------------------------------------------------*/
2152 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
2154 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
2157 size_t uxCharsWritten;
2159 if( iSnprintfReturnValue < 0 )
2161 /* Encoding error - Return 0 to indicate that nothing
2162 * was written to the buffer. */
2165 else if( iSnprintfReturnValue >= ( int ) n )
2167 /* This is the case when the supplied buffer is not
2168 * large to hold the generated string. Return the
2169 * number of characters actually written without
2170 * counting the terminating NULL character. */
2171 uxCharsWritten = n - 1U;
2175 /* Complete string was written to the buffer. */
2176 uxCharsWritten = ( size_t ) iSnprintfReturnValue;
2179 return uxCharsWritten;
2182 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
2183 /*-----------------------------------------------------------*/
2185 #if ( INCLUDE_vTaskDelete == 1 )
2187 void vTaskDelete( TaskHandle_t xTaskToDelete )
2191 traceENTER_vTaskDelete( xTaskToDelete );
2193 taskENTER_CRITICAL();
2195 /* If null is passed in here then it is the calling task that is
2197 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
2199 /* Remove task from the ready/delayed list. */
2200 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2202 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2206 mtCOVERAGE_TEST_MARKER();
2209 /* Is the task waiting on an event also? */
2210 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2212 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2216 mtCOVERAGE_TEST_MARKER();
2219 /* Increment the uxTaskNumber also so kernel aware debuggers can
2220 * detect that the task lists need re-generating. This is done before
2221 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
2225 /* If the task is running (or yielding), we must add it to the
2226 * termination list so that an idle task can delete it when it is
2227 * no longer running. */
2228 if( taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) != pdFALSE )
2230 /* A running task or a task which is scheduled to yield is being
2231 * deleted. This cannot complete when the task is still running
2232 * on a core, as a context switch to another task is required.
2233 * Place the task in the termination list. The idle task will check
2234 * the termination list and free up any memory allocated by the
2235 * scheduler for the TCB and stack of the deleted task. */
2236 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
2238 /* Increment the ucTasksDeleted variable so the idle task knows
2239 * there is a task that has been deleted and that it should therefore
2240 * check the xTasksWaitingTermination list. */
2241 ++uxDeletedTasksWaitingCleanUp;
2243 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
2244 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
2245 traceTASK_DELETE( pxTCB );
2247 /* The pre-delete hook is primarily for the Windows simulator,
2248 * in which Windows specific clean up operations are performed,
2249 * after which it is not possible to yield away from this task -
2250 * hence xYieldPending is used to latch that a context switch is
2252 #if ( configNUMBER_OF_CORES == 1 )
2253 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
2255 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
2260 --uxCurrentNumberOfTasks;
2261 traceTASK_DELETE( pxTCB );
2263 /* Reset the next expected unblock time in case it referred to
2264 * the task that has just been deleted. */
2265 prvResetNextTaskUnblockTime();
2269 #if ( configNUMBER_OF_CORES == 1 )
2271 taskEXIT_CRITICAL();
2273 /* If the task is not deleting itself, call prvDeleteTCB from outside of
2274 * critical section. If a task deletes itself, prvDeleteTCB is called
2275 * from prvCheckTasksWaitingTermination which is called from Idle task. */
2276 if( pxTCB != pxCurrentTCB )
2278 prvDeleteTCB( pxTCB );
2281 /* Force a reschedule if it is the currently running task that has just
2283 if( xSchedulerRunning != pdFALSE )
2285 if( pxTCB == pxCurrentTCB )
2287 configASSERT( uxSchedulerSuspended == 0 );
2288 portYIELD_WITHIN_API();
2292 mtCOVERAGE_TEST_MARKER();
2296 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2298 /* If a running task is not deleting itself, call prvDeleteTCB. If a running
2299 * task deletes itself, prvDeleteTCB is called from prvCheckTasksWaitingTermination
2300 * which is called from Idle task. */
2301 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
2303 prvDeleteTCB( pxTCB );
2306 /* Force a reschedule if the task that has just been deleted was running. */
2307 if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) )
2309 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2311 configASSERT( uxSchedulerSuspended == 0 );
2312 vTaskYieldWithinAPI();
2316 prvYieldCore( pxTCB->xTaskRunState );
2320 taskEXIT_CRITICAL();
2322 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2324 traceRETURN_vTaskDelete();
2327 #endif /* INCLUDE_vTaskDelete */
2328 /*-----------------------------------------------------------*/
2330 #if ( INCLUDE_xTaskDelayUntil == 1 )
2332 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2333 const TickType_t xTimeIncrement )
2335 TickType_t xTimeToWake;
2336 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2338 traceENTER_xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
2340 configASSERT( pxPreviousWakeTime );
2341 configASSERT( ( xTimeIncrement > 0U ) );
2345 /* Minor optimisation. The tick count cannot change in this
2347 const TickType_t xConstTickCount = xTickCount;
2349 configASSERT( uxSchedulerSuspended == 1U );
2351 /* Generate the tick time at which the task wants to wake. */
2352 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2354 if( xConstTickCount < *pxPreviousWakeTime )
2356 /* The tick count has overflowed since this function was
2357 * lasted called. In this case the only time we should ever
2358 * actually delay is if the wake time has also overflowed,
2359 * and the wake time is greater than the tick time. When this
2360 * is the case it is as if neither time had overflowed. */
2361 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2363 xShouldDelay = pdTRUE;
2367 mtCOVERAGE_TEST_MARKER();
2372 /* The tick time has not overflowed. In this case we will
2373 * delay if either the wake time has overflowed, and/or the
2374 * tick time is less than the wake time. */
2375 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2377 xShouldDelay = pdTRUE;
2381 mtCOVERAGE_TEST_MARKER();
2385 /* Update the wake time ready for the next call. */
2386 *pxPreviousWakeTime = xTimeToWake;
2388 if( xShouldDelay != pdFALSE )
2390 traceTASK_DELAY_UNTIL( xTimeToWake );
2392 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2393 * the time to wake, so subtract the current tick count. */
2394 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2398 mtCOVERAGE_TEST_MARKER();
2401 xAlreadyYielded = xTaskResumeAll();
2403 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2404 * have put ourselves to sleep. */
2405 if( xAlreadyYielded == pdFALSE )
2407 taskYIELD_WITHIN_API();
2411 mtCOVERAGE_TEST_MARKER();
2414 traceRETURN_xTaskDelayUntil( xShouldDelay );
2416 return xShouldDelay;
2419 #endif /* INCLUDE_xTaskDelayUntil */
2420 /*-----------------------------------------------------------*/
2422 #if ( INCLUDE_vTaskDelay == 1 )
2424 void vTaskDelay( const TickType_t xTicksToDelay )
2426 BaseType_t xAlreadyYielded = pdFALSE;
2428 traceENTER_vTaskDelay( xTicksToDelay );
2430 /* A delay time of zero just forces a reschedule. */
2431 if( xTicksToDelay > ( TickType_t ) 0U )
2435 configASSERT( uxSchedulerSuspended == 1U );
2439 /* A task that is removed from the event list while the
2440 * scheduler is suspended will not get placed in the ready
2441 * list or removed from the blocked list until the scheduler
2444 * This task cannot be in an event list as it is the currently
2445 * executing task. */
2446 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2448 xAlreadyYielded = xTaskResumeAll();
2452 mtCOVERAGE_TEST_MARKER();
2455 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2456 * have put ourselves to sleep. */
2457 if( xAlreadyYielded == pdFALSE )
2459 taskYIELD_WITHIN_API();
2463 mtCOVERAGE_TEST_MARKER();
2466 traceRETURN_vTaskDelay();
2469 #endif /* INCLUDE_vTaskDelay */
2470 /*-----------------------------------------------------------*/
2472 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2474 eTaskState eTaskGetState( TaskHandle_t xTask )
2477 List_t const * pxStateList;
2478 List_t const * pxEventList;
2479 List_t const * pxDelayedList;
2480 List_t const * pxOverflowedDelayedList;
2481 const TCB_t * const pxTCB = xTask;
2483 traceENTER_eTaskGetState( xTask );
2485 configASSERT( pxTCB );
2487 #if ( configNUMBER_OF_CORES == 1 )
2488 if( pxTCB == pxCurrentTCB )
2490 /* The task calling this function is querying its own state. */
2496 taskENTER_CRITICAL();
2498 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2499 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2500 pxDelayedList = pxDelayedTaskList;
2501 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2503 taskEXIT_CRITICAL();
2505 if( pxEventList == &xPendingReadyList )
2507 /* The task has been placed on the pending ready list, so its
2508 * state is eReady regardless of what list the task's state list
2509 * item is currently placed on. */
2512 else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2514 /* The task being queried is referenced from one of the Blocked
2519 #if ( INCLUDE_vTaskSuspend == 1 )
2520 else if( pxStateList == &xSuspendedTaskList )
2522 /* The task being queried is referenced from the suspended
2523 * list. Is it genuinely suspended or is it blocked
2525 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2527 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2531 /* The task does not appear on the event list item of
2532 * and of the RTOS objects, but could still be in the
2533 * blocked state if it is waiting on its notification
2534 * rather than waiting on an object. If not, is
2536 eReturn = eSuspended;
2538 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2540 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2547 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2549 eReturn = eSuspended;
2551 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2558 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2560 #if ( INCLUDE_vTaskDelete == 1 )
2561 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2563 /* The task being queried is referenced from the deleted
2564 * tasks list, or it is not referenced from any lists at
2570 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
2572 #if ( configNUMBER_OF_CORES == 1 )
2574 /* If the task is not in any other state, it must be in the
2575 * Ready (including pending ready) state. */
2578 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2580 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2582 /* Is it actively running on a core? */
2587 /* If the task is not in any other state, it must be in the
2588 * Ready (including pending ready) state. */
2592 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2596 traceRETURN_eTaskGetState( eReturn );
2599 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2601 #endif /* INCLUDE_eTaskGetState */
2602 /*-----------------------------------------------------------*/
2604 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2606 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2608 TCB_t const * pxTCB;
2609 UBaseType_t uxReturn;
2611 traceENTER_uxTaskPriorityGet( xTask );
2613 taskENTER_CRITICAL();
2615 /* If null is passed in here then it is the priority of the task
2616 * that called uxTaskPriorityGet() that is being queried. */
2617 pxTCB = prvGetTCBFromHandle( xTask );
2618 uxReturn = pxTCB->uxPriority;
2620 taskEXIT_CRITICAL();
2622 traceRETURN_uxTaskPriorityGet( uxReturn );
2627 #endif /* INCLUDE_uxTaskPriorityGet */
2628 /*-----------------------------------------------------------*/
2630 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2632 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2634 TCB_t const * pxTCB;
2635 UBaseType_t uxReturn;
2636 UBaseType_t uxSavedInterruptStatus;
2638 traceENTER_uxTaskPriorityGetFromISR( xTask );
2640 /* RTOS ports that support interrupt nesting have the concept of a
2641 * maximum system call (or maximum API call) interrupt priority.
2642 * Interrupts that are above the maximum system call priority are keep
2643 * permanently enabled, even when the RTOS kernel is in a critical section,
2644 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2645 * is defined in FreeRTOSConfig.h then
2646 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2647 * failure if a FreeRTOS API function is called from an interrupt that has
2648 * been assigned a priority above the configured maximum system call
2649 * priority. Only FreeRTOS functions that end in FromISR can be called
2650 * from interrupts that have been assigned a priority at or (logically)
2651 * below the maximum system call interrupt priority. FreeRTOS maintains a
2652 * separate interrupt safe API to ensure interrupt entry is as fast and as
2653 * simple as possible. More information (albeit Cortex-M specific) is
2654 * provided on the following link:
2655 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2656 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2658 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
2660 /* If null is passed in here then it is the priority of the calling
2661 * task that is being queried. */
2662 pxTCB = prvGetTCBFromHandle( xTask );
2663 uxReturn = pxTCB->uxPriority;
2665 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2667 traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
2672 #endif /* INCLUDE_uxTaskPriorityGet */
2673 /*-----------------------------------------------------------*/
2675 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2677 UBaseType_t uxTaskBasePriorityGet( const TaskHandle_t xTask )
2679 TCB_t const * pxTCB;
2680 UBaseType_t uxReturn;
2682 traceENTER_uxTaskBasePriorityGet( xTask );
2684 taskENTER_CRITICAL();
2686 /* If null is passed in here then it is the base priority of the task
2687 * that called uxTaskBasePriorityGet() that is being queried. */
2688 pxTCB = prvGetTCBFromHandle( xTask );
2689 uxReturn = pxTCB->uxBasePriority;
2691 taskEXIT_CRITICAL();
2693 traceRETURN_uxTaskBasePriorityGet( uxReturn );
2698 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2699 /*-----------------------------------------------------------*/
2701 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2703 UBaseType_t uxTaskBasePriorityGetFromISR( const TaskHandle_t xTask )
2705 TCB_t const * pxTCB;
2706 UBaseType_t uxReturn;
2707 UBaseType_t uxSavedInterruptStatus;
2709 traceENTER_uxTaskBasePriorityGetFromISR( xTask );
2711 /* RTOS ports that support interrupt nesting have the concept of a
2712 * maximum system call (or maximum API call) interrupt priority.
2713 * Interrupts that are above the maximum system call priority are keep
2714 * permanently enabled, even when the RTOS kernel is in a critical section,
2715 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2716 * is defined in FreeRTOSConfig.h then
2717 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2718 * failure if a FreeRTOS API function is called from an interrupt that has
2719 * been assigned a priority above the configured maximum system call
2720 * priority. Only FreeRTOS functions that end in FromISR can be called
2721 * from interrupts that have been assigned a priority at or (logically)
2722 * below the maximum system call interrupt priority. FreeRTOS maintains a
2723 * separate interrupt safe API to ensure interrupt entry is as fast and as
2724 * simple as possible. More information (albeit Cortex-M specific) is
2725 * provided on the following link:
2726 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2727 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2729 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
2731 /* If null is passed in here then it is the base priority of the calling
2732 * task that is being queried. */
2733 pxTCB = prvGetTCBFromHandle( xTask );
2734 uxReturn = pxTCB->uxBasePriority;
2736 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2738 traceRETURN_uxTaskBasePriorityGetFromISR( uxReturn );
2743 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2744 /*-----------------------------------------------------------*/
2746 #if ( INCLUDE_vTaskPrioritySet == 1 )
2748 void vTaskPrioritySet( TaskHandle_t xTask,
2749 UBaseType_t uxNewPriority )
2752 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2753 BaseType_t xYieldRequired = pdFALSE;
2755 #if ( configNUMBER_OF_CORES > 1 )
2756 BaseType_t xYieldForTask = pdFALSE;
2759 traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
2761 configASSERT( uxNewPriority < configMAX_PRIORITIES );
2763 /* Ensure the new priority is valid. */
2764 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2766 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2770 mtCOVERAGE_TEST_MARKER();
2773 taskENTER_CRITICAL();
2775 /* If null is passed in here then it is the priority of the calling
2776 * task that is being changed. */
2777 pxTCB = prvGetTCBFromHandle( xTask );
2779 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2781 #if ( configUSE_MUTEXES == 1 )
2783 uxCurrentBasePriority = pxTCB->uxBasePriority;
2787 uxCurrentBasePriority = pxTCB->uxPriority;
2791 if( uxCurrentBasePriority != uxNewPriority )
2793 /* The priority change may have readied a task of higher
2794 * priority than a running task. */
2795 if( uxNewPriority > uxCurrentBasePriority )
2797 #if ( configNUMBER_OF_CORES == 1 )
2799 if( pxTCB != pxCurrentTCB )
2801 /* The priority of a task other than the currently
2802 * running task is being raised. Is the priority being
2803 * raised above that of the running task? */
2804 if( uxNewPriority > pxCurrentTCB->uxPriority )
2806 xYieldRequired = pdTRUE;
2810 mtCOVERAGE_TEST_MARKER();
2815 /* The priority of the running task is being raised,
2816 * but the running task must already be the highest
2817 * priority task able to run so no yield is required. */
2820 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2822 /* The priority of a task is being raised so
2823 * perform a yield for this task later. */
2824 xYieldForTask = pdTRUE;
2826 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2828 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2830 /* Setting the priority of a running task down means
2831 * there may now be another task of higher priority that
2832 * is ready to execute. */
2833 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2834 if( pxTCB->xPreemptionDisable == pdFALSE )
2837 xYieldRequired = pdTRUE;
2842 /* Setting the priority of any other task down does not
2843 * require a yield as the running task must be above the
2844 * new priority of the task being modified. */
2847 /* Remember the ready list the task might be referenced from
2848 * before its uxPriority member is changed so the
2849 * taskRESET_READY_PRIORITY() macro can function correctly. */
2850 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2852 #if ( configUSE_MUTEXES == 1 )
2854 /* Only change the priority being used if the task is not
2855 * currently using an inherited priority or the new priority
2856 * is bigger than the inherited priority. */
2857 if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2859 pxTCB->uxPriority = uxNewPriority;
2863 mtCOVERAGE_TEST_MARKER();
2866 /* The base priority gets set whatever. */
2867 pxTCB->uxBasePriority = uxNewPriority;
2869 #else /* if ( configUSE_MUTEXES == 1 ) */
2871 pxTCB->uxPriority = uxNewPriority;
2873 #endif /* if ( configUSE_MUTEXES == 1 ) */
2875 /* Only reset the event list item value if the value is not
2876 * being used for anything else. */
2877 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0UL ) )
2879 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. */
2883 mtCOVERAGE_TEST_MARKER();
2886 /* If the task is in the blocked or suspended list we need do
2887 * nothing more than change its priority variable. However, if
2888 * the task is in a ready list it needs to be removed and placed
2889 * in the list appropriate to its new priority. */
2890 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2892 /* The task is currently in its ready list - remove before
2893 * adding it to its new ready list. As we are in a critical
2894 * section we can do this even if the scheduler is suspended. */
2895 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2897 /* It is known that the task is in its ready list so
2898 * there is no need to check again and the port level
2899 * reset macro can be called directly. */
2900 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2904 mtCOVERAGE_TEST_MARKER();
2907 prvAddTaskToReadyList( pxTCB );
2911 #if ( configNUMBER_OF_CORES == 1 )
2913 mtCOVERAGE_TEST_MARKER();
2917 /* It's possible that xYieldForTask was already set to pdTRUE because
2918 * its priority is being raised. However, since it is not in a ready list
2919 * we don't actually need to yield for it. */
2920 xYieldForTask = pdFALSE;
2925 if( xYieldRequired != pdFALSE )
2927 /* The running task priority is set down. Request the task to yield. */
2928 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2932 #if ( configNUMBER_OF_CORES > 1 )
2933 if( xYieldForTask != pdFALSE )
2935 /* The priority of the task is being raised. If a running
2936 * task has priority lower than this task, it should yield
2938 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2941 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2943 mtCOVERAGE_TEST_MARKER();
2947 /* Remove compiler warning about unused variables when the port
2948 * optimised task selection is not being used. */
2949 ( void ) uxPriorityUsedOnEntry;
2952 taskEXIT_CRITICAL();
2954 traceRETURN_vTaskPrioritySet();
2957 #endif /* INCLUDE_vTaskPrioritySet */
2958 /*-----------------------------------------------------------*/
2960 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2961 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2962 UBaseType_t uxCoreAffinityMask )
2966 UBaseType_t uxPrevCoreAffinityMask;
2968 #if ( configUSE_PREEMPTION == 1 )
2969 UBaseType_t uxPrevNotAllowedCores;
2972 traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
2974 taskENTER_CRITICAL();
2976 pxTCB = prvGetTCBFromHandle( xTask );
2978 uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2979 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2981 if( xSchedulerRunning != pdFALSE )
2983 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2985 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2987 /* If the task can no longer run on the core it was running,
2988 * request the core to yield. */
2989 if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
2991 prvYieldCore( xCoreID );
2996 #if ( configUSE_PREEMPTION == 1 )
2998 /* Calculate the cores on which this task was not allowed to
2999 * run previously. */
3000 uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
3002 /* Does the new core mask enables this task to run on any of the
3003 * previously not allowed cores? If yes, check if this task can be
3004 * scheduled on any of those cores. */
3005 if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
3007 prvYieldForTask( pxTCB );
3010 #else /* #if( configUSE_PREEMPTION == 1 ) */
3012 mtCOVERAGE_TEST_MARKER();
3014 #endif /* #if( configUSE_PREEMPTION == 1 ) */
3018 taskEXIT_CRITICAL();
3020 traceRETURN_vTaskCoreAffinitySet();
3022 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3023 /*-----------------------------------------------------------*/
3025 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
3026 UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
3028 const TCB_t * pxTCB;
3029 UBaseType_t uxCoreAffinityMask;
3031 traceENTER_vTaskCoreAffinityGet( xTask );
3033 taskENTER_CRITICAL();
3035 pxTCB = prvGetTCBFromHandle( xTask );
3036 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
3038 taskEXIT_CRITICAL();
3040 traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
3042 return uxCoreAffinityMask;
3044 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3046 /*-----------------------------------------------------------*/
3048 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3050 void vTaskPreemptionDisable( const TaskHandle_t xTask )
3054 traceENTER_vTaskPreemptionDisable( xTask );
3056 taskENTER_CRITICAL();
3058 pxTCB = prvGetTCBFromHandle( xTask );
3060 pxTCB->xPreemptionDisable = pdTRUE;
3062 taskEXIT_CRITICAL();
3064 traceRETURN_vTaskPreemptionDisable();
3067 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3068 /*-----------------------------------------------------------*/
3070 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3072 void vTaskPreemptionEnable( const TaskHandle_t xTask )
3077 traceENTER_vTaskPreemptionEnable( xTask );
3079 taskENTER_CRITICAL();
3081 pxTCB = prvGetTCBFromHandle( xTask );
3083 pxTCB->xPreemptionDisable = pdFALSE;
3085 if( xSchedulerRunning != pdFALSE )
3087 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3089 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
3090 prvYieldCore( xCoreID );
3094 taskEXIT_CRITICAL();
3096 traceRETURN_vTaskPreemptionEnable();
3099 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3100 /*-----------------------------------------------------------*/
3102 #if ( INCLUDE_vTaskSuspend == 1 )
3104 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
3108 #if ( configNUMBER_OF_CORES > 1 )
3109 BaseType_t xTaskRunningOnCore;
3112 traceENTER_vTaskSuspend( xTaskToSuspend );
3114 taskENTER_CRITICAL();
3116 /* If null is passed in here then it is the running task that is
3117 * being suspended. */
3118 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
3120 traceTASK_SUSPEND( pxTCB );
3122 #if ( configNUMBER_OF_CORES > 1 )
3123 xTaskRunningOnCore = pxTCB->xTaskRunState;
3126 /* Remove task from the ready/delayed list and place in the
3127 * suspended list. */
3128 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
3130 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
3134 mtCOVERAGE_TEST_MARKER();
3137 /* Is the task waiting on an event also? */
3138 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3140 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3144 mtCOVERAGE_TEST_MARKER();
3147 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
3149 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3153 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3155 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3157 /* The task was blocked to wait for a notification, but is
3158 * now suspended, so no notification was received. */
3159 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
3163 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3166 #if ( configNUMBER_OF_CORES == 1 )
3168 taskEXIT_CRITICAL();
3170 if( xSchedulerRunning != pdFALSE )
3172 /* Reset the next expected unblock time in case it referred to the
3173 * task that is now in the Suspended state. */
3174 taskENTER_CRITICAL();
3176 prvResetNextTaskUnblockTime();
3178 taskEXIT_CRITICAL();
3182 mtCOVERAGE_TEST_MARKER();
3185 if( pxTCB == pxCurrentTCB )
3187 if( xSchedulerRunning != pdFALSE )
3189 /* The current task has just been suspended. */
3190 configASSERT( uxSchedulerSuspended == 0 );
3191 portYIELD_WITHIN_API();
3195 /* The scheduler is not running, but the task that was pointed
3196 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
3197 * must be adjusted to point to a different task. */
3198 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
3200 /* No other tasks are ready, so set pxCurrentTCB back to
3201 * NULL so when the next task is created pxCurrentTCB will
3202 * be set to point to it no matter what its relative priority
3204 pxCurrentTCB = NULL;
3208 vTaskSwitchContext();
3214 mtCOVERAGE_TEST_MARKER();
3217 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3219 if( xSchedulerRunning != pdFALSE )
3221 /* Reset the next expected unblock time in case it referred to the
3222 * task that is now in the Suspended state. */
3223 prvResetNextTaskUnblockTime();
3227 mtCOVERAGE_TEST_MARKER();
3230 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3232 if( xSchedulerRunning != pdFALSE )
3234 if( xTaskRunningOnCore == ( BaseType_t ) portGET_CORE_ID() )
3236 /* The current task has just been suspended. */
3237 configASSERT( uxSchedulerSuspended == 0 );
3238 vTaskYieldWithinAPI();
3242 prvYieldCore( xTaskRunningOnCore );
3247 /* This code path is not possible because only Idle tasks are
3248 * assigned a core before the scheduler is started ( i.e.
3249 * taskTASK_IS_RUNNING is only true for idle tasks before
3250 * the scheduler is started ) and idle tasks cannot be
3252 mtCOVERAGE_TEST_MARKER();
3257 mtCOVERAGE_TEST_MARKER();
3260 taskEXIT_CRITICAL();
3262 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3264 traceRETURN_vTaskSuspend();
3267 #endif /* INCLUDE_vTaskSuspend */
3268 /*-----------------------------------------------------------*/
3270 #if ( INCLUDE_vTaskSuspend == 1 )
3272 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
3274 BaseType_t xReturn = pdFALSE;
3275 const TCB_t * const pxTCB = xTask;
3277 /* Accesses xPendingReadyList so must be called from a critical
3280 /* It does not make sense to check if the calling task is suspended. */
3281 configASSERT( xTask );
3283 /* Is the task being resumed actually in the suspended list? */
3284 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
3286 /* Has the task already been resumed from within an ISR? */
3287 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
3289 /* Is it in the suspended list because it is in the Suspended
3290 * state, or because it is blocked with no timeout? */
3291 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
3293 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3297 /* The task does not appear on the event list item of
3298 * and of the RTOS objects, but could still be in the
3299 * blocked state if it is waiting on its notification
3300 * rather than waiting on an object. If not, is
3304 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3306 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3313 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3317 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3321 mtCOVERAGE_TEST_MARKER();
3326 mtCOVERAGE_TEST_MARKER();
3331 mtCOVERAGE_TEST_MARKER();
3335 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
3337 #endif /* INCLUDE_vTaskSuspend */
3338 /*-----------------------------------------------------------*/
3340 #if ( INCLUDE_vTaskSuspend == 1 )
3342 void vTaskResume( TaskHandle_t xTaskToResume )
3344 TCB_t * const pxTCB = xTaskToResume;
3346 traceENTER_vTaskResume( xTaskToResume );
3348 /* It does not make sense to resume the calling task. */
3349 configASSERT( xTaskToResume );
3351 #if ( configNUMBER_OF_CORES == 1 )
3353 /* The parameter cannot be NULL as it is impossible to resume the
3354 * currently executing task. */
3355 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
3358 /* The parameter cannot be NULL as it is impossible to resume the
3359 * currently executing task. It is also impossible to resume a task
3360 * that is actively running on another core but it is not safe
3361 * to check their run state here. Therefore, we get into a critical
3362 * section and check if the task is actually suspended or not. */
3366 taskENTER_CRITICAL();
3368 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3370 traceTASK_RESUME( pxTCB );
3372 /* The ready list can be accessed even if the scheduler is
3373 * suspended because this is inside a critical section. */
3374 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3375 prvAddTaskToReadyList( pxTCB );
3377 /* This yield may not cause the task just resumed to run,
3378 * but will leave the lists in the correct state for the
3380 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
3384 mtCOVERAGE_TEST_MARKER();
3387 taskEXIT_CRITICAL();
3391 mtCOVERAGE_TEST_MARKER();
3394 traceRETURN_vTaskResume();
3397 #endif /* INCLUDE_vTaskSuspend */
3399 /*-----------------------------------------------------------*/
3401 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
3403 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
3405 BaseType_t xYieldRequired = pdFALSE;
3406 TCB_t * const pxTCB = xTaskToResume;
3407 UBaseType_t uxSavedInterruptStatus;
3409 traceENTER_xTaskResumeFromISR( xTaskToResume );
3411 configASSERT( xTaskToResume );
3413 /* RTOS ports that support interrupt nesting have the concept of a
3414 * maximum system call (or maximum API call) interrupt priority.
3415 * Interrupts that are above the maximum system call priority are keep
3416 * permanently enabled, even when the RTOS kernel is in a critical section,
3417 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
3418 * is defined in FreeRTOSConfig.h then
3419 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3420 * failure if a FreeRTOS API function is called from an interrupt that has
3421 * been assigned a priority above the configured maximum system call
3422 * priority. Only FreeRTOS functions that end in FromISR can be called
3423 * from interrupts that have been assigned a priority at or (logically)
3424 * below the maximum system call interrupt priority. FreeRTOS maintains a
3425 * separate interrupt safe API to ensure interrupt entry is as fast and as
3426 * simple as possible. More information (albeit Cortex-M specific) is
3427 * provided on the following link:
3428 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3429 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3431 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3433 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3435 traceTASK_RESUME_FROM_ISR( pxTCB );
3437 /* Check the ready lists can be accessed. */
3438 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3440 #if ( configNUMBER_OF_CORES == 1 )
3442 /* Ready lists can be accessed so move the task from the
3443 * suspended list to the ready list directly. */
3444 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3446 xYieldRequired = pdTRUE;
3448 /* Mark that a yield is pending in case the user is not
3449 * using the return value to initiate a context switch
3450 * from the ISR using portYIELD_FROM_ISR. */
3451 xYieldPendings[ 0 ] = pdTRUE;
3455 mtCOVERAGE_TEST_MARKER();
3458 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3460 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3461 prvAddTaskToReadyList( pxTCB );
3465 /* The delayed or ready lists cannot be accessed so the task
3466 * is held in the pending ready list until the scheduler is
3468 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3471 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3473 prvYieldForTask( pxTCB );
3475 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3477 xYieldRequired = pdTRUE;
3480 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3484 mtCOVERAGE_TEST_MARKER();
3487 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3489 traceRETURN_xTaskResumeFromISR( xYieldRequired );
3491 return xYieldRequired;
3494 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3495 /*-----------------------------------------------------------*/
3497 static BaseType_t prvCreateIdleTasks( void )
3499 BaseType_t xReturn = pdPASS;
3501 char cIdleName[ configMAX_TASK_NAME_LEN ];
3502 TaskFunction_t pxIdleTaskFunction = NULL;
3503 BaseType_t xIdleTaskNameIndex;
3505 for( xIdleTaskNameIndex = ( BaseType_t ) 0; xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN; xIdleTaskNameIndex++ )
3507 cIdleName[ xIdleTaskNameIndex ] = configIDLE_TASK_NAME[ xIdleTaskNameIndex ];
3509 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3510 * configMAX_TASK_NAME_LEN characters just in case the memory after the
3511 * string is not accessible (extremely unlikely). */
3512 if( cIdleName[ xIdleTaskNameIndex ] == ( char ) 0x00 )
3518 mtCOVERAGE_TEST_MARKER();
3522 /* Add each idle task at the lowest priority. */
3523 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3525 #if ( configNUMBER_OF_CORES == 1 )
3527 pxIdleTaskFunction = prvIdleTask;
3529 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3531 /* In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks
3532 * are also created to ensure that each core has an idle task to
3533 * run when no other task is available to run. */
3536 pxIdleTaskFunction = prvIdleTask;
3540 pxIdleTaskFunction = prvPassiveIdleTask;
3543 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3545 /* Update the idle task name with suffix to differentiate the idle tasks.
3546 * This function is not required in single core FreeRTOS since there is
3547 * only one idle task. */
3548 #if ( configNUMBER_OF_CORES > 1 )
3550 /* Append the idle task number to the end of the name if there is space. */
3551 if( xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3553 cIdleName[ xIdleTaskNameIndex ] = ( char ) ( xCoreID + '0' );
3555 /* And append a null character if there is space. */
3556 if( ( xIdleTaskNameIndex + 1 ) < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3558 cIdleName[ xIdleTaskNameIndex + 1 ] = '\0';
3562 mtCOVERAGE_TEST_MARKER();
3567 mtCOVERAGE_TEST_MARKER();
3570 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
3572 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3574 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3575 StackType_t * pxIdleTaskStackBuffer = NULL;
3576 uint32_t ulIdleTaskStackSize;
3578 /* The Idle task is created using user provided RAM - obtain the
3579 * address of the RAM then create the idle task. */
3580 #if ( configNUMBER_OF_CORES == 1 )
3582 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3588 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3592 vApplicationGetPassiveIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize, xCoreID - 1 );
3595 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
3596 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( pxIdleTaskFunction,
3598 ulIdleTaskStackSize,
3599 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3600 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3601 pxIdleTaskStackBuffer,
3602 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3604 if( xIdleTaskHandles[ xCoreID ] != NULL )
3613 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3615 /* The Idle task is being created using dynamically allocated RAM. */
3616 xReturn = xTaskCreate( pxIdleTaskFunction,
3618 configMINIMAL_STACK_SIZE,
3620 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3621 &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3623 #endif /* configSUPPORT_STATIC_ALLOCATION */
3625 /* Break the loop if any of the idle task is failed to be created. */
3626 if( xReturn == pdFAIL )
3632 mtCOVERAGE_TEST_MARKER();
3639 /*-----------------------------------------------------------*/
3641 void vTaskStartScheduler( void )
3645 traceENTER_vTaskStartScheduler();
3647 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3649 /* Sanity check that the UBaseType_t must have greater than or equal to
3650 * the number of bits as confNUMBER_OF_CORES. */
3651 configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3653 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3655 xReturn = prvCreateIdleTasks();
3657 #if ( configUSE_TIMERS == 1 )
3659 if( xReturn == pdPASS )
3661 xReturn = xTimerCreateTimerTask();
3665 mtCOVERAGE_TEST_MARKER();
3668 #endif /* configUSE_TIMERS */
3670 if( xReturn == pdPASS )
3672 /* freertos_tasks_c_additions_init() should only be called if the user
3673 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3674 * the only macro called by the function. */
3675 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3677 freertos_tasks_c_additions_init();
3681 /* Interrupts are turned off here, to ensure a tick does not occur
3682 * before or during the call to xPortStartScheduler(). The stacks of
3683 * the created tasks contain a status word with interrupts switched on
3684 * so interrupts will automatically get re-enabled when the first task
3686 portDISABLE_INTERRUPTS();
3688 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3690 /* Switch C-Runtime's TLS Block to point to the TLS
3691 * block specific to the task that will run first. */
3692 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3696 xNextTaskUnblockTime = portMAX_DELAY;
3697 xSchedulerRunning = pdTRUE;
3698 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3700 /* If configGENERATE_RUN_TIME_STATS is defined then the following
3701 * macro must be defined to configure the timer/counter used to generate
3702 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
3703 * is set to 0 and the following line fails to build then ensure you do not
3704 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3705 * FreeRTOSConfig.h file. */
3706 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3708 traceTASK_SWITCHED_IN();
3710 /* Setting up the timer tick is hardware specific and thus in the
3711 * portable interface. */
3712 xPortStartScheduler();
3714 /* In most cases, xPortStartScheduler() will not return. If it
3715 * returns pdTRUE then there was not enough heap memory available
3716 * to create either the Idle or the Timer task. If it returned
3717 * pdFALSE, then the application called xTaskEndScheduler().
3718 * Most ports don't implement xTaskEndScheduler() as there is
3719 * nothing to return to. */
3723 /* This line will only be reached if the kernel could not be started,
3724 * because there was not enough FreeRTOS heap to create the idle task
3725 * or the timer task. */
3726 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3729 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3730 * meaning xIdleTaskHandles are not used anywhere else. */
3731 ( void ) xIdleTaskHandles;
3733 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3734 * from getting optimized out as it is no longer used by the kernel. */
3735 ( void ) uxTopUsedPriority;
3737 traceRETURN_vTaskStartScheduler();
3739 /*-----------------------------------------------------------*/
3741 void vTaskEndScheduler( void )
3743 traceENTER_vTaskEndScheduler();
3745 /* Stop the scheduler interrupts and call the portable scheduler end
3746 * routine so the original ISRs can be restored if necessary. The port
3747 * layer must ensure interrupts enable bit is left in the correct state. */
3748 portDISABLE_INTERRUPTS();
3749 xSchedulerRunning = pdFALSE;
3750 vPortEndScheduler();
3752 traceRETURN_vTaskEndScheduler();
3754 /*----------------------------------------------------------*/
3756 void vTaskSuspendAll( void )
3758 traceENTER_vTaskSuspendAll();
3760 #if ( configNUMBER_OF_CORES == 1 )
3762 /* A critical section is not required as the variable is of type
3763 * BaseType_t. Please read Richard Barry's reply in the following link to a
3764 * post in the FreeRTOS support forum before reporting this as a bug! -
3765 * https://goo.gl/wu4acr */
3767 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3768 * do not otherwise exhibit real time behaviour. */
3769 portSOFTWARE_BARRIER();
3771 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3772 * is used to allow calls to vTaskSuspendAll() to nest. */
3773 ++uxSchedulerSuspended;
3775 /* Enforces ordering for ports and optimised compilers that may otherwise place
3776 * the above increment elsewhere. */
3777 portMEMORY_BARRIER();
3779 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3781 UBaseType_t ulState;
3783 /* This must only be called from within a task. */
3784 portASSERT_IF_IN_ISR();
3786 if( xSchedulerRunning != pdFALSE )
3788 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3789 * We must disable interrupts before we grab the locks in the event that this task is
3790 * interrupted and switches context before incrementing uxSchedulerSuspended.
3791 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3792 * uxSchedulerSuspended since that will prevent context switches. */
3793 ulState = portSET_INTERRUPT_MASK();
3795 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3796 * do not otherwise exhibit real time behaviour. */
3797 portSOFTWARE_BARRIER();
3799 portGET_TASK_LOCK();
3801 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3802 * purpose is to prevent altering the variable when fromISR APIs are readying
3804 if( uxSchedulerSuspended == 0U )
3806 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
3808 prvCheckForRunStateChange();
3812 mtCOVERAGE_TEST_MARKER();
3817 mtCOVERAGE_TEST_MARKER();
3822 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3823 * is used to allow calls to vTaskSuspendAll() to nest. */
3824 ++uxSchedulerSuspended;
3825 portRELEASE_ISR_LOCK();
3827 portCLEAR_INTERRUPT_MASK( ulState );
3831 mtCOVERAGE_TEST_MARKER();
3834 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3836 traceRETURN_vTaskSuspendAll();
3839 /*----------------------------------------------------------*/
3841 #if ( configUSE_TICKLESS_IDLE != 0 )
3843 static TickType_t prvGetExpectedIdleTime( void )
3846 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
3848 /* uxHigherPriorityReadyTasks takes care of the case where
3849 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3850 * task that are in the Ready state, even though the idle task is
3852 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3854 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3856 uxHigherPriorityReadyTasks = pdTRUE;
3861 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3863 /* When port optimised task selection is used the uxTopReadyPriority
3864 * variable is used as a bit map. If bits other than the least
3865 * significant bit are set then there are tasks that have a priority
3866 * above the idle priority that are in the Ready state. This takes
3867 * care of the case where the co-operative scheduler is in use. */
3868 if( uxTopReadyPriority > uxLeastSignificantBit )
3870 uxHigherPriorityReadyTasks = pdTRUE;
3873 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3875 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3879 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1U )
3881 /* There are other idle priority tasks in the ready state. If
3882 * time slicing is used then the very next tick interrupt must be
3886 else if( uxHigherPriorityReadyTasks != pdFALSE )
3888 /* There are tasks in the Ready state that have a priority above the
3889 * idle priority. This path can only be reached if
3890 * configUSE_PREEMPTION is 0. */
3895 xReturn = xNextTaskUnblockTime - xTickCount;
3901 #endif /* configUSE_TICKLESS_IDLE */
3902 /*----------------------------------------------------------*/
3904 BaseType_t xTaskResumeAll( void )
3906 TCB_t * pxTCB = NULL;
3907 BaseType_t xAlreadyYielded = pdFALSE;
3909 traceENTER_xTaskResumeAll();
3911 #if ( configNUMBER_OF_CORES > 1 )
3912 if( xSchedulerRunning != pdFALSE )
3915 /* It is possible that an ISR caused a task to be removed from an event
3916 * list while the scheduler was suspended. If this was the case then the
3917 * removed task will have been added to the xPendingReadyList. Once the
3918 * scheduler has been resumed it is safe to move all the pending ready
3919 * tasks from this list into their appropriate ready list. */
3920 taskENTER_CRITICAL();
3923 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3925 /* If uxSchedulerSuspended is zero then this function does not match a
3926 * previous call to vTaskSuspendAll(). */
3927 configASSERT( uxSchedulerSuspended != 0U );
3929 --uxSchedulerSuspended;
3930 portRELEASE_TASK_LOCK();
3932 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3934 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3936 /* Move any readied tasks from the pending list into the
3937 * appropriate ready list. */
3938 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3940 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3941 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
3942 portMEMORY_BARRIER();
3943 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
3944 prvAddTaskToReadyList( pxTCB );
3946 #if ( configNUMBER_OF_CORES == 1 )
3948 /* If the moved task has a priority higher than the current
3949 * task then a yield must be performed. */
3950 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3952 xYieldPendings[ xCoreID ] = pdTRUE;
3956 mtCOVERAGE_TEST_MARKER();
3959 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3961 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
3962 * If the current core yielded then vTaskSwitchContext() has already been called
3963 * which sets xYieldPendings for the current core to pdTRUE. */
3965 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3970 /* A task was unblocked while the scheduler was suspended,
3971 * which may have prevented the next unblock time from being
3972 * re-calculated, in which case re-calculate it now. Mainly
3973 * important for low power tickless implementations, where
3974 * this can prevent an unnecessary exit from low power
3976 prvResetNextTaskUnblockTime();
3979 /* If any ticks occurred while the scheduler was suspended then
3980 * they should be processed now. This ensures the tick count does
3981 * not slip, and that any delayed tasks are resumed at the correct
3984 * It should be safe to call xTaskIncrementTick here from any core
3985 * since we are in a critical section and xTaskIncrementTick itself
3986 * protects itself within a critical section. Suspending the scheduler
3987 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
3989 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3991 if( xPendedCounts > ( TickType_t ) 0U )
3995 if( xTaskIncrementTick() != pdFALSE )
3997 /* Other cores are interrupted from
3998 * within xTaskIncrementTick(). */
3999 xYieldPendings[ xCoreID ] = pdTRUE;
4003 mtCOVERAGE_TEST_MARKER();
4007 } while( xPendedCounts > ( TickType_t ) 0U );
4013 mtCOVERAGE_TEST_MARKER();
4017 if( xYieldPendings[ xCoreID ] != pdFALSE )
4019 #if ( configUSE_PREEMPTION != 0 )
4021 xAlreadyYielded = pdTRUE;
4023 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
4025 #if ( configNUMBER_OF_CORES == 1 )
4027 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
4029 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4033 mtCOVERAGE_TEST_MARKER();
4039 mtCOVERAGE_TEST_MARKER();
4042 taskEXIT_CRITICAL();
4045 traceRETURN_xTaskResumeAll( xAlreadyYielded );
4047 return xAlreadyYielded;
4049 /*-----------------------------------------------------------*/
4051 TickType_t xTaskGetTickCount( void )
4055 traceENTER_xTaskGetTickCount();
4057 /* Critical section required if running on a 16 bit processor. */
4058 portTICK_TYPE_ENTER_CRITICAL();
4060 xTicks = xTickCount;
4062 portTICK_TYPE_EXIT_CRITICAL();
4064 traceRETURN_xTaskGetTickCount( xTicks );
4068 /*-----------------------------------------------------------*/
4070 TickType_t xTaskGetTickCountFromISR( void )
4073 UBaseType_t uxSavedInterruptStatus;
4075 traceENTER_xTaskGetTickCountFromISR();
4077 /* RTOS ports that support interrupt nesting have the concept of a maximum
4078 * system call (or maximum API call) interrupt priority. Interrupts that are
4079 * above the maximum system call priority are kept permanently enabled, even
4080 * when the RTOS kernel is in a critical section, but cannot make any calls to
4081 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
4082 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
4083 * failure if a FreeRTOS API function is called from an interrupt that has been
4084 * assigned a priority above the configured maximum system call priority.
4085 * Only FreeRTOS functions that end in FromISR can be called from interrupts
4086 * that have been assigned a priority at or (logically) below the maximum
4087 * system call interrupt priority. FreeRTOS maintains a separate interrupt
4088 * safe API to ensure interrupt entry is as fast and as simple as possible.
4089 * More information (albeit Cortex-M specific) is provided on the following
4090 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
4091 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
4093 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
4095 xReturn = xTickCount;
4097 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
4099 traceRETURN_xTaskGetTickCountFromISR( xReturn );
4103 /*-----------------------------------------------------------*/
4105 UBaseType_t uxTaskGetNumberOfTasks( void )
4107 traceENTER_uxTaskGetNumberOfTasks();
4109 /* A critical section is not required because the variables are of type
4111 traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
4113 return uxCurrentNumberOfTasks;
4115 /*-----------------------------------------------------------*/
4117 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
4121 traceENTER_pcTaskGetName( xTaskToQuery );
4123 /* If null is passed in here then the name of the calling task is being
4125 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4126 configASSERT( pxTCB );
4128 traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
4130 return &( pxTCB->pcTaskName[ 0 ] );
4132 /*-----------------------------------------------------------*/
4134 #if ( INCLUDE_xTaskGetHandle == 1 )
4136 #if ( configNUMBER_OF_CORES == 1 )
4137 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4138 const char pcNameToQuery[] )
4142 TCB_t * pxReturn = NULL;
4145 BaseType_t xBreakLoop;
4147 /* This function is called with the scheduler suspended. */
4149 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4151 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4155 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4157 /* Check each character in the name looking for a match or
4159 xBreakLoop = pdFALSE;
4161 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4163 cNextChar = pxNextTCB->pcTaskName[ x ];
4165 if( cNextChar != pcNameToQuery[ x ] )
4167 /* Characters didn't match. */
4168 xBreakLoop = pdTRUE;
4170 else if( cNextChar == ( char ) 0x00 )
4172 /* Both strings terminated, a match must have been
4174 pxReturn = pxNextTCB;
4175 xBreakLoop = pdTRUE;
4179 mtCOVERAGE_TEST_MARKER();
4182 if( xBreakLoop != pdFALSE )
4188 if( pxReturn != NULL )
4190 /* The handle has been found. */
4193 } while( pxNextTCB != pxFirstTCB );
4197 mtCOVERAGE_TEST_MARKER();
4202 #else /* if ( configNUMBER_OF_CORES == 1 ) */
4203 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4204 const char pcNameToQuery[] )
4206 TCB_t * pxReturn = NULL;
4209 BaseType_t xBreakLoop;
4210 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
4211 ListItem_t * pxIterator;
4213 /* This function is called with the scheduler suspended. */
4215 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4217 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
4219 TCB_t * pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
4221 /* Check each character in the name looking for a match or
4223 xBreakLoop = pdFALSE;
4225 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4227 cNextChar = pxTCB->pcTaskName[ x ];
4229 if( cNextChar != pcNameToQuery[ x ] )
4231 /* Characters didn't match. */
4232 xBreakLoop = pdTRUE;
4234 else if( cNextChar == ( char ) 0x00 )
4236 /* Both strings terminated, a match must have been
4239 xBreakLoop = pdTRUE;
4243 mtCOVERAGE_TEST_MARKER();
4246 if( xBreakLoop != pdFALSE )
4252 if( pxReturn != NULL )
4254 /* The handle has been found. */
4261 mtCOVERAGE_TEST_MARKER();
4266 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4268 #endif /* INCLUDE_xTaskGetHandle */
4269 /*-----------------------------------------------------------*/
4271 #if ( INCLUDE_xTaskGetHandle == 1 )
4273 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
4275 UBaseType_t uxQueue = configMAX_PRIORITIES;
4278 traceENTER_xTaskGetHandle( pcNameToQuery );
4280 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
4281 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
4285 /* Search the ready lists. */
4289 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
4293 /* Found the handle. */
4296 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4298 /* Search the delayed lists. */
4301 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
4306 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
4309 #if ( INCLUDE_vTaskSuspend == 1 )
4313 /* Search the suspended list. */
4314 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
4319 #if ( INCLUDE_vTaskDelete == 1 )
4323 /* Search the deleted list. */
4324 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
4329 ( void ) xTaskResumeAll();
4331 traceRETURN_xTaskGetHandle( pxTCB );
4336 #endif /* INCLUDE_xTaskGetHandle */
4337 /*-----------------------------------------------------------*/
4339 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4341 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4342 StackType_t ** ppuxStackBuffer,
4343 StaticTask_t ** ppxTaskBuffer )
4348 traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4350 configASSERT( ppuxStackBuffer != NULL );
4351 configASSERT( ppxTaskBuffer != NULL );
4353 pxTCB = prvGetTCBFromHandle( xTask );
4355 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4357 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4359 *ppuxStackBuffer = pxTCB->pxStack;
4360 /* MISRA Ref 11.3.1 [Misaligned access] */
4361 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
4362 /* coverity[misra_c_2012_rule_11_3_violation] */
4363 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4366 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4368 *ppuxStackBuffer = pxTCB->pxStack;
4369 *ppxTaskBuffer = NULL;
4377 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4379 *ppuxStackBuffer = pxTCB->pxStack;
4380 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4383 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4385 traceRETURN_xTaskGetStaticBuffers( xReturn );
4390 #endif /* configSUPPORT_STATIC_ALLOCATION */
4391 /*-----------------------------------------------------------*/
4393 #if ( configUSE_TRACE_FACILITY == 1 )
4395 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4396 const UBaseType_t uxArraySize,
4397 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4399 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4401 traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4405 /* Is there a space in the array for each task in the system? */
4406 if( uxArraySize >= uxCurrentNumberOfTasks )
4408 /* Fill in an TaskStatus_t structure with information on each
4409 * task in the Ready state. */
4413 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4414 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4416 /* Fill in an TaskStatus_t structure with information on each
4417 * task in the Blocked state. */
4418 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4419 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4421 #if ( INCLUDE_vTaskDelete == 1 )
4423 /* Fill in an TaskStatus_t structure with information on
4424 * each task that has been deleted but not yet cleaned up. */
4425 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4429 #if ( INCLUDE_vTaskSuspend == 1 )
4431 /* Fill in an TaskStatus_t structure with information on
4432 * each task in the Suspended state. */
4433 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4437 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4439 if( pulTotalRunTime != NULL )
4441 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4442 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4444 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4448 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4450 if( pulTotalRunTime != NULL )
4452 *pulTotalRunTime = 0;
4455 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4459 mtCOVERAGE_TEST_MARKER();
4462 ( void ) xTaskResumeAll();
4464 traceRETURN_uxTaskGetSystemState( uxTask );
4469 #endif /* configUSE_TRACE_FACILITY */
4470 /*----------------------------------------------------------*/
4472 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4474 #if ( configNUMBER_OF_CORES == 1 )
4475 TaskHandle_t xTaskGetIdleTaskHandle( void )
4477 traceENTER_xTaskGetIdleTaskHandle();
4479 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4480 * started, then xIdleTaskHandles will be NULL. */
4481 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4483 traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4485 return xIdleTaskHandles[ 0 ];
4487 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
4489 TaskHandle_t xTaskGetIdleTaskHandleForCore( BaseType_t xCoreID )
4491 traceENTER_xTaskGetIdleTaskHandleForCore( xCoreID );
4493 /* Ensure the core ID is valid. */
4494 configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE );
4496 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4497 * started, then xIdleTaskHandles will be NULL. */
4498 configASSERT( ( xIdleTaskHandles[ xCoreID ] != NULL ) );
4500 traceRETURN_xTaskGetIdleTaskHandleForCore( xIdleTaskHandles[ xCoreID ] );
4502 return xIdleTaskHandles[ xCoreID ];
4505 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4506 /*----------------------------------------------------------*/
4508 /* This conditional compilation should use inequality to 0, not equality to 1.
4509 * This is to ensure vTaskStepTick() is available when user defined low power mode
4510 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4512 #if ( configUSE_TICKLESS_IDLE != 0 )
4514 void vTaskStepTick( TickType_t xTicksToJump )
4516 traceENTER_vTaskStepTick( xTicksToJump );
4518 /* Correct the tick count value after a period during which the tick
4519 * was suppressed. Note this does *not* call the tick hook function for
4520 * each stepped tick. */
4521 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
4523 if( ( xTickCount + xTicksToJump ) == xNextTaskUnblockTime )
4525 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4526 * xTaskIncrementTick() when the scheduler resumes. This ensures
4527 * that any delayed tasks are resumed at the correct time. */
4528 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4529 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4531 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4532 taskENTER_CRITICAL();
4536 taskEXIT_CRITICAL();
4541 mtCOVERAGE_TEST_MARKER();
4544 xTickCount += xTicksToJump;
4546 traceINCREASE_TICK_COUNT( xTicksToJump );
4547 traceRETURN_vTaskStepTick();
4550 #endif /* configUSE_TICKLESS_IDLE */
4551 /*----------------------------------------------------------*/
4553 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4555 BaseType_t xYieldOccurred;
4557 traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4559 /* Must not be called with the scheduler suspended as the implementation
4560 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4561 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4563 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4564 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4567 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4568 taskENTER_CRITICAL();
4570 xPendedTicks += xTicksToCatchUp;
4572 taskEXIT_CRITICAL();
4573 xYieldOccurred = xTaskResumeAll();
4575 traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4577 return xYieldOccurred;
4579 /*----------------------------------------------------------*/
4581 #if ( INCLUDE_xTaskAbortDelay == 1 )
4583 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4585 TCB_t * pxTCB = xTask;
4588 traceENTER_xTaskAbortDelay( xTask );
4590 configASSERT( pxTCB );
4594 /* A task can only be prematurely removed from the Blocked state if
4595 * it is actually in the Blocked state. */
4596 if( eTaskGetState( xTask ) == eBlocked )
4600 /* Remove the reference to the task from the blocked list. An
4601 * interrupt won't touch the xStateListItem because the
4602 * scheduler is suspended. */
4603 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4605 /* Is the task waiting on an event also? If so remove it from
4606 * the event list too. Interrupts can touch the event list item,
4607 * even though the scheduler is suspended, so a critical section
4609 taskENTER_CRITICAL();
4611 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4613 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4615 /* This lets the task know it was forcibly removed from the
4616 * blocked state so it should not re-evaluate its block time and
4617 * then block again. */
4618 pxTCB->ucDelayAborted = pdTRUE;
4622 mtCOVERAGE_TEST_MARKER();
4625 taskEXIT_CRITICAL();
4627 /* Place the unblocked task into the appropriate ready list. */
4628 prvAddTaskToReadyList( pxTCB );
4630 /* A task being unblocked cannot cause an immediate context
4631 * switch if preemption is turned off. */
4632 #if ( configUSE_PREEMPTION == 1 )
4634 #if ( configNUMBER_OF_CORES == 1 )
4636 /* Preemption is on, but a context switch should only be
4637 * performed if the unblocked task has a priority that is
4638 * higher than the currently executing task. */
4639 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4641 /* Pend the yield to be performed when the scheduler
4642 * is unsuspended. */
4643 xYieldPendings[ 0 ] = pdTRUE;
4647 mtCOVERAGE_TEST_MARKER();
4650 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4652 taskENTER_CRITICAL();
4654 prvYieldForTask( pxTCB );
4656 taskEXIT_CRITICAL();
4658 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4660 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4667 ( void ) xTaskResumeAll();
4669 traceRETURN_xTaskAbortDelay( xReturn );
4674 #endif /* INCLUDE_xTaskAbortDelay */
4675 /*----------------------------------------------------------*/
4677 BaseType_t xTaskIncrementTick( void )
4680 TickType_t xItemValue;
4681 BaseType_t xSwitchRequired = pdFALSE;
4683 #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4684 BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4685 #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4687 traceENTER_xTaskIncrementTick();
4689 /* Called by the portable layer each time a tick interrupt occurs.
4690 * Increments the tick then checks to see if the new tick value will cause any
4691 * tasks to be unblocked. */
4692 traceTASK_INCREMENT_TICK( xTickCount );
4694 /* Tick increment should occur on every kernel timer event. Core 0 has the
4695 * responsibility to increment the tick, or increment the pended ticks if the
4696 * scheduler is suspended. If pended ticks is greater than zero, the core that
4697 * calls xTaskResumeAll has the responsibility to increment the tick. */
4698 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4700 /* Minor optimisation. The tick count cannot change in this
4702 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4704 /* Increment the RTOS tick, switching the delayed and overflowed
4705 * delayed lists if it wraps to 0. */
4706 xTickCount = xConstTickCount;
4708 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
4710 taskSWITCH_DELAYED_LISTS();
4714 mtCOVERAGE_TEST_MARKER();
4717 /* See if this tick has made a timeout expire. Tasks are stored in
4718 * the queue in the order of their wake time - meaning once one task
4719 * has been found whose block time has not expired there is no need to
4720 * look any further down the list. */
4721 if( xConstTickCount >= xNextTaskUnblockTime )
4725 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4727 /* The delayed list is empty. Set xNextTaskUnblockTime
4728 * to the maximum possible value so it is extremely
4730 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4731 * next time through. */
4732 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4737 /* The delayed list is not empty, get the value of the
4738 * item at the head of the delayed list. This is the time
4739 * at which the task at the head of the delayed list must
4740 * be removed from the Blocked state. */
4741 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4742 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4744 if( xConstTickCount < xItemValue )
4746 /* It is not time to unblock this item yet, but the
4747 * item value is the time at which the task at the head
4748 * of the blocked list must be removed from the Blocked
4749 * state - so record the item value in
4750 * xNextTaskUnblockTime. */
4751 xNextTaskUnblockTime = xItemValue;
4752 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
4756 mtCOVERAGE_TEST_MARKER();
4759 /* It is time to remove the item from the Blocked state. */
4760 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4762 /* Is the task waiting on an event also? If so remove
4763 * it from the event list. */
4764 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4766 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4770 mtCOVERAGE_TEST_MARKER();
4773 /* Place the unblocked task into the appropriate ready
4775 prvAddTaskToReadyList( pxTCB );
4777 /* A task being unblocked cannot cause an immediate
4778 * context switch if preemption is turned off. */
4779 #if ( configUSE_PREEMPTION == 1 )
4781 #if ( configNUMBER_OF_CORES == 1 )
4783 /* Preemption is on, but a context switch should
4784 * only be performed if the unblocked task's
4785 * priority is higher than the currently executing
4787 * The case of equal priority tasks sharing
4788 * processing time (which happens when both
4789 * preemption and time slicing are on) is
4791 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4793 xSwitchRequired = pdTRUE;
4797 mtCOVERAGE_TEST_MARKER();
4800 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4802 prvYieldForTask( pxTCB );
4804 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4806 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4811 /* Tasks of equal priority to the currently running task will share
4812 * processing time (time slice) if preemption is on, and the application
4813 * writer has not explicitly turned time slicing off. */
4814 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4816 #if ( configNUMBER_OF_CORES == 1 )
4818 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > 1U )
4820 xSwitchRequired = pdTRUE;
4824 mtCOVERAGE_TEST_MARKER();
4827 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4831 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4833 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1U )
4835 xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4839 mtCOVERAGE_TEST_MARKER();
4843 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4845 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4847 #if ( configUSE_TICK_HOOK == 1 )
4849 /* Guard against the tick hook being called when the pended tick
4850 * count is being unwound (when the scheduler is being unlocked). */
4851 if( xPendedTicks == ( TickType_t ) 0 )
4853 vApplicationTickHook();
4857 mtCOVERAGE_TEST_MARKER();
4860 #endif /* configUSE_TICK_HOOK */
4862 #if ( configUSE_PREEMPTION == 1 )
4864 #if ( configNUMBER_OF_CORES == 1 )
4866 /* For single core the core ID is always 0. */
4867 if( xYieldPendings[ 0 ] != pdFALSE )
4869 xSwitchRequired = pdTRUE;
4873 mtCOVERAGE_TEST_MARKER();
4876 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4878 BaseType_t xCoreID, xCurrentCoreID;
4879 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4881 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4883 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4884 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4887 if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4889 if( xCoreID == xCurrentCoreID )
4891 xSwitchRequired = pdTRUE;
4895 prvYieldCore( xCoreID );
4900 mtCOVERAGE_TEST_MARKER();
4905 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4907 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4913 /* The tick hook gets called at regular intervals, even if the
4914 * scheduler is locked. */
4915 #if ( configUSE_TICK_HOOK == 1 )
4917 vApplicationTickHook();
4922 traceRETURN_xTaskIncrementTick( xSwitchRequired );
4924 return xSwitchRequired;
4926 /*-----------------------------------------------------------*/
4928 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4930 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4931 TaskHookFunction_t pxHookFunction )
4935 traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4937 /* If xTask is NULL then it is the task hook of the calling task that is
4941 xTCB = ( TCB_t * ) pxCurrentTCB;
4948 /* Save the hook function in the TCB. A critical section is required as
4949 * the value can be accessed from an interrupt. */
4950 taskENTER_CRITICAL();
4952 xTCB->pxTaskTag = pxHookFunction;
4954 taskEXIT_CRITICAL();
4956 traceRETURN_vTaskSetApplicationTaskTag();
4959 #endif /* configUSE_APPLICATION_TASK_TAG */
4960 /*-----------------------------------------------------------*/
4962 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4964 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4967 TaskHookFunction_t xReturn;
4969 traceENTER_xTaskGetApplicationTaskTag( xTask );
4971 /* If xTask is NULL then set the calling task's hook. */
4972 pxTCB = prvGetTCBFromHandle( xTask );
4974 /* Save the hook function in the TCB. A critical section is required as
4975 * the value can be accessed from an interrupt. */
4976 taskENTER_CRITICAL();
4978 xReturn = pxTCB->pxTaskTag;
4980 taskEXIT_CRITICAL();
4982 traceRETURN_xTaskGetApplicationTaskTag( xReturn );
4987 #endif /* configUSE_APPLICATION_TASK_TAG */
4988 /*-----------------------------------------------------------*/
4990 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4992 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
4995 TaskHookFunction_t xReturn;
4996 UBaseType_t uxSavedInterruptStatus;
4998 traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
5000 /* If xTask is NULL then set the calling task's hook. */
5001 pxTCB = prvGetTCBFromHandle( xTask );
5003 /* Save the hook function in the TCB. A critical section is required as
5004 * the value can be accessed from an interrupt. */
5005 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
5007 xReturn = pxTCB->pxTaskTag;
5009 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
5011 traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
5016 #endif /* configUSE_APPLICATION_TASK_TAG */
5017 /*-----------------------------------------------------------*/
5019 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5021 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
5022 void * pvParameter )
5027 traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
5029 /* If xTask is NULL then we are calling our own task hook. */
5032 xTCB = pxCurrentTCB;
5039 if( xTCB->pxTaskTag != NULL )
5041 xReturn = xTCB->pxTaskTag( pvParameter );
5048 traceRETURN_xTaskCallApplicationTaskHook( xReturn );
5053 #endif /* configUSE_APPLICATION_TASK_TAG */
5054 /*-----------------------------------------------------------*/
5056 #if ( configNUMBER_OF_CORES == 1 )
5057 void vTaskSwitchContext( void )
5059 traceENTER_vTaskSwitchContext();
5061 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5063 /* The scheduler is currently suspended - do not allow a context
5065 xYieldPendings[ 0 ] = pdTRUE;
5069 xYieldPendings[ 0 ] = pdFALSE;
5070 traceTASK_SWITCHED_OUT();
5072 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5074 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5075 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
5077 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
5080 /* Add the amount of time the task has been running to the
5081 * accumulated time so far. The time the task started running was
5082 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5083 * protection here so count values are only valid until the timer
5084 * overflows. The guard against negative values is to protect
5085 * against suspect run time stat counter implementations - which
5086 * are provided by the application, not the kernel. */
5087 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
5089 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
5093 mtCOVERAGE_TEST_MARKER();
5096 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
5098 #endif /* configGENERATE_RUN_TIME_STATS */
5100 /* Check for stack overflow, if configured. */
5101 taskCHECK_FOR_STACK_OVERFLOW();
5103 /* Before the currently running task is switched out, save its errno. */
5104 #if ( configUSE_POSIX_ERRNO == 1 )
5106 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
5110 /* Select a new task to run using either the generic C or port
5111 * optimised asm code. */
5112 taskSELECT_HIGHEST_PRIORITY_TASK(); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
5113 traceTASK_SWITCHED_IN();
5115 /* After the new task is switched in, update the global errno. */
5116 #if ( configUSE_POSIX_ERRNO == 1 )
5118 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
5122 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5124 /* Switch C-Runtime's TLS Block to point to the TLS
5125 * Block specific to this task. */
5126 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
5131 traceRETURN_vTaskSwitchContext();
5133 #else /* if ( configNUMBER_OF_CORES == 1 ) */
5134 void vTaskSwitchContext( BaseType_t xCoreID )
5136 traceENTER_vTaskSwitchContext();
5138 /* Acquire both locks:
5139 * - The ISR lock protects the ready list from simultaneous access by
5140 * both other ISRs and tasks.
5141 * - We also take the task lock to pause here in case another core has
5142 * suspended the scheduler. We don't want to simply set xYieldPending
5143 * and move on if another core suspended the scheduler. We should only
5144 * do that if the current core has suspended the scheduler. */
5146 portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
5149 /* vTaskSwitchContext() must never be called from within a critical section.
5150 * This is not necessarily true for single core FreeRTOS, but it is for this
5152 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
5154 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5156 /* The scheduler is currently suspended - do not allow a context
5158 xYieldPendings[ xCoreID ] = pdTRUE;
5162 xYieldPendings[ xCoreID ] = pdFALSE;
5163 traceTASK_SWITCHED_OUT();
5165 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5167 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5168 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
5170 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
5173 /* Add the amount of time the task has been running to the
5174 * accumulated time so far. The time the task started running was
5175 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5176 * protection here so count values are only valid until the timer
5177 * overflows. The guard against negative values is to protect
5178 * against suspect run time stat counter implementations - which
5179 * are provided by the application, not the kernel. */
5180 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
5182 pxCurrentTCBs[ xCoreID ]->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
5186 mtCOVERAGE_TEST_MARKER();
5189 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
5191 #endif /* configGENERATE_RUN_TIME_STATS */
5193 /* Check for stack overflow, if configured. */
5194 taskCHECK_FOR_STACK_OVERFLOW();
5196 /* Before the currently running task is switched out, save its errno. */
5197 #if ( configUSE_POSIX_ERRNO == 1 )
5199 pxCurrentTCBs[ xCoreID ]->iTaskErrno = FreeRTOS_errno;
5203 /* Select a new task to run. */
5204 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
5205 traceTASK_SWITCHED_IN();
5207 /* After the new task is switched in, update the global errno. */
5208 #if ( configUSE_POSIX_ERRNO == 1 )
5210 FreeRTOS_errno = pxCurrentTCBs[ xCoreID ]->iTaskErrno;
5214 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5216 /* Switch C-Runtime's TLS Block to point to the TLS
5217 * Block specific to this task. */
5218 configSET_TLS_BLOCK( pxCurrentTCBs[ xCoreID ]->xTLSBlock );
5223 portRELEASE_ISR_LOCK();
5224 portRELEASE_TASK_LOCK();
5226 traceRETURN_vTaskSwitchContext();
5228 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
5229 /*-----------------------------------------------------------*/
5231 void vTaskPlaceOnEventList( List_t * const pxEventList,
5232 const TickType_t xTicksToWait )
5234 traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
5236 configASSERT( pxEventList );
5238 /* THIS FUNCTION MUST BE CALLED WITH THE
5239 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
5241 /* Place the event list item of the TCB in the appropriate event list.
5242 * This is placed in the list in priority order so the highest priority task
5243 * is the first to be woken by the event.
5245 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
5246 * Normally, the xItemValue of a TCB's ListItem_t members is:
5247 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
5248 * Therefore, the event list is sorted in descending priority order.
5250 * The queue that contains the event list is locked, preventing
5251 * simultaneous access from interrupts. */
5252 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5254 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5256 traceRETURN_vTaskPlaceOnEventList();
5258 /*-----------------------------------------------------------*/
5260 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
5261 const TickType_t xItemValue,
5262 const TickType_t xTicksToWait )
5264 traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
5266 configASSERT( pxEventList );
5268 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5269 * the event groups implementation. */
5270 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5272 /* Store the item value in the event list item. It is safe to access the
5273 * event list item here as interrupts won't access the event list item of a
5274 * task that is not in the Blocked state. */
5275 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5277 /* Place the event list item of the TCB at the end of the appropriate event
5278 * list. It is safe to access the event list here because it is part of an
5279 * event group implementation - and interrupts don't access event groups
5280 * directly (instead they access them indirectly by pending function calls to
5281 * the task level). */
5282 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5284 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5286 traceRETURN_vTaskPlaceOnUnorderedEventList();
5288 /*-----------------------------------------------------------*/
5290 #if ( configUSE_TIMERS == 1 )
5292 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
5293 TickType_t xTicksToWait,
5294 const BaseType_t xWaitIndefinitely )
5296 traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
5298 configASSERT( pxEventList );
5300 /* This function should not be called by application code hence the
5301 * 'Restricted' in its name. It is not part of the public API. It is
5302 * designed for use by kernel code, and has special calling requirements -
5303 * it should be called with the scheduler suspended. */
5306 /* Place the event list item of the TCB in the appropriate event list.
5307 * In this case it is assume that this is the only task that is going to
5308 * be waiting on this event list, so the faster vListInsertEnd() function
5309 * can be used in place of vListInsert. */
5310 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5312 /* If the task should block indefinitely then set the block time to a
5313 * value that will be recognised as an indefinite delay inside the
5314 * prvAddCurrentTaskToDelayedList() function. */
5315 if( xWaitIndefinitely != pdFALSE )
5317 xTicksToWait = portMAX_DELAY;
5320 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
5321 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
5323 traceRETURN_vTaskPlaceOnEventListRestricted();
5326 #endif /* configUSE_TIMERS */
5327 /*-----------------------------------------------------------*/
5329 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
5331 TCB_t * pxUnblockedTCB;
5334 traceENTER_xTaskRemoveFromEventList( pxEventList );
5336 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
5337 * called from a critical section within an ISR. */
5339 /* The event list is sorted in priority order, so the first in the list can
5340 * be removed as it is known to be the highest priority. Remove the TCB from
5341 * the delayed list, and add it to the ready list.
5343 * If an event is for a queue that is locked then this function will never
5344 * get called - the lock count on the queue will get modified instead. This
5345 * means exclusive access to the event list is guaranteed here.
5347 * This function assumes that a check has already been made to ensure that
5348 * pxEventList is not empty. */
5349 pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
5350 configASSERT( pxUnblockedTCB );
5351 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5353 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5355 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5356 prvAddTaskToReadyList( pxUnblockedTCB );
5358 #if ( configUSE_TICKLESS_IDLE != 0 )
5360 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5361 * might be set to the blocked task's time out time. If the task is
5362 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5363 * normally left unchanged, because it is automatically reset to a new
5364 * value when the tick count equals xNextTaskUnblockTime. However if
5365 * tickless idling is used it might be more important to enter sleep mode
5366 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5367 * ensure it is updated at the earliest possible time. */
5368 prvResetNextTaskUnblockTime();
5374 /* The delayed and ready lists cannot be accessed, so hold this task
5375 * pending until the scheduler is resumed. */
5376 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5379 #if ( configNUMBER_OF_CORES == 1 )
5381 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5383 /* Return true if the task removed from the event list has a higher
5384 * priority than the calling task. This allows the calling task to know if
5385 * it should force a context switch now. */
5388 /* Mark that a yield is pending in case the user is not using the
5389 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5390 xYieldPendings[ 0 ] = pdTRUE;
5397 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5401 #if ( configUSE_PREEMPTION == 1 )
5403 prvYieldForTask( pxUnblockedTCB );
5405 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5410 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5412 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5414 traceRETURN_xTaskRemoveFromEventList( xReturn );
5417 /*-----------------------------------------------------------*/
5419 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5420 const TickType_t xItemValue )
5422 TCB_t * pxUnblockedTCB;
5424 traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5426 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5427 * the event flags implementation. */
5428 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5430 /* Store the new item value in the event list. */
5431 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5433 /* Remove the event list form the event flag. Interrupts do not access
5435 pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
5436 configASSERT( pxUnblockedTCB );
5437 listREMOVE_ITEM( pxEventListItem );
5439 #if ( configUSE_TICKLESS_IDLE != 0 )
5441 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5442 * might be set to the blocked task's time out time. If the task is
5443 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5444 * normally left unchanged, because it is automatically reset to a new
5445 * value when the tick count equals xNextTaskUnblockTime. However if
5446 * tickless idling is used it might be more important to enter sleep mode
5447 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5448 * ensure it is updated at the earliest possible time. */
5449 prvResetNextTaskUnblockTime();
5453 /* Remove the task from the delayed list and add it to the ready list. The
5454 * scheduler is suspended so interrupts will not be accessing the ready
5456 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5457 prvAddTaskToReadyList( pxUnblockedTCB );
5459 #if ( configNUMBER_OF_CORES == 1 )
5461 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5463 /* The unblocked task has a priority above that of the calling task, so
5464 * a context switch is required. This function is called with the
5465 * scheduler suspended so xYieldPending is set so the context switch
5466 * occurs immediately that the scheduler is resumed (unsuspended). */
5467 xYieldPendings[ 0 ] = pdTRUE;
5470 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5472 #if ( configUSE_PREEMPTION == 1 )
5474 taskENTER_CRITICAL();
5476 prvYieldForTask( pxUnblockedTCB );
5478 taskEXIT_CRITICAL();
5482 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5484 traceRETURN_vTaskRemoveFromUnorderedEventList();
5486 /*-----------------------------------------------------------*/
5488 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5490 traceENTER_vTaskSetTimeOutState( pxTimeOut );
5492 configASSERT( pxTimeOut );
5493 taskENTER_CRITICAL();
5495 pxTimeOut->xOverflowCount = xNumOfOverflows;
5496 pxTimeOut->xTimeOnEntering = xTickCount;
5498 taskEXIT_CRITICAL();
5500 traceRETURN_vTaskSetTimeOutState();
5502 /*-----------------------------------------------------------*/
5504 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5506 traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5508 /* For internal use only as it does not use a critical section. */
5509 pxTimeOut->xOverflowCount = xNumOfOverflows;
5510 pxTimeOut->xTimeOnEntering = xTickCount;
5512 traceRETURN_vTaskInternalSetTimeOutState();
5514 /*-----------------------------------------------------------*/
5516 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5517 TickType_t * const pxTicksToWait )
5521 traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5523 configASSERT( pxTimeOut );
5524 configASSERT( pxTicksToWait );
5526 taskENTER_CRITICAL();
5528 /* Minor optimisation. The tick count cannot change in this block. */
5529 const TickType_t xConstTickCount = xTickCount;
5530 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5532 #if ( INCLUDE_xTaskAbortDelay == 1 )
5533 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5535 /* The delay was aborted, which is not the same as a time out,
5536 * but has the same result. */
5537 pxCurrentTCB->ucDelayAborted = pdFALSE;
5543 #if ( INCLUDE_vTaskSuspend == 1 )
5544 if( *pxTicksToWait == portMAX_DELAY )
5546 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5547 * specified is the maximum block time then the task should block
5548 * indefinitely, and therefore never time out. */
5554 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
5556 /* The tick count is greater than the time at which
5557 * vTaskSetTimeout() was called, but has also overflowed since
5558 * vTaskSetTimeOut() was called. It must have wrapped all the way
5559 * around and gone past again. This passed since vTaskSetTimeout()
5562 *pxTicksToWait = ( TickType_t ) 0;
5564 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
5566 /* Not a genuine timeout. Adjust parameters for time remaining. */
5567 *pxTicksToWait -= xElapsedTime;
5568 vTaskInternalSetTimeOutState( pxTimeOut );
5573 *pxTicksToWait = ( TickType_t ) 0;
5577 taskEXIT_CRITICAL();
5579 traceRETURN_xTaskCheckForTimeOut( xReturn );
5583 /*-----------------------------------------------------------*/
5585 void vTaskMissedYield( void )
5587 traceENTER_vTaskMissedYield();
5589 /* Must be called from within a critical section. */
5590 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5592 traceRETURN_vTaskMissedYield();
5594 /*-----------------------------------------------------------*/
5596 #if ( configUSE_TRACE_FACILITY == 1 )
5598 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5600 UBaseType_t uxReturn;
5601 TCB_t const * pxTCB;
5603 traceENTER_uxTaskGetTaskNumber( xTask );
5608 uxReturn = pxTCB->uxTaskNumber;
5615 traceRETURN_uxTaskGetTaskNumber( uxReturn );
5620 #endif /* configUSE_TRACE_FACILITY */
5621 /*-----------------------------------------------------------*/
5623 #if ( configUSE_TRACE_FACILITY == 1 )
5625 void vTaskSetTaskNumber( TaskHandle_t xTask,
5626 const UBaseType_t uxHandle )
5630 traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5635 pxTCB->uxTaskNumber = uxHandle;
5638 traceRETURN_vTaskSetTaskNumber();
5641 #endif /* configUSE_TRACE_FACILITY */
5642 /*-----------------------------------------------------------*/
5645 * -----------------------------------------------------------
5646 * The passive idle task.
5647 * ----------------------------------------------------------
5649 * The passive idle task is used for all the additional cores in a SMP
5650 * system. There must be only 1 active idle task and the rest are passive
5653 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5654 * language extensions. The equivalent prototype for this function is:
5656 * void prvPassiveIdleTask( void *pvParameters );
5659 #if ( configNUMBER_OF_CORES > 1 )
5660 static portTASK_FUNCTION( prvPassiveIdleTask, pvParameters )
5662 ( void ) pvParameters;
5666 for( ; configCONTROL_INFINITE_LOOP(); )
5668 #if ( configUSE_PREEMPTION == 0 )
5670 /* If we are not using preemption we keep forcing a task switch to
5671 * see if any other task has become available. If we are using
5672 * preemption we don't need to do this as any task becoming available
5673 * will automatically get the processor anyway. */
5676 #endif /* configUSE_PREEMPTION */
5678 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5680 /* When using preemption tasks of equal priority will be
5681 * timesliced. If a task that is sharing the idle priority is ready
5682 * to run then the idle task should yield before the end of the
5685 * A critical region is not required here as we are just reading from
5686 * the list, and an occasional incorrect value will not matter. If
5687 * the ready list at the idle priority contains one more task than the
5688 * number of idle tasks, which is equal to the configured numbers of cores
5689 * then a task other than the idle task is ready to execute. */
5690 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5696 mtCOVERAGE_TEST_MARKER();
5699 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5701 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
5703 /* Call the user defined function from within the idle task. This
5704 * allows the application designer to add background functionality
5705 * without the overhead of a separate task.
5707 * This hook is intended to manage core activity such as disabling cores that go idle.
5709 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5710 * CALL A FUNCTION THAT MIGHT BLOCK. */
5711 vApplicationPassiveIdleHook();
5713 #endif /* configUSE_PASSIVE_IDLE_HOOK */
5716 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5719 * -----------------------------------------------------------
5721 * ----------------------------------------------------------
5723 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5724 * language extensions. The equivalent prototype for this function is:
5726 * void prvIdleTask( void *pvParameters );
5730 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5732 /* Stop warnings. */
5733 ( void ) pvParameters;
5735 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5736 * SCHEDULER IS STARTED. **/
5738 /* In case a task that has a secure context deletes itself, in which case
5739 * the idle task is responsible for deleting the task's secure context, if
5741 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5743 #if ( configNUMBER_OF_CORES > 1 )
5745 /* SMP all cores start up in the idle task. This initial yield gets the application
5749 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5751 for( ; configCONTROL_INFINITE_LOOP(); )
5753 /* See if any tasks have deleted themselves - if so then the idle task
5754 * is responsible for freeing the deleted task's TCB and stack. */
5755 prvCheckTasksWaitingTermination();
5757 #if ( configUSE_PREEMPTION == 0 )
5759 /* If we are not using preemption we keep forcing a task switch to
5760 * see if any other task has become available. If we are using
5761 * preemption we don't need to do this as any task becoming available
5762 * will automatically get the processor anyway. */
5765 #endif /* configUSE_PREEMPTION */
5767 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5769 /* When using preemption tasks of equal priority will be
5770 * timesliced. If a task that is sharing the idle priority is ready
5771 * to run then the idle task should yield before the end of the
5774 * A critical region is not required here as we are just reading from
5775 * the list, and an occasional incorrect value will not matter. If
5776 * the ready list at the idle priority contains one more task than the
5777 * number of idle tasks, which is equal to the configured numbers of cores
5778 * then a task other than the idle task is ready to execute. */
5779 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5785 mtCOVERAGE_TEST_MARKER();
5788 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5790 #if ( configUSE_IDLE_HOOK == 1 )
5792 /* Call the user defined function from within the idle task. */
5793 vApplicationIdleHook();
5795 #endif /* configUSE_IDLE_HOOK */
5797 /* This conditional compilation should use inequality to 0, not equality
5798 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5799 * user defined low power mode implementations require
5800 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5801 #if ( configUSE_TICKLESS_IDLE != 0 )
5803 TickType_t xExpectedIdleTime;
5805 /* It is not desirable to suspend then resume the scheduler on
5806 * each iteration of the idle task. Therefore, a preliminary
5807 * test of the expected idle time is performed without the
5808 * scheduler suspended. The result here is not necessarily
5810 xExpectedIdleTime = prvGetExpectedIdleTime();
5812 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5816 /* Now the scheduler is suspended, the expected idle
5817 * time can be sampled again, and this time its value can
5819 configASSERT( xNextTaskUnblockTime >= xTickCount );
5820 xExpectedIdleTime = prvGetExpectedIdleTime();
5822 /* Define the following macro to set xExpectedIdleTime to 0
5823 * if the application does not want
5824 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5825 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5827 if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5829 traceLOW_POWER_IDLE_BEGIN();
5830 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5831 traceLOW_POWER_IDLE_END();
5835 mtCOVERAGE_TEST_MARKER();
5838 ( void ) xTaskResumeAll();
5842 mtCOVERAGE_TEST_MARKER();
5845 #endif /* configUSE_TICKLESS_IDLE */
5847 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) )
5849 /* Call the user defined function from within the idle task. This
5850 * allows the application designer to add background functionality
5851 * without the overhead of a separate task.
5853 * This hook is intended to manage core activity such as disabling cores that go idle.
5855 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5856 * CALL A FUNCTION THAT MIGHT BLOCK. */
5857 vApplicationPassiveIdleHook();
5859 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) ) */
5862 /*-----------------------------------------------------------*/
5864 #if ( configUSE_TICKLESS_IDLE != 0 )
5866 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5868 #if ( INCLUDE_vTaskSuspend == 1 )
5869 /* The idle task exists in addition to the application tasks. */
5870 const UBaseType_t uxNonApplicationTasks = configNUMBER_OF_CORES;
5871 #endif /* INCLUDE_vTaskSuspend */
5873 eSleepModeStatus eReturn = eStandardSleep;
5875 traceENTER_eTaskConfirmSleepModeStatus();
5877 /* This function must be called from a critical section. */
5879 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0U )
5881 /* A task was made ready while the scheduler was suspended. */
5882 eReturn = eAbortSleep;
5884 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5886 /* A yield was pended while the scheduler was suspended. */
5887 eReturn = eAbortSleep;
5889 else if( xPendedTicks != 0U )
5891 /* A tick interrupt has already occurred but was held pending
5892 * because the scheduler is suspended. */
5893 eReturn = eAbortSleep;
5896 #if ( INCLUDE_vTaskSuspend == 1 )
5897 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5899 /* If all the tasks are in the suspended list (which might mean they
5900 * have an infinite block time rather than actually being suspended)
5901 * then it is safe to turn all clocks off and just wait for external
5903 eReturn = eNoTasksWaitingTimeout;
5905 #endif /* INCLUDE_vTaskSuspend */
5908 mtCOVERAGE_TEST_MARKER();
5911 traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5916 #endif /* configUSE_TICKLESS_IDLE */
5917 /*-----------------------------------------------------------*/
5919 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5921 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5927 traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
5929 if( ( xIndex >= 0 ) &&
5930 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5932 pxTCB = prvGetTCBFromHandle( xTaskToSet );
5933 configASSERT( pxTCB != NULL );
5934 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5937 traceRETURN_vTaskSetThreadLocalStoragePointer();
5940 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5941 /*-----------------------------------------------------------*/
5943 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5945 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5948 void * pvReturn = NULL;
5951 traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
5953 if( ( xIndex >= 0 ) &&
5954 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5956 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
5957 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
5964 traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
5969 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5970 /*-----------------------------------------------------------*/
5972 #if ( portUSING_MPU_WRAPPERS == 1 )
5974 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
5975 const MemoryRegion_t * const pxRegions )
5979 traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
5981 /* If null is passed in here then we are modifying the MPU settings of
5982 * the calling task. */
5983 pxTCB = prvGetTCBFromHandle( xTaskToModify );
5985 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
5987 traceRETURN_vTaskAllocateMPURegions();
5990 #endif /* portUSING_MPU_WRAPPERS */
5991 /*-----------------------------------------------------------*/
5993 static void prvInitialiseTaskLists( void )
5995 UBaseType_t uxPriority;
5997 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
5999 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
6002 vListInitialise( &xDelayedTaskList1 );
6003 vListInitialise( &xDelayedTaskList2 );
6004 vListInitialise( &xPendingReadyList );
6006 #if ( INCLUDE_vTaskDelete == 1 )
6008 vListInitialise( &xTasksWaitingTermination );
6010 #endif /* INCLUDE_vTaskDelete */
6012 #if ( INCLUDE_vTaskSuspend == 1 )
6014 vListInitialise( &xSuspendedTaskList );
6016 #endif /* INCLUDE_vTaskSuspend */
6018 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
6020 pxDelayedTaskList = &xDelayedTaskList1;
6021 pxOverflowDelayedTaskList = &xDelayedTaskList2;
6023 /*-----------------------------------------------------------*/
6025 static void prvCheckTasksWaitingTermination( void )
6027 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
6029 #if ( INCLUDE_vTaskDelete == 1 )
6033 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
6034 * being called too often in the idle task. */
6035 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6037 #if ( configNUMBER_OF_CORES == 1 )
6039 taskENTER_CRITICAL();
6042 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
6043 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6044 --uxCurrentNumberOfTasks;
6045 --uxDeletedTasksWaitingCleanUp;
6048 taskEXIT_CRITICAL();
6050 prvDeleteTCB( pxTCB );
6052 #else /* #if( configNUMBER_OF_CORES == 1 ) */
6056 taskENTER_CRITICAL();
6058 /* For SMP, multiple idles can be running simultaneously
6059 * and we need to check that other idles did not cleanup while we were
6060 * waiting to enter the critical section. */
6061 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6063 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) ); /*lint !e9079 void * is used as this macro is used with timers too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
6065 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
6067 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6068 --uxCurrentNumberOfTasks;
6069 --uxDeletedTasksWaitingCleanUp;
6073 /* The TCB to be deleted still has not yet been switched out
6074 * by the scheduler, so we will just exit this loop early and
6075 * try again next time. */
6076 taskEXIT_CRITICAL();
6081 taskEXIT_CRITICAL();
6085 prvDeleteTCB( pxTCB );
6088 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
6091 #endif /* INCLUDE_vTaskDelete */
6093 /*-----------------------------------------------------------*/
6095 #if ( configUSE_TRACE_FACILITY == 1 )
6097 void vTaskGetInfo( TaskHandle_t xTask,
6098 TaskStatus_t * pxTaskStatus,
6099 BaseType_t xGetFreeStackSpace,
6104 traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
6106 /* xTask is NULL then get the state of the calling task. */
6107 pxTCB = prvGetTCBFromHandle( xTask );
6109 pxTaskStatus->xHandle = pxTCB;
6110 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
6111 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
6112 pxTaskStatus->pxStackBase = pxTCB->pxStack;
6113 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
6114 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
6115 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
6117 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
6119 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
6121 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
6125 #if ( configUSE_MUTEXES == 1 )
6127 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
6131 pxTaskStatus->uxBasePriority = 0;
6135 #if ( configGENERATE_RUN_TIME_STATS == 1 )
6137 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
6141 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
6145 /* Obtaining the task state is a little fiddly, so is only done if the
6146 * value of eState passed into this function is eInvalid - otherwise the
6147 * state is just set to whatever is passed in. */
6148 if( eState != eInvalid )
6150 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6152 pxTaskStatus->eCurrentState = eRunning;
6156 pxTaskStatus->eCurrentState = eState;
6158 #if ( INCLUDE_vTaskSuspend == 1 )
6160 /* If the task is in the suspended list then there is a
6161 * chance it is actually just blocked indefinitely - so really
6162 * it should be reported as being in the Blocked state. */
6163 if( eState == eSuspended )
6167 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
6169 pxTaskStatus->eCurrentState = eBlocked;
6175 /* The task does not appear on the event list item of
6176 * and of the RTOS objects, but could still be in the
6177 * blocked state if it is waiting on its notification
6178 * rather than waiting on an object. If not, is
6180 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
6182 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
6184 pxTaskStatus->eCurrentState = eBlocked;
6190 ( void ) xTaskResumeAll();
6193 #endif /* INCLUDE_vTaskSuspend */
6195 /* Tasks can be in pending ready list and other state list at the
6196 * same time. These tasks are in ready state no matter what state
6197 * list the task is in. */
6198 taskENTER_CRITICAL();
6200 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
6202 pxTaskStatus->eCurrentState = eReady;
6205 taskEXIT_CRITICAL();
6210 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
6213 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
6214 * parameter is provided to allow it to be skipped. */
6215 if( xGetFreeStackSpace != pdFALSE )
6217 #if ( portSTACK_GROWTH > 0 )
6219 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
6223 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
6229 pxTaskStatus->usStackHighWaterMark = 0;
6232 traceRETURN_vTaskGetInfo();
6235 #endif /* configUSE_TRACE_FACILITY */
6236 /*-----------------------------------------------------------*/
6238 #if ( configUSE_TRACE_FACILITY == 1 )
6240 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
6244 configLIST_VOLATILE TCB_t * pxNextTCB;
6245 configLIST_VOLATILE TCB_t * pxFirstTCB;
6246 UBaseType_t uxTask = 0;
6248 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
6250 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
6252 /* Populate an TaskStatus_t structure within the
6253 * pxTaskStatusArray array for each task that is referenced from
6254 * pxList. See the definition of TaskStatus_t in task.h for the
6255 * meaning of each TaskStatus_t structure member. */
6258 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
6259 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
6261 } while( pxNextTCB != pxFirstTCB );
6265 mtCOVERAGE_TEST_MARKER();
6271 #endif /* configUSE_TRACE_FACILITY */
6272 /*-----------------------------------------------------------*/
6274 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
6276 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
6278 uint32_t ulCount = 0U;
6280 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
6282 pucStackByte -= portSTACK_GROWTH;
6286 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
6288 return ( configSTACK_DEPTH_TYPE ) ulCount;
6291 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
6292 /*-----------------------------------------------------------*/
6294 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
6296 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
6297 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
6298 * user to determine the return type. It gets around the problem of the value
6299 * overflowing on 8-bit types without breaking backward compatibility for
6300 * applications that expect an 8-bit return type. */
6301 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
6304 uint8_t * pucEndOfStack;
6305 configSTACK_DEPTH_TYPE uxReturn;
6307 traceENTER_uxTaskGetStackHighWaterMark2( xTask );
6309 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
6310 * the same except for their return type. Using configSTACK_DEPTH_TYPE
6311 * allows the user to determine the return type. It gets around the
6312 * problem of the value overflowing on 8-bit types without breaking
6313 * backward compatibility for applications that expect an 8-bit return
6316 pxTCB = prvGetTCBFromHandle( xTask );
6318 #if portSTACK_GROWTH < 0
6320 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6324 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6328 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
6330 traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
6335 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
6336 /*-----------------------------------------------------------*/
6338 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
6340 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
6343 uint8_t * pucEndOfStack;
6344 UBaseType_t uxReturn;
6346 traceENTER_uxTaskGetStackHighWaterMark( xTask );
6348 pxTCB = prvGetTCBFromHandle( xTask );
6350 #if portSTACK_GROWTH < 0
6352 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6356 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6360 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
6362 traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6367 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6368 /*-----------------------------------------------------------*/
6370 #if ( INCLUDE_vTaskDelete == 1 )
6372 static void prvDeleteTCB( TCB_t * pxTCB )
6374 /* This call is required specifically for the TriCore port. It must be
6375 * above the vPortFree() calls. The call is also used by ports/demos that
6376 * want to allocate and clean RAM statically. */
6377 portCLEAN_UP_TCB( pxTCB );
6379 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6381 /* Free up the memory allocated for the task's TLS Block. */
6382 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6386 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6388 /* The task can only have been allocated dynamically - free both
6389 * the stack and TCB. */
6390 vPortFreeStack( pxTCB->pxStack );
6393 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
6395 /* The task could have been allocated statically or dynamically, so
6396 * check what was statically allocated before trying to free the
6398 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6400 /* Both the stack and TCB were allocated dynamically, so both
6402 vPortFreeStack( pxTCB->pxStack );
6405 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6407 /* Only the stack was statically allocated, so the TCB is the
6408 * only memory that must be freed. */
6413 /* Neither the stack nor the TCB were allocated dynamically, so
6414 * nothing needs to be freed. */
6415 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6416 mtCOVERAGE_TEST_MARKER();
6419 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6422 #endif /* INCLUDE_vTaskDelete */
6423 /*-----------------------------------------------------------*/
6425 static void prvResetNextTaskUnblockTime( void )
6427 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6429 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
6430 * the maximum possible value so it is extremely unlikely that the
6431 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6432 * there is an item in the delayed list. */
6433 xNextTaskUnblockTime = portMAX_DELAY;
6437 /* The new current delayed list is not empty, get the value of
6438 * the item at the head of the delayed list. This is the time at
6439 * which the task at the head of the delayed list should be removed
6440 * from the Blocked state. */
6441 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6444 /*-----------------------------------------------------------*/
6446 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6448 #if ( configNUMBER_OF_CORES == 1 )
6449 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6451 TaskHandle_t xReturn;
6453 traceENTER_xTaskGetCurrentTaskHandle();
6455 /* A critical section is not required as this is not called from
6456 * an interrupt and the current TCB will always be the same for any
6457 * individual execution thread. */
6458 xReturn = pxCurrentTCB;
6460 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6464 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6465 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6467 TaskHandle_t xReturn;
6468 UBaseType_t uxSavedInterruptStatus;
6470 traceENTER_xTaskGetCurrentTaskHandle();
6472 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6474 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6476 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6478 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6483 TaskHandle_t xTaskGetCurrentTaskHandleForCore( BaseType_t xCoreID )
6485 TaskHandle_t xReturn = NULL;
6487 traceENTER_xTaskGetCurrentTaskHandleForCore( xCoreID );
6489 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6491 xReturn = pxCurrentTCBs[ xCoreID ];
6494 traceRETURN_xTaskGetCurrentTaskHandleForCore( xReturn );
6498 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6500 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
6501 /*-----------------------------------------------------------*/
6503 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6505 BaseType_t xTaskGetSchedulerState( void )
6509 traceENTER_xTaskGetSchedulerState();
6511 if( xSchedulerRunning == pdFALSE )
6513 xReturn = taskSCHEDULER_NOT_STARTED;
6517 #if ( configNUMBER_OF_CORES > 1 )
6518 taskENTER_CRITICAL();
6521 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6523 xReturn = taskSCHEDULER_RUNNING;
6527 xReturn = taskSCHEDULER_SUSPENDED;
6530 #if ( configNUMBER_OF_CORES > 1 )
6531 taskEXIT_CRITICAL();
6535 traceRETURN_xTaskGetSchedulerState( xReturn );
6540 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6541 /*-----------------------------------------------------------*/
6543 #if ( configUSE_MUTEXES == 1 )
6545 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6547 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6548 BaseType_t xReturn = pdFALSE;
6550 traceENTER_xTaskPriorityInherit( pxMutexHolder );
6552 /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6553 * inheritance is not applied in this scenario. */
6554 if( pxMutexHolder != NULL )
6556 /* If the holder of the mutex has a priority below the priority of
6557 * the task attempting to obtain the mutex then it will temporarily
6558 * inherit the priority of the task attempting to obtain the mutex. */
6559 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6561 /* Adjust the mutex holder state to account for its new
6562 * priority. Only reset the event list item value if the value is
6563 * not being used for anything else. */
6564 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0UL ) )
6566 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. */
6570 mtCOVERAGE_TEST_MARKER();
6573 /* If the task being modified is in the ready state it will need
6574 * to be moved into a new list. */
6575 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6577 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6579 /* It is known that the task is in its ready list so
6580 * there is no need to check again and the port level
6581 * reset macro can be called directly. */
6582 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6586 mtCOVERAGE_TEST_MARKER();
6589 /* Inherit the priority before being moved into the new list. */
6590 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6591 prvAddTaskToReadyList( pxMutexHolderTCB );
6592 #if ( configNUMBER_OF_CORES > 1 )
6594 /* The priority of the task is raised. Yield for this task
6595 * if it is not running. */
6596 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6598 prvYieldForTask( pxMutexHolderTCB );
6601 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6605 /* Just inherit the priority. */
6606 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6609 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6611 /* Inheritance occurred. */
6616 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6618 /* The base priority of the mutex holder is lower than the
6619 * priority of the task attempting to take the mutex, but the
6620 * current priority of the mutex holder is not lower than the
6621 * priority of the task attempting to take the mutex.
6622 * Therefore the mutex holder must have already inherited a
6623 * priority, but inheritance would have occurred if that had
6624 * not been the case. */
6629 mtCOVERAGE_TEST_MARKER();
6635 mtCOVERAGE_TEST_MARKER();
6638 traceRETURN_xTaskPriorityInherit( xReturn );
6643 #endif /* configUSE_MUTEXES */
6644 /*-----------------------------------------------------------*/
6646 #if ( configUSE_MUTEXES == 1 )
6648 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6650 TCB_t * const pxTCB = pxMutexHolder;
6651 BaseType_t xReturn = pdFALSE;
6653 traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6655 if( pxMutexHolder != NULL )
6657 /* A task can only have an inherited priority if it holds the mutex.
6658 * If the mutex is held by a task then it cannot be given from an
6659 * interrupt, and if a mutex is given by the holding task then it must
6660 * be the running state task. */
6661 configASSERT( pxTCB == pxCurrentTCB );
6662 configASSERT( pxTCB->uxMutexesHeld );
6663 ( pxTCB->uxMutexesHeld )--;
6665 /* Has the holder of the mutex inherited the priority of another
6667 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6669 /* Only disinherit if no other mutexes are held. */
6670 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6672 /* A task can only have an inherited priority if it holds
6673 * the mutex. If the mutex is held by a task then it cannot be
6674 * given from an interrupt, and if a mutex is given by the
6675 * holding task then it must be the running state task. Remove
6676 * the holding task from the ready list. */
6677 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6679 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6683 mtCOVERAGE_TEST_MARKER();
6686 /* Disinherit the priority before adding the task into the
6687 * new ready list. */
6688 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6689 pxTCB->uxPriority = pxTCB->uxBasePriority;
6691 /* Reset the event list item value. It cannot be in use for
6692 * any other purpose if this task is running, and it must be
6693 * running to give back the mutex. */
6694 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. */
6695 prvAddTaskToReadyList( pxTCB );
6696 #if ( configNUMBER_OF_CORES > 1 )
6698 /* The priority of the task is dropped. Yield the core on
6699 * which the task is running. */
6700 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6702 prvYieldCore( pxTCB->xTaskRunState );
6705 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6707 /* Return true to indicate that a context switch is required.
6708 * This is only actually required in the corner case whereby
6709 * multiple mutexes were held and the mutexes were given back
6710 * in an order different to that in which they were taken.
6711 * If a context switch did not occur when the first mutex was
6712 * returned, even if a task was waiting on it, then a context
6713 * switch should occur when the last mutex is returned whether
6714 * a task is waiting on it or not. */
6719 mtCOVERAGE_TEST_MARKER();
6724 mtCOVERAGE_TEST_MARKER();
6729 mtCOVERAGE_TEST_MARKER();
6732 traceRETURN_xTaskPriorityDisinherit( xReturn );
6737 #endif /* configUSE_MUTEXES */
6738 /*-----------------------------------------------------------*/
6740 #if ( configUSE_MUTEXES == 1 )
6742 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6743 UBaseType_t uxHighestPriorityWaitingTask )
6745 TCB_t * const pxTCB = pxMutexHolder;
6746 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6747 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6749 traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6751 if( pxMutexHolder != NULL )
6753 /* If pxMutexHolder is not NULL then the holder must hold at least
6755 configASSERT( pxTCB->uxMutexesHeld );
6757 /* Determine the priority to which the priority of the task that
6758 * holds the mutex should be set. This will be the greater of the
6759 * holding task's base priority and the priority of the highest
6760 * priority task that is waiting to obtain the mutex. */
6761 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6763 uxPriorityToUse = uxHighestPriorityWaitingTask;
6767 uxPriorityToUse = pxTCB->uxBasePriority;
6770 /* Does the priority need to change? */
6771 if( pxTCB->uxPriority != uxPriorityToUse )
6773 /* Only disinherit if no other mutexes are held. This is a
6774 * simplification in the priority inheritance implementation. If
6775 * the task that holds the mutex is also holding other mutexes then
6776 * the other mutexes may have caused the priority inheritance. */
6777 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6779 /* If a task has timed out because it already holds the
6780 * mutex it was trying to obtain then it cannot of inherited
6781 * its own priority. */
6782 configASSERT( pxTCB != pxCurrentTCB );
6784 /* Disinherit the priority, remembering the previous
6785 * priority to facilitate determining the subject task's
6787 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6788 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6789 pxTCB->uxPriority = uxPriorityToUse;
6791 /* Only reset the event list item value if the value is not
6792 * being used for anything else. */
6793 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0UL ) )
6795 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. */
6799 mtCOVERAGE_TEST_MARKER();
6802 /* If the running task is not the task that holds the mutex
6803 * then the task that holds the mutex could be in either the
6804 * Ready, Blocked or Suspended states. Only remove the task
6805 * from its current state list if it is in the Ready state as
6806 * the task's priority is going to change and there is one
6807 * Ready list per priority. */
6808 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6810 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6812 /* It is known that the task is in its ready list so
6813 * there is no need to check again and the port level
6814 * reset macro can be called directly. */
6815 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6819 mtCOVERAGE_TEST_MARKER();
6822 prvAddTaskToReadyList( pxTCB );
6823 #if ( configNUMBER_OF_CORES > 1 )
6825 /* The priority of the task is dropped. Yield the core on
6826 * which the task is running. */
6827 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6829 prvYieldCore( pxTCB->xTaskRunState );
6832 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6836 mtCOVERAGE_TEST_MARKER();
6841 mtCOVERAGE_TEST_MARKER();
6846 mtCOVERAGE_TEST_MARKER();
6851 mtCOVERAGE_TEST_MARKER();
6854 traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6857 #endif /* configUSE_MUTEXES */
6858 /*-----------------------------------------------------------*/
6860 #if ( configNUMBER_OF_CORES > 1 )
6862 /* If not in a critical section then yield immediately.
6863 * Otherwise set xYieldPendings to true to wait to
6864 * yield until exiting the critical section.
6866 void vTaskYieldWithinAPI( void )
6868 traceENTER_vTaskYieldWithinAPI();
6870 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6876 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6879 traceRETURN_vTaskYieldWithinAPI();
6881 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6883 /*-----------------------------------------------------------*/
6885 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6887 void vTaskEnterCritical( void )
6889 traceENTER_vTaskEnterCritical();
6891 portDISABLE_INTERRUPTS();
6893 if( xSchedulerRunning != pdFALSE )
6895 ( pxCurrentTCB->uxCriticalNesting )++;
6897 /* This is not the interrupt safe version of the enter critical
6898 * function so assert() if it is being called from an interrupt
6899 * context. Only API functions that end in "FromISR" can be used in an
6900 * interrupt. Only assert if the critical nesting count is 1 to
6901 * protect against recursive calls if the assert function also uses a
6902 * critical section. */
6903 if( pxCurrentTCB->uxCriticalNesting == 1U )
6905 portASSERT_IF_IN_ISR();
6910 mtCOVERAGE_TEST_MARKER();
6913 traceRETURN_vTaskEnterCritical();
6916 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6917 /*-----------------------------------------------------------*/
6919 #if ( configNUMBER_OF_CORES > 1 )
6921 void vTaskEnterCritical( void )
6923 traceENTER_vTaskEnterCritical();
6925 portDISABLE_INTERRUPTS();
6927 if( xSchedulerRunning != pdFALSE )
6929 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6931 portGET_TASK_LOCK();
6935 portINCREMENT_CRITICAL_NESTING_COUNT();
6937 /* This is not the interrupt safe version of the enter critical
6938 * function so assert() if it is being called from an interrupt
6939 * context. Only API functions that end in "FromISR" can be used in an
6940 * interrupt. Only assert if the critical nesting count is 1 to
6941 * protect against recursive calls if the assert function also uses a
6942 * critical section. */
6943 if( portGET_CRITICAL_NESTING_COUNT() == 1U )
6945 portASSERT_IF_IN_ISR();
6947 if( uxSchedulerSuspended == 0U )
6949 /* The only time there would be a problem is if this is called
6950 * before a context switch and vTaskExitCritical() is called
6951 * after pxCurrentTCB changes. Therefore this should not be
6952 * used within vTaskSwitchContext(). */
6953 prvCheckForRunStateChange();
6959 mtCOVERAGE_TEST_MARKER();
6962 traceRETURN_vTaskEnterCritical();
6965 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6967 /*-----------------------------------------------------------*/
6969 #if ( configNUMBER_OF_CORES > 1 )
6971 UBaseType_t vTaskEnterCriticalFromISR( void )
6973 UBaseType_t uxSavedInterruptStatus = 0;
6975 traceENTER_vTaskEnterCriticalFromISR();
6977 if( xSchedulerRunning != pdFALSE )
6979 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6981 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6986 portINCREMENT_CRITICAL_NESTING_COUNT();
6990 mtCOVERAGE_TEST_MARKER();
6993 traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
6995 return uxSavedInterruptStatus;
6998 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6999 /*-----------------------------------------------------------*/
7001 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
7003 void vTaskExitCritical( void )
7005 traceENTER_vTaskExitCritical();
7007 if( xSchedulerRunning != pdFALSE )
7009 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
7010 * does not match a previous call to vTaskEnterCritical(). */
7011 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
7013 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7014 * to exit critical section from ISR. */
7015 portASSERT_IF_IN_ISR();
7017 if( pxCurrentTCB->uxCriticalNesting > 0U )
7019 ( pxCurrentTCB->uxCriticalNesting )--;
7021 if( pxCurrentTCB->uxCriticalNesting == 0U )
7023 portENABLE_INTERRUPTS();
7027 mtCOVERAGE_TEST_MARKER();
7032 mtCOVERAGE_TEST_MARKER();
7037 mtCOVERAGE_TEST_MARKER();
7040 traceRETURN_vTaskExitCritical();
7043 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
7044 /*-----------------------------------------------------------*/
7046 #if ( configNUMBER_OF_CORES > 1 )
7048 void vTaskExitCritical( void )
7050 traceENTER_vTaskExitCritical();
7052 if( xSchedulerRunning != pdFALSE )
7054 /* If critical nesting count is zero then this function
7055 * does not match a previous call to vTaskEnterCritical(). */
7056 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7058 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7059 * to exit critical section from ISR. */
7060 portASSERT_IF_IN_ISR();
7062 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7064 portDECREMENT_CRITICAL_NESTING_COUNT();
7066 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7068 BaseType_t xYieldCurrentTask;
7070 /* Get the xYieldPending stats inside the critical section. */
7071 xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
7073 portRELEASE_ISR_LOCK();
7074 portRELEASE_TASK_LOCK();
7075 portENABLE_INTERRUPTS();
7077 /* When a task yields in a critical section it just sets
7078 * xYieldPending to true. So now that we have exited the
7079 * critical section check if xYieldPending is true, and
7081 if( xYieldCurrentTask != pdFALSE )
7088 mtCOVERAGE_TEST_MARKER();
7093 mtCOVERAGE_TEST_MARKER();
7098 mtCOVERAGE_TEST_MARKER();
7101 traceRETURN_vTaskExitCritical();
7104 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7105 /*-----------------------------------------------------------*/
7107 #if ( configNUMBER_OF_CORES > 1 )
7109 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
7111 traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
7113 if( xSchedulerRunning != pdFALSE )
7115 /* If critical nesting count is zero then this function
7116 * does not match a previous call to vTaskEnterCritical(). */
7117 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7119 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7121 portDECREMENT_CRITICAL_NESTING_COUNT();
7123 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7125 portRELEASE_ISR_LOCK();
7126 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
7130 mtCOVERAGE_TEST_MARKER();
7135 mtCOVERAGE_TEST_MARKER();
7140 mtCOVERAGE_TEST_MARKER();
7143 traceRETURN_vTaskExitCriticalFromISR();
7146 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7147 /*-----------------------------------------------------------*/
7149 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
7151 static char * prvWriteNameToBuffer( char * pcBuffer,
7152 const char * pcTaskName )
7156 /* Start by copying the entire string. */
7157 ( void ) strcpy( pcBuffer, pcTaskName );
7159 /* Pad the end of the string with spaces to ensure columns line up when
7161 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
7163 pcBuffer[ x ] = ' ';
7167 pcBuffer[ x ] = ( char ) 0x00;
7169 /* Return the new end of string. */
7170 return &( pcBuffer[ x ] );
7173 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
7174 /*-----------------------------------------------------------*/
7176 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
7178 void vTaskListTasks( char * pcWriteBuffer,
7179 size_t uxBufferLength )
7181 TaskStatus_t * pxTaskStatusArray;
7182 size_t uxConsumedBufferLength = 0;
7183 size_t uxCharsWrittenBySnprintf;
7184 int iSnprintfReturnValue;
7185 BaseType_t xOutputBufferFull = pdFALSE;
7186 UBaseType_t uxArraySize, x;
7189 traceENTER_vTaskListTasks( pcWriteBuffer, uxBufferLength );
7194 * This function is provided for convenience only, and is used by many
7195 * of the demo applications. Do not consider it to be part of the
7198 * vTaskListTasks() calls uxTaskGetSystemState(), then formats part of the
7199 * uxTaskGetSystemState() output into a human readable table that
7200 * displays task: names, states, priority, stack usage and task number.
7201 * Stack usage specified as the number of unused StackType_t words stack can hold
7202 * on top of stack - not the number of bytes.
7204 * vTaskListTasks() has a dependency on the snprintf() C library function that
7205 * might bloat the code size, use a lot of stack, and provide different
7206 * results on different platforms. An alternative, tiny, third party,
7207 * and limited functionality implementation of snprintf() is provided in
7208 * many of the FreeRTOS/Demo sub-directories in a file called
7209 * printf-stdarg.c (note printf-stdarg.c does not provide a full
7210 * snprintf() implementation!).
7212 * It is recommended that production systems call uxTaskGetSystemState()
7213 * directly to get access to raw stats data, rather than indirectly
7214 * through a call to vTaskListTasks().
7218 /* Make sure the write buffer does not contain a string. */
7219 *pcWriteBuffer = ( char ) 0x00;
7221 /* Take a snapshot of the number of tasks in case it changes while this
7222 * function is executing. */
7223 uxArraySize = uxCurrentNumberOfTasks;
7225 /* Allocate an array index for each task. NOTE! if
7226 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7227 * equate to NULL. */
7228 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation allocates a struct that has the alignment requirements of a pointer. */
7230 if( pxTaskStatusArray != NULL )
7232 /* Generate the (binary) data. */
7233 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
7235 /* Create a human readable table from the binary data. */
7236 for( x = 0; x < uxArraySize; x++ )
7238 switch( pxTaskStatusArray[ x ].eCurrentState )
7241 cStatus = tskRUNNING_CHAR;
7245 cStatus = tskREADY_CHAR;
7249 cStatus = tskBLOCKED_CHAR;
7253 cStatus = tskSUSPENDED_CHAR;
7257 cStatus = tskDELETED_CHAR;
7260 case eInvalid: /* Fall through. */
7261 default: /* Should not get here, but it is included
7262 * to prevent static checking errors. */
7263 cStatus = ( char ) 0x00;
7267 /* Is there enough space in the buffer to hold task name? */
7268 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7270 /* Write the task name to the string, padding with spaces so it
7271 * can be printed in tabular form more easily. */
7272 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7273 /* Do not count the terminating null character. */
7274 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7276 /* Is there space left in the buffer? -1 is done because snprintf
7277 * writes a terminating null character. So we are essentially
7278 * checking if the buffer has space to write at least one non-null
7280 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7282 /* Write the rest of the string. */
7283 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
7284 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7285 uxBufferLength - uxConsumedBufferLength,
7286 "\t%c\t%u\t%u\t%u\t0x%x\r\n",
7288 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7289 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7290 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber,
7291 ( 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. */
7292 #else /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7293 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7294 uxBufferLength - uxConsumedBufferLength,
7295 "\t%c\t%u\t%u\t%u\r\n",
7297 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7298 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7299 ( 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. */
7300 #endif /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7301 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7303 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7304 pcWriteBuffer += uxCharsWrittenBySnprintf; /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
7308 xOutputBufferFull = pdTRUE;
7313 xOutputBufferFull = pdTRUE;
7316 if( xOutputBufferFull == pdTRUE )
7322 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7323 * is 0 then vPortFree() will be #defined to nothing. */
7324 vPortFree( pxTaskStatusArray );
7328 mtCOVERAGE_TEST_MARKER();
7331 traceRETURN_vTaskListTasks();
7334 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7335 /*----------------------------------------------------------*/
7337 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
7339 void vTaskGetRunTimeStatistics( char * pcWriteBuffer,
7340 size_t uxBufferLength )
7342 TaskStatus_t * pxTaskStatusArray;
7343 size_t uxConsumedBufferLength = 0;
7344 size_t uxCharsWrittenBySnprintf;
7345 int iSnprintfReturnValue;
7346 BaseType_t xOutputBufferFull = pdFALSE;
7347 UBaseType_t uxArraySize, x;
7348 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
7350 traceENTER_vTaskGetRunTimeStatistics( pcWriteBuffer, uxBufferLength );
7355 * This function is provided for convenience only, and is used by many
7356 * of the demo applications. Do not consider it to be part of the
7359 * vTaskGetRunTimeStatistics() calls uxTaskGetSystemState(), then formats part
7360 * of the uxTaskGetSystemState() output into a human readable table that
7361 * displays the amount of time each task has spent in the Running state
7362 * in both absolute and percentage terms.
7364 * vTaskGetRunTimeStatistics() has a dependency on the snprintf() C library
7365 * function that might bloat the code size, use a lot of stack, and
7366 * provide different results on different platforms. An alternative,
7367 * tiny, third party, and limited functionality implementation of
7368 * snprintf() is provided in many of the FreeRTOS/Demo sub-directories in
7369 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
7370 * a full snprintf() implementation!).
7372 * It is recommended that production systems call uxTaskGetSystemState()
7373 * directly to get access to raw stats data, rather than indirectly
7374 * through a call to vTaskGetRunTimeStatistics().
7377 /* Make sure the write buffer does not contain a string. */
7378 *pcWriteBuffer = ( char ) 0x00;
7380 /* Take a snapshot of the number of tasks in case it changes while this
7381 * function is executing. */
7382 uxArraySize = uxCurrentNumberOfTasks;
7384 /* Allocate an array index for each task. NOTE! If
7385 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7386 * equate to NULL. */
7387 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation allocates a struct that has the alignment requirements of a pointer. */
7389 if( pxTaskStatusArray != NULL )
7391 /* Generate the (binary) data. */
7392 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
7394 /* For percentage calculations. */
7395 ulTotalTime /= ( ( configRUN_TIME_COUNTER_TYPE ) 100UL );
7397 /* Avoid divide by zero errors. */
7398 if( ulTotalTime > 0UL )
7400 /* Create a human readable table from the binary data. */
7401 for( x = 0; x < uxArraySize; x++ )
7403 /* What percentage of the total run time has the task used?
7404 * This will always be rounded down to the nearest integer.
7405 * ulTotalRunTime has already been divided by 100. */
7406 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
7408 /* Is there enough space in the buffer to hold task name? */
7409 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7411 /* Write the task name to the string, padding with
7412 * spaces so it can be printed in tabular form more
7414 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7415 /* Do not count the terminating null character. */
7416 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
7418 /* Is there space left in the buffer? -1 is done because snprintf
7419 * writes a terminating null character. So we are essentially
7420 * checking if the buffer has space to write at least one non-null
7422 if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
7424 if( ulStatsAsPercentage > 0UL )
7426 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7428 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7429 uxBufferLength - uxConsumedBufferLength,
7430 "\t%lu\t\t%lu%%\r\n",
7431 pxTaskStatusArray[ x ].ulRunTimeCounter,
7432 ulStatsAsPercentage );
7436 /* sizeof( int ) == sizeof( long ) so a smaller
7437 * printf() library can be used. */
7438 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7439 uxBufferLength - uxConsumedBufferLength,
7441 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter,
7442 ( 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. */
7444 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7448 /* If the percentage is zero here then the task has
7449 * consumed less than 1% of the total run time. */
7450 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7452 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7453 uxBufferLength - uxConsumedBufferLength,
7454 "\t%lu\t\t<1%%\r\n",
7455 pxTaskStatusArray[ x ].ulRunTimeCounter );
7459 /* sizeof( int ) == sizeof( long ) so a smaller
7460 * printf() library can be used. */
7461 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7462 uxBufferLength - uxConsumedBufferLength,
7464 ( 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. */
7466 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7469 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7470 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7471 pcWriteBuffer += uxCharsWrittenBySnprintf; /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
7475 xOutputBufferFull = pdTRUE;
7480 xOutputBufferFull = pdTRUE;
7483 if( xOutputBufferFull == pdTRUE )
7491 mtCOVERAGE_TEST_MARKER();
7494 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7495 * is 0 then vPortFree() will be #defined to nothing. */
7496 vPortFree( pxTaskStatusArray );
7500 mtCOVERAGE_TEST_MARKER();
7503 traceRETURN_vTaskGetRunTimeStatistics();
7506 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7507 /*-----------------------------------------------------------*/
7509 TickType_t uxTaskResetEventItemValue( void )
7511 TickType_t uxReturn;
7513 traceENTER_uxTaskResetEventItemValue();
7515 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7517 /* Reset the event list item to its normal value - so it can be used with
7518 * queues and semaphores. */
7519 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. */
7521 traceRETURN_uxTaskResetEventItemValue( uxReturn );
7525 /*-----------------------------------------------------------*/
7527 #if ( configUSE_MUTEXES == 1 )
7529 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7533 traceENTER_pvTaskIncrementMutexHeldCount();
7535 pxTCB = pxCurrentTCB;
7537 /* If xSemaphoreCreateMutex() is called before any tasks have been created
7538 * then pxCurrentTCB will be NULL. */
7541 ( pxTCB->uxMutexesHeld )++;
7544 traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7549 #endif /* configUSE_MUTEXES */
7550 /*-----------------------------------------------------------*/
7552 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7554 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7555 BaseType_t xClearCountOnExit,
7556 TickType_t xTicksToWait )
7559 BaseType_t xAlreadyYielded;
7561 traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7563 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7565 taskENTER_CRITICAL();
7567 /* Only block if the notification count is not already non-zero. */
7568 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0UL )
7570 /* Mark this task as waiting for a notification. */
7571 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7573 if( xTicksToWait > ( TickType_t ) 0 )
7575 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7577 /* We MUST suspend the scheduler before exiting the critical
7578 * section (i.e. before enabling interrupts).
7580 * If we do not do so, a notification sent from an ISR, which
7581 * happens after exiting the critical section and before
7582 * suspending the scheduler, will get lost. The sequence of
7584 * 1. Exit critical section.
7585 * 2. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7586 * task to the Ready list.
7587 * 3. Suspend scheduler.
7588 * 4. prvAddCurrentTaskToDelayedList moves the task to the
7589 * delayed or suspended list.
7590 * 5. Resume scheduler does not touch the task (because it is
7591 * not on the pendingReady list), effectively losing the
7592 * notification from the ISR.
7594 * The same does not happen when we suspend the scheduler before
7595 * exiting the critical section. The sequence of events in this
7597 * 1. Suspend scheduler.
7598 * 2. Exit critical section.
7599 * 3. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7600 * task to the pendingReady list as the scheduler is
7602 * 4. prvAddCurrentTaskToDelayedList adds the task to delayed or
7603 * suspended list. Note that this operation does not nullify
7604 * the add to pendingReady list done in the above step because
7605 * a different list item, namely xEventListItem, is used for
7606 * adding the task to the pendingReady list. In other words,
7607 * the task still remains on the pendingReady list.
7608 * 5. Resume scheduler moves the task from pendingReady list to
7613 taskEXIT_CRITICAL();
7615 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7617 xAlreadyYielded = xTaskResumeAll();
7619 if( xAlreadyYielded == pdFALSE )
7621 taskYIELD_WITHIN_API();
7625 mtCOVERAGE_TEST_MARKER();
7630 taskEXIT_CRITICAL();
7635 taskEXIT_CRITICAL();
7638 taskENTER_CRITICAL();
7640 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7641 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7643 if( ulReturn != 0UL )
7645 if( xClearCountOnExit != pdFALSE )
7647 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ( uint32_t ) 0UL;
7651 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7656 mtCOVERAGE_TEST_MARKER();
7659 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7661 taskEXIT_CRITICAL();
7663 traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7668 #endif /* configUSE_TASK_NOTIFICATIONS */
7669 /*-----------------------------------------------------------*/
7671 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7673 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7674 uint32_t ulBitsToClearOnEntry,
7675 uint32_t ulBitsToClearOnExit,
7676 uint32_t * pulNotificationValue,
7677 TickType_t xTicksToWait )
7679 BaseType_t xReturn, xAlreadyYielded;
7681 traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7683 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7685 taskENTER_CRITICAL();
7687 /* Only block if a notification is not already pending. */
7688 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7690 /* Clear bits in the task's notification value as bits may get
7691 * set by the notifying task or interrupt. This can be used to
7692 * clear the value to zero. */
7693 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7695 /* Mark this task as waiting for a notification. */
7696 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7698 if( xTicksToWait > ( TickType_t ) 0 )
7700 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7702 /* We MUST suspend the scheduler before exiting the critical
7703 * section (i.e. before enabling interrupts).
7705 * If we do not do so, a notification sent from an ISR, which
7706 * happens after exiting the critical section and before
7707 * suspending the scheduler, will get lost. The sequence of
7709 * 1. Exit critical section.
7710 * 2. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7711 * task to the Ready list.
7712 * 3. Suspend scheduler.
7713 * 4. prvAddCurrentTaskToDelayedList moves the task to the
7714 * delayed or suspended list.
7715 * 5. Resume scheduler does not touch the task (because it is
7716 * not on the pendingReady list), effectively losing the
7717 * notification from the ISR.
7719 * The same does not happen when we suspend the scheduler before
7720 * exiting the critical section. The sequence of events in this
7722 * 1. Suspend scheduler.
7723 * 2. Exit critical section.
7724 * 3. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7725 * task to the pendingReady list as the scheduler is
7727 * 4. prvAddCurrentTaskToDelayedList adds the task to delayed or
7728 * suspended list. Note that this operation does not nullify
7729 * the add to pendingReady list done in the above step because
7730 * a different list item, namely xEventListItem, is used for
7731 * adding the task to the pendingReady list. In other words,
7732 * the task still remains on the pendingReady list.
7733 * 5. Resume scheduler moves the task from pendingReady list to
7738 taskEXIT_CRITICAL();
7740 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7742 xAlreadyYielded = xTaskResumeAll();
7744 if( xAlreadyYielded == pdFALSE )
7746 taskYIELD_WITHIN_API();
7750 mtCOVERAGE_TEST_MARKER();
7755 taskEXIT_CRITICAL();
7760 taskEXIT_CRITICAL();
7763 taskENTER_CRITICAL();
7765 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7767 if( pulNotificationValue != NULL )
7769 /* Output the current notification value, which may or may not
7771 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7774 /* If ucNotifyValue is set then either the task never entered the
7775 * blocked state (because a notification was already pending) or the
7776 * task unblocked because of a notification. Otherwise the task
7777 * unblocked because of a timeout. */
7778 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7780 /* A notification was not received. */
7785 /* A notification was already pending or a notification was
7786 * received while the task was waiting. */
7787 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7791 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7793 taskEXIT_CRITICAL();
7795 traceRETURN_xTaskGenericNotifyWait( xReturn );
7800 #endif /* configUSE_TASK_NOTIFICATIONS */
7801 /*-----------------------------------------------------------*/
7803 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7805 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7806 UBaseType_t uxIndexToNotify,
7808 eNotifyAction eAction,
7809 uint32_t * pulPreviousNotificationValue )
7812 BaseType_t xReturn = pdPASS;
7813 uint8_t ucOriginalNotifyState;
7815 traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7817 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7818 configASSERT( xTaskToNotify );
7819 pxTCB = xTaskToNotify;
7821 taskENTER_CRITICAL();
7823 if( pulPreviousNotificationValue != NULL )
7825 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7828 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7830 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7835 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7839 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7842 case eSetValueWithOverwrite:
7843 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7846 case eSetValueWithoutOverwrite:
7848 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7850 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7854 /* The value could not be written to the task. */
7862 /* The task is being notified without its notify value being
7868 /* Should not get here if all enums are handled.
7869 * Artificially force an assert by testing a value the
7870 * compiler can't assume is const. */
7871 configASSERT( xTickCount == ( TickType_t ) 0 );
7876 traceTASK_NOTIFY( uxIndexToNotify );
7878 /* If the task is in the blocked state specifically to wait for a
7879 * notification then unblock it now. */
7880 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7882 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7883 prvAddTaskToReadyList( pxTCB );
7885 /* The task should not have been on an event list. */
7886 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7888 #if ( configUSE_TICKLESS_IDLE != 0 )
7890 /* If a task is blocked waiting for a notification then
7891 * xNextTaskUnblockTime might be set to the blocked task's time
7892 * out time. If the task is unblocked for a reason other than
7893 * a timeout xNextTaskUnblockTime is normally left unchanged,
7894 * because it will automatically get reset to a new value when
7895 * the tick count equals xNextTaskUnblockTime. However if
7896 * tickless idling is used it might be more important to enter
7897 * sleep mode at the earliest possible time - so reset
7898 * xNextTaskUnblockTime here to ensure it is updated at the
7899 * earliest possible time. */
7900 prvResetNextTaskUnblockTime();
7904 /* Check if the notified task has a priority above the currently
7905 * executing task. */
7906 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7910 mtCOVERAGE_TEST_MARKER();
7913 taskEXIT_CRITICAL();
7915 traceRETURN_xTaskGenericNotify( xReturn );
7920 #endif /* configUSE_TASK_NOTIFICATIONS */
7921 /*-----------------------------------------------------------*/
7923 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7925 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
7926 UBaseType_t uxIndexToNotify,
7928 eNotifyAction eAction,
7929 uint32_t * pulPreviousNotificationValue,
7930 BaseType_t * pxHigherPriorityTaskWoken )
7933 uint8_t ucOriginalNotifyState;
7934 BaseType_t xReturn = pdPASS;
7935 UBaseType_t uxSavedInterruptStatus;
7937 traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
7939 configASSERT( xTaskToNotify );
7940 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7942 /* RTOS ports that support interrupt nesting have the concept of a
7943 * maximum system call (or maximum API call) interrupt priority.
7944 * Interrupts that are above the maximum system call priority are keep
7945 * permanently enabled, even when the RTOS kernel is in a critical section,
7946 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7947 * is defined in FreeRTOSConfig.h then
7948 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7949 * failure if a FreeRTOS API function is called from an interrupt that has
7950 * been assigned a priority above the configured maximum system call
7951 * priority. Only FreeRTOS functions that end in FromISR can be called
7952 * from interrupts that have been assigned a priority at or (logically)
7953 * below the maximum system call interrupt priority. FreeRTOS maintains a
7954 * separate interrupt safe API to ensure interrupt entry is as fast and as
7955 * simple as possible. More information (albeit Cortex-M specific) is
7956 * provided on the following link:
7957 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7958 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7960 pxTCB = xTaskToNotify;
7962 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7964 if( pulPreviousNotificationValue != NULL )
7966 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7969 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7970 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7975 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7979 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7982 case eSetValueWithOverwrite:
7983 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7986 case eSetValueWithoutOverwrite:
7988 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7990 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7994 /* The value could not be written to the task. */
8002 /* The task is being notified without its notify value being
8008 /* Should not get here if all enums are handled.
8009 * Artificially force an assert by testing a value the
8010 * compiler can't assume is const. */
8011 configASSERT( xTickCount == ( TickType_t ) 0 );
8015 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
8017 /* If the task is in the blocked state specifically to wait for a
8018 * notification then unblock it now. */
8019 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8021 /* The task should not have been on an event list. */
8022 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8024 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8026 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8027 prvAddTaskToReadyList( pxTCB );
8031 /* The delayed and ready lists cannot be accessed, so hold
8032 * this task pending until the scheduler is resumed. */
8033 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8036 #if ( configNUMBER_OF_CORES == 1 )
8038 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8040 /* The notified task has a priority above the currently
8041 * executing task so a yield is required. */
8042 if( pxHigherPriorityTaskWoken != NULL )
8044 *pxHigherPriorityTaskWoken = pdTRUE;
8047 /* Mark that a yield is pending in case the user is not
8048 * using the "xHigherPriorityTaskWoken" parameter to an ISR
8049 * safe FreeRTOS function. */
8050 xYieldPendings[ 0 ] = pdTRUE;
8054 mtCOVERAGE_TEST_MARKER();
8057 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8059 #if ( configUSE_PREEMPTION == 1 )
8061 prvYieldForTask( pxTCB );
8063 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8065 if( pxHigherPriorityTaskWoken != NULL )
8067 *pxHigherPriorityTaskWoken = pdTRUE;
8071 #endif /* if ( configUSE_PREEMPTION == 1 ) */
8073 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8076 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8078 traceRETURN_xTaskGenericNotifyFromISR( xReturn );
8083 #endif /* configUSE_TASK_NOTIFICATIONS */
8084 /*-----------------------------------------------------------*/
8086 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8088 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
8089 UBaseType_t uxIndexToNotify,
8090 BaseType_t * pxHigherPriorityTaskWoken )
8093 uint8_t ucOriginalNotifyState;
8094 UBaseType_t uxSavedInterruptStatus;
8096 traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
8098 configASSERT( xTaskToNotify );
8099 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8101 /* RTOS ports that support interrupt nesting have the concept of a
8102 * maximum system call (or maximum API call) interrupt priority.
8103 * Interrupts that are above the maximum system call priority are keep
8104 * permanently enabled, even when the RTOS kernel is in a critical section,
8105 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
8106 * is defined in FreeRTOSConfig.h then
8107 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
8108 * failure if a FreeRTOS API function is called from an interrupt that has
8109 * been assigned a priority above the configured maximum system call
8110 * priority. Only FreeRTOS functions that end in FromISR can be called
8111 * from interrupts that have been assigned a priority at or (logically)
8112 * below the maximum system call interrupt priority. FreeRTOS maintains a
8113 * separate interrupt safe API to ensure interrupt entry is as fast and as
8114 * simple as possible. More information (albeit Cortex-M specific) is
8115 * provided on the following link:
8116 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
8117 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
8119 pxTCB = xTaskToNotify;
8121 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
8123 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8124 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8126 /* 'Giving' is equivalent to incrementing a count in a counting
8128 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8130 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
8132 /* If the task is in the blocked state specifically to wait for a
8133 * notification then unblock it now. */
8134 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8136 /* The task should not have been on an event list. */
8137 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8139 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8141 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8142 prvAddTaskToReadyList( pxTCB );
8146 /* The delayed and ready lists cannot be accessed, so hold
8147 * this task pending until the scheduler is resumed. */
8148 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8151 #if ( configNUMBER_OF_CORES == 1 )
8153 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8155 /* The notified task has a priority above the currently
8156 * executing task so a yield is required. */
8157 if( pxHigherPriorityTaskWoken != NULL )
8159 *pxHigherPriorityTaskWoken = pdTRUE;
8162 /* Mark that a yield is pending in case the user is not
8163 * using the "xHigherPriorityTaskWoken" parameter in an ISR
8164 * safe FreeRTOS function. */
8165 xYieldPendings[ 0 ] = pdTRUE;
8169 mtCOVERAGE_TEST_MARKER();
8172 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8174 #if ( configUSE_PREEMPTION == 1 )
8176 prvYieldForTask( pxTCB );
8178 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8180 if( pxHigherPriorityTaskWoken != NULL )
8182 *pxHigherPriorityTaskWoken = pdTRUE;
8186 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
8188 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8191 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8193 traceRETURN_vTaskGenericNotifyGiveFromISR();
8196 #endif /* configUSE_TASK_NOTIFICATIONS */
8197 /*-----------------------------------------------------------*/
8199 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8201 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
8202 UBaseType_t uxIndexToClear )
8207 traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
8209 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8211 /* If null is passed in here then it is the calling task that is having
8212 * its notification state cleared. */
8213 pxTCB = prvGetTCBFromHandle( xTask );
8215 taskENTER_CRITICAL();
8217 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
8219 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
8227 taskEXIT_CRITICAL();
8229 traceRETURN_xTaskGenericNotifyStateClear( xReturn );
8234 #endif /* configUSE_TASK_NOTIFICATIONS */
8235 /*-----------------------------------------------------------*/
8237 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8239 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
8240 UBaseType_t uxIndexToClear,
8241 uint32_t ulBitsToClear )
8246 traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
8248 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8250 /* If null is passed in here then it is the calling task that is having
8251 * its notification state cleared. */
8252 pxTCB = prvGetTCBFromHandle( xTask );
8254 taskENTER_CRITICAL();
8256 /* Return the notification as it was before the bits were cleared,
8257 * then clear the bit mask. */
8258 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
8259 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
8261 taskEXIT_CRITICAL();
8263 traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
8268 #endif /* configUSE_TASK_NOTIFICATIONS */
8269 /*-----------------------------------------------------------*/
8271 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8273 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
8277 traceENTER_ulTaskGetRunTimeCounter( xTask );
8279 pxTCB = prvGetTCBFromHandle( xTask );
8281 traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
8283 return pxTCB->ulRunTimeCounter;
8286 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8287 /*-----------------------------------------------------------*/
8289 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8291 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
8294 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8296 traceENTER_ulTaskGetRunTimePercent( xTask );
8298 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
8300 /* For percentage calculations. */
8301 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8303 /* Avoid divide by zero errors. */
8304 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8306 pxTCB = prvGetTCBFromHandle( xTask );
8307 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
8314 traceRETURN_ulTaskGetRunTimePercent( ulReturn );
8319 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8320 /*-----------------------------------------------------------*/
8322 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8324 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
8326 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
8329 traceENTER_ulTaskGetIdleRunTimeCounter();
8331 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8333 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8336 traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
8341 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8342 /*-----------------------------------------------------------*/
8344 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8346 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
8348 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8349 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
8352 traceENTER_ulTaskGetIdleRunTimePercent();
8354 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
8356 /* For percentage calculations. */
8357 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8359 /* Avoid divide by zero errors. */
8360 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8362 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8364 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8367 ulReturn = ulRunTimeCounter / ulTotalTime;
8374 traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
8379 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8380 /*-----------------------------------------------------------*/
8382 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
8383 const BaseType_t xCanBlockIndefinitely )
8385 TickType_t xTimeToWake;
8386 const TickType_t xConstTickCount = xTickCount;
8388 #if ( INCLUDE_xTaskAbortDelay == 1 )
8390 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
8391 * reset to pdFALSE so it can be detected as having been set to pdTRUE
8392 * when the task leaves the Blocked state. */
8393 pxCurrentTCB->ucDelayAborted = pdFALSE;
8397 /* Remove the task from the ready list before adding it to the blocked list
8398 * as the same list item is used for both lists. */
8399 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
8401 /* The current task must be in a ready list, so there is no need to
8402 * check, and the port reset macro can be called directly. */
8403 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. */
8407 mtCOVERAGE_TEST_MARKER();
8410 #if ( INCLUDE_vTaskSuspend == 1 )
8412 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
8414 /* Add the task to the suspended task list instead of a delayed task
8415 * list to ensure it is not woken by a timing event. It will block
8417 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
8421 /* Calculate the time at which the task should be woken if the event
8422 * does not occur. This may overflow but this doesn't matter, the
8423 * kernel will manage it correctly. */
8424 xTimeToWake = xConstTickCount + xTicksToWait;
8426 /* The list item will be inserted in wake time order. */
8427 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8429 if( xTimeToWake < xConstTickCount )
8431 /* Wake time has overflowed. Place this item in the overflow
8433 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8434 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8438 /* The wake time has not overflowed, so the current block list
8440 traceMOVED_TASK_TO_DELAYED_LIST();
8441 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8443 /* If the task entering the blocked state was placed at the
8444 * head of the list of blocked tasks then xNextTaskUnblockTime
8445 * needs to be updated too. */
8446 if( xTimeToWake < xNextTaskUnblockTime )
8448 xNextTaskUnblockTime = xTimeToWake;
8452 mtCOVERAGE_TEST_MARKER();
8457 #else /* INCLUDE_vTaskSuspend */
8459 /* Calculate the time at which the task should be woken if the event
8460 * does not occur. This may overflow but this doesn't matter, the kernel
8461 * will manage it correctly. */
8462 xTimeToWake = xConstTickCount + xTicksToWait;
8464 /* The list item will be inserted in wake time order. */
8465 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8467 if( xTimeToWake < xConstTickCount )
8469 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8470 /* Wake time has overflowed. Place this item in the overflow list. */
8471 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8475 traceMOVED_TASK_TO_DELAYED_LIST();
8476 /* The wake time has not overflowed, so the current block list is used. */
8477 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8479 /* If the task entering the blocked state was placed at the head of the
8480 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
8482 if( xTimeToWake < xNextTaskUnblockTime )
8484 xNextTaskUnblockTime = xTimeToWake;
8488 mtCOVERAGE_TEST_MARKER();
8492 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
8493 ( void ) xCanBlockIndefinitely;
8495 #endif /* INCLUDE_vTaskSuspend */
8497 /*-----------------------------------------------------------*/
8499 #if ( portUSING_MPU_WRAPPERS == 1 )
8501 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
8505 traceENTER_xTaskGetMPUSettings( xTask );
8507 pxTCB = prvGetTCBFromHandle( xTask );
8509 traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
8511 return &( pxTCB->xMPUSettings );
8514 #endif /* portUSING_MPU_WRAPPERS */
8515 /*-----------------------------------------------------------*/
8517 /* Code below here allows additional code to be inserted into this source file,
8518 * especially where access to file scope functions and data is needed (for example
8519 * when performing module tests). */
8521 #ifdef FREERTOS_MODULE_TEST
8522 #include "tasks_test_access_functions.h"
8526 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
8528 #include "freertos_tasks_c_additions.h"
8530 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8531 static void freertos_tasks_c_additions_init( void )
8533 FREERTOS_TASKS_C_ADDITIONS_INIT();
8537 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8538 /*-----------------------------------------------------------*/
8540 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8543 * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8544 * to provide the memory that is used by the Idle task. It is used when
8545 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8546 * it's own implementation of vApplicationGetIdleTaskMemory by setting
8547 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8549 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8550 StackType_t ** ppxIdleTaskStackBuffer,
8551 uint32_t * pulIdleTaskStackSize )
8553 static StaticTask_t xIdleTaskTCB;
8554 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8556 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8557 *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8558 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8561 #if ( configNUMBER_OF_CORES > 1 )
8563 void vApplicationGetPassiveIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8564 StackType_t ** ppxIdleTaskStackBuffer,
8565 uint32_t * pulIdleTaskStackSize,
8566 BaseType_t xPassiveIdleTaskIndex )
8568 static StaticTask_t xIdleTaskTCBs[ configNUMBER_OF_CORES - 1 ];
8569 static StackType_t uxIdleTaskStacks[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
8571 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCBs[ xPassiveIdleTaskIndex ] );
8572 *ppxIdleTaskStackBuffer = &( uxIdleTaskStacks[ xPassiveIdleTaskIndex ][ 0 ] );
8573 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8576 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
8578 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8579 /*-----------------------------------------------------------*/
8581 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8584 * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8585 * to provide the memory that is used by the Timer service task. It is used when
8586 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8587 * it's own implementation of vApplicationGetTimerTaskMemory by setting
8588 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8590 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8591 StackType_t ** ppxTimerTaskStackBuffer,
8592 uint32_t * pulTimerTaskStackSize )
8594 static StaticTask_t xTimerTaskTCB;
8595 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8597 *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8598 *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8599 *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8602 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8603 /*-----------------------------------------------------------*/