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 0x8000U
289 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
290 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
291 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
292 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 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 ) == 0 ) &&
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 ) == 0 )
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 ) != 0 )
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 pxNewTCB = ( TCB_t * ) pxTaskBuffer; /*lint !e740 !e9087 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
1275 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1276 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1278 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1280 /* Tasks can be created statically or dynamically, so note this
1281 * task was created statically in case the task is later deleted. */
1282 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1284 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1286 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1295 /*-----------------------------------------------------------*/
1297 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1298 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1299 const uint32_t ulStackDepth,
1300 void * const pvParameters,
1301 UBaseType_t uxPriority,
1302 StackType_t * const puxStackBuffer,
1303 StaticTask_t * const pxTaskBuffer )
1305 TaskHandle_t xReturn = NULL;
1308 traceENTER_xTaskCreateStatic( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer );
1310 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1312 if( pxNewTCB != NULL )
1314 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1316 /* Set the task's affinity before scheduling it. */
1317 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1321 prvAddNewTaskToReadyList( pxNewTCB );
1325 mtCOVERAGE_TEST_MARKER();
1328 traceRETURN_xTaskCreateStatic( xReturn );
1332 /*-----------------------------------------------------------*/
1334 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1335 TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1336 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1337 const uint32_t ulStackDepth,
1338 void * const pvParameters,
1339 UBaseType_t uxPriority,
1340 StackType_t * const puxStackBuffer,
1341 StaticTask_t * const pxTaskBuffer,
1342 UBaseType_t uxCoreAffinityMask )
1344 TaskHandle_t xReturn = NULL;
1347 traceENTER_xTaskCreateStaticAffinitySet( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, uxCoreAffinityMask );
1349 pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
1351 if( pxNewTCB != NULL )
1353 /* Set the task's affinity before scheduling it. */
1354 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1356 prvAddNewTaskToReadyList( pxNewTCB );
1360 mtCOVERAGE_TEST_MARKER();
1363 traceRETURN_xTaskCreateStaticAffinitySet( xReturn );
1367 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1369 #endif /* SUPPORT_STATIC_ALLOCATION */
1370 /*-----------------------------------------------------------*/
1372 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1373 static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
1374 TaskHandle_t * const pxCreatedTask )
1378 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1379 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1381 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1383 /* Allocate space for the TCB. Where the memory comes from depends
1384 * on the implementation of the port malloc function and whether or
1385 * not static allocation is being used. */
1386 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1387 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1389 /* Store the stack location in the TCB. */
1390 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1392 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1394 /* Tasks can be created statically or dynamically, so note this
1395 * task was created statically in case the task is later deleted. */
1396 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1398 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1400 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1401 pxTaskDefinition->pcName,
1402 ( uint32_t ) pxTaskDefinition->usStackDepth,
1403 pxTaskDefinition->pvParameters,
1404 pxTaskDefinition->uxPriority,
1405 pxCreatedTask, pxNewTCB,
1406 pxTaskDefinition->xRegions );
1415 /*-----------------------------------------------------------*/
1417 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1418 TaskHandle_t * pxCreatedTask )
1423 traceENTER_xTaskCreateRestrictedStatic( pxTaskDefinition, pxCreatedTask );
1425 configASSERT( pxTaskDefinition != NULL );
1427 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1429 if( pxNewTCB != NULL )
1431 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1433 /* Set the task's affinity before scheduling it. */
1434 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1438 prvAddNewTaskToReadyList( pxNewTCB );
1443 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1446 traceRETURN_xTaskCreateRestrictedStatic( xReturn );
1450 /*-----------------------------------------------------------*/
1452 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1453 BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1454 UBaseType_t uxCoreAffinityMask,
1455 TaskHandle_t * pxCreatedTask )
1460 traceENTER_xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1462 configASSERT( pxTaskDefinition != NULL );
1464 pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
1466 if( pxNewTCB != NULL )
1468 /* Set the task's affinity before scheduling it. */
1469 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1471 prvAddNewTaskToReadyList( pxNewTCB );
1476 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1479 traceRETURN_xTaskCreateRestrictedStaticAffinitySet( xReturn );
1483 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1485 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1486 /*-----------------------------------------------------------*/
1488 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1489 static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
1490 TaskHandle_t * const pxCreatedTask )
1494 configASSERT( pxTaskDefinition->puxStackBuffer );
1496 if( pxTaskDefinition->puxStackBuffer != NULL )
1498 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1500 if( pxNewTCB != NULL )
1502 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1504 /* Store the stack location in the TCB. */
1505 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1507 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1509 /* Tasks can be created statically or dynamically, so note
1510 * this task had a statically allocated stack in case it is
1511 * later deleted. The TCB was allocated dynamically. */
1512 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1514 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1516 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1517 pxTaskDefinition->pcName,
1518 ( uint32_t ) pxTaskDefinition->usStackDepth,
1519 pxTaskDefinition->pvParameters,
1520 pxTaskDefinition->uxPriority,
1521 pxCreatedTask, pxNewTCB,
1522 pxTaskDefinition->xRegions );
1532 /*-----------------------------------------------------------*/
1534 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1535 TaskHandle_t * pxCreatedTask )
1540 traceENTER_xTaskCreateRestricted( pxTaskDefinition, pxCreatedTask );
1542 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1544 if( pxNewTCB != NULL )
1546 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1548 /* Set the task's affinity before scheduling it. */
1549 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1551 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1553 prvAddNewTaskToReadyList( pxNewTCB );
1559 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1562 traceRETURN_xTaskCreateRestricted( xReturn );
1566 /*-----------------------------------------------------------*/
1568 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1569 BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1570 UBaseType_t uxCoreAffinityMask,
1571 TaskHandle_t * pxCreatedTask )
1576 traceENTER_xTaskCreateRestrictedAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
1578 pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
1580 if( pxNewTCB != NULL )
1582 /* Set the task's affinity before scheduling it. */
1583 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1585 prvAddNewTaskToReadyList( pxNewTCB );
1591 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1594 traceRETURN_xTaskCreateRestrictedAffinitySet( xReturn );
1598 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1601 #endif /* portUSING_MPU_WRAPPERS */
1602 /*-----------------------------------------------------------*/
1604 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1605 static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
1606 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1607 const configSTACK_DEPTH_TYPE usStackDepth,
1608 void * const pvParameters,
1609 UBaseType_t uxPriority,
1610 TaskHandle_t * const pxCreatedTask )
1614 /* If the stack grows down then allocate the stack then the TCB so the stack
1615 * does not grow into the TCB. Likewise if the stack grows up then allocate
1616 * the TCB then the stack. */
1617 #if ( portSTACK_GROWTH > 0 )
1619 /* Allocate space for the TCB. Where the memory comes from depends on
1620 * the implementation of the port malloc function and whether or not static
1621 * allocation is being used. */
1622 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1624 if( pxNewTCB != NULL )
1626 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1628 /* Allocate space for the stack used by the task being created.
1629 * The base of the stack memory stored in the TCB so the task can
1630 * be deleted later if required. */
1631 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1633 if( pxNewTCB->pxStack == NULL )
1635 /* Could not allocate the stack. Delete the allocated TCB. */
1636 vPortFree( pxNewTCB );
1641 #else /* portSTACK_GROWTH */
1643 StackType_t * pxStack;
1645 /* Allocate space for the stack used by the task being created. */
1646 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. */
1648 if( pxStack != NULL )
1650 /* Allocate space for the TCB. */
1651 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. */
1653 if( pxNewTCB != NULL )
1655 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1657 /* Store the stack location in the TCB. */
1658 pxNewTCB->pxStack = pxStack;
1662 /* The stack cannot be used as the TCB was not created. Free
1664 vPortFreeStack( pxStack );
1672 #endif /* portSTACK_GROWTH */
1674 if( pxNewTCB != NULL )
1676 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1678 /* Tasks can be created statically or dynamically, so note this
1679 * task was created dynamically in case it is later deleted. */
1680 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1682 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1684 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1689 /*-----------------------------------------------------------*/
1691 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1692 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1693 const configSTACK_DEPTH_TYPE usStackDepth,
1694 void * const pvParameters,
1695 UBaseType_t uxPriority,
1696 TaskHandle_t * const pxCreatedTask )
1701 traceENTER_xTaskCreate( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask );
1703 pxNewTCB = prvCreateTask( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask );
1705 if( pxNewTCB != NULL )
1707 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1709 /* Set the task's affinity before scheduling it. */
1710 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1714 prvAddNewTaskToReadyList( pxNewTCB );
1719 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1722 traceRETURN_xTaskCreate( xReturn );
1726 /*-----------------------------------------------------------*/
1728 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1729 BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1730 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1731 const configSTACK_DEPTH_TYPE usStackDepth,
1732 void * const pvParameters,
1733 UBaseType_t uxPriority,
1734 UBaseType_t uxCoreAffinityMask,
1735 TaskHandle_t * const pxCreatedTask )
1740 traceENTER_xTaskCreateAffinitySet( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, uxCoreAffinityMask, pxCreatedTask );
1742 pxNewTCB = prvCreateTask( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask );
1744 if( pxNewTCB != NULL )
1746 /* Set the task's affinity before scheduling it. */
1747 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1749 prvAddNewTaskToReadyList( pxNewTCB );
1754 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1757 traceRETURN_xTaskCreateAffinitySet( xReturn );
1761 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1763 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1764 /*-----------------------------------------------------------*/
1766 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1767 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1768 const uint32_t ulStackDepth,
1769 void * const pvParameters,
1770 UBaseType_t uxPriority,
1771 TaskHandle_t * const pxCreatedTask,
1773 const MemoryRegion_t * const xRegions )
1775 StackType_t * pxTopOfStack;
1778 #if ( portUSING_MPU_WRAPPERS == 1 )
1779 /* Should the task be created in privileged mode? */
1780 BaseType_t xRunPrivileged;
1782 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1784 xRunPrivileged = pdTRUE;
1788 xRunPrivileged = pdFALSE;
1790 uxPriority &= ~portPRIVILEGE_BIT;
1791 #endif /* portUSING_MPU_WRAPPERS == 1 */
1793 /* Avoid dependency on memset() if it is not required. */
1794 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1796 /* Fill the stack with a known value to assist debugging. */
1797 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1799 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1801 /* Calculate the top of stack address. This depends on whether the stack
1802 * grows from high memory to low (as per the 80x86) or vice versa.
1803 * portSTACK_GROWTH is used to make the result positive or negative as required
1805 #if ( portSTACK_GROWTH < 0 )
1807 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1808 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(). */
1810 /* Check the alignment of the calculated top of stack is correct. */
1811 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1813 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1815 /* Also record the stack's high address, which may assist
1817 pxNewTCB->pxEndOfStack = pxTopOfStack;
1819 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1821 #else /* portSTACK_GROWTH */
1823 pxTopOfStack = pxNewTCB->pxStack;
1824 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(). */
1826 /* Check the alignment of the calculated top of stack is correct. */
1827 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1829 /* The other extreme of the stack space is required if stack checking is
1831 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1833 #endif /* portSTACK_GROWTH */
1835 /* Store the task name in the TCB. */
1836 if( pcName != NULL )
1838 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1840 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1842 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1843 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1844 * string is not accessible (extremely unlikely). */
1845 if( pcName[ x ] == ( char ) 0x00 )
1851 mtCOVERAGE_TEST_MARKER();
1855 /* Ensure the name string is terminated in the case that the string length
1856 * was greater or equal to configMAX_TASK_NAME_LEN. */
1857 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1861 mtCOVERAGE_TEST_MARKER();
1864 /* This is used as an array index so must ensure it's not too large. */
1865 configASSERT( uxPriority < configMAX_PRIORITIES );
1867 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1869 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1873 mtCOVERAGE_TEST_MARKER();
1876 pxNewTCB->uxPriority = uxPriority;
1877 #if ( configUSE_MUTEXES == 1 )
1879 pxNewTCB->uxBasePriority = uxPriority;
1881 #endif /* configUSE_MUTEXES */
1883 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1884 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1886 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1887 * back to the containing TCB from a generic item in a list. */
1888 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1890 /* Event lists are always in priority order. */
1891 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. */
1892 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1894 #if ( portUSING_MPU_WRAPPERS == 1 )
1896 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1900 /* Avoid compiler warning about unreferenced parameter. */
1905 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
1907 /* Allocate and initialize memory for the task's TLS Block. */
1908 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
1912 /* Initialize the TCB stack to look as if the task was already running,
1913 * but had been interrupted by the scheduler. The return address is set
1914 * to the start of the task function. Once the stack has been initialised
1915 * the top of stack variable is updated. */
1916 #if ( portUSING_MPU_WRAPPERS == 1 )
1918 /* If the port has capability to detect stack overflow,
1919 * pass the stack end address to the stack initialization
1920 * function as well. */
1921 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1923 #if ( portSTACK_GROWTH < 0 )
1925 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1927 #else /* portSTACK_GROWTH */
1929 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1931 #endif /* portSTACK_GROWTH */
1933 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1935 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1937 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1939 #else /* portUSING_MPU_WRAPPERS */
1941 /* If the port has capability to detect stack overflow,
1942 * pass the stack end address to the stack initialization
1943 * function as well. */
1944 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1946 #if ( portSTACK_GROWTH < 0 )
1948 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1950 #else /* portSTACK_GROWTH */
1952 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1954 #endif /* portSTACK_GROWTH */
1956 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1958 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1960 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1962 #endif /* portUSING_MPU_WRAPPERS */
1964 /* Initialize task state and task attributes. */
1965 #if ( configNUMBER_OF_CORES > 1 )
1967 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1969 /* Is this an idle task? */
1970 if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvPassiveIdleTask ) )
1972 pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
1975 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
1977 if( pxCreatedTask != NULL )
1979 /* Pass the handle out in an anonymous way. The handle can be used to
1980 * change the created task's priority, delete the created task, etc.*/
1981 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1985 mtCOVERAGE_TEST_MARKER();
1988 /*-----------------------------------------------------------*/
1990 #if ( configNUMBER_OF_CORES == 1 )
1992 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1994 /* Ensure interrupts don't access the task lists while the lists are being
1996 taskENTER_CRITICAL();
1998 uxCurrentNumberOfTasks++;
2000 if( pxCurrentTCB == NULL )
2002 /* There are no other tasks, or all the other tasks are in
2003 * the suspended state - make this the current task. */
2004 pxCurrentTCB = pxNewTCB;
2006 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2008 /* This is the first task to be created so do the preliminary
2009 * initialisation required. We will not recover if this call
2010 * fails, but we will report the failure. */
2011 prvInitialiseTaskLists();
2015 mtCOVERAGE_TEST_MARKER();
2020 /* If the scheduler is not already running, make this task the
2021 * current task if it is the highest priority task to be created
2023 if( xSchedulerRunning == pdFALSE )
2025 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
2027 pxCurrentTCB = pxNewTCB;
2031 mtCOVERAGE_TEST_MARKER();
2036 mtCOVERAGE_TEST_MARKER();
2042 #if ( configUSE_TRACE_FACILITY == 1 )
2044 /* Add a counter into the TCB for tracing only. */
2045 pxNewTCB->uxTCBNumber = uxTaskNumber;
2047 #endif /* configUSE_TRACE_FACILITY */
2048 traceTASK_CREATE( pxNewTCB );
2050 prvAddTaskToReadyList( pxNewTCB );
2052 portSETUP_TCB( pxNewTCB );
2054 taskEXIT_CRITICAL();
2056 if( xSchedulerRunning != pdFALSE )
2058 /* If the created task is of a higher priority than the current task
2059 * then it should run now. */
2060 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2064 mtCOVERAGE_TEST_MARKER();
2068 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2070 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
2072 /* Ensure interrupts don't access the task lists while the lists are being
2074 taskENTER_CRITICAL();
2076 uxCurrentNumberOfTasks++;
2078 if( xSchedulerRunning == pdFALSE )
2080 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
2082 /* This is the first task to be created so do the preliminary
2083 * initialisation required. We will not recover if this call
2084 * fails, but we will report the failure. */
2085 prvInitialiseTaskLists();
2089 mtCOVERAGE_TEST_MARKER();
2092 if( ( pxNewTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
2096 /* Check if a core is free. */
2097 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
2099 if( pxCurrentTCBs[ xCoreID ] == NULL )
2101 pxNewTCB->xTaskRunState = xCoreID;
2102 pxCurrentTCBs[ xCoreID ] = pxNewTCB;
2107 mtCOVERAGE_TEST_MARKER();
2113 mtCOVERAGE_TEST_MARKER();
2119 #if ( configUSE_TRACE_FACILITY == 1 )
2121 /* Add a counter into the TCB for tracing only. */
2122 pxNewTCB->uxTCBNumber = uxTaskNumber;
2124 #endif /* configUSE_TRACE_FACILITY */
2125 traceTASK_CREATE( pxNewTCB );
2127 prvAddTaskToReadyList( pxNewTCB );
2129 portSETUP_TCB( pxNewTCB );
2131 if( xSchedulerRunning != pdFALSE )
2133 /* If the created task is of a higher priority than another
2134 * currently running task and preemption is on then it should
2136 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
2140 mtCOVERAGE_TEST_MARKER();
2143 taskEXIT_CRITICAL();
2146 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2147 /*-----------------------------------------------------------*/
2149 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
2151 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
2154 size_t uxCharsWritten;
2156 if( iSnprintfReturnValue < 0 )
2158 /* Encoding error - Return 0 to indicate that nothing
2159 * was written to the buffer. */
2162 else if( iSnprintfReturnValue >= ( int ) n )
2164 /* This is the case when the supplied buffer is not
2165 * large to hold the generated string. Return the
2166 * number of characters actually written without
2167 * counting the terminating NULL character. */
2168 uxCharsWritten = n - 1;
2172 /* Complete string was written to the buffer. */
2173 uxCharsWritten = ( size_t ) iSnprintfReturnValue;
2176 return uxCharsWritten;
2179 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
2180 /*-----------------------------------------------------------*/
2182 #if ( INCLUDE_vTaskDelete == 1 )
2184 void vTaskDelete( TaskHandle_t xTaskToDelete )
2188 traceENTER_vTaskDelete( xTaskToDelete );
2190 taskENTER_CRITICAL();
2192 /* If null is passed in here then it is the calling task that is
2194 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
2196 /* Remove task from the ready/delayed list. */
2197 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2199 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2203 mtCOVERAGE_TEST_MARKER();
2206 /* Is the task waiting on an event also? */
2207 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2209 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2213 mtCOVERAGE_TEST_MARKER();
2216 /* Increment the uxTaskNumber also so kernel aware debuggers can
2217 * detect that the task lists need re-generating. This is done before
2218 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
2222 /* If the task is running (or yielding), we must add it to the
2223 * termination list so that an idle task can delete it when it is
2224 * no longer running. */
2225 if( taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) != pdFALSE )
2227 /* A running task or a task which is scheduled to yield is being
2228 * deleted. This cannot complete when the task is still running
2229 * on a core, as a context switch to another task is required.
2230 * Place the task in the termination list. The idle task will check
2231 * the termination list and free up any memory allocated by the
2232 * scheduler for the TCB and stack of the deleted task. */
2233 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
2235 /* Increment the ucTasksDeleted variable so the idle task knows
2236 * there is a task that has been deleted and that it should therefore
2237 * check the xTasksWaitingTermination list. */
2238 ++uxDeletedTasksWaitingCleanUp;
2240 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
2241 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
2242 traceTASK_DELETE( pxTCB );
2244 /* The pre-delete hook is primarily for the Windows simulator,
2245 * in which Windows specific clean up operations are performed,
2246 * after which it is not possible to yield away from this task -
2247 * hence xYieldPending is used to latch that a context switch is
2249 #if ( configNUMBER_OF_CORES == 1 )
2250 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
2252 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
2257 --uxCurrentNumberOfTasks;
2258 traceTASK_DELETE( pxTCB );
2260 /* Reset the next expected unblock time in case it referred to
2261 * the task that has just been deleted. */
2262 prvResetNextTaskUnblockTime();
2266 #if ( configNUMBER_OF_CORES == 1 )
2268 taskEXIT_CRITICAL();
2270 /* If the task is not deleting itself, call prvDeleteTCB from outside of
2271 * critical section. If a task deletes itself, prvDeleteTCB is called
2272 * from prvCheckTasksWaitingTermination which is called from Idle task. */
2273 if( pxTCB != pxCurrentTCB )
2275 prvDeleteTCB( pxTCB );
2278 /* Force a reschedule if it is the currently running task that has just
2280 if( xSchedulerRunning != pdFALSE )
2282 if( pxTCB == pxCurrentTCB )
2284 configASSERT( uxSchedulerSuspended == 0 );
2285 portYIELD_WITHIN_API();
2289 mtCOVERAGE_TEST_MARKER();
2293 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2295 /* If a running task is not deleting itself, call prvDeleteTCB. If a running
2296 * task deletes itself, prvDeleteTCB is called from prvCheckTasksWaitingTermination
2297 * which is called from Idle task. */
2298 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
2300 prvDeleteTCB( pxTCB );
2303 /* Force a reschedule if the task that has just been deleted was running. */
2304 if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) )
2306 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2308 configASSERT( uxSchedulerSuspended == 0 );
2309 vTaskYieldWithinAPI();
2313 prvYieldCore( pxTCB->xTaskRunState );
2317 taskEXIT_CRITICAL();
2319 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2321 traceRETURN_vTaskDelete();
2324 #endif /* INCLUDE_vTaskDelete */
2325 /*-----------------------------------------------------------*/
2327 #if ( INCLUDE_xTaskDelayUntil == 1 )
2329 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2330 const TickType_t xTimeIncrement )
2332 TickType_t xTimeToWake;
2333 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2335 traceENTER_xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
2337 configASSERT( pxPreviousWakeTime );
2338 configASSERT( ( xTimeIncrement > 0U ) );
2342 /* Minor optimisation. The tick count cannot change in this
2344 const TickType_t xConstTickCount = xTickCount;
2346 configASSERT( uxSchedulerSuspended == 1U );
2348 /* Generate the tick time at which the task wants to wake. */
2349 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2351 if( xConstTickCount < *pxPreviousWakeTime )
2353 /* The tick count has overflowed since this function was
2354 * lasted called. In this case the only time we should ever
2355 * actually delay is if the wake time has also overflowed,
2356 * and the wake time is greater than the tick time. When this
2357 * is the case it is as if neither time had overflowed. */
2358 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2360 xShouldDelay = pdTRUE;
2364 mtCOVERAGE_TEST_MARKER();
2369 /* The tick time has not overflowed. In this case we will
2370 * delay if either the wake time has overflowed, and/or the
2371 * tick time is less than the wake time. */
2372 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2374 xShouldDelay = pdTRUE;
2378 mtCOVERAGE_TEST_MARKER();
2382 /* Update the wake time ready for the next call. */
2383 *pxPreviousWakeTime = xTimeToWake;
2385 if( xShouldDelay != pdFALSE )
2387 traceTASK_DELAY_UNTIL( xTimeToWake );
2389 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2390 * the time to wake, so subtract the current tick count. */
2391 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2395 mtCOVERAGE_TEST_MARKER();
2398 xAlreadyYielded = xTaskResumeAll();
2400 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2401 * have put ourselves to sleep. */
2402 if( xAlreadyYielded == pdFALSE )
2404 taskYIELD_WITHIN_API();
2408 mtCOVERAGE_TEST_MARKER();
2411 traceRETURN_xTaskDelayUntil( xShouldDelay );
2413 return xShouldDelay;
2416 #endif /* INCLUDE_xTaskDelayUntil */
2417 /*-----------------------------------------------------------*/
2419 #if ( INCLUDE_vTaskDelay == 1 )
2421 void vTaskDelay( const TickType_t xTicksToDelay )
2423 BaseType_t xAlreadyYielded = pdFALSE;
2425 traceENTER_vTaskDelay( xTicksToDelay );
2427 /* A delay time of zero just forces a reschedule. */
2428 if( xTicksToDelay > ( TickType_t ) 0U )
2432 configASSERT( uxSchedulerSuspended == 1U );
2436 /* A task that is removed from the event list while the
2437 * scheduler is suspended will not get placed in the ready
2438 * list or removed from the blocked list until the scheduler
2441 * This task cannot be in an event list as it is the currently
2442 * executing task. */
2443 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2445 xAlreadyYielded = xTaskResumeAll();
2449 mtCOVERAGE_TEST_MARKER();
2452 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2453 * have put ourselves to sleep. */
2454 if( xAlreadyYielded == pdFALSE )
2456 taskYIELD_WITHIN_API();
2460 mtCOVERAGE_TEST_MARKER();
2463 traceRETURN_vTaskDelay();
2466 #endif /* INCLUDE_vTaskDelay */
2467 /*-----------------------------------------------------------*/
2469 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2471 eTaskState eTaskGetState( TaskHandle_t xTask )
2474 List_t const * pxStateList;
2475 List_t const * pxEventList;
2476 List_t const * pxDelayedList;
2477 List_t const * pxOverflowedDelayedList;
2478 const TCB_t * const pxTCB = xTask;
2480 traceENTER_eTaskGetState( xTask );
2482 configASSERT( pxTCB );
2484 #if ( configNUMBER_OF_CORES == 1 )
2485 if( pxTCB == pxCurrentTCB )
2487 /* The task calling this function is querying its own state. */
2493 taskENTER_CRITICAL();
2495 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2496 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2497 pxDelayedList = pxDelayedTaskList;
2498 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2500 taskEXIT_CRITICAL();
2502 if( pxEventList == &xPendingReadyList )
2504 /* The task has been placed on the pending ready list, so its
2505 * state is eReady regardless of what list the task's state list
2506 * item is currently placed on. */
2509 else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2511 /* The task being queried is referenced from one of the Blocked
2516 #if ( INCLUDE_vTaskSuspend == 1 )
2517 else if( pxStateList == &xSuspendedTaskList )
2519 /* The task being queried is referenced from the suspended
2520 * list. Is it genuinely suspended or is it blocked
2522 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2524 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2528 /* The task does not appear on the event list item of
2529 * and of the RTOS objects, but could still be in the
2530 * blocked state if it is waiting on its notification
2531 * rather than waiting on an object. If not, is
2533 eReturn = eSuspended;
2535 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2537 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2544 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2546 eReturn = eSuspended;
2548 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2555 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2557 #if ( INCLUDE_vTaskDelete == 1 )
2558 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2560 /* The task being queried is referenced from the deleted
2561 * tasks list, or it is not referenced from any lists at
2567 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
2569 #if ( configNUMBER_OF_CORES == 1 )
2571 /* If the task is not in any other state, it must be in the
2572 * Ready (including pending ready) state. */
2575 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2577 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2579 /* Is it actively running on a core? */
2584 /* If the task is not in any other state, it must be in the
2585 * Ready (including pending ready) state. */
2589 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2593 traceRETURN_eTaskGetState( eReturn );
2596 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2598 #endif /* INCLUDE_eTaskGetState */
2599 /*-----------------------------------------------------------*/
2601 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2603 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2605 TCB_t const * pxTCB;
2606 UBaseType_t uxReturn;
2608 traceENTER_uxTaskPriorityGet( xTask );
2610 taskENTER_CRITICAL();
2612 /* If null is passed in here then it is the priority of the task
2613 * that called uxTaskPriorityGet() that is being queried. */
2614 pxTCB = prvGetTCBFromHandle( xTask );
2615 uxReturn = pxTCB->uxPriority;
2617 taskEXIT_CRITICAL();
2619 traceRETURN_uxTaskPriorityGet( uxReturn );
2624 #endif /* INCLUDE_uxTaskPriorityGet */
2625 /*-----------------------------------------------------------*/
2627 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2629 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2631 TCB_t const * pxTCB;
2632 UBaseType_t uxReturn;
2633 UBaseType_t uxSavedInterruptStatus;
2635 traceENTER_uxTaskPriorityGetFromISR( xTask );
2637 /* RTOS ports that support interrupt nesting have the concept of a
2638 * maximum system call (or maximum API call) interrupt priority.
2639 * Interrupts that are above the maximum system call priority are keep
2640 * permanently enabled, even when the RTOS kernel is in a critical section,
2641 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2642 * is defined in FreeRTOSConfig.h then
2643 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2644 * failure if a FreeRTOS API function is called from an interrupt that has
2645 * been assigned a priority above the configured maximum system call
2646 * priority. Only FreeRTOS functions that end in FromISR can be called
2647 * from interrupts that have been assigned a priority at or (logically)
2648 * below the maximum system call interrupt priority. FreeRTOS maintains a
2649 * separate interrupt safe API to ensure interrupt entry is as fast and as
2650 * simple as possible. More information (albeit Cortex-M specific) is
2651 * provided on the following link:
2652 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2653 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2655 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
2657 /* If null is passed in here then it is the priority of the calling
2658 * task that is being queried. */
2659 pxTCB = prvGetTCBFromHandle( xTask );
2660 uxReturn = pxTCB->uxPriority;
2662 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2664 traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
2669 #endif /* INCLUDE_uxTaskPriorityGet */
2670 /*-----------------------------------------------------------*/
2672 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2674 UBaseType_t uxTaskBasePriorityGet( const TaskHandle_t xTask )
2676 TCB_t const * pxTCB;
2677 UBaseType_t uxReturn;
2679 traceENTER_uxTaskBasePriorityGet( xTask );
2681 taskENTER_CRITICAL();
2683 /* If null is passed in here then it is the base priority of the task
2684 * that called uxTaskBasePriorityGet() that is being queried. */
2685 pxTCB = prvGetTCBFromHandle( xTask );
2686 uxReturn = pxTCB->uxBasePriority;
2688 taskEXIT_CRITICAL();
2690 traceRETURN_uxTaskBasePriorityGet( uxReturn );
2695 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2696 /*-----------------------------------------------------------*/
2698 #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
2700 UBaseType_t uxTaskBasePriorityGetFromISR( const TaskHandle_t xTask )
2702 TCB_t const * pxTCB;
2703 UBaseType_t uxReturn;
2704 UBaseType_t uxSavedInterruptStatus;
2706 traceENTER_uxTaskBasePriorityGetFromISR( xTask );
2708 /* RTOS ports that support interrupt nesting have the concept of a
2709 * maximum system call (or maximum API call) interrupt priority.
2710 * Interrupts that are above the maximum system call priority are keep
2711 * permanently enabled, even when the RTOS kernel is in a critical section,
2712 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2713 * is defined in FreeRTOSConfig.h then
2714 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2715 * failure if a FreeRTOS API function is called from an interrupt that has
2716 * been assigned a priority above the configured maximum system call
2717 * priority. Only FreeRTOS functions that end in FromISR can be called
2718 * from interrupts that have been assigned a priority at or (logically)
2719 * below the maximum system call interrupt priority. FreeRTOS maintains a
2720 * separate interrupt safe API to ensure interrupt entry is as fast and as
2721 * simple as possible. More information (albeit Cortex-M specific) is
2722 * provided on the following link:
2723 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2724 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2726 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
2728 /* If null is passed in here then it is the base priority of the calling
2729 * task that is being queried. */
2730 pxTCB = prvGetTCBFromHandle( xTask );
2731 uxReturn = pxTCB->uxBasePriority;
2733 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2735 traceRETURN_uxTaskBasePriorityGetFromISR( uxReturn );
2740 #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
2741 /*-----------------------------------------------------------*/
2743 #if ( INCLUDE_vTaskPrioritySet == 1 )
2745 void vTaskPrioritySet( TaskHandle_t xTask,
2746 UBaseType_t uxNewPriority )
2749 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2750 BaseType_t xYieldRequired = pdFALSE;
2752 #if ( configNUMBER_OF_CORES > 1 )
2753 BaseType_t xYieldForTask = pdFALSE;
2756 traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
2758 configASSERT( uxNewPriority < configMAX_PRIORITIES );
2760 /* Ensure the new priority is valid. */
2761 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2763 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2767 mtCOVERAGE_TEST_MARKER();
2770 taskENTER_CRITICAL();
2772 /* If null is passed in here then it is the priority of the calling
2773 * task that is being changed. */
2774 pxTCB = prvGetTCBFromHandle( xTask );
2776 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2778 #if ( configUSE_MUTEXES == 1 )
2780 uxCurrentBasePriority = pxTCB->uxBasePriority;
2784 uxCurrentBasePriority = pxTCB->uxPriority;
2788 if( uxCurrentBasePriority != uxNewPriority )
2790 /* The priority change may have readied a task of higher
2791 * priority than a running task. */
2792 if( uxNewPriority > uxCurrentBasePriority )
2794 #if ( configNUMBER_OF_CORES == 1 )
2796 if( pxTCB != pxCurrentTCB )
2798 /* The priority of a task other than the currently
2799 * running task is being raised. Is the priority being
2800 * raised above that of the running task? */
2801 if( uxNewPriority > pxCurrentTCB->uxPriority )
2803 xYieldRequired = pdTRUE;
2807 mtCOVERAGE_TEST_MARKER();
2812 /* The priority of the running task is being raised,
2813 * but the running task must already be the highest
2814 * priority task able to run so no yield is required. */
2817 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2819 /* The priority of a task is being raised so
2820 * perform a yield for this task later. */
2821 xYieldForTask = pdTRUE;
2823 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2825 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2827 /* Setting the priority of a running task down means
2828 * there may now be another task of higher priority that
2829 * is ready to execute. */
2830 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2831 if( pxTCB->xPreemptionDisable == pdFALSE )
2834 xYieldRequired = pdTRUE;
2839 /* Setting the priority of any other task down does not
2840 * require a yield as the running task must be above the
2841 * new priority of the task being modified. */
2844 /* Remember the ready list the task might be referenced from
2845 * before its uxPriority member is changed so the
2846 * taskRESET_READY_PRIORITY() macro can function correctly. */
2847 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2849 #if ( configUSE_MUTEXES == 1 )
2851 /* Only change the priority being used if the task is not
2852 * currently using an inherited priority or the new priority
2853 * is bigger than the inherited priority. */
2854 if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2856 pxTCB->uxPriority = uxNewPriority;
2860 mtCOVERAGE_TEST_MARKER();
2863 /* The base priority gets set whatever. */
2864 pxTCB->uxBasePriority = uxNewPriority;
2866 #else /* if ( configUSE_MUTEXES == 1 ) */
2868 pxTCB->uxPriority = uxNewPriority;
2870 #endif /* if ( configUSE_MUTEXES == 1 ) */
2872 /* Only reset the event list item value if the value is not
2873 * being used for anything else. */
2874 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2876 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. */
2880 mtCOVERAGE_TEST_MARKER();
2883 /* If the task is in the blocked or suspended list we need do
2884 * nothing more than change its priority variable. However, if
2885 * the task is in a ready list it needs to be removed and placed
2886 * in the list appropriate to its new priority. */
2887 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2889 /* The task is currently in its ready list - remove before
2890 * adding it to its new ready list. As we are in a critical
2891 * section we can do this even if the scheduler is suspended. */
2892 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2894 /* It is known that the task is in its ready list so
2895 * there is no need to check again and the port level
2896 * reset macro can be called directly. */
2897 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2901 mtCOVERAGE_TEST_MARKER();
2904 prvAddTaskToReadyList( pxTCB );
2908 #if ( configNUMBER_OF_CORES == 1 )
2910 mtCOVERAGE_TEST_MARKER();
2914 /* It's possible that xYieldForTask was already set to pdTRUE because
2915 * its priority is being raised. However, since it is not in a ready list
2916 * we don't actually need to yield for it. */
2917 xYieldForTask = pdFALSE;
2922 if( xYieldRequired != pdFALSE )
2924 /* The running task priority is set down. Request the task to yield. */
2925 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2929 #if ( configNUMBER_OF_CORES > 1 )
2930 if( xYieldForTask != pdFALSE )
2932 /* The priority of the task is being raised. If a running
2933 * task has priority lower than this task, it should yield
2935 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2938 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2940 mtCOVERAGE_TEST_MARKER();
2944 /* Remove compiler warning about unused variables when the port
2945 * optimised task selection is not being used. */
2946 ( void ) uxPriorityUsedOnEntry;
2949 taskEXIT_CRITICAL();
2951 traceRETURN_vTaskPrioritySet();
2954 #endif /* INCLUDE_vTaskPrioritySet */
2955 /*-----------------------------------------------------------*/
2957 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2958 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2959 UBaseType_t uxCoreAffinityMask )
2963 UBaseType_t uxPrevCoreAffinityMask;
2965 #if ( configUSE_PREEMPTION == 1 )
2966 UBaseType_t uxPrevNotAllowedCores;
2969 traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
2971 taskENTER_CRITICAL();
2973 pxTCB = prvGetTCBFromHandle( xTask );
2975 uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2976 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2978 if( xSchedulerRunning != pdFALSE )
2980 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2982 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2984 /* If the task can no longer run on the core it was running,
2985 * request the core to yield. */
2986 if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
2988 prvYieldCore( xCoreID );
2993 #if ( configUSE_PREEMPTION == 1 )
2995 /* Calculate the cores on which this task was not allowed to
2996 * run previously. */
2997 uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
2999 /* Does the new core mask enables this task to run on any of the
3000 * previously not allowed cores? If yes, check if this task can be
3001 * scheduled on any of those cores. */
3002 if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
3004 prvYieldForTask( pxTCB );
3007 #else /* #if( configUSE_PREEMPTION == 1 ) */
3009 mtCOVERAGE_TEST_MARKER();
3011 #endif /* #if( configUSE_PREEMPTION == 1 ) */
3015 taskEXIT_CRITICAL();
3017 traceRETURN_vTaskCoreAffinitySet();
3019 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3020 /*-----------------------------------------------------------*/
3022 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
3023 UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
3025 const TCB_t * pxTCB;
3026 UBaseType_t uxCoreAffinityMask;
3028 traceENTER_vTaskCoreAffinityGet( xTask );
3030 taskENTER_CRITICAL();
3032 pxTCB = prvGetTCBFromHandle( xTask );
3033 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
3035 taskEXIT_CRITICAL();
3037 traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
3039 return uxCoreAffinityMask;
3041 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
3043 /*-----------------------------------------------------------*/
3045 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3047 void vTaskPreemptionDisable( const TaskHandle_t xTask )
3051 traceENTER_vTaskPreemptionDisable( xTask );
3053 taskENTER_CRITICAL();
3055 pxTCB = prvGetTCBFromHandle( xTask );
3057 pxTCB->xPreemptionDisable = pdTRUE;
3059 taskEXIT_CRITICAL();
3061 traceRETURN_vTaskPreemptionDisable();
3064 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3065 /*-----------------------------------------------------------*/
3067 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3069 void vTaskPreemptionEnable( const TaskHandle_t xTask )
3074 traceENTER_vTaskPreemptionEnable( xTask );
3076 taskENTER_CRITICAL();
3078 pxTCB = prvGetTCBFromHandle( xTask );
3080 pxTCB->xPreemptionDisable = pdFALSE;
3082 if( xSchedulerRunning != pdFALSE )
3084 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3086 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
3087 prvYieldCore( xCoreID );
3091 taskEXIT_CRITICAL();
3093 traceRETURN_vTaskPreemptionEnable();
3096 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
3097 /*-----------------------------------------------------------*/
3099 #if ( INCLUDE_vTaskSuspend == 1 )
3101 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
3105 #if ( configNUMBER_OF_CORES > 1 )
3106 BaseType_t xTaskRunningOnCore;
3109 traceENTER_vTaskSuspend( xTaskToSuspend );
3111 taskENTER_CRITICAL();
3113 /* If null is passed in here then it is the running task that is
3114 * being suspended. */
3115 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
3117 traceTASK_SUSPEND( pxTCB );
3119 #if ( configNUMBER_OF_CORES > 1 )
3120 xTaskRunningOnCore = pxTCB->xTaskRunState;
3123 /* Remove task from the ready/delayed list and place in the
3124 * suspended list. */
3125 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
3127 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
3131 mtCOVERAGE_TEST_MARKER();
3134 /* Is the task waiting on an event also? */
3135 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3137 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3141 mtCOVERAGE_TEST_MARKER();
3144 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
3146 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3150 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3152 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3154 /* The task was blocked to wait for a notification, but is
3155 * now suspended, so no notification was received. */
3156 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
3160 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3163 #if ( configNUMBER_OF_CORES == 1 )
3165 taskEXIT_CRITICAL();
3167 if( xSchedulerRunning != pdFALSE )
3169 /* Reset the next expected unblock time in case it referred to the
3170 * task that is now in the Suspended state. */
3171 taskENTER_CRITICAL();
3173 prvResetNextTaskUnblockTime();
3175 taskEXIT_CRITICAL();
3179 mtCOVERAGE_TEST_MARKER();
3182 if( pxTCB == pxCurrentTCB )
3184 if( xSchedulerRunning != pdFALSE )
3186 /* The current task has just been suspended. */
3187 configASSERT( uxSchedulerSuspended == 0 );
3188 portYIELD_WITHIN_API();
3192 /* The scheduler is not running, but the task that was pointed
3193 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
3194 * must be adjusted to point to a different task. */
3195 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
3197 /* No other tasks are ready, so set pxCurrentTCB back to
3198 * NULL so when the next task is created pxCurrentTCB will
3199 * be set to point to it no matter what its relative priority
3201 pxCurrentTCB = NULL;
3205 vTaskSwitchContext();
3211 mtCOVERAGE_TEST_MARKER();
3214 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3216 if( xSchedulerRunning != pdFALSE )
3218 /* Reset the next expected unblock time in case it referred to the
3219 * task that is now in the Suspended state. */
3220 prvResetNextTaskUnblockTime();
3224 mtCOVERAGE_TEST_MARKER();
3227 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
3229 if( xSchedulerRunning != pdFALSE )
3231 if( xTaskRunningOnCore == ( BaseType_t ) portGET_CORE_ID() )
3233 /* The current task has just been suspended. */
3234 configASSERT( uxSchedulerSuspended == 0 );
3235 vTaskYieldWithinAPI();
3239 prvYieldCore( xTaskRunningOnCore );
3244 /* This code path is not possible because only Idle tasks are
3245 * assigned a core before the scheduler is started ( i.e.
3246 * taskTASK_IS_RUNNING is only true for idle tasks before
3247 * the scheduler is started ) and idle tasks cannot be
3249 mtCOVERAGE_TEST_MARKER();
3254 mtCOVERAGE_TEST_MARKER();
3257 taskEXIT_CRITICAL();
3259 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3261 traceRETURN_vTaskSuspend();
3264 #endif /* INCLUDE_vTaskSuspend */
3265 /*-----------------------------------------------------------*/
3267 #if ( INCLUDE_vTaskSuspend == 1 )
3269 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
3271 BaseType_t xReturn = pdFALSE;
3272 const TCB_t * const pxTCB = xTask;
3274 /* Accesses xPendingReadyList so must be called from a critical
3277 /* It does not make sense to check if the calling task is suspended. */
3278 configASSERT( xTask );
3280 /* Is the task being resumed actually in the suspended list? */
3281 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
3283 /* Has the task already been resumed from within an ISR? */
3284 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
3286 /* Is it in the suspended list because it is in the Suspended
3287 * state, or because it is blocked with no timeout? */
3288 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
3290 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
3294 /* The task does not appear on the event list item of
3295 * and of the RTOS objects, but could still be in the
3296 * blocked state if it is waiting on its notification
3297 * rather than waiting on an object. If not, is
3301 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
3303 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
3310 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3314 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
3318 mtCOVERAGE_TEST_MARKER();
3323 mtCOVERAGE_TEST_MARKER();
3328 mtCOVERAGE_TEST_MARKER();
3332 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
3334 #endif /* INCLUDE_vTaskSuspend */
3335 /*-----------------------------------------------------------*/
3337 #if ( INCLUDE_vTaskSuspend == 1 )
3339 void vTaskResume( TaskHandle_t xTaskToResume )
3341 TCB_t * const pxTCB = xTaskToResume;
3343 traceENTER_vTaskResume( xTaskToResume );
3345 /* It does not make sense to resume the calling task. */
3346 configASSERT( xTaskToResume );
3348 #if ( configNUMBER_OF_CORES == 1 )
3350 /* The parameter cannot be NULL as it is impossible to resume the
3351 * currently executing task. */
3352 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
3355 /* The parameter cannot be NULL as it is impossible to resume the
3356 * currently executing task. It is also impossible to resume a task
3357 * that is actively running on another core but it is not safe
3358 * to check their run state here. Therefore, we get into a critical
3359 * section and check if the task is actually suspended or not. */
3363 taskENTER_CRITICAL();
3365 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3367 traceTASK_RESUME( pxTCB );
3369 /* The ready list can be accessed even if the scheduler is
3370 * suspended because this is inside a critical section. */
3371 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3372 prvAddTaskToReadyList( pxTCB );
3374 /* This yield may not cause the task just resumed to run,
3375 * but will leave the lists in the correct state for the
3377 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
3381 mtCOVERAGE_TEST_MARKER();
3384 taskEXIT_CRITICAL();
3388 mtCOVERAGE_TEST_MARKER();
3391 traceRETURN_vTaskResume();
3394 #endif /* INCLUDE_vTaskSuspend */
3396 /*-----------------------------------------------------------*/
3398 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
3400 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
3402 BaseType_t xYieldRequired = pdFALSE;
3403 TCB_t * const pxTCB = xTaskToResume;
3404 UBaseType_t uxSavedInterruptStatus;
3406 traceENTER_xTaskResumeFromISR( xTaskToResume );
3408 configASSERT( xTaskToResume );
3410 /* RTOS ports that support interrupt nesting have the concept of a
3411 * maximum system call (or maximum API call) interrupt priority.
3412 * Interrupts that are above the maximum system call priority are keep
3413 * permanently enabled, even when the RTOS kernel is in a critical section,
3414 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
3415 * is defined in FreeRTOSConfig.h then
3416 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3417 * failure if a FreeRTOS API function is called from an interrupt that has
3418 * been assigned a priority above the configured maximum system call
3419 * priority. Only FreeRTOS functions that end in FromISR can be called
3420 * from interrupts that have been assigned a priority at or (logically)
3421 * below the maximum system call interrupt priority. FreeRTOS maintains a
3422 * separate interrupt safe API to ensure interrupt entry is as fast and as
3423 * simple as possible. More information (albeit Cortex-M specific) is
3424 * provided on the following link:
3425 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3426 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3428 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3430 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3432 traceTASK_RESUME_FROM_ISR( pxTCB );
3434 /* Check the ready lists can be accessed. */
3435 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3437 #if ( configNUMBER_OF_CORES == 1 )
3439 /* Ready lists can be accessed so move the task from the
3440 * suspended list to the ready list directly. */
3441 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3443 xYieldRequired = pdTRUE;
3445 /* Mark that a yield is pending in case the user is not
3446 * using the return value to initiate a context switch
3447 * from the ISR using portYIELD_FROM_ISR. */
3448 xYieldPendings[ 0 ] = pdTRUE;
3452 mtCOVERAGE_TEST_MARKER();
3455 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3457 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3458 prvAddTaskToReadyList( pxTCB );
3462 /* The delayed or ready lists cannot be accessed so the task
3463 * is held in the pending ready list until the scheduler is
3465 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3468 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3470 prvYieldForTask( pxTCB );
3472 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3474 xYieldRequired = pdTRUE;
3477 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3481 mtCOVERAGE_TEST_MARKER();
3484 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3486 traceRETURN_xTaskResumeFromISR( xYieldRequired );
3488 return xYieldRequired;
3491 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3492 /*-----------------------------------------------------------*/
3494 static BaseType_t prvCreateIdleTasks( void )
3496 BaseType_t xReturn = pdPASS;
3498 char cIdleName[ configMAX_TASK_NAME_LEN ];
3499 TaskFunction_t pxIdleTaskFunction = NULL;
3500 BaseType_t xIdleTaskNameIndex;
3502 for( xIdleTaskNameIndex = ( BaseType_t ) 0; xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN; xIdleTaskNameIndex++ )
3504 cIdleName[ xIdleTaskNameIndex ] = configIDLE_TASK_NAME[ xIdleTaskNameIndex ];
3506 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3507 * configMAX_TASK_NAME_LEN characters just in case the memory after the
3508 * string is not accessible (extremely unlikely). */
3509 if( cIdleName[ xIdleTaskNameIndex ] == ( char ) 0x00 )
3515 mtCOVERAGE_TEST_MARKER();
3519 /* Add each idle task at the lowest priority. */
3520 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3522 #if ( configNUMBER_OF_CORES == 1 )
3524 pxIdleTaskFunction = prvIdleTask;
3526 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3528 /* In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks
3529 * are also created to ensure that each core has an idle task to
3530 * run when no other task is available to run. */
3533 pxIdleTaskFunction = prvIdleTask;
3537 pxIdleTaskFunction = prvPassiveIdleTask;
3540 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3542 /* Update the idle task name with suffix to differentiate the idle tasks.
3543 * This function is not required in single core FreeRTOS since there is
3544 * only one idle task. */
3545 #if ( configNUMBER_OF_CORES > 1 )
3547 /* Append the idle task number to the end of the name if there is space. */
3548 if( xIdleTaskNameIndex < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3550 cIdleName[ xIdleTaskNameIndex ] = ( char ) ( xCoreID + '0' );
3552 /* And append a null character if there is space. */
3553 if( ( xIdleTaskNameIndex + 1 ) < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3555 cIdleName[ xIdleTaskNameIndex + 1 ] = '\0';
3559 mtCOVERAGE_TEST_MARKER();
3564 mtCOVERAGE_TEST_MARKER();
3567 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
3569 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3571 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3572 StackType_t * pxIdleTaskStackBuffer = NULL;
3573 uint32_t ulIdleTaskStackSize;
3575 /* The Idle task is created using user provided RAM - obtain the
3576 * address of the RAM then create the idle task. */
3577 #if ( configNUMBER_OF_CORES == 1 )
3578 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3580 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize, xCoreID );
3582 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( pxIdleTaskFunction,
3584 ulIdleTaskStackSize,
3585 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3586 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3587 pxIdleTaskStackBuffer,
3588 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3590 if( xIdleTaskHandles[ xCoreID ] != NULL )
3599 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3601 /* The Idle task is being created using dynamically allocated RAM. */
3602 xReturn = xTaskCreate( pxIdleTaskFunction,
3604 configMINIMAL_STACK_SIZE,
3606 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3607 &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3609 #endif /* configSUPPORT_STATIC_ALLOCATION */
3611 /* Break the loop if any of the idle task is failed to be created. */
3612 if( xReturn == pdFAIL )
3618 mtCOVERAGE_TEST_MARKER();
3625 /*-----------------------------------------------------------*/
3627 void vTaskStartScheduler( void )
3631 traceENTER_vTaskStartScheduler();
3633 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3635 /* Sanity check that the UBaseType_t must have greater than or equal to
3636 * the number of bits as confNUMBER_OF_CORES. */
3637 configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3639 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3641 xReturn = prvCreateIdleTasks();
3643 #if ( configUSE_TIMERS == 1 )
3645 if( xReturn == pdPASS )
3647 xReturn = xTimerCreateTimerTask();
3651 mtCOVERAGE_TEST_MARKER();
3654 #endif /* configUSE_TIMERS */
3656 if( xReturn == pdPASS )
3658 /* freertos_tasks_c_additions_init() should only be called if the user
3659 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3660 * the only macro called by the function. */
3661 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3663 freertos_tasks_c_additions_init();
3667 /* Interrupts are turned off here, to ensure a tick does not occur
3668 * before or during the call to xPortStartScheduler(). The stacks of
3669 * the created tasks contain a status word with interrupts switched on
3670 * so interrupts will automatically get re-enabled when the first task
3672 portDISABLE_INTERRUPTS();
3674 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3676 /* Switch C-Runtime's TLS Block to point to the TLS
3677 * block specific to the task that will run first. */
3678 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3682 xNextTaskUnblockTime = portMAX_DELAY;
3683 xSchedulerRunning = pdTRUE;
3684 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3686 /* If configGENERATE_RUN_TIME_STATS is defined then the following
3687 * macro must be defined to configure the timer/counter used to generate
3688 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
3689 * is set to 0 and the following line fails to build then ensure you do not
3690 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3691 * FreeRTOSConfig.h file. */
3692 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3694 traceTASK_SWITCHED_IN();
3696 /* Setting up the timer tick is hardware specific and thus in the
3697 * portable interface. */
3698 xPortStartScheduler();
3700 /* In most cases, xPortStartScheduler() will not return. If it
3701 * returns pdTRUE then there was not enough heap memory available
3702 * to create either the Idle or the Timer task. If it returned
3703 * pdFALSE, then the application called xTaskEndScheduler().
3704 * Most ports don't implement xTaskEndScheduler() as there is
3705 * nothing to return to. */
3709 /* This line will only be reached if the kernel could not be started,
3710 * because there was not enough FreeRTOS heap to create the idle task
3711 * or the timer task. */
3712 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3715 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3716 * meaning xIdleTaskHandles are not used anywhere else. */
3717 ( void ) xIdleTaskHandles;
3719 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3720 * from getting optimized out as it is no longer used by the kernel. */
3721 ( void ) uxTopUsedPriority;
3723 traceRETURN_vTaskStartScheduler();
3725 /*-----------------------------------------------------------*/
3727 void vTaskEndScheduler( void )
3729 traceENTER_vTaskEndScheduler();
3731 /* Stop the scheduler interrupts and call the portable scheduler end
3732 * routine so the original ISRs can be restored if necessary. The port
3733 * layer must ensure interrupts enable bit is left in the correct state. */
3734 portDISABLE_INTERRUPTS();
3735 xSchedulerRunning = pdFALSE;
3736 vPortEndScheduler();
3738 traceRETURN_vTaskEndScheduler();
3740 /*----------------------------------------------------------*/
3742 void vTaskSuspendAll( void )
3744 traceENTER_vTaskSuspendAll();
3746 #if ( configNUMBER_OF_CORES == 1 )
3748 /* A critical section is not required as the variable is of type
3749 * BaseType_t. Please read Richard Barry's reply in the following link to a
3750 * post in the FreeRTOS support forum before reporting this as a bug! -
3751 * https://goo.gl/wu4acr */
3753 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3754 * do not otherwise exhibit real time behaviour. */
3755 portSOFTWARE_BARRIER();
3757 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3758 * is used to allow calls to vTaskSuspendAll() to nest. */
3759 ++uxSchedulerSuspended;
3761 /* Enforces ordering for ports and optimised compilers that may otherwise place
3762 * the above increment elsewhere. */
3763 portMEMORY_BARRIER();
3765 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3767 UBaseType_t ulState;
3769 /* This must only be called from within a task. */
3770 portASSERT_IF_IN_ISR();
3772 if( xSchedulerRunning != pdFALSE )
3774 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3775 * We must disable interrupts before we grab the locks in the event that this task is
3776 * interrupted and switches context before incrementing uxSchedulerSuspended.
3777 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3778 * uxSchedulerSuspended since that will prevent context switches. */
3779 ulState = portSET_INTERRUPT_MASK();
3781 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3782 * do not otherwise exhibit real time behaviour. */
3783 portSOFTWARE_BARRIER();
3785 portGET_TASK_LOCK();
3787 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3788 * purpose is to prevent altering the variable when fromISR APIs are readying
3790 if( uxSchedulerSuspended == 0U )
3792 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
3794 prvCheckForRunStateChange();
3798 mtCOVERAGE_TEST_MARKER();
3803 mtCOVERAGE_TEST_MARKER();
3808 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3809 * is used to allow calls to vTaskSuspendAll() to nest. */
3810 ++uxSchedulerSuspended;
3811 portRELEASE_ISR_LOCK();
3813 portCLEAR_INTERRUPT_MASK( ulState );
3817 mtCOVERAGE_TEST_MARKER();
3820 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3822 traceRETURN_vTaskSuspendAll();
3825 /*----------------------------------------------------------*/
3827 #if ( configUSE_TICKLESS_IDLE != 0 )
3829 static TickType_t prvGetExpectedIdleTime( void )
3832 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
3834 /* uxHigherPriorityReadyTasks takes care of the case where
3835 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3836 * task that are in the Ready state, even though the idle task is
3838 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3840 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3842 uxHigherPriorityReadyTasks = pdTRUE;
3847 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3849 /* When port optimised task selection is used the uxTopReadyPriority
3850 * variable is used as a bit map. If bits other than the least
3851 * significant bit are set then there are tasks that have a priority
3852 * above the idle priority that are in the Ready state. This takes
3853 * care of the case where the co-operative scheduler is in use. */
3854 if( uxTopReadyPriority > uxLeastSignificantBit )
3856 uxHigherPriorityReadyTasks = pdTRUE;
3859 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3861 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3865 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
3867 /* There are other idle priority tasks in the ready state. If
3868 * time slicing is used then the very next tick interrupt must be
3872 else if( uxHigherPriorityReadyTasks != pdFALSE )
3874 /* There are tasks in the Ready state that have a priority above the
3875 * idle priority. This path can only be reached if
3876 * configUSE_PREEMPTION is 0. */
3881 xReturn = xNextTaskUnblockTime - xTickCount;
3887 #endif /* configUSE_TICKLESS_IDLE */
3888 /*----------------------------------------------------------*/
3890 BaseType_t xTaskResumeAll( void )
3892 TCB_t * pxTCB = NULL;
3893 BaseType_t xAlreadyYielded = pdFALSE;
3895 traceENTER_xTaskResumeAll();
3897 #if ( configNUMBER_OF_CORES > 1 )
3898 if( xSchedulerRunning != pdFALSE )
3901 /* It is possible that an ISR caused a task to be removed from an event
3902 * list while the scheduler was suspended. If this was the case then the
3903 * removed task will have been added to the xPendingReadyList. Once the
3904 * scheduler has been resumed it is safe to move all the pending ready
3905 * tasks from this list into their appropriate ready list. */
3906 taskENTER_CRITICAL();
3909 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3911 /* If uxSchedulerSuspended is zero then this function does not match a
3912 * previous call to vTaskSuspendAll(). */
3913 configASSERT( uxSchedulerSuspended != 0U );
3915 --uxSchedulerSuspended;
3916 portRELEASE_TASK_LOCK();
3918 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3920 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3922 /* Move any readied tasks from the pending list into the
3923 * appropriate ready list. */
3924 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3926 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. */
3927 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
3928 portMEMORY_BARRIER();
3929 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
3930 prvAddTaskToReadyList( pxTCB );
3932 #if ( configNUMBER_OF_CORES == 1 )
3934 /* If the moved task has a priority higher than the current
3935 * task then a yield must be performed. */
3936 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3938 xYieldPendings[ xCoreID ] = pdTRUE;
3942 mtCOVERAGE_TEST_MARKER();
3945 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3947 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
3948 * If the current core yielded then vTaskSwitchContext() has already been called
3949 * which sets xYieldPendings for the current core to pdTRUE. */
3951 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3956 /* A task was unblocked while the scheduler was suspended,
3957 * which may have prevented the next unblock time from being
3958 * re-calculated, in which case re-calculate it now. Mainly
3959 * important for low power tickless implementations, where
3960 * this can prevent an unnecessary exit from low power
3962 prvResetNextTaskUnblockTime();
3965 /* If any ticks occurred while the scheduler was suspended then
3966 * they should be processed now. This ensures the tick count does
3967 * not slip, and that any delayed tasks are resumed at the correct
3970 * It should be safe to call xTaskIncrementTick here from any core
3971 * since we are in a critical section and xTaskIncrementTick itself
3972 * protects itself within a critical section. Suspending the scheduler
3973 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
3975 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3977 if( xPendedCounts > ( TickType_t ) 0U )
3981 if( xTaskIncrementTick() != pdFALSE )
3983 /* Other cores are interrupted from
3984 * within xTaskIncrementTick(). */
3985 xYieldPendings[ xCoreID ] = pdTRUE;
3989 mtCOVERAGE_TEST_MARKER();
3993 } while( xPendedCounts > ( TickType_t ) 0U );
3999 mtCOVERAGE_TEST_MARKER();
4003 if( xYieldPendings[ xCoreID ] != pdFALSE )
4005 #if ( configUSE_PREEMPTION != 0 )
4007 xAlreadyYielded = pdTRUE;
4009 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
4011 #if ( configNUMBER_OF_CORES == 1 )
4013 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
4015 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4019 mtCOVERAGE_TEST_MARKER();
4025 mtCOVERAGE_TEST_MARKER();
4028 taskEXIT_CRITICAL();
4031 traceRETURN_xTaskResumeAll( xAlreadyYielded );
4033 return xAlreadyYielded;
4035 /*-----------------------------------------------------------*/
4037 TickType_t xTaskGetTickCount( void )
4041 traceENTER_xTaskGetTickCount();
4043 /* Critical section required if running on a 16 bit processor. */
4044 portTICK_TYPE_ENTER_CRITICAL();
4046 xTicks = xTickCount;
4048 portTICK_TYPE_EXIT_CRITICAL();
4050 traceRETURN_xTaskGetTickCount( xTicks );
4054 /*-----------------------------------------------------------*/
4056 TickType_t xTaskGetTickCountFromISR( void )
4059 UBaseType_t uxSavedInterruptStatus;
4061 traceENTER_xTaskGetTickCountFromISR();
4063 /* RTOS ports that support interrupt nesting have the concept of a maximum
4064 * system call (or maximum API call) interrupt priority. Interrupts that are
4065 * above the maximum system call priority are kept permanently enabled, even
4066 * when the RTOS kernel is in a critical section, but cannot make any calls to
4067 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
4068 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
4069 * failure if a FreeRTOS API function is called from an interrupt that has been
4070 * assigned a priority above the configured maximum system call priority.
4071 * Only FreeRTOS functions that end in FromISR can be called from interrupts
4072 * that have been assigned a priority at or (logically) below the maximum
4073 * system call interrupt priority. FreeRTOS maintains a separate interrupt
4074 * safe API to ensure interrupt entry is as fast and as simple as possible.
4075 * More information (albeit Cortex-M specific) is provided on the following
4076 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
4077 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
4079 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
4081 xReturn = xTickCount;
4083 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
4085 traceRETURN_xTaskGetTickCountFromISR( xReturn );
4089 /*-----------------------------------------------------------*/
4091 UBaseType_t uxTaskGetNumberOfTasks( void )
4093 traceENTER_uxTaskGetNumberOfTasks();
4095 /* A critical section is not required because the variables are of type
4097 traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
4099 return uxCurrentNumberOfTasks;
4101 /*-----------------------------------------------------------*/
4103 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
4107 traceENTER_pcTaskGetName( xTaskToQuery );
4109 /* If null is passed in here then the name of the calling task is being
4111 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4112 configASSERT( pxTCB );
4114 traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
4116 return &( pxTCB->pcTaskName[ 0 ] );
4118 /*-----------------------------------------------------------*/
4120 #if ( INCLUDE_xTaskGetHandle == 1 )
4122 #if ( configNUMBER_OF_CORES == 1 )
4123 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4124 const char pcNameToQuery[] )
4128 TCB_t * pxReturn = NULL;
4131 BaseType_t xBreakLoop;
4133 /* This function is called with the scheduler suspended. */
4135 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4137 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. */
4141 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. */
4143 /* Check each character in the name looking for a match or
4145 xBreakLoop = pdFALSE;
4147 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4149 cNextChar = pxNextTCB->pcTaskName[ x ];
4151 if( cNextChar != pcNameToQuery[ x ] )
4153 /* Characters didn't match. */
4154 xBreakLoop = pdTRUE;
4156 else if( cNextChar == ( char ) 0x00 )
4158 /* Both strings terminated, a match must have been
4160 pxReturn = pxNextTCB;
4161 xBreakLoop = pdTRUE;
4165 mtCOVERAGE_TEST_MARKER();
4168 if( xBreakLoop != pdFALSE )
4174 if( pxReturn != NULL )
4176 /* The handle has been found. */
4179 } while( pxNextTCB != pxFirstTCB );
4183 mtCOVERAGE_TEST_MARKER();
4188 #else /* if ( configNUMBER_OF_CORES == 1 ) */
4189 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
4190 const char pcNameToQuery[] )
4192 TCB_t * pxReturn = NULL;
4195 BaseType_t xBreakLoop;
4196 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
4197 ListItem_t * pxIterator;
4199 /* This function is called with the scheduler suspended. */
4201 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4203 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
4205 TCB_t * pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
4207 /* Check each character in the name looking for a match or
4209 xBreakLoop = pdFALSE;
4211 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
4213 cNextChar = pxTCB->pcTaskName[ x ];
4215 if( cNextChar != pcNameToQuery[ x ] )
4217 /* Characters didn't match. */
4218 xBreakLoop = pdTRUE;
4220 else if( cNextChar == ( char ) 0x00 )
4222 /* Both strings terminated, a match must have been
4225 xBreakLoop = pdTRUE;
4229 mtCOVERAGE_TEST_MARKER();
4232 if( xBreakLoop != pdFALSE )
4238 if( pxReturn != NULL )
4240 /* The handle has been found. */
4247 mtCOVERAGE_TEST_MARKER();
4252 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4254 #endif /* INCLUDE_xTaskGetHandle */
4255 /*-----------------------------------------------------------*/
4257 #if ( INCLUDE_xTaskGetHandle == 1 )
4259 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
4261 UBaseType_t uxQueue = configMAX_PRIORITIES;
4264 traceENTER_xTaskGetHandle( pcNameToQuery );
4266 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
4267 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
4271 /* Search the ready lists. */
4275 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
4279 /* Found the handle. */
4282 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4284 /* Search the delayed lists. */
4287 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
4292 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
4295 #if ( INCLUDE_vTaskSuspend == 1 )
4299 /* Search the suspended list. */
4300 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
4305 #if ( INCLUDE_vTaskDelete == 1 )
4309 /* Search the deleted list. */
4310 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
4315 ( void ) xTaskResumeAll();
4317 traceRETURN_xTaskGetHandle( pxTCB );
4322 #endif /* INCLUDE_xTaskGetHandle */
4323 /*-----------------------------------------------------------*/
4325 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4327 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4328 StackType_t ** ppuxStackBuffer,
4329 StaticTask_t ** ppxTaskBuffer )
4334 traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4336 configASSERT( ppuxStackBuffer != NULL );
4337 configASSERT( ppxTaskBuffer != NULL );
4339 pxTCB = prvGetTCBFromHandle( xTask );
4341 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4343 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4345 *ppuxStackBuffer = pxTCB->pxStack;
4346 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4349 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4351 *ppuxStackBuffer = pxTCB->pxStack;
4352 *ppxTaskBuffer = NULL;
4360 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4362 *ppuxStackBuffer = pxTCB->pxStack;
4363 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4366 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4368 traceRETURN_xTaskGetStaticBuffers( xReturn );
4373 #endif /* configSUPPORT_STATIC_ALLOCATION */
4374 /*-----------------------------------------------------------*/
4376 #if ( configUSE_TRACE_FACILITY == 1 )
4378 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4379 const UBaseType_t uxArraySize,
4380 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4382 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4384 traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4388 /* Is there a space in the array for each task in the system? */
4389 if( uxArraySize >= uxCurrentNumberOfTasks )
4391 /* Fill in an TaskStatus_t structure with information on each
4392 * task in the Ready state. */
4396 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4397 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4399 /* Fill in an TaskStatus_t structure with information on each
4400 * task in the Blocked state. */
4401 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4402 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4404 #if ( INCLUDE_vTaskDelete == 1 )
4406 /* Fill in an TaskStatus_t structure with information on
4407 * each task that has been deleted but not yet cleaned up. */
4408 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4412 #if ( INCLUDE_vTaskSuspend == 1 )
4414 /* Fill in an TaskStatus_t structure with information on
4415 * each task in the Suspended state. */
4416 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4420 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4422 if( pulTotalRunTime != NULL )
4424 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4425 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4427 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4431 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4433 if( pulTotalRunTime != NULL )
4435 *pulTotalRunTime = 0;
4438 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4442 mtCOVERAGE_TEST_MARKER();
4445 ( void ) xTaskResumeAll();
4447 traceRETURN_uxTaskGetSystemState( uxTask );
4452 #endif /* configUSE_TRACE_FACILITY */
4453 /*----------------------------------------------------------*/
4455 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4457 #if ( configNUMBER_OF_CORES == 1 )
4459 TaskHandle_t xTaskGetIdleTaskHandle( void )
4461 traceENTER_xTaskGetIdleTaskHandle();
4463 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4464 * started, then xIdleTaskHandles will be NULL. */
4465 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4467 traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4469 return xIdleTaskHandles[ 0 ];
4472 #else /* if ( configNUMBER_OF_CORES == 1 ) */
4474 TaskHandle_t xTaskGetIdleTaskHandle( BaseType_t xCoreID )
4476 traceENTER_xTaskGetIdleTaskHandle( xCoreID );
4478 /* Ensure the core ID is valid. */
4479 configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE );
4481 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4482 * started, then xIdleTaskHandles will be NULL. */
4483 configASSERT( ( xIdleTaskHandles[ xCoreID ] != NULL ) );
4485 traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ xCoreID ] );
4487 return xIdleTaskHandles[ xCoreID ];
4490 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
4492 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4493 /*----------------------------------------------------------*/
4495 /* This conditional compilation should use inequality to 0, not equality to 1.
4496 * This is to ensure vTaskStepTick() is available when user defined low power mode
4497 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4499 #if ( configUSE_TICKLESS_IDLE != 0 )
4501 void vTaskStepTick( TickType_t xTicksToJump )
4503 traceENTER_vTaskStepTick( xTicksToJump );
4505 /* Correct the tick count value after a period during which the tick
4506 * was suppressed. Note this does *not* call the tick hook function for
4507 * each stepped tick. */
4508 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
4510 if( ( xTickCount + xTicksToJump ) == xNextTaskUnblockTime )
4512 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4513 * xTaskIncrementTick() when the scheduler resumes. This ensures
4514 * that any delayed tasks are resumed at the correct time. */
4515 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4516 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4518 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4519 taskENTER_CRITICAL();
4523 taskEXIT_CRITICAL();
4528 mtCOVERAGE_TEST_MARKER();
4531 xTickCount += xTicksToJump;
4533 traceINCREASE_TICK_COUNT( xTicksToJump );
4534 traceRETURN_vTaskStepTick();
4537 #endif /* configUSE_TICKLESS_IDLE */
4538 /*----------------------------------------------------------*/
4540 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4542 BaseType_t xYieldOccurred;
4544 traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4546 /* Must not be called with the scheduler suspended as the implementation
4547 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4548 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4550 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4551 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4554 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4555 taskENTER_CRITICAL();
4557 xPendedTicks += xTicksToCatchUp;
4559 taskEXIT_CRITICAL();
4560 xYieldOccurred = xTaskResumeAll();
4562 traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4564 return xYieldOccurred;
4566 /*----------------------------------------------------------*/
4568 #if ( INCLUDE_xTaskAbortDelay == 1 )
4570 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4572 TCB_t * pxTCB = xTask;
4575 traceENTER_xTaskAbortDelay( xTask );
4577 configASSERT( pxTCB );
4581 /* A task can only be prematurely removed from the Blocked state if
4582 * it is actually in the Blocked state. */
4583 if( eTaskGetState( xTask ) == eBlocked )
4587 /* Remove the reference to the task from the blocked list. An
4588 * interrupt won't touch the xStateListItem because the
4589 * scheduler is suspended. */
4590 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4592 /* Is the task waiting on an event also? If so remove it from
4593 * the event list too. Interrupts can touch the event list item,
4594 * even though the scheduler is suspended, so a critical section
4596 taskENTER_CRITICAL();
4598 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4600 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4602 /* This lets the task know it was forcibly removed from the
4603 * blocked state so it should not re-evaluate its block time and
4604 * then block again. */
4605 pxTCB->ucDelayAborted = pdTRUE;
4609 mtCOVERAGE_TEST_MARKER();
4612 taskEXIT_CRITICAL();
4614 /* Place the unblocked task into the appropriate ready list. */
4615 prvAddTaskToReadyList( pxTCB );
4617 /* A task being unblocked cannot cause an immediate context
4618 * switch if preemption is turned off. */
4619 #if ( configUSE_PREEMPTION == 1 )
4621 #if ( configNUMBER_OF_CORES == 1 )
4623 /* Preemption is on, but a context switch should only be
4624 * performed if the unblocked task has a priority that is
4625 * higher than the currently executing task. */
4626 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4628 /* Pend the yield to be performed when the scheduler
4629 * is unsuspended. */
4630 xYieldPendings[ 0 ] = pdTRUE;
4634 mtCOVERAGE_TEST_MARKER();
4637 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4639 taskENTER_CRITICAL();
4641 prvYieldForTask( pxTCB );
4643 taskEXIT_CRITICAL();
4645 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4647 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4654 ( void ) xTaskResumeAll();
4656 traceRETURN_xTaskAbortDelay( xReturn );
4661 #endif /* INCLUDE_xTaskAbortDelay */
4662 /*----------------------------------------------------------*/
4664 BaseType_t xTaskIncrementTick( void )
4667 TickType_t xItemValue;
4668 BaseType_t xSwitchRequired = pdFALSE;
4670 #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4671 BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4672 #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4674 traceENTER_xTaskIncrementTick();
4676 /* Called by the portable layer each time a tick interrupt occurs.
4677 * Increments the tick then checks to see if the new tick value will cause any
4678 * tasks to be unblocked. */
4679 traceTASK_INCREMENT_TICK( xTickCount );
4681 /* Tick increment should occur on every kernel timer event. Core 0 has the
4682 * responsibility to increment the tick, or increment the pended ticks if the
4683 * scheduler is suspended. If pended ticks is greater than zero, the core that
4684 * calls xTaskResumeAll has the responsibility to increment the tick. */
4685 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4687 /* Minor optimisation. The tick count cannot change in this
4689 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4691 /* Increment the RTOS tick, switching the delayed and overflowed
4692 * delayed lists if it wraps to 0. */
4693 xTickCount = xConstTickCount;
4695 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
4697 taskSWITCH_DELAYED_LISTS();
4701 mtCOVERAGE_TEST_MARKER();
4704 /* See if this tick has made a timeout expire. Tasks are stored in
4705 * the queue in the order of their wake time - meaning once one task
4706 * has been found whose block time has not expired there is no need to
4707 * look any further down the list. */
4708 if( xConstTickCount >= xNextTaskUnblockTime )
4712 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4714 /* The delayed list is empty. Set xNextTaskUnblockTime
4715 * to the maximum possible value so it is extremely
4717 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4718 * next time through. */
4719 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4724 /* The delayed list is not empty, get the value of the
4725 * item at the head of the delayed list. This is the time
4726 * at which the task at the head of the delayed list must
4727 * be removed from the Blocked state. */
4728 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. */
4729 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4731 if( xConstTickCount < xItemValue )
4733 /* It is not time to unblock this item yet, but the
4734 * item value is the time at which the task at the head
4735 * of the blocked list must be removed from the Blocked
4736 * state - so record the item value in
4737 * xNextTaskUnblockTime. */
4738 xNextTaskUnblockTime = xItemValue;
4739 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
4743 mtCOVERAGE_TEST_MARKER();
4746 /* It is time to remove the item from the Blocked state. */
4747 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4749 /* Is the task waiting on an event also? If so remove
4750 * it from the event list. */
4751 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4753 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4757 mtCOVERAGE_TEST_MARKER();
4760 /* Place the unblocked task into the appropriate ready
4762 prvAddTaskToReadyList( pxTCB );
4764 /* A task being unblocked cannot cause an immediate
4765 * context switch if preemption is turned off. */
4766 #if ( configUSE_PREEMPTION == 1 )
4768 #if ( configNUMBER_OF_CORES == 1 )
4770 /* Preemption is on, but a context switch should
4771 * only be performed if the unblocked task's
4772 * priority is higher than the currently executing
4774 * The case of equal priority tasks sharing
4775 * processing time (which happens when both
4776 * preemption and time slicing are on) is
4778 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4780 xSwitchRequired = pdTRUE;
4784 mtCOVERAGE_TEST_MARKER();
4787 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4789 prvYieldForTask( pxTCB );
4791 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4793 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4798 /* Tasks of equal priority to the currently running task will share
4799 * processing time (time slice) if preemption is on, and the application
4800 * writer has not explicitly turned time slicing off. */
4801 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4803 #if ( configNUMBER_OF_CORES == 1 )
4805 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 )
4807 xSwitchRequired = pdTRUE;
4811 mtCOVERAGE_TEST_MARKER();
4814 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4818 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4820 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1 )
4822 xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4826 mtCOVERAGE_TEST_MARKER();
4830 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4832 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4834 #if ( configUSE_TICK_HOOK == 1 )
4836 /* Guard against the tick hook being called when the pended tick
4837 * count is being unwound (when the scheduler is being unlocked). */
4838 if( xPendedTicks == ( TickType_t ) 0 )
4840 vApplicationTickHook();
4844 mtCOVERAGE_TEST_MARKER();
4847 #endif /* configUSE_TICK_HOOK */
4849 #if ( configUSE_PREEMPTION == 1 )
4851 #if ( configNUMBER_OF_CORES == 1 )
4853 /* For single core the core ID is always 0. */
4854 if( xYieldPendings[ 0 ] != pdFALSE )
4856 xSwitchRequired = pdTRUE;
4860 mtCOVERAGE_TEST_MARKER();
4863 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4865 BaseType_t xCoreID, xCurrentCoreID;
4866 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4868 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4870 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4871 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4874 if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4876 if( xCoreID == xCurrentCoreID )
4878 xSwitchRequired = pdTRUE;
4882 prvYieldCore( xCoreID );
4887 mtCOVERAGE_TEST_MARKER();
4892 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4894 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4900 /* The tick hook gets called at regular intervals, even if the
4901 * scheduler is locked. */
4902 #if ( configUSE_TICK_HOOK == 1 )
4904 vApplicationTickHook();
4909 traceRETURN_xTaskIncrementTick( xSwitchRequired );
4911 return xSwitchRequired;
4913 /*-----------------------------------------------------------*/
4915 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4917 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4918 TaskHookFunction_t pxHookFunction )
4922 traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4924 /* If xTask is NULL then it is the task hook of the calling task that is
4928 xTCB = ( TCB_t * ) pxCurrentTCB;
4935 /* Save the hook function in the TCB. A critical section is required as
4936 * the value can be accessed from an interrupt. */
4937 taskENTER_CRITICAL();
4939 xTCB->pxTaskTag = pxHookFunction;
4941 taskEXIT_CRITICAL();
4943 traceRETURN_vTaskSetApplicationTaskTag();
4946 #endif /* configUSE_APPLICATION_TASK_TAG */
4947 /*-----------------------------------------------------------*/
4949 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4951 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4954 TaskHookFunction_t xReturn;
4956 traceENTER_xTaskGetApplicationTaskTag( xTask );
4958 /* If xTask is NULL then set the calling task's hook. */
4959 pxTCB = prvGetTCBFromHandle( xTask );
4961 /* Save the hook function in the TCB. A critical section is required as
4962 * the value can be accessed from an interrupt. */
4963 taskENTER_CRITICAL();
4965 xReturn = pxTCB->pxTaskTag;
4967 taskEXIT_CRITICAL();
4969 traceRETURN_xTaskGetApplicationTaskTag( xReturn );
4974 #endif /* configUSE_APPLICATION_TASK_TAG */
4975 /*-----------------------------------------------------------*/
4977 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4979 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
4982 TaskHookFunction_t xReturn;
4983 UBaseType_t uxSavedInterruptStatus;
4985 traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
4987 /* If xTask is NULL then set the calling task's hook. */
4988 pxTCB = prvGetTCBFromHandle( xTask );
4990 /* Save the hook function in the TCB. A critical section is required as
4991 * the value can be accessed from an interrupt. */
4992 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
4994 xReturn = pxTCB->pxTaskTag;
4996 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
4998 traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
5003 #endif /* configUSE_APPLICATION_TASK_TAG */
5004 /*-----------------------------------------------------------*/
5006 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
5008 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
5009 void * pvParameter )
5014 traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
5016 /* If xTask is NULL then we are calling our own task hook. */
5019 xTCB = pxCurrentTCB;
5026 if( xTCB->pxTaskTag != NULL )
5028 xReturn = xTCB->pxTaskTag( pvParameter );
5035 traceRETURN_xTaskCallApplicationTaskHook( xReturn );
5040 #endif /* configUSE_APPLICATION_TASK_TAG */
5041 /*-----------------------------------------------------------*/
5043 #if ( configNUMBER_OF_CORES == 1 )
5044 void vTaskSwitchContext( void )
5046 traceENTER_vTaskSwitchContext();
5048 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5050 /* The scheduler is currently suspended - do not allow a context
5052 xYieldPendings[ 0 ] = pdTRUE;
5056 xYieldPendings[ 0 ] = pdFALSE;
5057 traceTASK_SWITCHED_OUT();
5059 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5061 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5062 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
5064 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
5067 /* Add the amount of time the task has been running to the
5068 * accumulated time so far. The time the task started running was
5069 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5070 * protection here so count values are only valid until the timer
5071 * overflows. The guard against negative values is to protect
5072 * against suspect run time stat counter implementations - which
5073 * are provided by the application, not the kernel. */
5074 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
5076 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
5080 mtCOVERAGE_TEST_MARKER();
5083 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
5085 #endif /* configGENERATE_RUN_TIME_STATS */
5087 /* Check for stack overflow, if configured. */
5088 taskCHECK_FOR_STACK_OVERFLOW();
5090 /* Before the currently running task is switched out, save its errno. */
5091 #if ( configUSE_POSIX_ERRNO == 1 )
5093 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
5097 /* Select a new task to run using either the generic C or port
5098 * optimised asm code. */
5099 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. */
5100 traceTASK_SWITCHED_IN();
5102 /* After the new task is switched in, update the global errno. */
5103 #if ( configUSE_POSIX_ERRNO == 1 )
5105 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
5109 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5111 /* Switch C-Runtime's TLS Block to point to the TLS
5112 * Block specific to this task. */
5113 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
5118 traceRETURN_vTaskSwitchContext();
5120 #else /* if ( configNUMBER_OF_CORES == 1 ) */
5121 void vTaskSwitchContext( BaseType_t xCoreID )
5123 traceENTER_vTaskSwitchContext();
5125 /* Acquire both locks:
5126 * - The ISR lock protects the ready list from simultaneous access by
5127 * both other ISRs and tasks.
5128 * - We also take the task lock to pause here in case another core has
5129 * suspended the scheduler. We don't want to simply set xYieldPending
5130 * and move on if another core suspended the scheduler. We should only
5131 * do that if the current core has suspended the scheduler. */
5133 portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
5136 /* vTaskSwitchContext() must never be called from within a critical section.
5137 * This is not necessarily true for single core FreeRTOS, but it is for this
5139 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
5141 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
5143 /* The scheduler is currently suspended - do not allow a context
5145 xYieldPendings[ xCoreID ] = pdTRUE;
5149 xYieldPendings[ xCoreID ] = pdFALSE;
5150 traceTASK_SWITCHED_OUT();
5152 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5154 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
5155 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
5157 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
5160 /* Add the amount of time the task has been running to the
5161 * accumulated time so far. The time the task started running was
5162 * stored in ulTaskSwitchedInTime. Note that there is no overflow
5163 * protection here so count values are only valid until the timer
5164 * overflows. The guard against negative values is to protect
5165 * against suspect run time stat counter implementations - which
5166 * are provided by the application, not the kernel. */
5167 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
5169 pxCurrentTCBs[ xCoreID ]->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
5173 mtCOVERAGE_TEST_MARKER();
5176 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
5178 #endif /* configGENERATE_RUN_TIME_STATS */
5180 /* Check for stack overflow, if configured. */
5181 taskCHECK_FOR_STACK_OVERFLOW();
5183 /* Before the currently running task is switched out, save its errno. */
5184 #if ( configUSE_POSIX_ERRNO == 1 )
5186 pxCurrentTCBs[ xCoreID ]->iTaskErrno = FreeRTOS_errno;
5190 /* Select a new task to run. */
5191 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
5192 traceTASK_SWITCHED_IN();
5194 /* After the new task is switched in, update the global errno. */
5195 #if ( configUSE_POSIX_ERRNO == 1 )
5197 FreeRTOS_errno = pxCurrentTCBs[ xCoreID ]->iTaskErrno;
5201 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5203 /* Switch C-Runtime's TLS Block to point to the TLS
5204 * Block specific to this task. */
5205 configSET_TLS_BLOCK( pxCurrentTCBs[ xCoreID ]->xTLSBlock );
5210 portRELEASE_ISR_LOCK();
5211 portRELEASE_TASK_LOCK();
5213 traceRETURN_vTaskSwitchContext();
5215 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
5216 /*-----------------------------------------------------------*/
5218 void vTaskPlaceOnEventList( List_t * const pxEventList,
5219 const TickType_t xTicksToWait )
5221 traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
5223 configASSERT( pxEventList );
5225 /* THIS FUNCTION MUST BE CALLED WITH THE
5226 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
5228 /* Place the event list item of the TCB in the appropriate event list.
5229 * This is placed in the list in priority order so the highest priority task
5230 * is the first to be woken by the event.
5232 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
5233 * Normally, the xItemValue of a TCB's ListItem_t members is:
5234 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
5235 * Therefore, the event list is sorted in descending priority order.
5237 * The queue that contains the event list is locked, preventing
5238 * simultaneous access from interrupts. */
5239 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5241 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5243 traceRETURN_vTaskPlaceOnEventList();
5245 /*-----------------------------------------------------------*/
5247 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
5248 const TickType_t xItemValue,
5249 const TickType_t xTicksToWait )
5251 traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
5253 configASSERT( pxEventList );
5255 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5256 * the event groups implementation. */
5257 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5259 /* Store the item value in the event list item. It is safe to access the
5260 * event list item here as interrupts won't access the event list item of a
5261 * task that is not in the Blocked state. */
5262 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5264 /* Place the event list item of the TCB at the end of the appropriate event
5265 * list. It is safe to access the event list here because it is part of an
5266 * event group implementation - and interrupts don't access event groups
5267 * directly (instead they access them indirectly by pending function calls to
5268 * the task level). */
5269 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5271 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5273 traceRETURN_vTaskPlaceOnUnorderedEventList();
5275 /*-----------------------------------------------------------*/
5277 #if ( configUSE_TIMERS == 1 )
5279 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
5280 TickType_t xTicksToWait,
5281 const BaseType_t xWaitIndefinitely )
5283 traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
5285 configASSERT( pxEventList );
5287 /* This function should not be called by application code hence the
5288 * 'Restricted' in its name. It is not part of the public API. It is
5289 * designed for use by kernel code, and has special calling requirements -
5290 * it should be called with the scheduler suspended. */
5293 /* Place the event list item of the TCB in the appropriate event list.
5294 * In this case it is assume that this is the only task that is going to
5295 * be waiting on this event list, so the faster vListInsertEnd() function
5296 * can be used in place of vListInsert. */
5297 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5299 /* If the task should block indefinitely then set the block time to a
5300 * value that will be recognised as an indefinite delay inside the
5301 * prvAddCurrentTaskToDelayedList() function. */
5302 if( xWaitIndefinitely != pdFALSE )
5304 xTicksToWait = portMAX_DELAY;
5307 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
5308 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
5310 traceRETURN_vTaskPlaceOnEventListRestricted();
5313 #endif /* configUSE_TIMERS */
5314 /*-----------------------------------------------------------*/
5316 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
5318 TCB_t * pxUnblockedTCB;
5321 traceENTER_xTaskRemoveFromEventList( pxEventList );
5323 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
5324 * called from a critical section within an ISR. */
5326 /* The event list is sorted in priority order, so the first in the list can
5327 * be removed as it is known to be the highest priority. Remove the TCB from
5328 * the delayed list, and add it to the ready list.
5330 * If an event is for a queue that is locked then this function will never
5331 * get called - the lock count on the queue will get modified instead. This
5332 * means exclusive access to the event list is guaranteed here.
5334 * This function assumes that a check has already been made to ensure that
5335 * pxEventList is not empty. */
5336 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. */
5337 configASSERT( pxUnblockedTCB );
5338 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5340 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5342 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5343 prvAddTaskToReadyList( pxUnblockedTCB );
5345 #if ( configUSE_TICKLESS_IDLE != 0 )
5347 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5348 * might be set to the blocked task's time out time. If the task is
5349 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5350 * normally left unchanged, because it is automatically reset to a new
5351 * value when the tick count equals xNextTaskUnblockTime. However if
5352 * tickless idling is used it might be more important to enter sleep mode
5353 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5354 * ensure it is updated at the earliest possible time. */
5355 prvResetNextTaskUnblockTime();
5361 /* The delayed and ready lists cannot be accessed, so hold this task
5362 * pending until the scheduler is resumed. */
5363 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5366 #if ( configNUMBER_OF_CORES == 1 )
5368 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5370 /* Return true if the task removed from the event list has a higher
5371 * priority than the calling task. This allows the calling task to know if
5372 * it should force a context switch now. */
5375 /* Mark that a yield is pending in case the user is not using the
5376 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5377 xYieldPendings[ 0 ] = pdTRUE;
5384 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5388 #if ( configUSE_PREEMPTION == 1 )
5390 prvYieldForTask( pxUnblockedTCB );
5392 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5397 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5399 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5401 traceRETURN_xTaskRemoveFromEventList( xReturn );
5404 /*-----------------------------------------------------------*/
5406 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5407 const TickType_t xItemValue )
5409 TCB_t * pxUnblockedTCB;
5411 traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5413 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5414 * the event flags implementation. */
5415 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5417 /* Store the new item value in the event list. */
5418 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5420 /* Remove the event list form the event flag. Interrupts do not access
5422 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. */
5423 configASSERT( pxUnblockedTCB );
5424 listREMOVE_ITEM( pxEventListItem );
5426 #if ( configUSE_TICKLESS_IDLE != 0 )
5428 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5429 * might be set to the blocked task's time out time. If the task is
5430 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5431 * normally left unchanged, because it is automatically reset to a new
5432 * value when the tick count equals xNextTaskUnblockTime. However if
5433 * tickless idling is used it might be more important to enter sleep mode
5434 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5435 * ensure it is updated at the earliest possible time. */
5436 prvResetNextTaskUnblockTime();
5440 /* Remove the task from the delayed list and add it to the ready list. The
5441 * scheduler is suspended so interrupts will not be accessing the ready
5443 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5444 prvAddTaskToReadyList( pxUnblockedTCB );
5446 #if ( configNUMBER_OF_CORES == 1 )
5448 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5450 /* The unblocked task has a priority above that of the calling task, so
5451 * a context switch is required. This function is called with the
5452 * scheduler suspended so xYieldPending is set so the context switch
5453 * occurs immediately that the scheduler is resumed (unsuspended). */
5454 xYieldPendings[ 0 ] = pdTRUE;
5457 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5459 #if ( configUSE_PREEMPTION == 1 )
5461 taskENTER_CRITICAL();
5463 prvYieldForTask( pxUnblockedTCB );
5465 taskEXIT_CRITICAL();
5469 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5471 traceRETURN_vTaskRemoveFromUnorderedEventList();
5473 /*-----------------------------------------------------------*/
5475 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5477 traceENTER_vTaskSetTimeOutState( pxTimeOut );
5479 configASSERT( pxTimeOut );
5480 taskENTER_CRITICAL();
5482 pxTimeOut->xOverflowCount = xNumOfOverflows;
5483 pxTimeOut->xTimeOnEntering = xTickCount;
5485 taskEXIT_CRITICAL();
5487 traceRETURN_vTaskSetTimeOutState();
5489 /*-----------------------------------------------------------*/
5491 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5493 traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5495 /* For internal use only as it does not use a critical section. */
5496 pxTimeOut->xOverflowCount = xNumOfOverflows;
5497 pxTimeOut->xTimeOnEntering = xTickCount;
5499 traceRETURN_vTaskInternalSetTimeOutState();
5501 /*-----------------------------------------------------------*/
5503 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5504 TickType_t * const pxTicksToWait )
5508 traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5510 configASSERT( pxTimeOut );
5511 configASSERT( pxTicksToWait );
5513 taskENTER_CRITICAL();
5515 /* Minor optimisation. The tick count cannot change in this block. */
5516 const TickType_t xConstTickCount = xTickCount;
5517 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5519 #if ( INCLUDE_xTaskAbortDelay == 1 )
5520 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5522 /* The delay was aborted, which is not the same as a time out,
5523 * but has the same result. */
5524 pxCurrentTCB->ucDelayAborted = pdFALSE;
5530 #if ( INCLUDE_vTaskSuspend == 1 )
5531 if( *pxTicksToWait == portMAX_DELAY )
5533 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5534 * specified is the maximum block time then the task should block
5535 * indefinitely, and therefore never time out. */
5541 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
5543 /* The tick count is greater than the time at which
5544 * vTaskSetTimeout() was called, but has also overflowed since
5545 * vTaskSetTimeOut() was called. It must have wrapped all the way
5546 * around and gone past again. This passed since vTaskSetTimeout()
5549 *pxTicksToWait = ( TickType_t ) 0;
5551 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
5553 /* Not a genuine timeout. Adjust parameters for time remaining. */
5554 *pxTicksToWait -= xElapsedTime;
5555 vTaskInternalSetTimeOutState( pxTimeOut );
5560 *pxTicksToWait = ( TickType_t ) 0;
5564 taskEXIT_CRITICAL();
5566 traceRETURN_xTaskCheckForTimeOut( xReturn );
5570 /*-----------------------------------------------------------*/
5572 void vTaskMissedYield( void )
5574 traceENTER_vTaskMissedYield();
5576 /* Must be called from within a critical section. */
5577 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5579 traceRETURN_vTaskMissedYield();
5581 /*-----------------------------------------------------------*/
5583 #if ( configUSE_TRACE_FACILITY == 1 )
5585 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5587 UBaseType_t uxReturn;
5588 TCB_t const * pxTCB;
5590 traceENTER_uxTaskGetTaskNumber( xTask );
5595 uxReturn = pxTCB->uxTaskNumber;
5602 traceRETURN_uxTaskGetTaskNumber( uxReturn );
5607 #endif /* configUSE_TRACE_FACILITY */
5608 /*-----------------------------------------------------------*/
5610 #if ( configUSE_TRACE_FACILITY == 1 )
5612 void vTaskSetTaskNumber( TaskHandle_t xTask,
5613 const UBaseType_t uxHandle )
5617 traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5622 pxTCB->uxTaskNumber = uxHandle;
5625 traceRETURN_vTaskSetTaskNumber();
5628 #endif /* configUSE_TRACE_FACILITY */
5629 /*-----------------------------------------------------------*/
5632 * -----------------------------------------------------------
5633 * The passive idle task.
5634 * ----------------------------------------------------------
5636 * The passive idle task is used for all the additional cores in a SMP
5637 * system. There must be only 1 active idle task and the rest are passive
5640 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5641 * language extensions. The equivalent prototype for this function is:
5643 * void prvPassiveIdleTask( void *pvParameters );
5646 #if ( configNUMBER_OF_CORES > 1 )
5647 static portTASK_FUNCTION( prvPassiveIdleTask, pvParameters )
5649 ( void ) pvParameters;
5653 for( ; configCONTROL_INFINITE_LOOP(); )
5655 #if ( configUSE_PREEMPTION == 0 )
5657 /* If we are not using preemption we keep forcing a task switch to
5658 * see if any other task has become available. If we are using
5659 * preemption we don't need to do this as any task becoming available
5660 * will automatically get the processor anyway. */
5663 #endif /* configUSE_PREEMPTION */
5665 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5667 /* When using preemption tasks of equal priority will be
5668 * timesliced. If a task that is sharing the idle priority is ready
5669 * to run then the idle task should yield before the end of the
5672 * A critical region is not required here as we are just reading from
5673 * the list, and an occasional incorrect value will not matter. If
5674 * the ready list at the idle priority contains one more task than the
5675 * number of idle tasks, which is equal to the configured numbers of cores
5676 * then a task other than the idle task is ready to execute. */
5677 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5683 mtCOVERAGE_TEST_MARKER();
5686 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5688 #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
5690 /* Call the user defined function from within the idle task. This
5691 * allows the application designer to add background functionality
5692 * without the overhead of a separate task.
5694 * This hook is intended to manage core activity such as disabling cores that go idle.
5696 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5697 * CALL A FUNCTION THAT MIGHT BLOCK. */
5698 vApplicationPassiveIdleHook();
5700 #endif /* configUSE_PASSIVE_IDLE_HOOK */
5703 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5706 * -----------------------------------------------------------
5708 * ----------------------------------------------------------
5710 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5711 * language extensions. The equivalent prototype for this function is:
5713 * void prvIdleTask( void *pvParameters );
5717 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5719 /* Stop warnings. */
5720 ( void ) pvParameters;
5722 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5723 * SCHEDULER IS STARTED. **/
5725 /* In case a task that has a secure context deletes itself, in which case
5726 * the idle task is responsible for deleting the task's secure context, if
5728 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5730 #if ( configNUMBER_OF_CORES > 1 )
5732 /* SMP all cores start up in the idle task. This initial yield gets the application
5736 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5738 for( ; configCONTROL_INFINITE_LOOP(); )
5740 /* See if any tasks have deleted themselves - if so then the idle task
5741 * is responsible for freeing the deleted task's TCB and stack. */
5742 prvCheckTasksWaitingTermination();
5744 #if ( configUSE_PREEMPTION == 0 )
5746 /* If we are not using preemption we keep forcing a task switch to
5747 * see if any other task has become available. If we are using
5748 * preemption we don't need to do this as any task becoming available
5749 * will automatically get the processor anyway. */
5752 #endif /* configUSE_PREEMPTION */
5754 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5756 /* When using preemption tasks of equal priority will be
5757 * timesliced. If a task that is sharing the idle priority is ready
5758 * to run then the idle task should yield before the end of the
5761 * A critical region is not required here as we are just reading from
5762 * the list, and an occasional incorrect value will not matter. If
5763 * the ready list at the idle priority contains one more task than the
5764 * number of idle tasks, which is equal to the configured numbers of cores
5765 * then a task other than the idle task is ready to execute. */
5766 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5772 mtCOVERAGE_TEST_MARKER();
5775 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5777 #if ( configUSE_IDLE_HOOK == 1 )
5779 /* Call the user defined function from within the idle task. */
5780 vApplicationIdleHook();
5782 #endif /* configUSE_IDLE_HOOK */
5784 /* This conditional compilation should use inequality to 0, not equality
5785 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5786 * user defined low power mode implementations require
5787 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5788 #if ( configUSE_TICKLESS_IDLE != 0 )
5790 TickType_t xExpectedIdleTime;
5792 /* It is not desirable to suspend then resume the scheduler on
5793 * each iteration of the idle task. Therefore, a preliminary
5794 * test of the expected idle time is performed without the
5795 * scheduler suspended. The result here is not necessarily
5797 xExpectedIdleTime = prvGetExpectedIdleTime();
5799 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5803 /* Now the scheduler is suspended, the expected idle
5804 * time can be sampled again, and this time its value can
5806 configASSERT( xNextTaskUnblockTime >= xTickCount );
5807 xExpectedIdleTime = prvGetExpectedIdleTime();
5809 /* Define the following macro to set xExpectedIdleTime to 0
5810 * if the application does not want
5811 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5812 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5814 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5816 traceLOW_POWER_IDLE_BEGIN();
5817 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5818 traceLOW_POWER_IDLE_END();
5822 mtCOVERAGE_TEST_MARKER();
5825 ( void ) xTaskResumeAll();
5829 mtCOVERAGE_TEST_MARKER();
5832 #endif /* configUSE_TICKLESS_IDLE */
5834 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) )
5836 /* Call the user defined function from within the idle task. This
5837 * allows the application designer to add background functionality
5838 * without the overhead of a separate task.
5840 * This hook is intended to manage core activity such as disabling cores that go idle.
5842 * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5843 * CALL A FUNCTION THAT MIGHT BLOCK. */
5844 vApplicationPassiveIdleHook();
5846 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) ) */
5849 /*-----------------------------------------------------------*/
5851 #if ( configUSE_TICKLESS_IDLE != 0 )
5853 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5855 #if ( INCLUDE_vTaskSuspend == 1 )
5856 /* The idle task exists in addition to the application tasks. */
5857 const UBaseType_t uxNonApplicationTasks = 1;
5858 #endif /* INCLUDE_vTaskSuspend */
5860 eSleepModeStatus eReturn = eStandardSleep;
5862 traceENTER_eTaskConfirmSleepModeStatus();
5864 /* This function must be called from a critical section. */
5866 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
5868 /* A task was made ready while the scheduler was suspended. */
5869 eReturn = eAbortSleep;
5871 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5873 /* A yield was pended while the scheduler was suspended. */
5874 eReturn = eAbortSleep;
5876 else if( xPendedTicks != 0 )
5878 /* A tick interrupt has already occurred but was held pending
5879 * because the scheduler is suspended. */
5880 eReturn = eAbortSleep;
5883 #if ( INCLUDE_vTaskSuspend == 1 )
5884 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5886 /* If all the tasks are in the suspended list (which might mean they
5887 * have an infinite block time rather than actually being suspended)
5888 * then it is safe to turn all clocks off and just wait for external
5890 eReturn = eNoTasksWaitingTimeout;
5892 #endif /* INCLUDE_vTaskSuspend */
5895 mtCOVERAGE_TEST_MARKER();
5898 traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5903 #endif /* configUSE_TICKLESS_IDLE */
5904 /*-----------------------------------------------------------*/
5906 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5908 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5914 traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
5916 if( ( xIndex >= 0 ) &&
5917 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5919 pxTCB = prvGetTCBFromHandle( xTaskToSet );
5920 configASSERT( pxTCB != NULL );
5921 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5924 traceRETURN_vTaskSetThreadLocalStoragePointer();
5927 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5928 /*-----------------------------------------------------------*/
5930 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5932 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5935 void * pvReturn = NULL;
5938 traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
5940 if( ( xIndex >= 0 ) &&
5941 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5943 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
5944 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
5951 traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
5956 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5957 /*-----------------------------------------------------------*/
5959 #if ( portUSING_MPU_WRAPPERS == 1 )
5961 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
5962 const MemoryRegion_t * const pxRegions )
5966 traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
5968 /* If null is passed in here then we are modifying the MPU settings of
5969 * the calling task. */
5970 pxTCB = prvGetTCBFromHandle( xTaskToModify );
5972 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
5974 traceRETURN_vTaskAllocateMPURegions();
5977 #endif /* portUSING_MPU_WRAPPERS */
5978 /*-----------------------------------------------------------*/
5980 static void prvInitialiseTaskLists( void )
5982 UBaseType_t uxPriority;
5984 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
5986 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
5989 vListInitialise( &xDelayedTaskList1 );
5990 vListInitialise( &xDelayedTaskList2 );
5991 vListInitialise( &xPendingReadyList );
5993 #if ( INCLUDE_vTaskDelete == 1 )
5995 vListInitialise( &xTasksWaitingTermination );
5997 #endif /* INCLUDE_vTaskDelete */
5999 #if ( INCLUDE_vTaskSuspend == 1 )
6001 vListInitialise( &xSuspendedTaskList );
6003 #endif /* INCLUDE_vTaskSuspend */
6005 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
6007 pxDelayedTaskList = &xDelayedTaskList1;
6008 pxOverflowDelayedTaskList = &xDelayedTaskList2;
6010 /*-----------------------------------------------------------*/
6012 static void prvCheckTasksWaitingTermination( void )
6014 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
6016 #if ( INCLUDE_vTaskDelete == 1 )
6020 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
6021 * being called too often in the idle task. */
6022 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6024 #if ( configNUMBER_OF_CORES == 1 )
6026 taskENTER_CRITICAL();
6029 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. */
6030 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6031 --uxCurrentNumberOfTasks;
6032 --uxDeletedTasksWaitingCleanUp;
6035 taskEXIT_CRITICAL();
6037 prvDeleteTCB( pxTCB );
6039 #else /* #if( configNUMBER_OF_CORES == 1 ) */
6043 taskENTER_CRITICAL();
6045 /* For SMP, multiple idles can be running simultaneously
6046 * and we need to check that other idles did not cleanup while we were
6047 * waiting to enter the critical section. */
6048 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
6050 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. */
6052 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
6054 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6055 --uxCurrentNumberOfTasks;
6056 --uxDeletedTasksWaitingCleanUp;
6060 /* The TCB to be deleted still has not yet been switched out
6061 * by the scheduler, so we will just exit this loop early and
6062 * try again next time. */
6063 taskEXIT_CRITICAL();
6068 taskEXIT_CRITICAL();
6072 prvDeleteTCB( pxTCB );
6075 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
6078 #endif /* INCLUDE_vTaskDelete */
6080 /*-----------------------------------------------------------*/
6082 #if ( configUSE_TRACE_FACILITY == 1 )
6084 void vTaskGetInfo( TaskHandle_t xTask,
6085 TaskStatus_t * pxTaskStatus,
6086 BaseType_t xGetFreeStackSpace,
6091 traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
6093 /* xTask is NULL then get the state of the calling task. */
6094 pxTCB = prvGetTCBFromHandle( xTask );
6096 pxTaskStatus->xHandle = pxTCB;
6097 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
6098 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
6099 pxTaskStatus->pxStackBase = pxTCB->pxStack;
6100 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
6101 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
6102 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
6104 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
6106 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
6108 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
6112 #if ( configUSE_MUTEXES == 1 )
6114 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
6118 pxTaskStatus->uxBasePriority = 0;
6122 #if ( configGENERATE_RUN_TIME_STATS == 1 )
6124 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
6128 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
6132 /* Obtaining the task state is a little fiddly, so is only done if the
6133 * value of eState passed into this function is eInvalid - otherwise the
6134 * state is just set to whatever is passed in. */
6135 if( eState != eInvalid )
6137 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6139 pxTaskStatus->eCurrentState = eRunning;
6143 pxTaskStatus->eCurrentState = eState;
6145 #if ( INCLUDE_vTaskSuspend == 1 )
6147 /* If the task is in the suspended list then there is a
6148 * chance it is actually just blocked indefinitely - so really
6149 * it should be reported as being in the Blocked state. */
6150 if( eState == eSuspended )
6154 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
6156 pxTaskStatus->eCurrentState = eBlocked;
6162 /* The task does not appear on the event list item of
6163 * and of the RTOS objects, but could still be in the
6164 * blocked state if it is waiting on its notification
6165 * rather than waiting on an object. If not, is
6167 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
6169 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
6171 pxTaskStatus->eCurrentState = eBlocked;
6177 ( void ) xTaskResumeAll();
6180 #endif /* INCLUDE_vTaskSuspend */
6182 /* Tasks can be in pending ready list and other state list at the
6183 * same time. These tasks are in ready state no matter what state
6184 * list the task is in. */
6185 taskENTER_CRITICAL();
6187 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
6189 pxTaskStatus->eCurrentState = eReady;
6192 taskEXIT_CRITICAL();
6197 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
6200 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
6201 * parameter is provided to allow it to be skipped. */
6202 if( xGetFreeStackSpace != pdFALSE )
6204 #if ( portSTACK_GROWTH > 0 )
6206 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
6210 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
6216 pxTaskStatus->usStackHighWaterMark = 0;
6219 traceRETURN_vTaskGetInfo();
6222 #endif /* configUSE_TRACE_FACILITY */
6223 /*-----------------------------------------------------------*/
6225 #if ( configUSE_TRACE_FACILITY == 1 )
6227 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
6231 configLIST_VOLATILE TCB_t * pxNextTCB;
6232 configLIST_VOLATILE TCB_t * pxFirstTCB;
6233 UBaseType_t uxTask = 0;
6235 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
6237 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. */
6239 /* Populate an TaskStatus_t structure within the
6240 * pxTaskStatusArray array for each task that is referenced from
6241 * pxList. See the definition of TaskStatus_t in task.h for the
6242 * meaning of each TaskStatus_t structure member. */
6245 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. */
6246 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
6248 } while( pxNextTCB != pxFirstTCB );
6252 mtCOVERAGE_TEST_MARKER();
6258 #endif /* configUSE_TRACE_FACILITY */
6259 /*-----------------------------------------------------------*/
6261 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
6263 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
6265 uint32_t ulCount = 0U;
6267 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
6269 pucStackByte -= portSTACK_GROWTH;
6273 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
6275 return ( configSTACK_DEPTH_TYPE ) ulCount;
6278 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
6279 /*-----------------------------------------------------------*/
6281 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
6283 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
6284 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
6285 * user to determine the return type. It gets around the problem of the value
6286 * overflowing on 8-bit types without breaking backward compatibility for
6287 * applications that expect an 8-bit return type. */
6288 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
6291 uint8_t * pucEndOfStack;
6292 configSTACK_DEPTH_TYPE uxReturn;
6294 traceENTER_uxTaskGetStackHighWaterMark2( xTask );
6296 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
6297 * the same except for their return type. Using configSTACK_DEPTH_TYPE
6298 * allows the user to determine the return type. It gets around the
6299 * problem of the value overflowing on 8-bit types without breaking
6300 * backward compatibility for applications that expect an 8-bit return
6303 pxTCB = prvGetTCBFromHandle( xTask );
6305 #if portSTACK_GROWTH < 0
6307 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6311 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6315 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
6317 traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
6322 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
6323 /*-----------------------------------------------------------*/
6325 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
6327 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
6330 uint8_t * pucEndOfStack;
6331 UBaseType_t uxReturn;
6333 traceENTER_uxTaskGetStackHighWaterMark( xTask );
6335 pxTCB = prvGetTCBFromHandle( xTask );
6337 #if portSTACK_GROWTH < 0
6339 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6343 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6347 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
6349 traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6354 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6355 /*-----------------------------------------------------------*/
6357 #if ( INCLUDE_vTaskDelete == 1 )
6359 static void prvDeleteTCB( TCB_t * pxTCB )
6361 /* This call is required specifically for the TriCore port. It must be
6362 * above the vPortFree() calls. The call is also used by ports/demos that
6363 * want to allocate and clean RAM statically. */
6364 portCLEAN_UP_TCB( pxTCB );
6366 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6368 /* Free up the memory allocated for the task's TLS Block. */
6369 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6373 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6375 /* The task can only have been allocated dynamically - free both
6376 * the stack and TCB. */
6377 vPortFreeStack( pxTCB->pxStack );
6380 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
6382 /* The task could have been allocated statically or dynamically, so
6383 * check what was statically allocated before trying to free the
6385 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6387 /* Both the stack and TCB were allocated dynamically, so both
6389 vPortFreeStack( pxTCB->pxStack );
6392 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6394 /* Only the stack was statically allocated, so the TCB is the
6395 * only memory that must be freed. */
6400 /* Neither the stack nor the TCB were allocated dynamically, so
6401 * nothing needs to be freed. */
6402 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6403 mtCOVERAGE_TEST_MARKER();
6406 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6409 #endif /* INCLUDE_vTaskDelete */
6410 /*-----------------------------------------------------------*/
6412 static void prvResetNextTaskUnblockTime( void )
6414 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6416 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
6417 * the maximum possible value so it is extremely unlikely that the
6418 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6419 * there is an item in the delayed list. */
6420 xNextTaskUnblockTime = portMAX_DELAY;
6424 /* The new current delayed list is not empty, get the value of
6425 * the item at the head of the delayed list. This is the time at
6426 * which the task at the head of the delayed list should be removed
6427 * from the Blocked state. */
6428 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6431 /*-----------------------------------------------------------*/
6433 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6435 #if ( configNUMBER_OF_CORES == 1 )
6436 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6438 TaskHandle_t xReturn;
6440 traceENTER_xTaskGetCurrentTaskHandle();
6442 /* A critical section is not required as this is not called from
6443 * an interrupt and the current TCB will always be the same for any
6444 * individual execution thread. */
6445 xReturn = pxCurrentTCB;
6447 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6451 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6452 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6454 TaskHandle_t xReturn;
6455 UBaseType_t uxSavedInterruptStatus;
6457 traceENTER_xTaskGetCurrentTaskHandle();
6459 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6461 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6463 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6465 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6470 TaskHandle_t xTaskGetCurrentTaskHandleForCore( BaseType_t xCoreID )
6472 TaskHandle_t xReturn = NULL;
6474 traceENTER_xTaskGetCurrentTaskHandleForCore( xCoreID );
6476 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6478 xReturn = pxCurrentTCBs[ xCoreID ];
6481 traceRETURN_xTaskGetCurrentTaskHandleForCore( xReturn );
6485 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6487 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
6488 /*-----------------------------------------------------------*/
6490 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6492 BaseType_t xTaskGetSchedulerState( void )
6496 traceENTER_xTaskGetSchedulerState();
6498 if( xSchedulerRunning == pdFALSE )
6500 xReturn = taskSCHEDULER_NOT_STARTED;
6504 #if ( configNUMBER_OF_CORES > 1 )
6505 taskENTER_CRITICAL();
6508 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6510 xReturn = taskSCHEDULER_RUNNING;
6514 xReturn = taskSCHEDULER_SUSPENDED;
6517 #if ( configNUMBER_OF_CORES > 1 )
6518 taskEXIT_CRITICAL();
6522 traceRETURN_xTaskGetSchedulerState( xReturn );
6527 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6528 /*-----------------------------------------------------------*/
6530 #if ( configUSE_MUTEXES == 1 )
6532 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6534 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6535 BaseType_t xReturn = pdFALSE;
6537 traceENTER_xTaskPriorityInherit( pxMutexHolder );
6539 /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6540 * inheritance is not applied in this scenario. */
6541 if( pxMutexHolder != NULL )
6543 /* If the holder of the mutex has a priority below the priority of
6544 * the task attempting to obtain the mutex then it will temporarily
6545 * inherit the priority of the task attempting to obtain the mutex. */
6546 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6548 /* Adjust the mutex holder state to account for its new
6549 * priority. Only reset the event list item value if the value is
6550 * not being used for anything else. */
6551 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
6553 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. */
6557 mtCOVERAGE_TEST_MARKER();
6560 /* If the task being modified is in the ready state it will need
6561 * to be moved into a new list. */
6562 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6564 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6566 /* It is known that the task is in its ready list so
6567 * there is no need to check again and the port level
6568 * reset macro can be called directly. */
6569 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6573 mtCOVERAGE_TEST_MARKER();
6576 /* Inherit the priority before being moved into the new list. */
6577 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6578 prvAddTaskToReadyList( pxMutexHolderTCB );
6579 #if ( configNUMBER_OF_CORES > 1 )
6581 /* The priority of the task is raised. Yield for this task
6582 * if it is not running. */
6583 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6585 prvYieldForTask( pxMutexHolderTCB );
6588 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6592 /* Just inherit the priority. */
6593 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6596 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6598 /* Inheritance occurred. */
6603 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6605 /* The base priority of the mutex holder is lower than the
6606 * priority of the task attempting to take the mutex, but the
6607 * current priority of the mutex holder is not lower than the
6608 * priority of the task attempting to take the mutex.
6609 * Therefore the mutex holder must have already inherited a
6610 * priority, but inheritance would have occurred if that had
6611 * not been the case. */
6616 mtCOVERAGE_TEST_MARKER();
6622 mtCOVERAGE_TEST_MARKER();
6625 traceRETURN_xTaskPriorityInherit( xReturn );
6630 #endif /* configUSE_MUTEXES */
6631 /*-----------------------------------------------------------*/
6633 #if ( configUSE_MUTEXES == 1 )
6635 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6637 TCB_t * const pxTCB = pxMutexHolder;
6638 BaseType_t xReturn = pdFALSE;
6640 traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6642 if( pxMutexHolder != NULL )
6644 /* A task can only have an inherited priority if it holds the mutex.
6645 * If the mutex is held by a task then it cannot be given from an
6646 * interrupt, and if a mutex is given by the holding task then it must
6647 * be the running state task. */
6648 configASSERT( pxTCB == pxCurrentTCB );
6649 configASSERT( pxTCB->uxMutexesHeld );
6650 ( pxTCB->uxMutexesHeld )--;
6652 /* Has the holder of the mutex inherited the priority of another
6654 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6656 /* Only disinherit if no other mutexes are held. */
6657 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6659 /* A task can only have an inherited priority if it holds
6660 * the mutex. If the mutex is held by a task then it cannot be
6661 * given from an interrupt, and if a mutex is given by the
6662 * holding task then it must be the running state task. Remove
6663 * the holding task from the ready list. */
6664 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6666 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6670 mtCOVERAGE_TEST_MARKER();
6673 /* Disinherit the priority before adding the task into the
6674 * new ready list. */
6675 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6676 pxTCB->uxPriority = pxTCB->uxBasePriority;
6678 /* Reset the event list item value. It cannot be in use for
6679 * any other purpose if this task is running, and it must be
6680 * running to give back the mutex. */
6681 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. */
6682 prvAddTaskToReadyList( pxTCB );
6683 #if ( configNUMBER_OF_CORES > 1 )
6685 /* The priority of the task is dropped. Yield the core on
6686 * which the task is running. */
6687 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6689 prvYieldCore( pxTCB->xTaskRunState );
6692 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6694 /* Return true to indicate that a context switch is required.
6695 * This is only actually required in the corner case whereby
6696 * multiple mutexes were held and the mutexes were given back
6697 * in an order different to that in which they were taken.
6698 * If a context switch did not occur when the first mutex was
6699 * returned, even if a task was waiting on it, then a context
6700 * switch should occur when the last mutex is returned whether
6701 * a task is waiting on it or not. */
6706 mtCOVERAGE_TEST_MARKER();
6711 mtCOVERAGE_TEST_MARKER();
6716 mtCOVERAGE_TEST_MARKER();
6719 traceRETURN_xTaskPriorityDisinherit( xReturn );
6724 #endif /* configUSE_MUTEXES */
6725 /*-----------------------------------------------------------*/
6727 #if ( configUSE_MUTEXES == 1 )
6729 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6730 UBaseType_t uxHighestPriorityWaitingTask )
6732 TCB_t * const pxTCB = pxMutexHolder;
6733 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6734 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6736 traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6738 if( pxMutexHolder != NULL )
6740 /* If pxMutexHolder is not NULL then the holder must hold at least
6742 configASSERT( pxTCB->uxMutexesHeld );
6744 /* Determine the priority to which the priority of the task that
6745 * holds the mutex should be set. This will be the greater of the
6746 * holding task's base priority and the priority of the highest
6747 * priority task that is waiting to obtain the mutex. */
6748 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6750 uxPriorityToUse = uxHighestPriorityWaitingTask;
6754 uxPriorityToUse = pxTCB->uxBasePriority;
6757 /* Does the priority need to change? */
6758 if( pxTCB->uxPriority != uxPriorityToUse )
6760 /* Only disinherit if no other mutexes are held. This is a
6761 * simplification in the priority inheritance implementation. If
6762 * the task that holds the mutex is also holding other mutexes then
6763 * the other mutexes may have caused the priority inheritance. */
6764 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6766 /* If a task has timed out because it already holds the
6767 * mutex it was trying to obtain then it cannot of inherited
6768 * its own priority. */
6769 configASSERT( pxTCB != pxCurrentTCB );
6771 /* Disinherit the priority, remembering the previous
6772 * priority to facilitate determining the subject task's
6774 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6775 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6776 pxTCB->uxPriority = uxPriorityToUse;
6778 /* Only reset the event list item value if the value is not
6779 * being used for anything else. */
6780 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
6782 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. */
6786 mtCOVERAGE_TEST_MARKER();
6789 /* If the running task is not the task that holds the mutex
6790 * then the task that holds the mutex could be in either the
6791 * Ready, Blocked or Suspended states. Only remove the task
6792 * from its current state list if it is in the Ready state as
6793 * the task's priority is going to change and there is one
6794 * Ready list per priority. */
6795 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6797 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6799 /* It is known that the task is in its ready list so
6800 * there is no need to check again and the port level
6801 * reset macro can be called directly. */
6802 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6806 mtCOVERAGE_TEST_MARKER();
6809 prvAddTaskToReadyList( pxTCB );
6810 #if ( configNUMBER_OF_CORES > 1 )
6812 /* The priority of the task is dropped. Yield the core on
6813 * which the task is running. */
6814 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6816 prvYieldCore( pxTCB->xTaskRunState );
6819 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6823 mtCOVERAGE_TEST_MARKER();
6828 mtCOVERAGE_TEST_MARKER();
6833 mtCOVERAGE_TEST_MARKER();
6838 mtCOVERAGE_TEST_MARKER();
6841 traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6844 #endif /* configUSE_MUTEXES */
6845 /*-----------------------------------------------------------*/
6847 #if ( configNUMBER_OF_CORES > 1 )
6849 /* If not in a critical section then yield immediately.
6850 * Otherwise set xYieldPendings to true to wait to
6851 * yield until exiting the critical section.
6853 void vTaskYieldWithinAPI( void )
6855 traceENTER_vTaskYieldWithinAPI();
6857 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6863 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6866 traceRETURN_vTaskYieldWithinAPI();
6868 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6870 /*-----------------------------------------------------------*/
6872 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6874 void vTaskEnterCritical( void )
6876 traceENTER_vTaskEnterCritical();
6878 portDISABLE_INTERRUPTS();
6880 if( xSchedulerRunning != pdFALSE )
6882 ( pxCurrentTCB->uxCriticalNesting )++;
6884 /* This is not the interrupt safe version of the enter critical
6885 * function so assert() if it is being called from an interrupt
6886 * context. Only API functions that end in "FromISR" can be used in an
6887 * interrupt. Only assert if the critical nesting count is 1 to
6888 * protect against recursive calls if the assert function also uses a
6889 * critical section. */
6890 if( pxCurrentTCB->uxCriticalNesting == 1 )
6892 portASSERT_IF_IN_ISR();
6897 mtCOVERAGE_TEST_MARKER();
6900 traceRETURN_vTaskEnterCritical();
6903 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6904 /*-----------------------------------------------------------*/
6906 #if ( configNUMBER_OF_CORES > 1 )
6908 void vTaskEnterCritical( void )
6910 traceENTER_vTaskEnterCritical();
6912 portDISABLE_INTERRUPTS();
6914 if( xSchedulerRunning != pdFALSE )
6916 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6918 portGET_TASK_LOCK();
6922 portINCREMENT_CRITICAL_NESTING_COUNT();
6924 /* This is not the interrupt safe version of the enter critical
6925 * function so assert() if it is being called from an interrupt
6926 * context. Only API functions that end in "FromISR" can be used in an
6927 * interrupt. Only assert if the critical nesting count is 1 to
6928 * protect against recursive calls if the assert function also uses a
6929 * critical section. */
6930 if( portGET_CRITICAL_NESTING_COUNT() == 1U )
6932 portASSERT_IF_IN_ISR();
6934 if( uxSchedulerSuspended == 0U )
6936 /* The only time there would be a problem is if this is called
6937 * before a context switch and vTaskExitCritical() is called
6938 * after pxCurrentTCB changes. Therefore this should not be
6939 * used within vTaskSwitchContext(). */
6940 prvCheckForRunStateChange();
6946 mtCOVERAGE_TEST_MARKER();
6949 traceRETURN_vTaskEnterCritical();
6952 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6954 /*-----------------------------------------------------------*/
6956 #if ( configNUMBER_OF_CORES > 1 )
6958 UBaseType_t vTaskEnterCriticalFromISR( void )
6960 UBaseType_t uxSavedInterruptStatus = 0;
6962 traceENTER_vTaskEnterCriticalFromISR();
6964 if( xSchedulerRunning != pdFALSE )
6966 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6968 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6973 portINCREMENT_CRITICAL_NESTING_COUNT();
6977 mtCOVERAGE_TEST_MARKER();
6980 traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
6982 return uxSavedInterruptStatus;
6985 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6986 /*-----------------------------------------------------------*/
6988 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6990 void vTaskExitCritical( void )
6992 traceENTER_vTaskExitCritical();
6994 if( xSchedulerRunning != pdFALSE )
6996 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
6997 * does not match a previous call to vTaskEnterCritical(). */
6998 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
7000 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7001 * to exit critical section from ISR. */
7002 portASSERT_IF_IN_ISR();
7004 if( pxCurrentTCB->uxCriticalNesting > 0U )
7006 ( pxCurrentTCB->uxCriticalNesting )--;
7008 if( pxCurrentTCB->uxCriticalNesting == 0U )
7010 portENABLE_INTERRUPTS();
7014 mtCOVERAGE_TEST_MARKER();
7019 mtCOVERAGE_TEST_MARKER();
7024 mtCOVERAGE_TEST_MARKER();
7027 traceRETURN_vTaskExitCritical();
7030 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
7031 /*-----------------------------------------------------------*/
7033 #if ( configNUMBER_OF_CORES > 1 )
7035 void vTaskExitCritical( void )
7037 traceENTER_vTaskExitCritical();
7039 if( xSchedulerRunning != pdFALSE )
7041 /* If critical nesting count is zero then this function
7042 * does not match a previous call to vTaskEnterCritical(). */
7043 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7045 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
7046 * to exit critical section from ISR. */
7047 portASSERT_IF_IN_ISR();
7049 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7051 portDECREMENT_CRITICAL_NESTING_COUNT();
7053 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7055 BaseType_t xYieldCurrentTask;
7057 /* Get the xYieldPending stats inside the critical section. */
7058 xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
7060 portRELEASE_ISR_LOCK();
7061 portRELEASE_TASK_LOCK();
7062 portENABLE_INTERRUPTS();
7064 /* When a task yields in a critical section it just sets
7065 * xYieldPending to true. So now that we have exited the
7066 * critical section check if xYieldPending is true, and
7068 if( xYieldCurrentTask != pdFALSE )
7075 mtCOVERAGE_TEST_MARKER();
7080 mtCOVERAGE_TEST_MARKER();
7085 mtCOVERAGE_TEST_MARKER();
7088 traceRETURN_vTaskExitCritical();
7091 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7092 /*-----------------------------------------------------------*/
7094 #if ( configNUMBER_OF_CORES > 1 )
7096 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
7098 traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
7100 if( xSchedulerRunning != pdFALSE )
7102 /* If critical nesting count is zero then this function
7103 * does not match a previous call to vTaskEnterCritical(). */
7104 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
7106 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
7108 portDECREMENT_CRITICAL_NESTING_COUNT();
7110 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
7112 portRELEASE_ISR_LOCK();
7113 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
7117 mtCOVERAGE_TEST_MARKER();
7122 mtCOVERAGE_TEST_MARKER();
7127 mtCOVERAGE_TEST_MARKER();
7130 traceRETURN_vTaskExitCriticalFromISR();
7133 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
7134 /*-----------------------------------------------------------*/
7136 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
7138 static char * prvWriteNameToBuffer( char * pcBuffer,
7139 const char * pcTaskName )
7143 /* Start by copying the entire string. */
7144 ( void ) strcpy( pcBuffer, pcTaskName );
7146 /* Pad the end of the string with spaces to ensure columns line up when
7148 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
7150 pcBuffer[ x ] = ' ';
7154 pcBuffer[ x ] = ( char ) 0x00;
7156 /* Return the new end of string. */
7157 return &( pcBuffer[ x ] );
7160 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
7161 /*-----------------------------------------------------------*/
7163 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
7165 void vTaskListTasks( char * pcWriteBuffer,
7166 size_t uxBufferLength )
7168 TaskStatus_t * pxTaskStatusArray;
7169 size_t uxConsumedBufferLength = 0;
7170 size_t uxCharsWrittenBySnprintf;
7171 int iSnprintfReturnValue;
7172 BaseType_t xOutputBufferFull = pdFALSE;
7173 UBaseType_t uxArraySize, x;
7176 traceENTER_vTaskListTasks( pcWriteBuffer, uxBufferLength );
7181 * This function is provided for convenience only, and is used by many
7182 * of the demo applications. Do not consider it to be part of the
7185 * vTaskListTasks() calls uxTaskGetSystemState(), then formats part of the
7186 * uxTaskGetSystemState() output into a human readable table that
7187 * displays task: names, states, priority, stack usage and task number.
7188 * Stack usage specified as the number of unused StackType_t words stack can hold
7189 * on top of stack - not the number of bytes.
7191 * vTaskListTasks() has a dependency on the snprintf() C library function that
7192 * might bloat the code size, use a lot of stack, and provide different
7193 * results on different platforms. An alternative, tiny, third party,
7194 * and limited functionality implementation of snprintf() is provided in
7195 * many of the FreeRTOS/Demo sub-directories in a file called
7196 * printf-stdarg.c (note printf-stdarg.c does not provide a full
7197 * snprintf() implementation!).
7199 * It is recommended that production systems call uxTaskGetSystemState()
7200 * directly to get access to raw stats data, rather than indirectly
7201 * through a call to vTaskListTasks().
7205 /* Make sure the write buffer does not contain a string. */
7206 *pcWriteBuffer = ( char ) 0x00;
7208 /* Take a snapshot of the number of tasks in case it changes while this
7209 * function is executing. */
7210 uxArraySize = uxCurrentNumberOfTasks;
7212 /* Allocate an array index for each task. NOTE! if
7213 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7214 * equate to NULL. */
7215 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. */
7217 if( pxTaskStatusArray != NULL )
7219 /* Generate the (binary) data. */
7220 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
7222 /* Create a human readable table from the binary data. */
7223 for( x = 0; ( x < uxArraySize ) && ( xOutputBufferFull == pdFALSE ); x++ )
7225 switch( pxTaskStatusArray[ x ].eCurrentState )
7228 cStatus = tskRUNNING_CHAR;
7232 cStatus = tskREADY_CHAR;
7236 cStatus = tskBLOCKED_CHAR;
7240 cStatus = tskSUSPENDED_CHAR;
7244 cStatus = tskDELETED_CHAR;
7247 case eInvalid: /* Fall through. */
7248 default: /* Should not get here, but it is included
7249 * to prevent static checking errors. */
7250 cStatus = ( char ) 0x00;
7254 /* Is there enough space in the buffer to hold task name? */
7255 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7257 /* Write the task name to the string, padding with spaces so it
7258 * can be printed in tabular form more easily. */
7259 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7260 /* Do not count the terminating null character. */
7261 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1 );
7263 /* Is there space left in the buffer? -1 is done because snprintf
7264 * writes a terminating null character. So we are essentially
7265 * checking if the buffer has space to write at least one non-null
7267 if( uxConsumedBufferLength < ( uxBufferLength - 1 ) )
7269 /* Write the rest of the string. */
7270 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
7271 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7272 uxBufferLength - uxConsumedBufferLength,
7273 "\t%c\t%u\t%u\t%u\t0x%x\r\n",
7275 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7276 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7277 ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber,
7278 ( 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. */
7279 #else /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7280 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7281 uxBufferLength - uxConsumedBufferLength,
7282 "\t%c\t%u\t%u\t%u\r\n",
7284 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
7285 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
7286 ( 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. */
7287 #endif /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
7288 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7290 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7291 pcWriteBuffer += uxCharsWrittenBySnprintf; /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
7295 xOutputBufferFull = pdTRUE;
7300 xOutputBufferFull = pdTRUE;
7304 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7305 * is 0 then vPortFree() will be #defined to nothing. */
7306 vPortFree( pxTaskStatusArray );
7310 mtCOVERAGE_TEST_MARKER();
7313 traceRETURN_vTaskListTasks();
7316 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7317 /*----------------------------------------------------------*/
7319 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
7321 void vTaskGetRunTimeStatistics( char * pcWriteBuffer,
7322 size_t uxBufferLength )
7324 TaskStatus_t * pxTaskStatusArray;
7325 size_t uxConsumedBufferLength = 0;
7326 size_t uxCharsWrittenBySnprintf;
7327 int iSnprintfReturnValue;
7328 BaseType_t xOutputBufferFull = pdFALSE;
7329 UBaseType_t uxArraySize, x;
7330 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
7332 traceENTER_vTaskGetRunTimeStatistics( pcWriteBuffer, uxBufferLength );
7337 * This function is provided for convenience only, and is used by many
7338 * of the demo applications. Do not consider it to be part of the
7341 * vTaskGetRunTimeStatistics() calls uxTaskGetSystemState(), then formats part
7342 * of the uxTaskGetSystemState() output into a human readable table that
7343 * displays the amount of time each task has spent in the Running state
7344 * in both absolute and percentage terms.
7346 * vTaskGetRunTimeStatistics() has a dependency on the snprintf() C library
7347 * function that might bloat the code size, use a lot of stack, and
7348 * provide different results on different platforms. An alternative,
7349 * tiny, third party, and limited functionality implementation of
7350 * snprintf() is provided in many of the FreeRTOS/Demo sub-directories in
7351 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
7352 * a full snprintf() implementation!).
7354 * It is recommended that production systems call uxTaskGetSystemState()
7355 * directly to get access to raw stats data, rather than indirectly
7356 * through a call to vTaskGetRunTimeStatistics().
7359 /* Make sure the write buffer does not contain a string. */
7360 *pcWriteBuffer = ( char ) 0x00;
7362 /* Take a snapshot of the number of tasks in case it changes while this
7363 * function is executing. */
7364 uxArraySize = uxCurrentNumberOfTasks;
7366 /* Allocate an array index for each task. NOTE! If
7367 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7368 * equate to NULL. */
7369 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. */
7371 if( pxTaskStatusArray != NULL )
7373 /* Generate the (binary) data. */
7374 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
7376 /* For percentage calculations. */
7377 ulTotalTime /= 100UL;
7379 /* Avoid divide by zero errors. */
7380 if( ulTotalTime > 0UL )
7382 /* Create a human readable table from the binary data. */
7383 for( x = 0; ( x < uxArraySize ) && ( xOutputBufferFull == pdFALSE ); x++ )
7385 /* What percentage of the total run time has the task used?
7386 * This will always be rounded down to the nearest integer.
7387 * ulTotalRunTime has already been divided by 100. */
7388 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
7390 /* Is there enough space in the buffer to hold task name? */
7391 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7393 /* Write the task name to the string, padding with
7394 * spaces so it can be printed in tabular form more
7396 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7397 /* Do not count the terminating null character. */
7398 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1 );
7400 /* Is there space left in the buffer? -1 is done because snprintf
7401 * writes a terminating null character. So we are essentially
7402 * checking if the buffer has space to write at least one non-null
7404 if( uxConsumedBufferLength < ( uxBufferLength - 1 ) )
7406 if( ulStatsAsPercentage > 0UL )
7408 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7410 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7411 uxBufferLength - uxConsumedBufferLength,
7412 "\t%lu\t\t%lu%%\r\n",
7413 pxTaskStatusArray[ x ].ulRunTimeCounter,
7414 ulStatsAsPercentage );
7418 /* sizeof( int ) == sizeof( long ) so a smaller
7419 * printf() library can be used. */
7420 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7421 uxBufferLength - uxConsumedBufferLength,
7423 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter,
7424 ( 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. */
7426 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7430 /* If the percentage is zero here then the task has
7431 * consumed less than 1% of the total run time. */
7432 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7434 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7435 uxBufferLength - uxConsumedBufferLength,
7436 "\t%lu\t\t<1%%\r\n",
7437 pxTaskStatusArray[ x ].ulRunTimeCounter );
7441 /* sizeof( int ) == sizeof( long ) so a smaller
7442 * printf() library can be used. */
7443 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7444 uxBufferLength - uxConsumedBufferLength,
7446 ( 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. */
7448 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7451 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7452 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7453 pcWriteBuffer += uxCharsWrittenBySnprintf; /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
7457 xOutputBufferFull = pdTRUE;
7462 xOutputBufferFull = pdTRUE;
7468 mtCOVERAGE_TEST_MARKER();
7471 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7472 * is 0 then vPortFree() will be #defined to nothing. */
7473 vPortFree( pxTaskStatusArray );
7477 mtCOVERAGE_TEST_MARKER();
7480 traceRETURN_vTaskGetRunTimeStatistics();
7483 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7484 /*-----------------------------------------------------------*/
7486 TickType_t uxTaskResetEventItemValue( void )
7488 TickType_t uxReturn;
7490 traceENTER_uxTaskResetEventItemValue();
7492 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7494 /* Reset the event list item to its normal value - so it can be used with
7495 * queues and semaphores. */
7496 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. */
7498 traceRETURN_uxTaskResetEventItemValue( uxReturn );
7502 /*-----------------------------------------------------------*/
7504 #if ( configUSE_MUTEXES == 1 )
7506 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7510 traceENTER_pvTaskIncrementMutexHeldCount();
7512 pxTCB = pxCurrentTCB;
7514 /* If xSemaphoreCreateMutex() is called before any tasks have been created
7515 * then pxCurrentTCB will be NULL. */
7518 ( pxTCB->uxMutexesHeld )++;
7521 traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7526 #endif /* configUSE_MUTEXES */
7527 /*-----------------------------------------------------------*/
7529 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7531 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7532 BaseType_t xClearCountOnExit,
7533 TickType_t xTicksToWait )
7536 BaseType_t xAlreadyYielded;
7538 traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7540 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7542 taskENTER_CRITICAL();
7544 /* Only block if the notification count is not already non-zero. */
7545 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0UL )
7547 /* Mark this task as waiting for a notification. */
7548 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7550 if( xTicksToWait > ( TickType_t ) 0 )
7552 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7554 /* We MUST suspend the scheduler before exiting the critical
7555 * section (i.e. before enabling interrupts).
7557 * If we do not do so, a notification sent from an ISR, which
7558 * happens after exiting the critical section and before
7559 * suspending the scheduler, will get lost. The sequence of
7561 * 1. Exit critical section.
7562 * 2. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7563 * task to the Ready list.
7564 * 3. Suspend scheduler.
7565 * 4. prvAddCurrentTaskToDelayedList moves the task to the
7566 * delayed or suspended list.
7567 * 5. Resume scheduler does not touch the task (because it is
7568 * not on the pendingReady list), effectively losing the
7569 * notification from the ISR.
7571 * The same does not happen when we suspend the scheduler before
7572 * exiting the critical section. The sequence of events in this
7574 * 1. Suspend scheduler.
7575 * 2. Exit critical section.
7576 * 3. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7577 * task to the pendingReady list as the scheduler is
7579 * 4. prvAddCurrentTaskToDelayedList adds the task to delayed or
7580 * suspended list. Note that this operation does not nullify
7581 * the add to pendingReady list done in the above step because
7582 * a different list item, namely xEventListItem, is used for
7583 * adding the task to the pendingReady list. In other words,
7584 * the task still remains on the pendingReady list.
7585 * 5. Resume scheduler moves the task from pendingReady list to
7590 taskEXIT_CRITICAL();
7592 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7594 xAlreadyYielded = xTaskResumeAll();
7596 if( xAlreadyYielded == pdFALSE )
7598 taskYIELD_WITHIN_API();
7602 mtCOVERAGE_TEST_MARKER();
7607 taskEXIT_CRITICAL();
7612 taskEXIT_CRITICAL();
7615 taskENTER_CRITICAL();
7617 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7618 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7620 if( ulReturn != 0UL )
7622 if( xClearCountOnExit != pdFALSE )
7624 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = 0UL;
7628 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7633 mtCOVERAGE_TEST_MARKER();
7636 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7638 taskEXIT_CRITICAL();
7640 traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7645 #endif /* configUSE_TASK_NOTIFICATIONS */
7646 /*-----------------------------------------------------------*/
7648 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7650 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7651 uint32_t ulBitsToClearOnEntry,
7652 uint32_t ulBitsToClearOnExit,
7653 uint32_t * pulNotificationValue,
7654 TickType_t xTicksToWait )
7656 BaseType_t xReturn, xAlreadyYielded;
7658 traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7660 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7662 taskENTER_CRITICAL();
7664 /* Only block if a notification is not already pending. */
7665 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7667 /* Clear bits in the task's notification value as bits may get
7668 * set by the notifying task or interrupt. This can be used to
7669 * clear the value to zero. */
7670 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7672 /* Mark this task as waiting for a notification. */
7673 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7675 if( xTicksToWait > ( TickType_t ) 0 )
7677 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7679 /* We MUST suspend the scheduler before exiting the critical
7680 * section (i.e. before enabling interrupts).
7682 * If we do not do so, a notification sent from an ISR, which
7683 * happens after exiting the critical section and before
7684 * suspending the scheduler, will get lost. The sequence of
7686 * 1. Exit critical section.
7687 * 2. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7688 * task to the Ready list.
7689 * 3. Suspend scheduler.
7690 * 4. prvAddCurrentTaskToDelayedList moves the task to the
7691 * delayed or suspended list.
7692 * 5. Resume scheduler does not touch the task (because it is
7693 * not on the pendingReady list), effectively losing the
7694 * notification from the ISR.
7696 * The same does not happen when we suspend the scheduler before
7697 * exiting the critical section. The sequence of events in this
7699 * 1. Suspend scheduler.
7700 * 2. Exit critical section.
7701 * 3. Interrupt - ISR calls xTaskNotifyFromISR which adds the
7702 * task to the pendingReady list as the scheduler is
7704 * 4. prvAddCurrentTaskToDelayedList adds the task to delayed or
7705 * suspended list. Note that this operation does not nullify
7706 * the add to pendingReady list done in the above step because
7707 * a different list item, namely xEventListItem, is used for
7708 * adding the task to the pendingReady list. In other words,
7709 * the task still remains on the pendingReady list.
7710 * 5. Resume scheduler moves the task from pendingReady list to
7715 taskEXIT_CRITICAL();
7717 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7719 xAlreadyYielded = xTaskResumeAll();
7721 if( xAlreadyYielded == pdFALSE )
7723 taskYIELD_WITHIN_API();
7727 mtCOVERAGE_TEST_MARKER();
7732 taskEXIT_CRITICAL();
7737 taskEXIT_CRITICAL();
7740 taskENTER_CRITICAL();
7742 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7744 if( pulNotificationValue != NULL )
7746 /* Output the current notification value, which may or may not
7748 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7751 /* If ucNotifyValue is set then either the task never entered the
7752 * blocked state (because a notification was already pending) or the
7753 * task unblocked because of a notification. Otherwise the task
7754 * unblocked because of a timeout. */
7755 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7757 /* A notification was not received. */
7762 /* A notification was already pending or a notification was
7763 * received while the task was waiting. */
7764 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7768 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7770 taskEXIT_CRITICAL();
7772 traceRETURN_xTaskGenericNotifyWait( xReturn );
7777 #endif /* configUSE_TASK_NOTIFICATIONS */
7778 /*-----------------------------------------------------------*/
7780 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7782 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7783 UBaseType_t uxIndexToNotify,
7785 eNotifyAction eAction,
7786 uint32_t * pulPreviousNotificationValue )
7789 BaseType_t xReturn = pdPASS;
7790 uint8_t ucOriginalNotifyState;
7792 traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7794 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7795 configASSERT( xTaskToNotify );
7796 pxTCB = xTaskToNotify;
7798 taskENTER_CRITICAL();
7800 if( pulPreviousNotificationValue != NULL )
7802 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7805 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7807 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7812 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7816 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7819 case eSetValueWithOverwrite:
7820 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7823 case eSetValueWithoutOverwrite:
7825 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7827 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7831 /* The value could not be written to the task. */
7839 /* The task is being notified without its notify value being
7845 /* Should not get here if all enums are handled.
7846 * Artificially force an assert by testing a value the
7847 * compiler can't assume is const. */
7848 configASSERT( xTickCount == ( TickType_t ) 0 );
7853 traceTASK_NOTIFY( uxIndexToNotify );
7855 /* If the task is in the blocked state specifically to wait for a
7856 * notification then unblock it now. */
7857 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7859 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7860 prvAddTaskToReadyList( pxTCB );
7862 /* The task should not have been on an event list. */
7863 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7865 #if ( configUSE_TICKLESS_IDLE != 0 )
7867 /* If a task is blocked waiting for a notification then
7868 * xNextTaskUnblockTime might be set to the blocked task's time
7869 * out time. If the task is unblocked for a reason other than
7870 * a timeout xNextTaskUnblockTime is normally left unchanged,
7871 * because it will automatically get reset to a new value when
7872 * the tick count equals xNextTaskUnblockTime. However if
7873 * tickless idling is used it might be more important to enter
7874 * sleep mode at the earliest possible time - so reset
7875 * xNextTaskUnblockTime here to ensure it is updated at the
7876 * earliest possible time. */
7877 prvResetNextTaskUnblockTime();
7881 /* Check if the notified task has a priority above the currently
7882 * executing task. */
7883 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7887 mtCOVERAGE_TEST_MARKER();
7890 taskEXIT_CRITICAL();
7892 traceRETURN_xTaskGenericNotify( xReturn );
7897 #endif /* configUSE_TASK_NOTIFICATIONS */
7898 /*-----------------------------------------------------------*/
7900 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7902 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
7903 UBaseType_t uxIndexToNotify,
7905 eNotifyAction eAction,
7906 uint32_t * pulPreviousNotificationValue,
7907 BaseType_t * pxHigherPriorityTaskWoken )
7910 uint8_t ucOriginalNotifyState;
7911 BaseType_t xReturn = pdPASS;
7912 UBaseType_t uxSavedInterruptStatus;
7914 traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
7916 configASSERT( xTaskToNotify );
7917 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7919 /* RTOS ports that support interrupt nesting have the concept of a
7920 * maximum system call (or maximum API call) interrupt priority.
7921 * Interrupts that are above the maximum system call priority are keep
7922 * permanently enabled, even when the RTOS kernel is in a critical section,
7923 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7924 * is defined in FreeRTOSConfig.h then
7925 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7926 * failure if a FreeRTOS API function is called from an interrupt that has
7927 * been assigned a priority above the configured maximum system call
7928 * priority. Only FreeRTOS functions that end in FromISR can be called
7929 * from interrupts that have been assigned a priority at or (logically)
7930 * below the maximum system call interrupt priority. FreeRTOS maintains a
7931 * separate interrupt safe API to ensure interrupt entry is as fast and as
7932 * simple as possible. More information (albeit Cortex-M specific) is
7933 * provided on the following link:
7934 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7935 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7937 pxTCB = xTaskToNotify;
7939 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7941 if( pulPreviousNotificationValue != NULL )
7943 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7946 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7947 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7952 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7956 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7959 case eSetValueWithOverwrite:
7960 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7963 case eSetValueWithoutOverwrite:
7965 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7967 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7971 /* The value could not be written to the task. */
7979 /* The task is being notified without its notify value being
7985 /* Should not get here if all enums are handled.
7986 * Artificially force an assert by testing a value the
7987 * compiler can't assume is const. */
7988 configASSERT( xTickCount == ( TickType_t ) 0 );
7992 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
7994 /* If the task is in the blocked state specifically to wait for a
7995 * notification then unblock it now. */
7996 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7998 /* The task should not have been on an event list. */
7999 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8001 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8003 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8004 prvAddTaskToReadyList( pxTCB );
8008 /* The delayed and ready lists cannot be accessed, so hold
8009 * this task pending until the scheduler is resumed. */
8010 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8013 #if ( configNUMBER_OF_CORES == 1 )
8015 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8017 /* The notified task has a priority above the currently
8018 * executing task so a yield is required. */
8019 if( pxHigherPriorityTaskWoken != NULL )
8021 *pxHigherPriorityTaskWoken = pdTRUE;
8024 /* Mark that a yield is pending in case the user is not
8025 * using the "xHigherPriorityTaskWoken" parameter to an ISR
8026 * safe FreeRTOS function. */
8027 xYieldPendings[ 0 ] = pdTRUE;
8031 mtCOVERAGE_TEST_MARKER();
8034 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8036 #if ( configUSE_PREEMPTION == 1 )
8038 prvYieldForTask( pxTCB );
8040 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8042 if( pxHigherPriorityTaskWoken != NULL )
8044 *pxHigherPriorityTaskWoken = pdTRUE;
8048 #endif /* if ( configUSE_PREEMPTION == 1 ) */
8050 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8053 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8055 traceRETURN_xTaskGenericNotifyFromISR( xReturn );
8060 #endif /* configUSE_TASK_NOTIFICATIONS */
8061 /*-----------------------------------------------------------*/
8063 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8065 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
8066 UBaseType_t uxIndexToNotify,
8067 BaseType_t * pxHigherPriorityTaskWoken )
8070 uint8_t ucOriginalNotifyState;
8071 UBaseType_t uxSavedInterruptStatus;
8073 traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
8075 configASSERT( xTaskToNotify );
8076 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8078 /* RTOS ports that support interrupt nesting have the concept of a
8079 * maximum system call (or maximum API call) interrupt priority.
8080 * Interrupts that are above the maximum system call priority are keep
8081 * permanently enabled, even when the RTOS kernel is in a critical section,
8082 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
8083 * is defined in FreeRTOSConfig.h then
8084 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
8085 * failure if a FreeRTOS API function is called from an interrupt that has
8086 * been assigned a priority above the configured maximum system call
8087 * priority. Only FreeRTOS functions that end in FromISR can be called
8088 * from interrupts that have been assigned a priority at or (logically)
8089 * below the maximum system call interrupt priority. FreeRTOS maintains a
8090 * separate interrupt safe API to ensure interrupt entry is as fast and as
8091 * simple as possible. More information (albeit Cortex-M specific) is
8092 * provided on the following link:
8093 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
8094 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
8096 pxTCB = xTaskToNotify;
8098 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
8100 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
8101 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
8103 /* 'Giving' is equivalent to incrementing a count in a counting
8105 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
8107 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
8109 /* If the task is in the blocked state specifically to wait for a
8110 * notification then unblock it now. */
8111 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
8113 /* The task should not have been on an event list. */
8114 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
8116 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
8118 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
8119 prvAddTaskToReadyList( pxTCB );
8123 /* The delayed and ready lists cannot be accessed, so hold
8124 * this task pending until the scheduler is resumed. */
8125 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
8128 #if ( configNUMBER_OF_CORES == 1 )
8130 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
8132 /* The notified task has a priority above the currently
8133 * executing task so a yield is required. */
8134 if( pxHigherPriorityTaskWoken != NULL )
8136 *pxHigherPriorityTaskWoken = pdTRUE;
8139 /* Mark that a yield is pending in case the user is not
8140 * using the "xHigherPriorityTaskWoken" parameter in an ISR
8141 * safe FreeRTOS function. */
8142 xYieldPendings[ 0 ] = pdTRUE;
8146 mtCOVERAGE_TEST_MARKER();
8149 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8151 #if ( configUSE_PREEMPTION == 1 )
8153 prvYieldForTask( pxTCB );
8155 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
8157 if( pxHigherPriorityTaskWoken != NULL )
8159 *pxHigherPriorityTaskWoken = pdTRUE;
8163 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
8165 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8168 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
8170 traceRETURN_vTaskGenericNotifyGiveFromISR();
8173 #endif /* configUSE_TASK_NOTIFICATIONS */
8174 /*-----------------------------------------------------------*/
8176 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8178 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
8179 UBaseType_t uxIndexToClear )
8184 traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
8186 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8188 /* If null is passed in here then it is the calling task that is having
8189 * its notification state cleared. */
8190 pxTCB = prvGetTCBFromHandle( xTask );
8192 taskENTER_CRITICAL();
8194 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
8196 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
8204 taskEXIT_CRITICAL();
8206 traceRETURN_xTaskGenericNotifyStateClear( xReturn );
8211 #endif /* configUSE_TASK_NOTIFICATIONS */
8212 /*-----------------------------------------------------------*/
8214 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
8216 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
8217 UBaseType_t uxIndexToClear,
8218 uint32_t ulBitsToClear )
8223 traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
8225 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
8227 /* If null is passed in here then it is the calling task that is having
8228 * its notification state cleared. */
8229 pxTCB = prvGetTCBFromHandle( xTask );
8231 taskENTER_CRITICAL();
8233 /* Return the notification as it was before the bits were cleared,
8234 * then clear the bit mask. */
8235 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
8236 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
8238 taskEXIT_CRITICAL();
8240 traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
8245 #endif /* configUSE_TASK_NOTIFICATIONS */
8246 /*-----------------------------------------------------------*/
8248 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8250 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
8254 traceENTER_ulTaskGetRunTimeCounter( xTask );
8256 pxTCB = prvGetTCBFromHandle( xTask );
8258 traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
8260 return pxTCB->ulRunTimeCounter;
8263 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8264 /*-----------------------------------------------------------*/
8266 #if ( configGENERATE_RUN_TIME_STATS == 1 )
8268 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
8271 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8273 traceENTER_ulTaskGetRunTimePercent( xTask );
8275 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
8277 /* For percentage calculations. */
8278 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8280 /* Avoid divide by zero errors. */
8281 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8283 pxTCB = prvGetTCBFromHandle( xTask );
8284 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
8291 traceRETURN_ulTaskGetRunTimePercent( ulReturn );
8296 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
8297 /*-----------------------------------------------------------*/
8299 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8301 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
8303 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
8306 traceENTER_ulTaskGetIdleRunTimeCounter();
8308 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8310 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8313 traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
8318 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8319 /*-----------------------------------------------------------*/
8321 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
8323 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
8325 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
8326 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
8329 traceENTER_ulTaskGetIdleRunTimePercent();
8331 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
8333 /* For percentage calculations. */
8334 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
8336 /* Avoid divide by zero errors. */
8337 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
8339 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
8341 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
8344 ulReturn = ulRunTimeCounter / ulTotalTime;
8351 traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
8356 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
8357 /*-----------------------------------------------------------*/
8359 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
8360 const BaseType_t xCanBlockIndefinitely )
8362 TickType_t xTimeToWake;
8363 const TickType_t xConstTickCount = xTickCount;
8365 #if ( INCLUDE_xTaskAbortDelay == 1 )
8367 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
8368 * reset to pdFALSE so it can be detected as having been set to pdTRUE
8369 * when the task leaves the Blocked state. */
8370 pxCurrentTCB->ucDelayAborted = pdFALSE;
8374 /* Remove the task from the ready list before adding it to the blocked list
8375 * as the same list item is used for both lists. */
8376 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
8378 /* The current task must be in a ready list, so there is no need to
8379 * check, and the port reset macro can be called directly. */
8380 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. */
8384 mtCOVERAGE_TEST_MARKER();
8387 #if ( INCLUDE_vTaskSuspend == 1 )
8389 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
8391 /* Add the task to the suspended task list instead of a delayed task
8392 * list to ensure it is not woken by a timing event. It will block
8394 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
8398 /* Calculate the time at which the task should be woken if the event
8399 * does not occur. This may overflow but this doesn't matter, the
8400 * kernel will manage it correctly. */
8401 xTimeToWake = xConstTickCount + xTicksToWait;
8403 /* The list item will be inserted in wake time order. */
8404 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8406 if( xTimeToWake < xConstTickCount )
8408 /* Wake time has overflowed. Place this item in the overflow
8410 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8411 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8415 /* The wake time has not overflowed, so the current block list
8417 traceMOVED_TASK_TO_DELAYED_LIST();
8418 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8420 /* If the task entering the blocked state was placed at the
8421 * head of the list of blocked tasks then xNextTaskUnblockTime
8422 * needs to be updated too. */
8423 if( xTimeToWake < xNextTaskUnblockTime )
8425 xNextTaskUnblockTime = xTimeToWake;
8429 mtCOVERAGE_TEST_MARKER();
8434 #else /* INCLUDE_vTaskSuspend */
8436 /* Calculate the time at which the task should be woken if the event
8437 * does not occur. This may overflow but this doesn't matter, the kernel
8438 * will manage it correctly. */
8439 xTimeToWake = xConstTickCount + xTicksToWait;
8441 /* The list item will be inserted in wake time order. */
8442 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8444 if( xTimeToWake < xConstTickCount )
8446 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8447 /* Wake time has overflowed. Place this item in the overflow list. */
8448 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8452 traceMOVED_TASK_TO_DELAYED_LIST();
8453 /* The wake time has not overflowed, so the current block list is used. */
8454 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8456 /* If the task entering the blocked state was placed at the head of the
8457 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
8459 if( xTimeToWake < xNextTaskUnblockTime )
8461 xNextTaskUnblockTime = xTimeToWake;
8465 mtCOVERAGE_TEST_MARKER();
8469 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
8470 ( void ) xCanBlockIndefinitely;
8472 #endif /* INCLUDE_vTaskSuspend */
8474 /*-----------------------------------------------------------*/
8476 #if ( portUSING_MPU_WRAPPERS == 1 )
8478 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
8482 traceENTER_xTaskGetMPUSettings( xTask );
8484 pxTCB = prvGetTCBFromHandle( xTask );
8486 traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
8488 return &( pxTCB->xMPUSettings );
8491 #endif /* portUSING_MPU_WRAPPERS */
8492 /*-----------------------------------------------------------*/
8494 /* Code below here allows additional code to be inserted into this source file,
8495 * especially where access to file scope functions and data is needed (for example
8496 * when performing module tests). */
8498 #ifdef FREERTOS_MODULE_TEST
8499 #include "tasks_test_access_functions.h"
8503 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
8505 #include "freertos_tasks_c_additions.h"
8507 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8508 static void freertos_tasks_c_additions_init( void )
8510 FREERTOS_TASKS_C_ADDITIONS_INIT();
8514 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8515 /*-----------------------------------------------------------*/
8517 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8520 * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8521 * to provide the memory that is used by the Idle task. It is used when
8522 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8523 * it's own implementation of vApplicationGetIdleTaskMemory by setting
8524 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8526 #if ( configNUMBER_OF_CORES == 1 )
8528 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8529 StackType_t ** ppxIdleTaskStackBuffer,
8530 uint32_t * pulIdleTaskStackSize )
8532 static StaticTask_t xIdleTaskTCB;
8533 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8535 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8536 *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8537 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8540 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
8542 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8543 StackType_t ** ppxIdleTaskStackBuffer,
8544 uint32_t * pulIdleTaskStackSize,
8545 BaseType_t xCoreId )
8547 static StaticTask_t xIdleTaskTCBs[ configNUMBER_OF_CORES ];
8548 static StackType_t uxIdleTaskStacks[ configNUMBER_OF_CORES ][ configMINIMAL_STACK_SIZE ];
8550 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCBs[ xCoreId ] );
8551 *ppxIdleTaskStackBuffer = &( uxIdleTaskStacks[ xCoreId ][ 0 ] );
8552 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8555 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
8557 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8558 /*-----------------------------------------------------------*/
8560 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8563 * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8564 * to provide the memory that is used by the Timer service task. It is used when
8565 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8566 * it's own implementation of vApplicationGetTimerTaskMemory by setting
8567 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8569 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8570 StackType_t ** ppxTimerTaskStackBuffer,
8571 uint32_t * pulTimerTaskStackSize )
8573 static StaticTask_t xTimerTaskTCB;
8574 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8576 *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8577 *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8578 *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8581 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8582 /*-----------------------------------------------------------*/