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 /* Code below here allows infinite loop controlling, especially for the infinite loop
321 * in idle task function (for example when performing unit tests). */
322 #ifndef INFINITE_LOOP
323 #define INFINITE_LOOP() 1
326 #define taskBITS_PER_BYTE ( ( size_t ) 8 )
329 * Task control block. A task control block (TCB) is allocated for each task,
330 * and stores task state information, including a pointer to the task's context
331 * (the task's run time environment, including register values)
333 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
335 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. */
337 #if ( portUSING_MPU_WRAPPERS == 1 )
338 xMPU_SETTINGS xMPUSettings; /**< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
341 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
342 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. */
345 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 ). */
346 ListItem_t xEventListItem; /**< Used to reference a task from an event list. */
347 UBaseType_t uxPriority; /**< The priority of the task. 0 is the lowest priority. */
348 StackType_t * pxStack; /**< Points to the start of the stack. */
349 #if ( configNUMBER_OF_CORES > 1 )
350 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. */
351 UBaseType_t uxTaskAttributes; /**< Task's attributes - currently used to identify the idle tasks. */
353 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. */
355 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
356 BaseType_t xPreemptionDisable; /**< Used to prevent the task from being preempted. */
359 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
360 StackType_t * pxEndOfStack; /**< Points to the highest valid address for the stack. */
363 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
364 UBaseType_t uxCriticalNesting; /**< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
367 #if ( configUSE_TRACE_FACILITY == 1 )
368 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. */
369 UBaseType_t uxTaskNumber; /**< Stores a number specifically for use by third party trace code. */
372 #if ( configUSE_MUTEXES == 1 )
373 UBaseType_t uxBasePriority; /**< The priority last assigned to the task - used by the priority inheritance mechanism. */
374 UBaseType_t uxMutexesHeld;
377 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
378 TaskHookFunction_t pxTaskTag;
381 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
382 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
385 #if ( configGENERATE_RUN_TIME_STATS == 1 )
386 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /**< Stores the amount of time the task has spent in the Running state. */
389 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
390 configTLS_BLOCK_TYPE xTLSBlock; /**< Memory block used as Thread Local Storage (TLS) Block for the task. */
393 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
394 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
395 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
398 /* See the comments in FreeRTOS.h with the definition of
399 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
400 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
401 uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
404 #if ( INCLUDE_xTaskAbortDelay == 1 )
405 uint8_t ucDelayAborted;
408 #if ( configUSE_POSIX_ERRNO == 1 )
413 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
414 * below to enable the use of older kernel aware debuggers. */
415 typedef tskTCB TCB_t;
417 /*lint -save -e956 A manual analysis and inspection has been used to determine
418 * which static variables must be declared volatile. */
419 #if ( configNUMBER_OF_CORES == 1 )
420 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
422 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
423 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
424 /* coverity[misra_c_2012_rule_8_4_violation] */
425 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ];
426 #define pxCurrentTCB xTaskGetCurrentTaskHandle()
429 /* Lists for ready and blocked tasks. --------------------
430 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
431 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
432 * the static qualifier. */
433 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
434 PRIVILEGED_DATA static List_t xDelayedTaskList1; /**< Delayed tasks. */
435 PRIVILEGED_DATA static List_t xDelayedTaskList2; /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
436 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /**< Points to the delayed task list currently being used. */
437 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. */
438 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. */
440 #if ( INCLUDE_vTaskDelete == 1 )
442 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
443 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
447 #if ( INCLUDE_vTaskSuspend == 1 )
449 PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
453 /* Global POSIX errno. Its value is changed upon context switching to match
454 * the errno of the currently running task. */
455 #if ( configUSE_POSIX_ERRNO == 1 )
456 int FreeRTOS_errno = 0;
459 /* Other file private variables. --------------------------------*/
460 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
461 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
462 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
463 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
464 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
465 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
466 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
467 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
468 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
469 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. */
471 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
472 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
473 * to determine the number of priority lists to read back from the remote target. */
474 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
476 /* Context switches are held pending while the scheduler is suspended. Also,
477 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
478 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
479 * If an interrupt needs to unblock a task while the scheduler is suspended then it
480 * moves the task's event list item into the xPendingReadyList, ready for the
481 * kernel to move the task from the pending ready list into the real ready list
482 * when the scheduler is unsuspended. The pending ready list itself can only be
483 * accessed from a critical section.
485 * Updates to uxSchedulerSuspended must be protected by both the task lock and the ISR lock
486 * and must not be done from an ISR. Reads must be protected by either lock and may be done
487 * from either an ISR or a task. */
488 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
490 #if ( configGENERATE_RUN_TIME_STATS == 1 )
492 /* Do not move these variables to function scope as doing so prevents the
493 * code working with debuggers that need to remove the static qualifier. */
494 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. */
495 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. */
499 #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configNUMBER_OF_CORES > 1 )
501 /* Do not move these variables to function scope as doing so prevents the
502 * code working with debuggers that need to remove the static qualifier. */
503 static StaticTask_t xIdleTCBBuffers[ configNUMBER_OF_CORES - 1 ];
504 static StackType_t xIdleTaskStackBuffers[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
506 #endif /* #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
510 /*-----------------------------------------------------------*/
512 /* File private functions. --------------------------------*/
515 * Creates the idle tasks during scheduler start.
517 static BaseType_t prvCreateIdleTasks( void );
519 #if ( configNUMBER_OF_CORES > 1 )
522 * Checks to see if another task moved the current task out of the ready
523 * list while it was waiting to enter a critical section and yields, if so.
525 static void prvCheckForRunStateChange( void );
526 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
528 #if ( configNUMBER_OF_CORES > 1 )
531 * Yields the given core.
533 static void prvYieldCore( BaseType_t xCoreID );
534 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
536 #if ( configNUMBER_OF_CORES > 1 )
539 * Yields a core, or cores if multiple priorities are not allowed to run
540 * simultaneously, to allow the task pxTCB to run.
542 static void prvYieldForTask( const TCB_t * pxTCB );
543 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
545 #if ( configNUMBER_OF_CORES > 1 )
548 * Selects the highest priority available task for the given core.
550 static void prvSelectHighestPriorityTask( BaseType_t xCoreID );
551 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
554 * Utility task that simply returns pdTRUE if the task referenced by xTask is
555 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
556 * is in any other state.
558 #if ( INCLUDE_vTaskSuspend == 1 )
560 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
562 #endif /* INCLUDE_vTaskSuspend */
565 * Utility to ready all the lists used by the scheduler. This is called
566 * automatically upon the creation of the first task.
568 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
571 * The idle task, which as all tasks is implemented as a never ending loop.
572 * The idle task is automatically created and added to the ready lists upon
573 * creation of the first user task.
575 * In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 minimal idle tasks are also
576 * created to ensure that each core has an idle task to run when no other
577 * task is available to run.
579 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
580 * language extensions. The equivalent prototype for these functions are:
582 * void prvIdleTask( void *pvParameters );
583 * void prvMinimalIdleTask( void *pvParameters );
586 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
587 #if ( configNUMBER_OF_CORES > 1 )
588 static portTASK_FUNCTION_PROTO( prvMinimalIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
592 * Utility to free all memory allocated by the scheduler to hold a TCB,
593 * including the stack pointed to by the TCB.
595 * This does not free memory allocated by the task itself (i.e. memory
596 * allocated by calls to pvPortMalloc from within the tasks application code).
598 #if ( INCLUDE_vTaskDelete == 1 )
600 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
605 * Used only by the idle task. This checks to see if anything has been placed
606 * in the list of tasks waiting to be deleted. If so the task is cleaned up
607 * and its TCB deleted.
609 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
612 * The currently executing task is entering the Blocked state. Add the task to
613 * either the current or the overflow delayed task list.
615 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
616 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
619 * Fills an TaskStatus_t structure with information on each task that is
620 * referenced from the pxList list (which may be a ready list, a delayed list,
621 * a suspended list, etc.).
623 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
624 * NORMAL APPLICATION CODE.
626 #if ( configUSE_TRACE_FACILITY == 1 )
628 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
630 eTaskState eState ) PRIVILEGED_FUNCTION;
635 * Searches pxList for a task with name pcNameToQuery - returning a handle to
636 * the task if it is found, or NULL if the task is not found.
638 #if ( INCLUDE_xTaskGetHandle == 1 )
640 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
641 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
646 * When a task is created, the stack of the task is filled with a known value.
647 * This function determines the 'high water mark' of the task stack by
648 * determining how much of the stack remains at the original preset value.
650 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
652 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
657 * Return the amount of time, in ticks, that will pass before the kernel will
658 * next move a task from the Blocked state to the Running state.
660 * This conditional compilation should use inequality to 0, not equality to 1.
661 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
662 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
663 * set to a value other than 1.
665 #if ( configUSE_TICKLESS_IDLE != 0 )
667 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
672 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
673 * will exit the Blocked state.
675 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
677 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
680 * Helper function used to pad task names with spaces when printing out
681 * human readable tables of task information.
683 static char * prvWriteNameToBuffer( char * pcBuffer,
684 const char * pcTaskName ) PRIVILEGED_FUNCTION;
689 * Called after a Task_t structure has been allocated either statically or
690 * dynamically to fill in the structure's members.
692 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
693 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
694 const uint32_t ulStackDepth,
695 void * const pvParameters,
696 UBaseType_t uxPriority,
697 TaskHandle_t * const pxCreatedTask,
699 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
702 * Called after a new task has been created and initialised to place the task
703 * under the control of the scheduler.
705 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
708 * freertos_tasks_c_additions_init() should only be called if the user definable
709 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
710 * called by the function.
712 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
714 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
718 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
719 extern void vApplicationMinimalIdleHook( void );
720 #endif /* #if ( configUSE_MINIMAL_IDLE_HOOK == 1 ) */
722 /*-----------------------------------------------------------*/
724 #if ( configNUMBER_OF_CORES > 1 )
725 static void prvCheckForRunStateChange( void )
727 UBaseType_t uxPrevCriticalNesting;
728 const TCB_t * pxThisTCB;
730 /* This must only be called from within a task. */
731 portASSERT_IF_IN_ISR();
733 /* This function is always called with interrupts disabled
734 * so this is safe. */
735 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
737 while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD )
739 /* We are only here if we just entered a critical section
740 * or if we just suspended the scheduler, and another task
741 * has requested that we yield.
743 * This is slightly complicated since we need to save and restore
744 * the suspension and critical nesting counts, as well as release
745 * and reacquire the correct locks. And then, do it all over again
746 * if our state changed again during the reacquisition. */
747 uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
749 if( uxPrevCriticalNesting > 0U )
751 portSET_CRITICAL_NESTING_COUNT( 0U );
752 portRELEASE_ISR_LOCK();
756 /* The scheduler is suspended. uxSchedulerSuspended is updated
757 * only when the task is not requested to yield. */
758 mtCOVERAGE_TEST_MARKER();
761 portRELEASE_TASK_LOCK();
762 portMEMORY_BARRIER();
763 configASSERT( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD );
765 portENABLE_INTERRUPTS();
767 /* Enabling interrupts should cause this core to immediately
768 * service the pending interrupt and yield. If the run state is still
769 * yielding here then that is a problem. */
770 configASSERT( pxThisTCB->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD );
772 portDISABLE_INTERRUPTS();
776 portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
778 if( uxPrevCriticalNesting == 0U )
780 portRELEASE_ISR_LOCK();
784 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
786 /*-----------------------------------------------------------*/
788 #if ( configNUMBER_OF_CORES > 1 )
789 static void prvYieldCore( BaseType_t xCoreID )
791 /* This must be called from a critical section and xCoreID must be valid. */
792 if( ( portCHECK_IF_IN_ISR() == pdTRUE ) && ( xCoreID == ( BaseType_t ) portGET_CORE_ID() ) )
794 xYieldPendings[ xCoreID ] = pdTRUE;
798 if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD )
800 if( xCoreID == ( BaseType_t ) portGET_CORE_ID() )
802 xYieldPendings[ xCoreID ] = pdTRUE;
806 portYIELD_CORE( xCoreID );
807 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_SCHEDULED_TO_YIELD;
812 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
813 /*-----------------------------------------------------------*/
815 #if ( configNUMBER_OF_CORES > 1 )
816 static void prvYieldForTask( const TCB_t * pxTCB )
818 BaseType_t xLowestPriorityToPreempt;
819 BaseType_t xCurrentCoreTaskPriority;
820 BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
823 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
824 BaseType_t xYieldCount = 0;
825 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
827 /* This must be called from a critical section. */
828 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
830 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
832 /* No task should yield for this one if it is a lower priority
833 * than priority level of currently ready tasks. */
834 if( pxTCB->uxPriority >= uxTopReadyPriority )
836 /* Yield is not required for a task which is already running. */
837 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
840 xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
842 /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
843 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
844 --xLowestPriorityToPreempt;
846 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
848 xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
850 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
851 if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
853 xCurrentCoreTaskPriority = xCurrentCoreTaskPriority - 1;
856 if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
858 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
859 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
862 if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
864 #if ( configUSE_CORE_AFFINITY == 1 )
865 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
868 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
869 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
872 xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
873 xLowestPriorityCore = xCoreID;
879 mtCOVERAGE_TEST_MARKER();
883 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
885 /* Yield all currently running non-idle tasks with a priority lower than
886 * the task that needs to run. */
887 if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
888 ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
890 prvYieldCore( xCoreID );
895 mtCOVERAGE_TEST_MARKER();
898 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
902 mtCOVERAGE_TEST_MARKER();
906 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
907 if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
908 #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
909 if( xLowestPriorityCore >= 0 )
910 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
912 prvYieldCore( xLowestPriorityCore );
915 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
916 /* Verify that the calling core always yields to higher priority tasks. */
917 if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0 ) &&
918 ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
920 configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) ||
921 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) );
926 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
927 /*-----------------------------------------------------------*/
929 #if ( configNUMBER_OF_CORES > 1 )
930 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
932 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
933 BaseType_t xTaskScheduled = pdFALSE;
934 BaseType_t xDecrementTopPriority = pdTRUE;
936 #if ( configUSE_CORE_AFFINITY == 1 )
937 const TCB_t * pxPreviousTCB = NULL;
939 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
940 BaseType_t xPriorityDropped = pdFALSE;
943 /* This function should be called when scheduler is running. */
944 configASSERT( xSchedulerRunning == pdTRUE );
946 /* A new task is created and a running task with the same priority yields
947 * itself to run the new task. When a running task yields itself, it is still
948 * in the ready list. This running task will be selected before the new task
949 * since the new task is always added to the end of the ready list.
950 * The other problem is that the running task still in the same position of
951 * the ready list when it yields itself. It is possible that it will be selected
952 * earlier then other tasks which waits longer than this task.
954 * To fix these problems, the running task should be put to the end of the
955 * ready list before searching for the ready task in the ready list. */
956 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
957 &pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
959 ( void ) uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
960 vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
961 &pxCurrentTCBs[ xCoreID ]->xStateListItem );
964 while( xTaskScheduled == pdFALSE )
966 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
968 if( uxCurrentPriority < uxTopReadyPriority )
970 /* We can't schedule any tasks, other than idle, that have a
971 * priority lower than the priority of a task currently running
972 * on another core. */
973 uxCurrentPriority = tskIDLE_PRIORITY;
978 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
980 const List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
981 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxReadyList );
982 ListItem_t * pxIterator;
984 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
985 * must not be decremented any further. */
986 xDecrementTopPriority = pdFALSE;
988 for( pxIterator = listGET_HEAD_ENTRY( pxReadyList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
990 TCB_t * pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
992 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
994 /* When falling back to the idle priority because only one priority
995 * level is allowed to run at a time, we should ONLY schedule the true
996 * idle tasks, not user tasks at the idle priority. */
997 if( uxCurrentPriority < uxTopReadyPriority )
999 if( ( pxTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0 )
1005 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1007 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
1009 #if ( configUSE_CORE_AFFINITY == 1 )
1010 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1013 /* If the task is not being executed by any core swap it in. */
1014 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
1015 #if ( configUSE_CORE_AFFINITY == 1 )
1016 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
1018 pxTCB->xTaskRunState = xCoreID;
1019 pxCurrentTCBs[ xCoreID ] = pxTCB;
1020 xTaskScheduled = pdTRUE;
1023 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
1025 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) );
1027 #if ( configUSE_CORE_AFFINITY == 1 )
1028 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1031 /* The task is already running on this core, mark it as scheduled. */
1032 pxTCB->xTaskRunState = xCoreID;
1033 xTaskScheduled = pdTRUE;
1038 /* This task is running on the core other than xCoreID. */
1039 mtCOVERAGE_TEST_MARKER();
1042 if( xTaskScheduled != pdFALSE )
1044 /* A task has been selected to run on this core. */
1051 if( xDecrementTopPriority != pdFALSE )
1053 uxTopReadyPriority--;
1054 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1056 xPriorityDropped = pdTRUE;
1062 /* There are configNUMBER_OF_CORES Idle tasks created when scheduler started.
1063 * The scheduler should be able to select a task to run when uxCurrentPriority
1064 * is tskIDLE_PRIORITY. uxCurrentPriority is never decreased to value blow
1065 * tskIDLE_PRIORITY. */
1066 if( uxCurrentPriority > tskIDLE_PRIORITY )
1068 uxCurrentPriority--;
1072 /* This function is called when idle task is not created. Break the
1073 * loop to prevent uxCurrentPriority overrun. */
1078 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1080 if( xTaskScheduled == pdTRUE )
1082 if( xPriorityDropped != pdFALSE )
1084 /* There may be several ready tasks that were being prevented from running because there was
1085 * a higher priority task running. Now that the last of the higher priority tasks is no longer
1086 * running, make sure all the other idle tasks yield. */
1089 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUMBER_OF_CORES; x++ )
1091 if( ( pxCurrentTCBs[ x ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0 )
1099 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1101 #if ( configUSE_CORE_AFFINITY == 1 )
1103 if( xTaskScheduled == pdTRUE )
1105 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
1107 /* A ready task was just evicted from this core. See if it can be
1108 * scheduled on any other core. */
1109 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
1110 BaseType_t xLowestPriority = ( BaseType_t ) pxPreviousTCB->uxPriority;
1111 BaseType_t xLowestPriorityCore = -1;
1114 if( ( pxPreviousTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1116 xLowestPriority = xLowestPriority - 1;
1119 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1121 /* The ready task that was removed from this core is not excluded from it.
1122 * Only look at the intersection of the cores the removed task is allowed to run
1123 * on with the cores that the new task is excluded from. It is possible that the
1124 * new task was only placed onto this core because it is excluded from another.
1125 * Check to see if the previous task could run on one of those cores. */
1126 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1130 /* The ready task that was removed from this core is excluded from it. */
1133 uxCoreMap &= ( ( 1U << configNUMBER_OF_CORES ) - 1U );
1135 for( x = ( ( BaseType_t ) configNUMBER_OF_CORES - 1 ); x >= ( BaseType_t ) 0; x-- )
1137 UBaseType_t uxCore = ( UBaseType_t ) x;
1138 BaseType_t xTaskPriority;
1140 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << uxCore ) ) != 0U )
1142 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority;
1144 if( ( pxCurrentTCBs[ uxCore ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1146 xTaskPriority = xTaskPriority - ( BaseType_t ) 1;
1149 uxCoreMap &= ~( ( UBaseType_t ) 1U << uxCore );
1151 if( ( xTaskPriority < xLowestPriority ) &&
1152 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ] ) != pdFALSE ) &&
1153 ( xYieldPendings[ uxCore ] == pdFALSE ) )
1155 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1156 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1159 xLowestPriority = xTaskPriority;
1160 xLowestPriorityCore = ( BaseType_t ) uxCore;
1166 if( xLowestPriorityCore >= 0 )
1168 prvYieldCore( xLowestPriorityCore );
1173 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) */
1176 #endif /* ( configNUMBER_OF_CORES > 1 ) */
1178 /*-----------------------------------------------------------*/
1180 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1182 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1183 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1184 const uint32_t ulStackDepth,
1185 void * const pvParameters,
1186 UBaseType_t uxPriority,
1187 StackType_t * const puxStackBuffer,
1188 StaticTask_t * const pxTaskBuffer )
1189 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1191 return xTaskCreateStaticAffinitySet( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, tskNO_AFFINITY );
1194 TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1195 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1196 const uint32_t ulStackDepth,
1197 void * const pvParameters,
1198 UBaseType_t uxPriority,
1199 StackType_t * const puxStackBuffer,
1200 StaticTask_t * const pxTaskBuffer,
1201 UBaseType_t uxCoreAffinityMask )
1202 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1205 TaskHandle_t xReturn;
1207 configASSERT( puxStackBuffer != NULL );
1208 configASSERT( pxTaskBuffer != NULL );
1210 #if ( configASSERT_DEFINED == 1 )
1212 /* Sanity check that the size of the structure used to declare a
1213 * variable of type StaticTask_t equals the size of the real task
1215 volatile size_t xSize = sizeof( StaticTask_t );
1216 configASSERT( xSize == sizeof( TCB_t ) );
1217 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1219 #endif /* configASSERT_DEFINED */
1221 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1223 /* The memory used for the task's TCB and stack are passed into this
1224 * function - use them. */
1225 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. */
1226 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1227 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1229 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1231 /* Tasks can be created statically or dynamically, so note this
1232 * task was created statically in case the task is later deleted. */
1233 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1235 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1237 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1239 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1241 /* Set the task's affinity before scheduling it. */
1242 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1246 prvAddNewTaskToReadyList( pxNewTCB );
1256 #endif /* SUPPORT_STATIC_ALLOCATION */
1257 /*-----------------------------------------------------------*/
1259 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1261 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1262 TaskHandle_t * pxCreatedTask )
1263 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1265 return xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1268 BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1269 UBaseType_t uxCoreAffinityMask,
1270 TaskHandle_t * pxCreatedTask )
1271 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1274 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1276 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1277 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1279 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1281 /* Allocate space for the TCB. Where the memory comes from depends
1282 * on the implementation of the port malloc function and whether or
1283 * not static allocation is being used. */
1284 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1285 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1287 /* Store the stack location in the TCB. */
1288 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1290 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1292 /* Tasks can be created statically or dynamically, so note this
1293 * task was created statically in case the task is later deleted. */
1294 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1296 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1298 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1299 pxTaskDefinition->pcName,
1300 ( uint32_t ) pxTaskDefinition->usStackDepth,
1301 pxTaskDefinition->pvParameters,
1302 pxTaskDefinition->uxPriority,
1303 pxCreatedTask, pxNewTCB,
1304 pxTaskDefinition->xRegions );
1306 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1308 /* Set the task's affinity before scheduling it. */
1309 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1313 prvAddNewTaskToReadyList( pxNewTCB );
1320 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1321 /*-----------------------------------------------------------*/
1323 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1325 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1326 TaskHandle_t * pxCreatedTask )
1327 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1329 return xTaskCreateRestrictedAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1332 BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1333 UBaseType_t uxCoreAffinityMask,
1334 TaskHandle_t * pxCreatedTask )
1335 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1338 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1340 configASSERT( pxTaskDefinition->puxStackBuffer );
1342 if( pxTaskDefinition->puxStackBuffer != NULL )
1344 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1346 if( pxNewTCB != NULL )
1348 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1350 /* Store the stack location in the TCB. */
1351 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1353 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1355 /* Tasks can be created statically or dynamically, so note
1356 * this task had a statically allocated stack in case it is
1357 * later deleted. The TCB was allocated dynamically. */
1358 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1360 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1362 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1363 pxTaskDefinition->pcName,
1364 ( uint32_t ) pxTaskDefinition->usStackDepth,
1365 pxTaskDefinition->pvParameters,
1366 pxTaskDefinition->uxPriority,
1367 pxCreatedTask, pxNewTCB,
1368 pxTaskDefinition->xRegions );
1370 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1372 /* Set the task's affinity before scheduling it. */
1373 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1377 prvAddNewTaskToReadyList( pxNewTCB );
1385 #endif /* portUSING_MPU_WRAPPERS */
1386 /*-----------------------------------------------------------*/
1388 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1390 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1391 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1392 const configSTACK_DEPTH_TYPE usStackDepth,
1393 void * const pvParameters,
1394 UBaseType_t uxPriority,
1395 TaskHandle_t * const pxCreatedTask )
1396 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1398 return xTaskCreateAffinitySet( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, tskNO_AFFINITY, pxCreatedTask );
1401 BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1402 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1403 const configSTACK_DEPTH_TYPE usStackDepth,
1404 void * const pvParameters,
1405 UBaseType_t uxPriority,
1406 UBaseType_t uxCoreAffinityMask,
1407 TaskHandle_t * const pxCreatedTask )
1408 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1413 /* If the stack grows down then allocate the stack then the TCB so the stack
1414 * does not grow into the TCB. Likewise if the stack grows up then allocate
1415 * the TCB then the stack. */
1416 #if ( portSTACK_GROWTH > 0 )
1418 /* Allocate space for the TCB. Where the memory comes from depends on
1419 * the implementation of the port malloc function and whether or not static
1420 * allocation is being used. */
1421 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1423 if( pxNewTCB != NULL )
1425 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1427 /* Allocate space for the stack used by the task being created.
1428 * The base of the stack memory stored in the TCB so the task can
1429 * be deleted later if required. */
1430 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1432 if( pxNewTCB->pxStack == NULL )
1434 /* Could not allocate the stack. Delete the allocated TCB. */
1435 vPortFree( pxNewTCB );
1440 #else /* portSTACK_GROWTH */
1442 StackType_t * pxStack;
1444 /* Allocate space for the stack used by the task being created. */
1445 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. */
1447 if( pxStack != NULL )
1449 /* Allocate space for the TCB. */
1450 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. */
1452 if( pxNewTCB != NULL )
1454 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1456 /* Store the stack location in the TCB. */
1457 pxNewTCB->pxStack = pxStack;
1461 /* The stack cannot be used as the TCB was not created. Free
1463 vPortFreeStack( pxStack );
1471 #endif /* portSTACK_GROWTH */
1473 if( pxNewTCB != NULL )
1475 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1477 /* Tasks can be created statically or dynamically, so note this
1478 * task was created dynamically in case it is later deleted. */
1479 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1481 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1483 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1485 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1487 /* Set the task's affinity before scheduling it. */
1488 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1492 prvAddNewTaskToReadyList( pxNewTCB );
1497 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1503 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1504 /*-----------------------------------------------------------*/
1506 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1507 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1508 const uint32_t ulStackDepth,
1509 void * const pvParameters,
1510 UBaseType_t uxPriority,
1511 TaskHandle_t * const pxCreatedTask,
1513 const MemoryRegion_t * const xRegions )
1515 StackType_t * pxTopOfStack;
1518 #if ( portUSING_MPU_WRAPPERS == 1 )
1519 /* Should the task be created in privileged mode? */
1520 BaseType_t xRunPrivileged;
1522 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1524 xRunPrivileged = pdTRUE;
1528 xRunPrivileged = pdFALSE;
1530 uxPriority &= ~portPRIVILEGE_BIT;
1531 #endif /* portUSING_MPU_WRAPPERS == 1 */
1533 /* Avoid dependency on memset() if it is not required. */
1534 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1536 /* Fill the stack with a known value to assist debugging. */
1537 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1539 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1541 /* Calculate the top of stack address. This depends on whether the stack
1542 * grows from high memory to low (as per the 80x86) or vice versa.
1543 * portSTACK_GROWTH is used to make the result positive or negative as required
1545 #if ( portSTACK_GROWTH < 0 )
1547 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1548 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(). */
1550 /* Check the alignment of the calculated top of stack is correct. */
1551 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1553 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1555 /* Also record the stack's high address, which may assist
1557 pxNewTCB->pxEndOfStack = pxTopOfStack;
1559 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1561 #else /* portSTACK_GROWTH */
1563 pxTopOfStack = pxNewTCB->pxStack;
1564 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(). */
1566 /* Check the alignment of the calculated top of stack is correct. */
1567 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1569 /* The other extreme of the stack space is required if stack checking is
1571 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1573 #endif /* portSTACK_GROWTH */
1575 /* Store the task name in the TCB. */
1576 if( pcName != NULL )
1578 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1580 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1582 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1583 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1584 * string is not accessible (extremely unlikely). */
1585 if( pcName[ x ] == ( char ) 0x00 )
1591 mtCOVERAGE_TEST_MARKER();
1595 /* Ensure the name string is terminated in the case that the string length
1596 * was greater or equal to configMAX_TASK_NAME_LEN. */
1597 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1601 mtCOVERAGE_TEST_MARKER();
1604 /* This is used as an array index so must ensure it's not too large. */
1605 configASSERT( uxPriority < configMAX_PRIORITIES );
1607 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1609 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1613 mtCOVERAGE_TEST_MARKER();
1616 pxNewTCB->uxPriority = uxPriority;
1617 #if ( configUSE_MUTEXES == 1 )
1619 pxNewTCB->uxBasePriority = uxPriority;
1621 #endif /* configUSE_MUTEXES */
1623 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1624 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1626 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1627 * back to the containing TCB from a generic item in a list. */
1628 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1630 /* Event lists are always in priority order. */
1631 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. */
1632 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1634 #if ( portUSING_MPU_WRAPPERS == 1 )
1636 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1640 /* Avoid compiler warning about unreferenced parameter. */
1645 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
1647 /* Allocate and initialize memory for the task's TLS Block. */
1648 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
1652 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1654 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1658 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1660 pxNewTCB->xPreemptionDisable = 0;
1664 /* Initialize the TCB stack to look as if the task was already running,
1665 * but had been interrupted by the scheduler. The return address is set
1666 * to the start of the task function. Once the stack has been initialised
1667 * the top of stack variable is updated. */
1668 #if ( portUSING_MPU_WRAPPERS == 1 )
1670 /* If the port has capability to detect stack overflow,
1671 * pass the stack end address to the stack initialization
1672 * function as well. */
1673 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1675 #if ( portSTACK_GROWTH < 0 )
1677 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1679 #else /* portSTACK_GROWTH */
1681 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1683 #endif /* portSTACK_GROWTH */
1685 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1687 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1689 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1691 #else /* portUSING_MPU_WRAPPERS */
1693 /* If the port has capability to detect stack overflow,
1694 * pass the stack end address to the stack initialization
1695 * function as well. */
1696 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1698 #if ( portSTACK_GROWTH < 0 )
1700 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1702 #else /* portSTACK_GROWTH */
1704 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1706 #endif /* portSTACK_GROWTH */
1708 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1710 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1712 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1714 #endif /* portUSING_MPU_WRAPPERS */
1716 /* Initialize task state and task attributes. */
1717 #if ( configNUMBER_OF_CORES > 1 )
1719 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1721 /* Is this an idle task? */
1722 if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvMinimalIdleTask ) )
1724 pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
1727 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
1729 if( pxCreatedTask != NULL )
1731 /* Pass the handle out in an anonymous way. The handle can be used to
1732 * change the created task's priority, delete the created task, etc.*/
1733 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1737 mtCOVERAGE_TEST_MARKER();
1740 /*-----------------------------------------------------------*/
1742 #if ( configNUMBER_OF_CORES == 1 )
1744 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1746 /* Ensure interrupts don't access the task lists while the lists are being
1748 taskENTER_CRITICAL();
1750 uxCurrentNumberOfTasks++;
1752 if( pxCurrentTCB == NULL )
1754 /* There are no other tasks, or all the other tasks are in
1755 * the suspended state - make this the current task. */
1756 pxCurrentTCB = pxNewTCB;
1758 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1760 /* This is the first task to be created so do the preliminary
1761 * initialisation required. We will not recover if this call
1762 * fails, but we will report the failure. */
1763 prvInitialiseTaskLists();
1767 mtCOVERAGE_TEST_MARKER();
1772 /* If the scheduler is not already running, make this task the
1773 * current task if it is the highest priority task to be created
1775 if( xSchedulerRunning == pdFALSE )
1777 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
1779 pxCurrentTCB = pxNewTCB;
1783 mtCOVERAGE_TEST_MARKER();
1788 mtCOVERAGE_TEST_MARKER();
1794 #if ( configUSE_TRACE_FACILITY == 1 )
1796 /* Add a counter into the TCB for tracing only. */
1797 pxNewTCB->uxTCBNumber = uxTaskNumber;
1799 #endif /* configUSE_TRACE_FACILITY */
1800 traceTASK_CREATE( pxNewTCB );
1802 prvAddTaskToReadyList( pxNewTCB );
1804 portSETUP_TCB( pxNewTCB );
1806 taskEXIT_CRITICAL();
1808 if( xSchedulerRunning != pdFALSE )
1810 /* If the created task is of a higher priority than the current task
1811 * then it should run now. */
1812 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
1816 mtCOVERAGE_TEST_MARKER();
1820 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
1822 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1824 /* Ensure interrupts don't access the task lists while the lists are being
1826 taskENTER_CRITICAL();
1828 uxCurrentNumberOfTasks++;
1830 if( xSchedulerRunning == pdFALSE )
1832 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1834 /* This is the first task to be created so do the preliminary
1835 * initialisation required. We will not recover if this call
1836 * fails, but we will report the failure. */
1837 prvInitialiseTaskLists();
1841 mtCOVERAGE_TEST_MARKER();
1844 if( ( pxNewTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1848 /* Check if a core is free. */
1849 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
1851 if( pxCurrentTCBs[ xCoreID ] == NULL )
1853 pxNewTCB->xTaskRunState = xCoreID;
1854 pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1859 mtCOVERAGE_TEST_MARKER();
1865 mtCOVERAGE_TEST_MARKER();
1871 #if ( configUSE_TRACE_FACILITY == 1 )
1873 /* Add a counter into the TCB for tracing only. */
1874 pxNewTCB->uxTCBNumber = uxTaskNumber;
1876 #endif /* configUSE_TRACE_FACILITY */
1877 traceTASK_CREATE( pxNewTCB );
1879 prvAddTaskToReadyList( pxNewTCB );
1881 portSETUP_TCB( pxNewTCB );
1883 if( xSchedulerRunning != pdFALSE )
1885 /* If the created task is of a higher priority than another
1886 * currently running task and preemption is on then it should
1888 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
1892 mtCOVERAGE_TEST_MARKER();
1895 taskEXIT_CRITICAL();
1898 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
1899 /*-----------------------------------------------------------*/
1901 #if ( INCLUDE_vTaskDelete == 1 )
1903 void vTaskDelete( TaskHandle_t xTaskToDelete )
1907 taskENTER_CRITICAL();
1909 /* If null is passed in here then it is the calling task that is
1911 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1913 /* Remove task from the ready/delayed list. */
1914 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1916 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1920 mtCOVERAGE_TEST_MARKER();
1923 /* Is the task waiting on an event also? */
1924 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1926 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1930 mtCOVERAGE_TEST_MARKER();
1933 /* Increment the uxTaskNumber also so kernel aware debuggers can
1934 * detect that the task lists need re-generating. This is done before
1935 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1939 /* If the task is running (or yielding), we must add it to the
1940 * termination list so that an idle task can delete it when it is
1941 * no longer running. */
1942 if( taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) != pdFALSE )
1944 /* A running task or a task which is scheduled to yield is being
1945 * deleted. This cannot complete when the task is still running
1946 * on a core, as a context switch to another task is required.
1947 * Place the task in the termination list. The idle task will check
1948 * the termination list and free up any memory allocated by the
1949 * scheduler for the TCB and stack of the deleted task. */
1950 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1952 /* Increment the ucTasksDeleted variable so the idle task knows
1953 * there is a task that has been deleted and that it should therefore
1954 * check the xTasksWaitingTermination list. */
1955 ++uxDeletedTasksWaitingCleanUp;
1957 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1958 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1959 traceTASK_DELETE( pxTCB );
1961 /* The pre-delete hook is primarily for the Windows simulator,
1962 * in which Windows specific clean up operations are performed,
1963 * after which it is not possible to yield away from this task -
1964 * hence xYieldPending is used to latch that a context switch is
1966 #if ( configNUMBER_OF_CORES == 1 )
1967 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
1969 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
1974 --uxCurrentNumberOfTasks;
1975 traceTASK_DELETE( pxTCB );
1977 /* Reset the next expected unblock time in case it referred to
1978 * the task that has just been deleted. */
1979 prvResetNextTaskUnblockTime();
1983 #if ( configNUMBER_OF_CORES == 1 )
1985 taskEXIT_CRITICAL();
1987 /* If the task is not deleting itself, call prvDeleteTCB from outside of
1988 * critical section. If a task deletes itself, prvDeleteTCB is called
1989 * from prvCheckTasksWaitingTermination which is called from Idle task. */
1990 if( pxTCB != pxCurrentTCB )
1992 prvDeleteTCB( pxTCB );
1995 /* Force a reschedule if it is the currently running task that has just
1997 if( xSchedulerRunning != pdFALSE )
1999 if( pxTCB == pxCurrentTCB )
2001 configASSERT( uxSchedulerSuspended == 0 );
2002 portYIELD_WITHIN_API();
2006 mtCOVERAGE_TEST_MARKER();
2010 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2012 /* If a running task is not deleting itself, call prvDeleteTCB. If a running
2013 * task deletes itself, prvDeleteTCB is called from prvCheckTasksWaitingTermination
2014 * which is called from Idle task. */
2015 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
2017 prvDeleteTCB( pxTCB );
2020 /* Force a reschedule if the task that has just been deleted was running. */
2021 if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) )
2023 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2025 configASSERT( uxSchedulerSuspended == 0 );
2026 vTaskYieldWithinAPI();
2030 prvYieldCore( pxTCB->xTaskRunState );
2034 taskEXIT_CRITICAL();
2036 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2039 #endif /* INCLUDE_vTaskDelete */
2040 /*-----------------------------------------------------------*/
2042 #if ( INCLUDE_xTaskDelayUntil == 1 )
2044 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2045 const TickType_t xTimeIncrement )
2047 TickType_t xTimeToWake;
2048 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2050 configASSERT( pxPreviousWakeTime );
2051 configASSERT( ( xTimeIncrement > 0U ) );
2055 /* Minor optimisation. The tick count cannot change in this
2057 const TickType_t xConstTickCount = xTickCount;
2059 configASSERT( uxSchedulerSuspended == 1U );
2061 /* Generate the tick time at which the task wants to wake. */
2062 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2064 if( xConstTickCount < *pxPreviousWakeTime )
2066 /* The tick count has overflowed since this function was
2067 * lasted called. In this case the only time we should ever
2068 * actually delay is if the wake time has also overflowed,
2069 * and the wake time is greater than the tick time. When this
2070 * is the case it is as if neither time had overflowed. */
2071 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2073 xShouldDelay = pdTRUE;
2077 mtCOVERAGE_TEST_MARKER();
2082 /* The tick time has not overflowed. In this case we will
2083 * delay if either the wake time has overflowed, and/or the
2084 * tick time is less than the wake time. */
2085 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2087 xShouldDelay = pdTRUE;
2091 mtCOVERAGE_TEST_MARKER();
2095 /* Update the wake time ready for the next call. */
2096 *pxPreviousWakeTime = xTimeToWake;
2098 if( xShouldDelay != pdFALSE )
2100 traceTASK_DELAY_UNTIL( xTimeToWake );
2102 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2103 * the time to wake, so subtract the current tick count. */
2104 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2108 mtCOVERAGE_TEST_MARKER();
2111 xAlreadyYielded = xTaskResumeAll();
2113 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2114 * have put ourselves to sleep. */
2115 if( xAlreadyYielded == pdFALSE )
2117 #if ( configNUMBER_OF_CORES == 1 )
2118 portYIELD_WITHIN_API();
2120 vTaskYieldWithinAPI();
2125 mtCOVERAGE_TEST_MARKER();
2128 return xShouldDelay;
2131 #endif /* INCLUDE_xTaskDelayUntil */
2132 /*-----------------------------------------------------------*/
2134 #if ( INCLUDE_vTaskDelay == 1 )
2136 void vTaskDelay( const TickType_t xTicksToDelay )
2138 BaseType_t xAlreadyYielded = pdFALSE;
2140 /* A delay time of zero just forces a reschedule. */
2141 if( xTicksToDelay > ( TickType_t ) 0U )
2145 configASSERT( uxSchedulerSuspended == 1U );
2149 /* A task that is removed from the event list while the
2150 * scheduler is suspended will not get placed in the ready
2151 * list or removed from the blocked list until the scheduler
2154 * This task cannot be in an event list as it is the currently
2155 * executing task. */
2156 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2158 xAlreadyYielded = xTaskResumeAll();
2162 mtCOVERAGE_TEST_MARKER();
2165 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2166 * have put ourselves to sleep. */
2167 if( xAlreadyYielded == pdFALSE )
2169 #if ( configNUMBER_OF_CORES == 1 )
2170 portYIELD_WITHIN_API();
2172 vTaskYieldWithinAPI();
2177 mtCOVERAGE_TEST_MARKER();
2181 #endif /* INCLUDE_vTaskDelay */
2182 /*-----------------------------------------------------------*/
2184 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2186 eTaskState eTaskGetState( TaskHandle_t xTask )
2189 List_t const * pxStateList;
2190 List_t const * pxEventList;
2191 List_t const * pxDelayedList;
2192 List_t const * pxOverflowedDelayedList;
2193 const TCB_t * const pxTCB = xTask;
2195 configASSERT( pxTCB );
2197 #if ( configNUMBER_OF_CORES == 1 )
2198 if( pxTCB == pxCurrentTCB )
2200 /* The task calling this function is querying its own state. */
2206 taskENTER_CRITICAL();
2208 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2209 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2210 pxDelayedList = pxDelayedTaskList;
2211 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2213 taskEXIT_CRITICAL();
2215 if( pxEventList == &xPendingReadyList )
2217 /* The task has been placed on the pending ready list, so its
2218 * state is eReady regardless of what list the task's state list
2219 * item is currently placed on. */
2222 else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2224 /* The task being queried is referenced from one of the Blocked
2229 #if ( INCLUDE_vTaskSuspend == 1 )
2230 else if( pxStateList == &xSuspendedTaskList )
2232 /* The task being queried is referenced from the suspended
2233 * list. Is it genuinely suspended or is it blocked
2235 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2237 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2241 /* The task does not appear on the event list item of
2242 * and of the RTOS objects, but could still be in the
2243 * blocked state if it is waiting on its notification
2244 * rather than waiting on an object. If not, is
2246 eReturn = eSuspended;
2248 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2250 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2257 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2259 eReturn = eSuspended;
2261 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2268 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2270 #if ( INCLUDE_vTaskDelete == 1 )
2271 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2273 /* The task being queried is referenced from the deleted
2274 * tasks list, or it is not referenced from any lists at
2280 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
2282 #if ( configNUMBER_OF_CORES == 1 )
2284 /* If the task is not in any other state, it must be in the
2285 * Ready (including pending ready) state. */
2288 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2290 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2292 /* Is it actively running on a core? */
2297 /* If the task is not in any other state, it must be in the
2298 * Ready (including pending ready) state. */
2302 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2307 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2309 #endif /* INCLUDE_eTaskGetState */
2310 /*-----------------------------------------------------------*/
2312 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2314 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2316 TCB_t const * pxTCB;
2317 UBaseType_t uxReturn;
2319 taskENTER_CRITICAL();
2321 /* If null is passed in here then it is the priority of the task
2322 * that called uxTaskPriorityGet() that is being queried. */
2323 pxTCB = prvGetTCBFromHandle( xTask );
2324 uxReturn = pxTCB->uxPriority;
2326 taskEXIT_CRITICAL();
2331 #endif /* INCLUDE_uxTaskPriorityGet */
2332 /*-----------------------------------------------------------*/
2334 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2336 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2338 TCB_t const * pxTCB;
2339 UBaseType_t uxReturn;
2340 UBaseType_t uxSavedInterruptStatus;
2342 /* RTOS ports that support interrupt nesting have the concept of a
2343 * maximum system call (or maximum API call) interrupt priority.
2344 * Interrupts that are above the maximum system call priority are keep
2345 * permanently enabled, even when the RTOS kernel is in a critical section,
2346 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2347 * is defined in FreeRTOSConfig.h then
2348 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2349 * failure if a FreeRTOS API function is called from an interrupt that has
2350 * been assigned a priority above the configured maximum system call
2351 * priority. Only FreeRTOS functions that end in FromISR can be called
2352 * from interrupts that have been assigned a priority at or (logically)
2353 * below the maximum system call interrupt priority. FreeRTOS maintains a
2354 * separate interrupt safe API to ensure interrupt entry is as fast and as
2355 * simple as possible. More information (albeit Cortex-M specific) is
2356 * provided on the following link:
2357 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2358 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2360 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
2362 /* If null is passed in here then it is the priority of the calling
2363 * task that is being queried. */
2364 pxTCB = prvGetTCBFromHandle( xTask );
2365 uxReturn = pxTCB->uxPriority;
2367 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2372 #endif /* INCLUDE_uxTaskPriorityGet */
2373 /*-----------------------------------------------------------*/
2375 #if ( INCLUDE_vTaskPrioritySet == 1 )
2377 void vTaskPrioritySet( TaskHandle_t xTask,
2378 UBaseType_t uxNewPriority )
2381 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2382 BaseType_t xYieldRequired = pdFALSE;
2384 #if ( configNUMBER_OF_CORES > 1 )
2385 BaseType_t xYieldForTask = pdFALSE;
2388 configASSERT( uxNewPriority < configMAX_PRIORITIES );
2390 /* Ensure the new priority is valid. */
2391 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2393 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2397 mtCOVERAGE_TEST_MARKER();
2400 taskENTER_CRITICAL();
2402 /* If null is passed in here then it is the priority of the calling
2403 * task that is being changed. */
2404 pxTCB = prvGetTCBFromHandle( xTask );
2406 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2408 #if ( configUSE_MUTEXES == 1 )
2410 uxCurrentBasePriority = pxTCB->uxBasePriority;
2414 uxCurrentBasePriority = pxTCB->uxPriority;
2418 if( uxCurrentBasePriority != uxNewPriority )
2420 /* The priority change may have readied a task of higher
2421 * priority than a running task. */
2422 if( uxNewPriority > uxCurrentBasePriority )
2424 #if ( configNUMBER_OF_CORES == 1 )
2426 if( pxTCB != pxCurrentTCB )
2428 /* The priority of a task other than the currently
2429 * running task is being raised. Is the priority being
2430 * raised above that of the running task? */
2431 if( uxNewPriority > pxCurrentTCB->uxPriority )
2433 xYieldRequired = pdTRUE;
2437 mtCOVERAGE_TEST_MARKER();
2442 /* The priority of the running task is being raised,
2443 * but the running task must already be the highest
2444 * priority task able to run so no yield is required. */
2447 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2449 /* The priority of a task is being raised so
2450 * perform a yield for this task later. */
2451 xYieldForTask = pdTRUE;
2453 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2455 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2457 /* Setting the priority of a running task down means
2458 * there may now be another task of higher priority that
2459 * is ready to execute. */
2460 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2461 if( pxTCB->xPreemptionDisable == pdFALSE )
2464 xYieldRequired = pdTRUE;
2469 /* Setting the priority of any other task down does not
2470 * require a yield as the running task must be above the
2471 * new priority of the task being modified. */
2474 /* Remember the ready list the task might be referenced from
2475 * before its uxPriority member is changed so the
2476 * taskRESET_READY_PRIORITY() macro can function correctly. */
2477 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2479 #if ( configUSE_MUTEXES == 1 )
2481 /* Only change the priority being used if the task is not
2482 * currently using an inherited priority or the new priority
2483 * is bigger than the inherited priority. */
2484 if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2486 pxTCB->uxPriority = uxNewPriority;
2490 mtCOVERAGE_TEST_MARKER();
2493 /* The base priority gets set whatever. */
2494 pxTCB->uxBasePriority = uxNewPriority;
2496 #else /* if ( configUSE_MUTEXES == 1 ) */
2498 pxTCB->uxPriority = uxNewPriority;
2500 #endif /* if ( configUSE_MUTEXES == 1 ) */
2502 /* Only reset the event list item value if the value is not
2503 * being used for anything else. */
2504 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2506 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. */
2510 mtCOVERAGE_TEST_MARKER();
2513 /* If the task is in the blocked or suspended list we need do
2514 * nothing more than change its priority variable. However, if
2515 * the task is in a ready list it needs to be removed and placed
2516 * in the list appropriate to its new priority. */
2517 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2519 /* The task is currently in its ready list - remove before
2520 * adding it to its new ready list. As we are in a critical
2521 * section we can do this even if the scheduler is suspended. */
2522 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2524 /* It is known that the task is in its ready list so
2525 * there is no need to check again and the port level
2526 * reset macro can be called directly. */
2527 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2531 mtCOVERAGE_TEST_MARKER();
2534 prvAddTaskToReadyList( pxTCB );
2538 #if ( configNUMBER_OF_CORES == 1 )
2540 mtCOVERAGE_TEST_MARKER();
2544 /* It's possible that xYieldForTask was already set to pdTRUE because
2545 * its priority is being raised. However, since it is not in a ready list
2546 * we don't actually need to yield for it. */
2547 xYieldForTask = pdFALSE;
2552 if( xYieldRequired != pdFALSE )
2554 /* The running task priority is set down. Request the task to yield. */
2555 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2559 #if ( configNUMBER_OF_CORES > 1 )
2560 if( xYieldForTask != pdFALSE )
2562 /* The priority of the task is being raised. If a running
2563 * task has priority lower than this task, it should yield
2565 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2568 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2570 mtCOVERAGE_TEST_MARKER();
2574 /* Remove compiler warning about unused variables when the port
2575 * optimised task selection is not being used. */
2576 ( void ) uxPriorityUsedOnEntry;
2579 taskEXIT_CRITICAL();
2582 #endif /* INCLUDE_vTaskPrioritySet */
2583 /*-----------------------------------------------------------*/
2585 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2586 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2587 UBaseType_t uxCoreAffinityMask )
2591 UBaseType_t uxPrevCoreAffinityMask;
2593 #if ( configUSE_PREEMPTION == 1 )
2594 UBaseType_t uxPrevNotAllowedCores;
2597 taskENTER_CRITICAL();
2599 pxTCB = prvGetTCBFromHandle( xTask );
2601 uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2602 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2604 if( xSchedulerRunning != pdFALSE )
2606 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2608 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2610 /* If the task can no longer run on the core it was running,
2611 * request the core to yield. */
2612 if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
2614 prvYieldCore( xCoreID );
2619 #if ( configUSE_PREEMPTION == 1 )
2621 /* Calculate the cores on which this task was not allowed to
2622 * run previously. */
2623 uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
2625 /* Does the new core mask enables this task to run on any of the
2626 * previously not allowed cores? If yes, check if this task can be
2627 * scheduled on any of those cores. */
2628 if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
2630 prvYieldForTask( pxTCB );
2633 #else /* #if( configUSE_PREEMPTION == 1 ) */
2635 mtCOVERAGE_TEST_MARKER();
2637 #endif /* #if( configUSE_PREEMPTION == 1 ) */
2641 taskEXIT_CRITICAL();
2643 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
2644 /*-----------------------------------------------------------*/
2646 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2647 UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
2649 const TCB_t * pxTCB;
2650 UBaseType_t uxCoreAffinityMask;
2652 taskENTER_CRITICAL();
2654 pxTCB = prvGetTCBFromHandle( xTask );
2655 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2657 taskEXIT_CRITICAL();
2659 return uxCoreAffinityMask;
2661 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
2663 /*-----------------------------------------------------------*/
2665 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2667 void vTaskPreemptionDisable( const TaskHandle_t xTask )
2671 taskENTER_CRITICAL();
2673 pxTCB = prvGetTCBFromHandle( xTask );
2675 pxTCB->xPreemptionDisable = pdTRUE;
2677 taskEXIT_CRITICAL();
2680 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
2681 /*-----------------------------------------------------------*/
2683 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2685 void vTaskPreemptionEnable( const TaskHandle_t xTask )
2690 taskENTER_CRITICAL();
2692 pxTCB = prvGetTCBFromHandle( xTask );
2694 pxTCB->xPreemptionDisable = pdFALSE;
2696 if( xSchedulerRunning != pdFALSE )
2698 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2700 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2701 prvYieldCore( xCoreID );
2705 taskEXIT_CRITICAL();
2708 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
2709 /*-----------------------------------------------------------*/
2711 #if ( INCLUDE_vTaskSuspend == 1 )
2713 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2717 #if ( configNUMBER_OF_CORES > 1 )
2718 BaseType_t xTaskRunningOnCore;
2721 taskENTER_CRITICAL();
2723 /* If null is passed in here then it is the running task that is
2724 * being suspended. */
2725 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2727 traceTASK_SUSPEND( pxTCB );
2729 #if ( configNUMBER_OF_CORES > 1 )
2730 xTaskRunningOnCore = pxTCB->xTaskRunState;
2733 /* Remove task from the ready/delayed list and place in the
2734 * suspended list. */
2735 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2737 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2741 mtCOVERAGE_TEST_MARKER();
2744 /* Is the task waiting on an event also? */
2745 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2747 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2751 mtCOVERAGE_TEST_MARKER();
2754 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2756 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2760 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2762 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2764 /* The task was blocked to wait for a notification, but is
2765 * now suspended, so no notification was received. */
2766 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2770 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2773 #if ( configNUMBER_OF_CORES == 1 )
2775 taskEXIT_CRITICAL();
2777 if( xSchedulerRunning != pdFALSE )
2779 /* Reset the next expected unblock time in case it referred to the
2780 * task that is now in the Suspended state. */
2781 taskENTER_CRITICAL();
2783 prvResetNextTaskUnblockTime();
2785 taskEXIT_CRITICAL();
2789 mtCOVERAGE_TEST_MARKER();
2792 if( pxTCB == pxCurrentTCB )
2794 if( xSchedulerRunning != pdFALSE )
2796 /* The current task has just been suspended. */
2797 configASSERT( uxSchedulerSuspended == 0 );
2798 portYIELD_WITHIN_API();
2802 /* The scheduler is not running, but the task that was pointed
2803 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
2804 * must be adjusted to point to a different task. */
2805 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
2807 /* No other tasks are ready, so set pxCurrentTCB back to
2808 * NULL so when the next task is created pxCurrentTCB will
2809 * be set to point to it no matter what its relative priority
2811 pxCurrentTCB = NULL;
2815 vTaskSwitchContext();
2821 mtCOVERAGE_TEST_MARKER();
2824 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2826 if( xSchedulerRunning != pdFALSE )
2828 /* Reset the next expected unblock time in case it referred to the
2829 * task that is now in the Suspended state. */
2830 prvResetNextTaskUnblockTime();
2834 mtCOVERAGE_TEST_MARKER();
2837 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2839 if( xSchedulerRunning != pdFALSE )
2841 if( xTaskRunningOnCore == ( BaseType_t ) portGET_CORE_ID() )
2843 /* The current task has just been suspended. */
2844 configASSERT( uxSchedulerSuspended == 0 );
2845 vTaskYieldWithinAPI();
2849 prvYieldCore( xTaskRunningOnCore );
2854 /* This code path is not possible because only Idle tasks are
2855 * assigned a core before the scheduler is started ( i.e.
2856 * taskTASK_IS_RUNNING is only true for idle tasks before
2857 * the scheduler is started ) and idle tasks cannot be
2859 mtCOVERAGE_TEST_MARKER();
2864 mtCOVERAGE_TEST_MARKER();
2867 taskEXIT_CRITICAL();
2869 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2872 #endif /* INCLUDE_vTaskSuspend */
2873 /*-----------------------------------------------------------*/
2875 #if ( INCLUDE_vTaskSuspend == 1 )
2877 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
2879 BaseType_t xReturn = pdFALSE;
2880 const TCB_t * const pxTCB = xTask;
2882 /* Accesses xPendingReadyList so must be called from a critical
2885 /* It does not make sense to check if the calling task is suspended. */
2886 configASSERT( xTask );
2888 /* Is the task being resumed actually in the suspended list? */
2889 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
2891 /* Has the task already been resumed from within an ISR? */
2892 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
2894 /* Is it in the suspended list because it is in the Suspended
2895 * state, or because is is blocked with no timeout? */
2896 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
2902 mtCOVERAGE_TEST_MARKER();
2907 mtCOVERAGE_TEST_MARKER();
2912 mtCOVERAGE_TEST_MARKER();
2916 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2918 #endif /* INCLUDE_vTaskSuspend */
2919 /*-----------------------------------------------------------*/
2921 #if ( INCLUDE_vTaskSuspend == 1 )
2923 void vTaskResume( TaskHandle_t xTaskToResume )
2925 TCB_t * const pxTCB = xTaskToResume;
2927 /* It does not make sense to resume the calling task. */
2928 configASSERT( xTaskToResume );
2930 #if ( configNUMBER_OF_CORES == 1 )
2932 /* The parameter cannot be NULL as it is impossible to resume the
2933 * currently executing task. */
2934 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
2937 /* The parameter cannot be NULL as it is impossible to resume the
2938 * currently executing task. It is also impossible to resume a task
2939 * that is actively running on another core but it is not safe
2940 * to check their run state here. Therefore, we get into a critical
2941 * section and check if the task is actually suspended or not. */
2945 taskENTER_CRITICAL();
2947 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2949 traceTASK_RESUME( pxTCB );
2951 /* The ready list can be accessed even if the scheduler is
2952 * suspended because this is inside a critical section. */
2953 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2954 prvAddTaskToReadyList( pxTCB );
2956 /* This yield may not cause the task just resumed to run,
2957 * but will leave the lists in the correct state for the
2959 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2963 mtCOVERAGE_TEST_MARKER();
2966 taskEXIT_CRITICAL();
2970 mtCOVERAGE_TEST_MARKER();
2974 #endif /* INCLUDE_vTaskSuspend */
2976 /*-----------------------------------------------------------*/
2978 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
2980 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
2982 BaseType_t xYieldRequired = pdFALSE;
2983 TCB_t * const pxTCB = xTaskToResume;
2984 UBaseType_t uxSavedInterruptStatus;
2986 configASSERT( xTaskToResume );
2988 /* RTOS ports that support interrupt nesting have the concept of a
2989 * maximum system call (or maximum API call) interrupt priority.
2990 * Interrupts that are above the maximum system call priority are keep
2991 * permanently enabled, even when the RTOS kernel is in a critical section,
2992 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2993 * is defined in FreeRTOSConfig.h then
2994 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2995 * failure if a FreeRTOS API function is called from an interrupt that has
2996 * been assigned a priority above the configured maximum system call
2997 * priority. Only FreeRTOS functions that end in FromISR can be called
2998 * from interrupts that have been assigned a priority at or (logically)
2999 * below the maximum system call interrupt priority. FreeRTOS maintains a
3000 * separate interrupt safe API to ensure interrupt entry is as fast and as
3001 * simple as possible. More information (albeit Cortex-M specific) is
3002 * provided on the following link:
3003 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3004 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3006 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3008 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3010 traceTASK_RESUME_FROM_ISR( pxTCB );
3012 /* Check the ready lists can be accessed. */
3013 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3015 #if ( configNUMBER_OF_CORES == 1 )
3017 /* Ready lists can be accessed so move the task from the
3018 * suspended list to the ready list directly. */
3019 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3021 xYieldRequired = pdTRUE;
3023 /* Mark that a yield is pending in case the user is not
3024 * using the return value to initiate a context switch
3025 * from the ISR using portYIELD_FROM_ISR. */
3026 xYieldPendings[ 0 ] = pdTRUE;
3030 mtCOVERAGE_TEST_MARKER();
3033 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3035 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3036 prvAddTaskToReadyList( pxTCB );
3040 /* The delayed or ready lists cannot be accessed so the task
3041 * is held in the pending ready list until the scheduler is
3043 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3046 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3048 prvYieldForTask( pxTCB );
3050 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3052 xYieldRequired = pdTRUE;
3055 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3059 mtCOVERAGE_TEST_MARKER();
3062 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3064 return xYieldRequired;
3067 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3068 /*-----------------------------------------------------------*/
3070 static BaseType_t prvCreateIdleTasks( void )
3072 BaseType_t xReturn = pdPASS;
3074 #if ( configNUMBER_OF_CORES == 1 )
3076 /* Add the idle task at the lowest priority. */
3077 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3079 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3080 StackType_t * pxIdleTaskStackBuffer = NULL;
3081 uint32_t ulIdleTaskStackSize;
3083 /* The Idle task is created using user provided RAM - obtain the
3084 * address of the RAM then create the idle task. */
3085 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3086 xIdleTaskHandles[ 0 ] = xTaskCreateStatic( prvIdleTask,
3087 configIDLE_TASK_NAME,
3088 ulIdleTaskStackSize,
3089 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3090 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3091 pxIdleTaskStackBuffer,
3092 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3094 if( xIdleTaskHandles[ 0 ] != NULL )
3103 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3105 /* The Idle task is being created using dynamically allocated RAM. */
3106 xReturn = xTaskCreate( prvIdleTask,
3107 configIDLE_TASK_NAME,
3108 configMINIMAL_STACK_SIZE,
3110 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3111 &xIdleTaskHandles[ 0 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3113 #endif /* configSUPPORT_STATIC_ALLOCATION */
3115 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3118 char cIdleName[ configMAX_TASK_NAME_LEN ];
3120 /* Add each idle task at the lowest priority. */
3121 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3125 if( xReturn == pdFAIL )
3131 mtCOVERAGE_TEST_MARKER();
3134 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3136 cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
3138 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3139 * configMAX_TASK_NAME_LEN characters just in case the memory after the
3140 * string is not accessible (extremely unlikely). */
3141 if( cIdleName[ x ] == ( char ) 0x00 )
3147 mtCOVERAGE_TEST_MARKER();
3151 /* Append the idle task number to the end of the name if there is space. */
3152 if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3154 cIdleName[ x ] = ( char ) ( xCoreID + '0' );
3157 /* And append a null character if there is space. */
3158 if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3160 cIdleName[ x ] = '\0';
3164 mtCOVERAGE_TEST_MARKER();
3169 mtCOVERAGE_TEST_MARKER();
3172 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3176 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3177 StackType_t * pxIdleTaskStackBuffer = NULL;
3178 uint32_t ulIdleTaskStackSize;
3180 /* The Idle task is created using user provided RAM - obtain the
3181 * address of the RAM then create the idle task. */
3182 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3183 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
3185 ulIdleTaskStackSize,
3186 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3187 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3188 pxIdleTaskStackBuffer,
3189 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3193 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( prvMinimalIdleTask,
3195 configMINIMAL_STACK_SIZE,
3196 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3197 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3198 xIdleTaskStackBuffers[ xCoreID - 1 ],
3199 &xIdleTCBBuffers[ xCoreID - 1 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3202 if( xIdleTaskHandles[ xCoreID ] != NULL )
3211 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3215 /* The Idle task is being created using dynamically allocated RAM. */
3216 xReturn = xTaskCreate( prvIdleTask,
3218 configMINIMAL_STACK_SIZE,
3220 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3221 &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3225 xReturn = xTaskCreate( prvMinimalIdleTask,
3227 configMINIMAL_STACK_SIZE,
3229 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3230 &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3233 #endif /* configSUPPORT_STATIC_ALLOCATION */
3236 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3241 /*-----------------------------------------------------------*/
3243 void vTaskStartScheduler( void )
3247 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3249 /* Sanity check that the UBaseType_t must have greater than or equal to
3250 * the number of bits as confNUMBER_OF_CORES. */
3251 configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3253 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3255 xReturn = prvCreateIdleTasks();
3257 #if ( configUSE_TIMERS == 1 )
3259 if( xReturn == pdPASS )
3261 xReturn = xTimerCreateTimerTask();
3265 mtCOVERAGE_TEST_MARKER();
3268 #endif /* configUSE_TIMERS */
3270 if( xReturn == pdPASS )
3272 /* freertos_tasks_c_additions_init() should only be called if the user
3273 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3274 * the only macro called by the function. */
3275 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3277 freertos_tasks_c_additions_init();
3281 /* Interrupts are turned off here, to ensure a tick does not occur
3282 * before or during the call to xPortStartScheduler(). The stacks of
3283 * the created tasks contain a status word with interrupts switched on
3284 * so interrupts will automatically get re-enabled when the first task
3286 portDISABLE_INTERRUPTS();
3288 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3290 /* Switch C-Runtime's TLS Block to point to the TLS
3291 * block specific to the task that will run first. */
3292 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3296 xNextTaskUnblockTime = portMAX_DELAY;
3297 xSchedulerRunning = pdTRUE;
3298 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3300 /* If configGENERATE_RUN_TIME_STATS is defined then the following
3301 * macro must be defined to configure the timer/counter used to generate
3302 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
3303 * is set to 0 and the following line fails to build then ensure you do not
3304 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3305 * FreeRTOSConfig.h file. */
3306 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3308 traceTASK_SWITCHED_IN();
3310 /* Setting up the timer tick is hardware specific and thus in the
3311 * portable interface. */
3312 xPortStartScheduler();
3314 /* In most cases, xPortStartScheduler() will not return. If it
3315 * returns pdTRUE then there was not enough heap memory available
3316 * to create either the Idle or the Timer task. If it returned
3317 * pdFALSE, then the application called xTaskEndScheduler().
3318 * Most ports don't implement xTaskEndScheduler() as there is
3319 * nothing to return to. */
3323 /* This line will only be reached if the kernel could not be started,
3324 * because there was not enough FreeRTOS heap to create the idle task
3325 * or the timer task. */
3326 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3329 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3330 * meaning xIdleTaskHandles are not used anywhere else. */
3331 ( void ) xIdleTaskHandles;
3333 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3334 * from getting optimized out as it is no longer used by the kernel. */
3335 ( void ) uxTopUsedPriority;
3337 /*-----------------------------------------------------------*/
3339 void vTaskEndScheduler( void )
3341 /* Stop the scheduler interrupts and call the portable scheduler end
3342 * routine so the original ISRs can be restored if necessary. The port
3343 * layer must ensure interrupts enable bit is left in the correct state. */
3344 portDISABLE_INTERRUPTS();
3345 xSchedulerRunning = pdFALSE;
3346 vPortEndScheduler();
3348 /*----------------------------------------------------------*/
3350 void vTaskSuspendAll( void )
3352 #if ( configNUMBER_OF_CORES == 1 )
3354 /* A critical section is not required as the variable is of type
3355 * BaseType_t. Please read Richard Barry's reply in the following link to a
3356 * post in the FreeRTOS support forum before reporting this as a bug! -
3357 * https://goo.gl/wu4acr */
3359 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3360 * do not otherwise exhibit real time behaviour. */
3361 portSOFTWARE_BARRIER();
3363 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3364 * is used to allow calls to vTaskSuspendAll() to nest. */
3365 ++uxSchedulerSuspended;
3367 /* Enforces ordering for ports and optimised compilers that may otherwise place
3368 * the above increment elsewhere. */
3369 portMEMORY_BARRIER();
3371 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3373 UBaseType_t ulState;
3375 /* This must only be called from within a task. */
3376 portASSERT_IF_IN_ISR();
3378 if( xSchedulerRunning != pdFALSE )
3380 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3381 * We must disable interrupts before we grab the locks in the event that this task is
3382 * interrupted and switches context before incrementing uxSchedulerSuspended.
3383 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3384 * uxSchedulerSuspended since that will prevent context switches. */
3385 ulState = portSET_INTERRUPT_MASK();
3387 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3388 * do not otherwise exhibit real time behaviour. */
3389 portSOFTWARE_BARRIER();
3391 portGET_TASK_LOCK();
3393 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3394 * purpose is to prevent altering the variable when fromISR APIs are readying
3396 if( uxSchedulerSuspended == 0U )
3398 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
3400 prvCheckForRunStateChange();
3404 mtCOVERAGE_TEST_MARKER();
3409 mtCOVERAGE_TEST_MARKER();
3414 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3415 * is used to allow calls to vTaskSuspendAll() to nest. */
3416 ++uxSchedulerSuspended;
3417 portRELEASE_ISR_LOCK();
3419 portCLEAR_INTERRUPT_MASK( ulState );
3423 mtCOVERAGE_TEST_MARKER();
3426 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3429 /*----------------------------------------------------------*/
3431 #if ( configUSE_TICKLESS_IDLE != 0 )
3433 static TickType_t prvGetExpectedIdleTime( void )
3436 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
3438 /* uxHigherPriorityReadyTasks takes care of the case where
3439 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3440 * task that are in the Ready state, even though the idle task is
3442 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3444 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3446 uxHigherPriorityReadyTasks = pdTRUE;
3451 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3453 /* When port optimised task selection is used the uxTopReadyPriority
3454 * variable is used as a bit map. If bits other than the least
3455 * significant bit are set then there are tasks that have a priority
3456 * above the idle priority that are in the Ready state. This takes
3457 * care of the case where the co-operative scheduler is in use. */
3458 if( uxTopReadyPriority > uxLeastSignificantBit )
3460 uxHigherPriorityReadyTasks = pdTRUE;
3463 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3465 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3469 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
3471 /* There are other idle priority tasks in the ready state. If
3472 * time slicing is used then the very next tick interrupt must be
3476 else if( uxHigherPriorityReadyTasks != pdFALSE )
3478 /* There are tasks in the Ready state that have a priority above the
3479 * idle priority. This path can only be reached if
3480 * configUSE_PREEMPTION is 0. */
3485 xReturn = xNextTaskUnblockTime - xTickCount;
3491 #endif /* configUSE_TICKLESS_IDLE */
3492 /*----------------------------------------------------------*/
3494 BaseType_t xTaskResumeAll( void )
3496 TCB_t * pxTCB = NULL;
3497 BaseType_t xAlreadyYielded = pdFALSE;
3499 #if ( configNUMBER_OF_CORES > 1 )
3500 if( xSchedulerRunning != pdFALSE )
3503 /* It is possible that an ISR caused a task to be removed from an event
3504 * list while the scheduler was suspended. If this was the case then the
3505 * removed task will have been added to the xPendingReadyList. Once the
3506 * scheduler has been resumed it is safe to move all the pending ready
3507 * tasks from this list into their appropriate ready list. */
3508 taskENTER_CRITICAL();
3511 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3513 /* If uxSchedulerSuspended is zero then this function does not match a
3514 * previous call to vTaskSuspendAll(). */
3515 configASSERT( uxSchedulerSuspended != 0U );
3517 --uxSchedulerSuspended;
3518 portRELEASE_TASK_LOCK();
3520 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3522 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3524 /* Move any readied tasks from the pending list into the
3525 * appropriate ready list. */
3526 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3528 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. */
3529 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
3530 portMEMORY_BARRIER();
3531 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
3532 prvAddTaskToReadyList( pxTCB );
3534 #if ( configNUMBER_OF_CORES == 1 )
3536 /* If the moved task has a priority higher than the current
3537 * task then a yield must be performed. */
3538 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3540 xYieldPendings[ xCoreID ] = pdTRUE;
3544 mtCOVERAGE_TEST_MARKER();
3547 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3549 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
3550 * If the current core yielded then vTaskSwitchContext() has already been called
3551 * which sets xYieldPendings for the current core to pdTRUE. */
3553 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3558 /* A task was unblocked while the scheduler was suspended,
3559 * which may have prevented the next unblock time from being
3560 * re-calculated, in which case re-calculate it now. Mainly
3561 * important for low power tickless implementations, where
3562 * this can prevent an unnecessary exit from low power
3564 prvResetNextTaskUnblockTime();
3567 /* If any ticks occurred while the scheduler was suspended then
3568 * they should be processed now. This ensures the tick count does
3569 * not slip, and that any delayed tasks are resumed at the correct
3572 * It should be safe to call xTaskIncrementTick here from any core
3573 * since we are in a critical section and xTaskIncrementTick itself
3574 * protects itself within a critical section. Suspending the scheduler
3575 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
3577 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3579 if( xPendedCounts > ( TickType_t ) 0U )
3583 if( xTaskIncrementTick() != pdFALSE )
3585 /* Other cores are interrupted from
3586 * within xTaskIncrementTick(). */
3587 xYieldPendings[ xCoreID ] = pdTRUE;
3591 mtCOVERAGE_TEST_MARKER();
3595 } while( xPendedCounts > ( TickType_t ) 0U );
3601 mtCOVERAGE_TEST_MARKER();
3605 if( xYieldPendings[ xCoreID ] != pdFALSE )
3607 #if ( configUSE_PREEMPTION != 0 )
3609 xAlreadyYielded = pdTRUE;
3611 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
3613 #if ( configNUMBER_OF_CORES == 1 )
3615 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
3617 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3621 mtCOVERAGE_TEST_MARKER();
3627 mtCOVERAGE_TEST_MARKER();
3630 taskEXIT_CRITICAL();
3633 return xAlreadyYielded;
3635 /*-----------------------------------------------------------*/
3637 TickType_t xTaskGetTickCount( void )
3641 /* Critical section required if running on a 16 bit processor. */
3642 portTICK_TYPE_ENTER_CRITICAL();
3644 xTicks = xTickCount;
3646 portTICK_TYPE_EXIT_CRITICAL();
3650 /*-----------------------------------------------------------*/
3652 TickType_t xTaskGetTickCountFromISR( void )
3655 UBaseType_t uxSavedInterruptStatus;
3657 /* RTOS ports that support interrupt nesting have the concept of a maximum
3658 * system call (or maximum API call) interrupt priority. Interrupts that are
3659 * above the maximum system call priority are kept permanently enabled, even
3660 * when the RTOS kernel is in a critical section, but cannot make any calls to
3661 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
3662 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3663 * failure if a FreeRTOS API function is called from an interrupt that has been
3664 * assigned a priority above the configured maximum system call priority.
3665 * Only FreeRTOS functions that end in FromISR can be called from interrupts
3666 * that have been assigned a priority at or (logically) below the maximum
3667 * system call interrupt priority. FreeRTOS maintains a separate interrupt
3668 * safe API to ensure interrupt entry is as fast and as simple as possible.
3669 * More information (albeit Cortex-M specific) is provided on the following
3670 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3671 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3673 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
3675 xReturn = xTickCount;
3677 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3681 /*-----------------------------------------------------------*/
3683 UBaseType_t uxTaskGetNumberOfTasks( void )
3685 /* A critical section is not required because the variables are of type
3687 return uxCurrentNumberOfTasks;
3689 /*-----------------------------------------------------------*/
3691 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3695 /* If null is passed in here then the name of the calling task is being
3697 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3698 configASSERT( pxTCB );
3699 return &( pxTCB->pcTaskName[ 0 ] );
3701 /*-----------------------------------------------------------*/
3703 #if ( INCLUDE_xTaskGetHandle == 1 )
3705 #if ( configNUMBER_OF_CORES == 1 )
3706 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3707 const char pcNameToQuery[] )
3711 TCB_t * pxReturn = NULL;
3714 BaseType_t xBreakLoop;
3716 /* This function is called with the scheduler suspended. */
3718 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3720 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. */
3724 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. */
3726 /* Check each character in the name looking for a match or
3728 xBreakLoop = pdFALSE;
3730 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3732 cNextChar = pxNextTCB->pcTaskName[ x ];
3734 if( cNextChar != pcNameToQuery[ x ] )
3736 /* Characters didn't match. */
3737 xBreakLoop = pdTRUE;
3739 else if( cNextChar == ( char ) 0x00 )
3741 /* Both strings terminated, a match must have been
3743 pxReturn = pxNextTCB;
3744 xBreakLoop = pdTRUE;
3748 mtCOVERAGE_TEST_MARKER();
3751 if( xBreakLoop != pdFALSE )
3757 if( pxReturn != NULL )
3759 /* The handle has been found. */
3762 } while( pxNextTCB != pxFirstTCB );
3766 mtCOVERAGE_TEST_MARKER();
3771 #else /* if ( configNUMBER_OF_CORES == 1 ) */
3772 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3773 const char pcNameToQuery[] )
3775 TCB_t * pxReturn = NULL;
3778 BaseType_t xBreakLoop;
3779 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
3780 ListItem_t * pxIterator;
3782 /* This function is called with the scheduler suspended. */
3784 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3786 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
3788 TCB_t * pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
3790 /* Check each character in the name looking for a match or
3792 xBreakLoop = pdFALSE;
3794 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3796 cNextChar = pxTCB->pcTaskName[ x ];
3798 if( cNextChar != pcNameToQuery[ x ] )
3800 /* Characters didn't match. */
3801 xBreakLoop = pdTRUE;
3803 else if( cNextChar == ( char ) 0x00 )
3805 /* Both strings terminated, a match must have been
3808 xBreakLoop = pdTRUE;
3812 mtCOVERAGE_TEST_MARKER();
3815 if( xBreakLoop != pdFALSE )
3821 if( pxReturn != NULL )
3823 /* The handle has been found. */
3830 mtCOVERAGE_TEST_MARKER();
3835 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3837 #endif /* INCLUDE_xTaskGetHandle */
3838 /*-----------------------------------------------------------*/
3840 #if ( INCLUDE_xTaskGetHandle == 1 )
3842 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3844 UBaseType_t uxQueue = configMAX_PRIORITIES;
3847 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
3848 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
3852 /* Search the ready lists. */
3856 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
3860 /* Found the handle. */
3863 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3865 /* Search the delayed lists. */
3868 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
3873 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
3876 #if ( INCLUDE_vTaskSuspend == 1 )
3880 /* Search the suspended list. */
3881 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
3886 #if ( INCLUDE_vTaskDelete == 1 )
3890 /* Search the deleted list. */
3891 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
3896 ( void ) xTaskResumeAll();
3901 #endif /* INCLUDE_xTaskGetHandle */
3902 /*-----------------------------------------------------------*/
3904 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3906 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
3907 StackType_t ** ppuxStackBuffer,
3908 StaticTask_t ** ppxTaskBuffer )
3913 configASSERT( ppuxStackBuffer != NULL );
3914 configASSERT( ppxTaskBuffer != NULL );
3916 pxTCB = prvGetTCBFromHandle( xTask );
3918 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
3920 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
3922 *ppuxStackBuffer = pxTCB->pxStack;
3923 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
3926 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
3928 *ppuxStackBuffer = pxTCB->pxStack;
3929 *ppxTaskBuffer = NULL;
3937 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
3939 *ppuxStackBuffer = pxTCB->pxStack;
3940 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
3943 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
3948 #endif /* configSUPPORT_STATIC_ALLOCATION */
3949 /*-----------------------------------------------------------*/
3951 #if ( configUSE_TRACE_FACILITY == 1 )
3953 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
3954 const UBaseType_t uxArraySize,
3955 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
3957 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
3961 /* Is there a space in the array for each task in the system? */
3962 if( uxArraySize >= uxCurrentNumberOfTasks )
3964 /* Fill in an TaskStatus_t structure with information on each
3965 * task in the Ready state. */
3969 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
3970 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3972 /* Fill in an TaskStatus_t structure with information on each
3973 * task in the Blocked state. */
3974 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
3975 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
3977 #if ( INCLUDE_vTaskDelete == 1 )
3979 /* Fill in an TaskStatus_t structure with information on
3980 * each task that has been deleted but not yet cleaned up. */
3981 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
3985 #if ( INCLUDE_vTaskSuspend == 1 )
3987 /* Fill in an TaskStatus_t structure with information on
3988 * each task in the Suspended state. */
3989 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
3993 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3995 if( pulTotalRunTime != NULL )
3997 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3998 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4000 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4004 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4006 if( pulTotalRunTime != NULL )
4008 *pulTotalRunTime = 0;
4011 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4015 mtCOVERAGE_TEST_MARKER();
4018 ( void ) xTaskResumeAll();
4023 #endif /* configUSE_TRACE_FACILITY */
4024 /*----------------------------------------------------------*/
4026 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4028 /* SMP_TODO : This function returns only idle task handle for core 0.
4029 * Consider to add another function to return the idle task handles. */
4030 TaskHandle_t xTaskGetIdleTaskHandle( void )
4032 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4033 * started, then xIdleTaskHandles will be NULL. */
4034 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4035 return xIdleTaskHandles[ 0 ];
4038 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4039 /*----------------------------------------------------------*/
4041 /* This conditional compilation should use inequality to 0, not equality to 1.
4042 * This is to ensure vTaskStepTick() is available when user defined low power mode
4043 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4045 #if ( configUSE_TICKLESS_IDLE != 0 )
4047 void vTaskStepTick( TickType_t xTicksToJump )
4049 /* Correct the tick count value after a period during which the tick
4050 * was suppressed. Note this does *not* call the tick hook function for
4051 * each stepped tick. */
4052 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
4054 if( ( xTickCount + xTicksToJump ) == xNextTaskUnblockTime )
4056 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4057 * xTaskIncrementTick() when the scheduler resumes. This ensures
4058 * that any delayed tasks are resumed at the correct time. */
4059 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4060 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4062 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4063 taskENTER_CRITICAL();
4067 taskEXIT_CRITICAL();
4072 mtCOVERAGE_TEST_MARKER();
4075 xTickCount += xTicksToJump;
4076 traceINCREASE_TICK_COUNT( xTicksToJump );
4079 #endif /* configUSE_TICKLESS_IDLE */
4080 /*----------------------------------------------------------*/
4082 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4084 BaseType_t xYieldOccurred;
4086 /* Must not be called with the scheduler suspended as the implementation
4087 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4088 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4090 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4091 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4094 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4095 taskENTER_CRITICAL();
4097 xPendedTicks += xTicksToCatchUp;
4099 taskEXIT_CRITICAL();
4100 xYieldOccurred = xTaskResumeAll();
4102 return xYieldOccurred;
4104 /*----------------------------------------------------------*/
4106 #if ( INCLUDE_xTaskAbortDelay == 1 )
4108 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4110 TCB_t * pxTCB = xTask;
4113 configASSERT( pxTCB );
4117 /* A task can only be prematurely removed from the Blocked state if
4118 * it is actually in the Blocked state. */
4119 if( eTaskGetState( xTask ) == eBlocked )
4123 /* Remove the reference to the task from the blocked list. An
4124 * interrupt won't touch the xStateListItem because the
4125 * scheduler is suspended. */
4126 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4128 /* Is the task waiting on an event also? If so remove it from
4129 * the event list too. Interrupts can touch the event list item,
4130 * even though the scheduler is suspended, so a critical section
4132 taskENTER_CRITICAL();
4134 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4136 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4138 /* This lets the task know it was forcibly removed from the
4139 * blocked state so it should not re-evaluate its block time and
4140 * then block again. */
4141 pxTCB->ucDelayAborted = pdTRUE;
4145 mtCOVERAGE_TEST_MARKER();
4148 taskEXIT_CRITICAL();
4150 /* Place the unblocked task into the appropriate ready list. */
4151 prvAddTaskToReadyList( pxTCB );
4153 /* A task being unblocked cannot cause an immediate context
4154 * switch if preemption is turned off. */
4155 #if ( configUSE_PREEMPTION == 1 )
4157 #if ( configNUMBER_OF_CORES == 1 )
4159 /* Preemption is on, but a context switch should only be
4160 * performed if the unblocked task has a priority that is
4161 * higher than the currently executing task. */
4162 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4164 /* Pend the yield to be performed when the scheduler
4165 * is unsuspended. */
4166 xYieldPendings[ 0 ] = pdTRUE;
4170 mtCOVERAGE_TEST_MARKER();
4173 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4175 taskENTER_CRITICAL();
4177 prvYieldForTask( pxTCB );
4179 taskEXIT_CRITICAL();
4181 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4183 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4190 ( void ) xTaskResumeAll();
4195 #endif /* INCLUDE_xTaskAbortDelay */
4196 /*----------------------------------------------------------*/
4198 BaseType_t xTaskIncrementTick( void )
4201 TickType_t xItemValue;
4202 BaseType_t xSwitchRequired = pdFALSE;
4204 #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4205 BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4206 #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4208 /* Called by the portable layer each time a tick interrupt occurs.
4209 * Increments the tick then checks to see if the new tick value will cause any
4210 * tasks to be unblocked. */
4211 traceTASK_INCREMENT_TICK( xTickCount );
4213 /* Tick increment should occur on every kernel timer event. Core 0 has the
4214 * responsibility to increment the tick, or increment the pended ticks if the
4215 * scheduler is suspended. If pended ticks is greater than zero, the core that
4216 * calls xTaskResumeAll has the responsibility to increment the tick. */
4217 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4219 /* Minor optimisation. The tick count cannot change in this
4221 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4223 /* Increment the RTOS tick, switching the delayed and overflowed
4224 * delayed lists if it wraps to 0. */
4225 xTickCount = xConstTickCount;
4227 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
4229 taskSWITCH_DELAYED_LISTS();
4233 mtCOVERAGE_TEST_MARKER();
4236 /* See if this tick has made a timeout expire. Tasks are stored in
4237 * the queue in the order of their wake time - meaning once one task
4238 * has been found whose block time has not expired there is no need to
4239 * look any further down the list. */
4240 if( xConstTickCount >= xNextTaskUnblockTime )
4244 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4246 /* The delayed list is empty. Set xNextTaskUnblockTime
4247 * to the maximum possible value so it is extremely
4249 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4250 * next time through. */
4251 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4256 /* The delayed list is not empty, get the value of the
4257 * item at the head of the delayed list. This is the time
4258 * at which the task at the head of the delayed list must
4259 * be removed from the Blocked state. */
4260 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. */
4261 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4263 if( xConstTickCount < xItemValue )
4265 /* It is not time to unblock this item yet, but the
4266 * item value is the time at which the task at the head
4267 * of the blocked list must be removed from the Blocked
4268 * state - so record the item value in
4269 * xNextTaskUnblockTime. */
4270 xNextTaskUnblockTime = xItemValue;
4271 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
4275 mtCOVERAGE_TEST_MARKER();
4278 /* It is time to remove the item from the Blocked state. */
4279 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4281 /* Is the task waiting on an event also? If so remove
4282 * it from the event list. */
4283 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4285 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4289 mtCOVERAGE_TEST_MARKER();
4292 /* Place the unblocked task into the appropriate ready
4294 prvAddTaskToReadyList( pxTCB );
4296 /* A task being unblocked cannot cause an immediate
4297 * context switch if preemption is turned off. */
4298 #if ( configUSE_PREEMPTION == 1 )
4300 #if ( configNUMBER_OF_CORES == 1 )
4302 /* Preemption is on, but a context switch should
4303 * only be performed if the unblocked task's
4304 * priority is higher than the currently executing
4306 * The case of equal priority tasks sharing
4307 * processing time (which happens when both
4308 * preemption and time slicing are on) is
4310 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4312 xSwitchRequired = pdTRUE;
4316 mtCOVERAGE_TEST_MARKER();
4319 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4321 prvYieldForTask( pxTCB );
4323 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4325 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4330 /* Tasks of equal priority to the currently running task will share
4331 * processing time (time slice) if preemption is on, and the application
4332 * writer has not explicitly turned time slicing off. */
4333 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4335 #if ( configNUMBER_OF_CORES == 1 )
4337 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 )
4339 xSwitchRequired = pdTRUE;
4343 mtCOVERAGE_TEST_MARKER();
4346 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4350 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4352 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1 )
4354 xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4358 mtCOVERAGE_TEST_MARKER();
4362 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4364 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4366 #if ( configUSE_TICK_HOOK == 1 )
4368 /* Guard against the tick hook being called when the pended tick
4369 * count is being unwound (when the scheduler is being unlocked). */
4370 if( xPendedTicks == ( TickType_t ) 0 )
4372 vApplicationTickHook();
4376 mtCOVERAGE_TEST_MARKER();
4379 #endif /* configUSE_TICK_HOOK */
4381 #if ( configUSE_PREEMPTION == 1 )
4383 #if ( configNUMBER_OF_CORES == 1 )
4385 /* For single core the core ID is always 0. */
4386 if( xYieldPendings[ 0 ] != pdFALSE )
4388 xSwitchRequired = pdTRUE;
4392 mtCOVERAGE_TEST_MARKER();
4395 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4397 BaseType_t xCoreID, xCurrentCoreID;
4398 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4400 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4402 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4403 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4406 if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4408 if( xCoreID == xCurrentCoreID )
4410 xSwitchRequired = pdTRUE;
4414 prvYieldCore( xCoreID );
4419 mtCOVERAGE_TEST_MARKER();
4424 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4426 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4432 /* The tick hook gets called at regular intervals, even if the
4433 * scheduler is locked. */
4434 #if ( configUSE_TICK_HOOK == 1 )
4436 vApplicationTickHook();
4441 return xSwitchRequired;
4443 /*-----------------------------------------------------------*/
4445 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4447 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4448 TaskHookFunction_t pxHookFunction )
4452 /* If xTask is NULL then it is the task hook of the calling task that is
4456 xTCB = ( TCB_t * ) pxCurrentTCB;
4463 /* Save the hook function in the TCB. A critical section is required as
4464 * the value can be accessed from an interrupt. */
4465 taskENTER_CRITICAL();
4467 xTCB->pxTaskTag = pxHookFunction;
4469 taskEXIT_CRITICAL();
4472 #endif /* configUSE_APPLICATION_TASK_TAG */
4473 /*-----------------------------------------------------------*/
4475 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4477 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4480 TaskHookFunction_t xReturn;
4482 /* If xTask is NULL then set the calling task's hook. */
4483 pxTCB = prvGetTCBFromHandle( xTask );
4485 /* Save the hook function in the TCB. A critical section is required as
4486 * the value can be accessed from an interrupt. */
4487 taskENTER_CRITICAL();
4489 xReturn = pxTCB->pxTaskTag;
4491 taskEXIT_CRITICAL();
4496 #endif /* configUSE_APPLICATION_TASK_TAG */
4497 /*-----------------------------------------------------------*/
4499 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4501 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
4504 TaskHookFunction_t xReturn;
4505 UBaseType_t uxSavedInterruptStatus;
4507 /* If xTask is NULL then set the calling task's hook. */
4508 pxTCB = prvGetTCBFromHandle( xTask );
4510 /* Save the hook function in the TCB. A critical section is required as
4511 * the value can be accessed from an interrupt. */
4512 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
4514 xReturn = pxTCB->pxTaskTag;
4516 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
4521 #endif /* configUSE_APPLICATION_TASK_TAG */
4522 /*-----------------------------------------------------------*/
4524 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4526 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
4527 void * pvParameter )
4532 /* If xTask is NULL then we are calling our own task hook. */
4535 xTCB = pxCurrentTCB;
4542 if( xTCB->pxTaskTag != NULL )
4544 xReturn = xTCB->pxTaskTag( pvParameter );
4554 #endif /* configUSE_APPLICATION_TASK_TAG */
4555 /*-----------------------------------------------------------*/
4557 #if ( configNUMBER_OF_CORES == 1 )
4558 void vTaskSwitchContext( void )
4560 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
4562 /* The scheduler is currently suspended - do not allow a context
4564 xYieldPendings[ 0 ] = pdTRUE;
4568 xYieldPendings[ 0 ] = pdFALSE;
4569 traceTASK_SWITCHED_OUT();
4571 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4573 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4574 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
4576 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
4579 /* Add the amount of time the task has been running to the
4580 * accumulated time so far. The time the task started running was
4581 * stored in ulTaskSwitchedInTime. Note that there is no overflow
4582 * protection here so count values are only valid until the timer
4583 * overflows. The guard against negative values is to protect
4584 * against suspect run time stat counter implementations - which
4585 * are provided by the application, not the kernel. */
4586 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
4588 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
4592 mtCOVERAGE_TEST_MARKER();
4595 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
4597 #endif /* configGENERATE_RUN_TIME_STATS */
4599 /* Check for stack overflow, if configured. */
4600 taskCHECK_FOR_STACK_OVERFLOW();
4602 /* Before the currently running task is switched out, save its errno. */
4603 #if ( configUSE_POSIX_ERRNO == 1 )
4605 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
4609 /* Select a new task to run using either the generic C or port
4610 * optimised asm code. */
4611 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. */
4612 traceTASK_SWITCHED_IN();
4614 /* After the new task is switched in, update the global errno. */
4615 #if ( configUSE_POSIX_ERRNO == 1 )
4617 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
4621 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
4623 /* Switch C-Runtime's TLS Block to point to the TLS
4624 * Block specific to this task. */
4625 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
4630 #else /* if ( configNUMBER_OF_CORES == 1 ) */
4631 void vTaskSwitchContext( BaseType_t xCoreID )
4633 /* Acquire both locks:
4634 * - The ISR lock protects the ready list from simultaneous access by
4635 * both other ISRs and tasks.
4636 * - We also take the task lock to pause here in case another core has
4637 * suspended the scheduler. We don't want to simply set xYieldPending
4638 * and move on if another core suspended the scheduler. We should only
4639 * do that if the current core has suspended the scheduler. */
4641 portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
4644 /* vTaskSwitchContext() must never be called from within a critical section.
4645 * This is not necessarily true for single core FreeRTOS, but it is for this
4647 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
4649 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
4651 /* The scheduler is currently suspended - do not allow a context
4653 xYieldPendings[ xCoreID ] = pdTRUE;
4657 xYieldPendings[ xCoreID ] = pdFALSE;
4658 traceTASK_SWITCHED_OUT();
4660 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4662 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4663 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
4665 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
4668 /* Add the amount of time the task has been running to the
4669 * accumulated time so far. The time the task started running was
4670 * stored in ulTaskSwitchedInTime. Note that there is no overflow
4671 * protection here so count values are only valid until the timer
4672 * overflows. The guard against negative values is to protect
4673 * against suspect run time stat counter implementations - which
4674 * are provided by the application, not the kernel. */
4675 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
4677 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
4681 mtCOVERAGE_TEST_MARKER();
4684 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
4686 #endif /* configGENERATE_RUN_TIME_STATS */
4688 /* Check for stack overflow, if configured. */
4689 taskCHECK_FOR_STACK_OVERFLOW();
4691 /* Before the currently running task is switched out, save its errno. */
4692 #if ( configUSE_POSIX_ERRNO == 1 )
4694 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
4698 /* Select a new task to run. */
4699 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
4700 traceTASK_SWITCHED_IN();
4702 /* After the new task is switched in, update the global errno. */
4703 #if ( configUSE_POSIX_ERRNO == 1 )
4705 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
4709 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
4711 /* Switch C-Runtime's TLS Block to point to the TLS
4712 * Block specific to this task. */
4713 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
4718 portRELEASE_ISR_LOCK();
4719 portRELEASE_TASK_LOCK();
4721 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
4722 /*-----------------------------------------------------------*/
4724 void vTaskPlaceOnEventList( List_t * const pxEventList,
4725 const TickType_t xTicksToWait )
4727 configASSERT( pxEventList );
4729 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
4730 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
4732 /* Place the event list item of the TCB in the appropriate event list.
4733 * This is placed in the list in priority order so the highest priority task
4734 * is the first to be woken by the event.
4736 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
4737 * Normally, the xItemValue of a TCB's ListItem_t members is:
4738 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
4739 * Therefore, the event list is sorted in descending priority order.
4741 * The queue that contains the event list is locked, preventing
4742 * simultaneous access from interrupts. */
4743 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4745 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4747 /*-----------------------------------------------------------*/
4749 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
4750 const TickType_t xItemValue,
4751 const TickType_t xTicksToWait )
4753 configASSERT( pxEventList );
4755 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
4756 * the event groups implementation. */
4757 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4759 /* Store the item value in the event list item. It is safe to access the
4760 * event list item here as interrupts won't access the event list item of a
4761 * task that is not in the Blocked state. */
4762 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4764 /* Place the event list item of the TCB at the end of the appropriate event
4765 * list. It is safe to access the event list here because it is part of an
4766 * event group implementation - and interrupts don't access event groups
4767 * directly (instead they access them indirectly by pending function calls to
4768 * the task level). */
4769 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4771 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4773 /*-----------------------------------------------------------*/
4775 #if ( configUSE_TIMERS == 1 )
4777 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
4778 TickType_t xTicksToWait,
4779 const BaseType_t xWaitIndefinitely )
4781 configASSERT( pxEventList );
4783 /* This function should not be called by application code hence the
4784 * 'Restricted' in its name. It is not part of the public API. It is
4785 * designed for use by kernel code, and has special calling requirements -
4786 * it should be called with the scheduler suspended. */
4789 /* Place the event list item of the TCB in the appropriate event list.
4790 * In this case it is assume that this is the only task that is going to
4791 * be waiting on this event list, so the faster vListInsertEnd() function
4792 * can be used in place of vListInsert. */
4793 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4795 /* If the task should block indefinitely then set the block time to a
4796 * value that will be recognised as an indefinite delay inside the
4797 * prvAddCurrentTaskToDelayedList() function. */
4798 if( xWaitIndefinitely != pdFALSE )
4800 xTicksToWait = portMAX_DELAY;
4803 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
4804 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
4807 #endif /* configUSE_TIMERS */
4808 /*-----------------------------------------------------------*/
4810 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
4812 TCB_t * pxUnblockedTCB;
4815 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
4816 * called from a critical section within an ISR. */
4818 /* The event list is sorted in priority order, so the first in the list can
4819 * be removed as it is known to be the highest priority. Remove the TCB from
4820 * the delayed list, and add it to the ready list.
4822 * If an event is for a queue that is locked then this function will never
4823 * get called - the lock count on the queue will get modified instead. This
4824 * means exclusive access to the event list is guaranteed here.
4826 * This function assumes that a check has already been made to ensure that
4827 * pxEventList is not empty. */
4828 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. */
4829 configASSERT( pxUnblockedTCB );
4830 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
4832 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4834 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
4835 prvAddTaskToReadyList( pxUnblockedTCB );
4837 #if ( configUSE_TICKLESS_IDLE != 0 )
4839 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4840 * might be set to the blocked task's time out time. If the task is
4841 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4842 * normally left unchanged, because it is automatically reset to a new
4843 * value when the tick count equals xNextTaskUnblockTime. However if
4844 * tickless idling is used it might be more important to enter sleep mode
4845 * at the earliest possible time - so reset xNextTaskUnblockTime here to
4846 * ensure it is updated at the earliest possible time. */
4847 prvResetNextTaskUnblockTime();
4853 /* The delayed and ready lists cannot be accessed, so hold this task
4854 * pending until the scheduler is resumed. */
4855 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
4858 #if ( configNUMBER_OF_CORES == 1 )
4860 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
4862 /* Return true if the task removed from the event list has a higher
4863 * priority than the calling task. This allows the calling task to know if
4864 * it should force a context switch now. */
4867 /* Mark that a yield is pending in case the user is not using the
4868 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
4869 xYieldPendings[ 0 ] = pdTRUE;
4876 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4880 #if ( configUSE_PREEMPTION == 1 )
4882 prvYieldForTask( pxUnblockedTCB );
4884 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
4889 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4891 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4895 /*-----------------------------------------------------------*/
4897 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
4898 const TickType_t xItemValue )
4900 TCB_t * pxUnblockedTCB;
4902 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
4903 * the event flags implementation. */
4904 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4906 /* Store the new item value in the event list. */
4907 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4909 /* Remove the event list form the event flag. Interrupts do not access
4911 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. */
4912 configASSERT( pxUnblockedTCB );
4913 listREMOVE_ITEM( pxEventListItem );
4915 #if ( configUSE_TICKLESS_IDLE != 0 )
4917 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4918 * might be set to the blocked task's time out time. If the task is
4919 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4920 * normally left unchanged, because it is automatically reset to a new
4921 * value when the tick count equals xNextTaskUnblockTime. However if
4922 * tickless idling is used it might be more important to enter sleep mode
4923 * at the earliest possible time - so reset xNextTaskUnblockTime here to
4924 * ensure it is updated at the earliest possible time. */
4925 prvResetNextTaskUnblockTime();
4929 /* Remove the task from the delayed list and add it to the ready list. The
4930 * scheduler is suspended so interrupts will not be accessing the ready
4932 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
4933 prvAddTaskToReadyList( pxUnblockedTCB );
4935 #if ( configNUMBER_OF_CORES == 1 )
4937 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
4939 /* The unblocked task has a priority above that of the calling task, so
4940 * a context switch is required. This function is called with the
4941 * scheduler suspended so xYieldPending is set so the context switch
4942 * occurs immediately that the scheduler is resumed (unsuspended). */
4943 xYieldPendings[ 0 ] = pdTRUE;
4946 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4948 #if ( configUSE_PREEMPTION == 1 )
4950 taskENTER_CRITICAL();
4952 prvYieldForTask( pxUnblockedTCB );
4954 taskEXIT_CRITICAL();
4958 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4960 /*-----------------------------------------------------------*/
4962 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
4964 configASSERT( pxTimeOut );
4965 taskENTER_CRITICAL();
4967 pxTimeOut->xOverflowCount = xNumOfOverflows;
4968 pxTimeOut->xTimeOnEntering = xTickCount;
4970 taskEXIT_CRITICAL();
4972 /*-----------------------------------------------------------*/
4974 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
4976 /* For internal use only as it does not use a critical section. */
4977 pxTimeOut->xOverflowCount = xNumOfOverflows;
4978 pxTimeOut->xTimeOnEntering = xTickCount;
4980 /*-----------------------------------------------------------*/
4982 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
4983 TickType_t * const pxTicksToWait )
4987 configASSERT( pxTimeOut );
4988 configASSERT( pxTicksToWait );
4990 taskENTER_CRITICAL();
4992 /* Minor optimisation. The tick count cannot change in this block. */
4993 const TickType_t xConstTickCount = xTickCount;
4994 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
4996 #if ( INCLUDE_xTaskAbortDelay == 1 )
4997 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
4999 /* The delay was aborted, which is not the same as a time out,
5000 * but has the same result. */
5001 pxCurrentTCB->ucDelayAborted = pdFALSE;
5007 #if ( INCLUDE_vTaskSuspend == 1 )
5008 if( *pxTicksToWait == portMAX_DELAY )
5010 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5011 * specified is the maximum block time then the task should block
5012 * indefinitely, and therefore never time out. */
5018 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
5020 /* The tick count is greater than the time at which
5021 * vTaskSetTimeout() was called, but has also overflowed since
5022 * vTaskSetTimeOut() was called. It must have wrapped all the way
5023 * around and gone past again. This passed since vTaskSetTimeout()
5026 *pxTicksToWait = ( TickType_t ) 0;
5028 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
5030 /* Not a genuine timeout. Adjust parameters for time remaining. */
5031 *pxTicksToWait -= xElapsedTime;
5032 vTaskInternalSetTimeOutState( pxTimeOut );
5037 *pxTicksToWait = ( TickType_t ) 0;
5041 taskEXIT_CRITICAL();
5045 /*-----------------------------------------------------------*/
5047 void vTaskMissedYield( void )
5049 /* Must be called from within a critical section. */
5050 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5052 /*-----------------------------------------------------------*/
5054 #if ( configUSE_TRACE_FACILITY == 1 )
5056 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5058 UBaseType_t uxReturn;
5059 TCB_t const * pxTCB;
5064 uxReturn = pxTCB->uxTaskNumber;
5074 #endif /* configUSE_TRACE_FACILITY */
5075 /*-----------------------------------------------------------*/
5077 #if ( configUSE_TRACE_FACILITY == 1 )
5079 void vTaskSetTaskNumber( TaskHandle_t xTask,
5080 const UBaseType_t uxHandle )
5087 pxTCB->uxTaskNumber = uxHandle;
5091 #endif /* configUSE_TRACE_FACILITY */
5092 /*-----------------------------------------------------------*/
5095 * -----------------------------------------------------------
5096 * The MinimalIdle task.
5097 * ----------------------------------------------------------
5099 * The minimal idle task is used for all the additional cores in a SMP
5100 * system. There must be only 1 idle task and the rest are minimal idle
5103 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5104 * language extensions. The equivalent prototype for this function is:
5106 * void prvMinimalIdleTask( void *pvParameters );
5109 #if ( configNUMBER_OF_CORES > 1 )
5110 static portTASK_FUNCTION( prvMinimalIdleTask, pvParameters )
5112 ( void ) pvParameters;
5116 for( ; INFINITE_LOOP(); )
5118 #if ( configUSE_PREEMPTION == 0 )
5120 /* If we are not using preemption we keep forcing a task switch to
5121 * see if any other task has become available. If we are using
5122 * preemption we don't need to do this as any task becoming available
5123 * will automatically get the processor anyway. */
5126 #endif /* configUSE_PREEMPTION */
5128 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5130 /* When using preemption tasks of equal priority will be
5131 * timesliced. If a task that is sharing the idle priority is ready
5132 * to run then the idle task should yield before the end of the
5135 * A critical region is not required here as we are just reading from
5136 * the list, and an occasional incorrect value will not matter. If
5137 * the ready list at the idle priority contains one more task than the
5138 * number of idle tasks, which is equal to the configured numbers of cores
5139 * then a task other than the idle task is ready to execute. */
5140 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5146 mtCOVERAGE_TEST_MARKER();
5149 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5151 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
5153 /* Call the user defined function from within the idle task. This
5154 * allows the application designer to add background functionality
5155 * without the overhead of a separate task.
5157 * This hook is intended to manage core activity such as disabling cores that go idle.
5159 * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5160 * CALL A FUNCTION THAT MIGHT BLOCK. */
5161 vApplicationMinimalIdleHook();
5163 #endif /* configUSE_MINIMAL_IDLE_HOOK */
5166 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5169 * -----------------------------------------------------------
5171 * ----------------------------------------------------------
5173 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5174 * language extensions. The equivalent prototype for this function is:
5176 * void prvIdleTask( void *pvParameters );
5180 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5182 /* Stop warnings. */
5183 ( void ) pvParameters;
5185 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5186 * SCHEDULER IS STARTED. **/
5188 /* In case a task that has a secure context deletes itself, in which case
5189 * the idle task is responsible for deleting the task's secure context, if
5191 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5193 #if ( configNUMBER_OF_CORES > 1 )
5195 /* SMP all cores start up in the idle task. This initial yield gets the application
5199 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5201 for( ; INFINITE_LOOP(); )
5203 /* See if any tasks have deleted themselves - if so then the idle task
5204 * is responsible for freeing the deleted task's TCB and stack. */
5205 prvCheckTasksWaitingTermination();
5207 #if ( configUSE_PREEMPTION == 0 )
5209 /* If we are not using preemption we keep forcing a task switch to
5210 * see if any other task has become available. If we are using
5211 * preemption we don't need to do this as any task becoming available
5212 * will automatically get the processor anyway. */
5215 #endif /* configUSE_PREEMPTION */
5217 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5219 /* When using preemption tasks of equal priority will be
5220 * timesliced. If a task that is sharing the idle priority is ready
5221 * to run then the idle task should yield before the end of the
5224 * A critical region is not required here as we are just reading from
5225 * the list, and an occasional incorrect value will not matter. If
5226 * the ready list at the idle priority contains one more task than the
5227 * number of idle tasks, which is equal to the configured numbers of cores
5228 * then a task other than the idle task is ready to execute. */
5229 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5235 mtCOVERAGE_TEST_MARKER();
5238 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5240 #if ( configUSE_IDLE_HOOK == 1 )
5242 /* Call the user defined function from within the idle task. */
5243 vApplicationIdleHook();
5245 #endif /* configUSE_IDLE_HOOK */
5247 /* This conditional compilation should use inequality to 0, not equality
5248 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5249 * user defined low power mode implementations require
5250 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5251 #if ( configUSE_TICKLESS_IDLE != 0 )
5253 TickType_t xExpectedIdleTime;
5255 /* It is not desirable to suspend then resume the scheduler on
5256 * each iteration of the idle task. Therefore, a preliminary
5257 * test of the expected idle time is performed without the
5258 * scheduler suspended. The result here is not necessarily
5260 xExpectedIdleTime = prvGetExpectedIdleTime();
5262 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5266 /* Now the scheduler is suspended, the expected idle
5267 * time can be sampled again, and this time its value can
5269 configASSERT( xNextTaskUnblockTime >= xTickCount );
5270 xExpectedIdleTime = prvGetExpectedIdleTime();
5272 /* Define the following macro to set xExpectedIdleTime to 0
5273 * if the application does not want
5274 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5275 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5277 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5279 traceLOW_POWER_IDLE_BEGIN();
5280 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5281 traceLOW_POWER_IDLE_END();
5285 mtCOVERAGE_TEST_MARKER();
5288 ( void ) xTaskResumeAll();
5292 mtCOVERAGE_TEST_MARKER();
5295 #endif /* configUSE_TICKLESS_IDLE */
5297 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_MINIMAL_IDLE_HOOK == 1 ) )
5299 /* Call the user defined function from within the idle task. This
5300 * allows the application designer to add background functionality
5301 * without the overhead of a separate task.
5303 * This hook is intended to manage core activity such as disabling cores that go idle.
5305 * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5306 * CALL A FUNCTION THAT MIGHT BLOCK. */
5307 vApplicationMinimalIdleHook();
5309 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_MINIMAL_IDLE_HOOK == 1 ) ) */
5312 /*-----------------------------------------------------------*/
5314 #if ( configUSE_TICKLESS_IDLE != 0 )
5316 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5318 #if ( INCLUDE_vTaskSuspend == 1 )
5319 /* The idle task exists in addition to the application tasks. */
5320 const UBaseType_t uxNonApplicationTasks = 1;
5321 #endif /* INCLUDE_vTaskSuspend */
5323 eSleepModeStatus eReturn = eStandardSleep;
5325 /* This function must be called from a critical section. */
5327 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
5329 /* A task was made ready while the scheduler was suspended. */
5330 eReturn = eAbortSleep;
5332 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5334 /* A yield was pended while the scheduler was suspended. */
5335 eReturn = eAbortSleep;
5337 else if( xPendedTicks != 0 )
5339 /* A tick interrupt has already occurred but was held pending
5340 * because the scheduler is suspended. */
5341 eReturn = eAbortSleep;
5344 #if ( INCLUDE_vTaskSuspend == 1 )
5345 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5347 /* If all the tasks are in the suspended list (which might mean they
5348 * have an infinite block time rather than actually being suspended)
5349 * then it is safe to turn all clocks off and just wait for external
5351 eReturn = eNoTasksWaitingTimeout;
5353 #endif /* INCLUDE_vTaskSuspend */
5356 mtCOVERAGE_TEST_MARKER();
5362 #endif /* configUSE_TICKLESS_IDLE */
5363 /*-----------------------------------------------------------*/
5365 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5367 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5373 if( ( xIndex >= 0 ) &&
5374 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5376 pxTCB = prvGetTCBFromHandle( xTaskToSet );
5377 configASSERT( pxTCB != NULL );
5378 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5382 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5383 /*-----------------------------------------------------------*/
5385 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5387 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5390 void * pvReturn = NULL;
5393 if( ( xIndex >= 0 ) &&
5394 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5396 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
5397 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
5407 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5408 /*-----------------------------------------------------------*/
5410 #if ( portUSING_MPU_WRAPPERS == 1 )
5412 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
5413 const MemoryRegion_t * const pxRegions )
5417 /* If null is passed in here then we are modifying the MPU settings of
5418 * the calling task. */
5419 pxTCB = prvGetTCBFromHandle( xTaskToModify );
5421 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
5424 #endif /* portUSING_MPU_WRAPPERS */
5425 /*-----------------------------------------------------------*/
5427 static void prvInitialiseTaskLists( void )
5429 UBaseType_t uxPriority;
5431 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
5433 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
5436 vListInitialise( &xDelayedTaskList1 );
5437 vListInitialise( &xDelayedTaskList2 );
5438 vListInitialise( &xPendingReadyList );
5440 #if ( INCLUDE_vTaskDelete == 1 )
5442 vListInitialise( &xTasksWaitingTermination );
5444 #endif /* INCLUDE_vTaskDelete */
5446 #if ( INCLUDE_vTaskSuspend == 1 )
5448 vListInitialise( &xSuspendedTaskList );
5450 #endif /* INCLUDE_vTaskSuspend */
5452 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
5454 pxDelayedTaskList = &xDelayedTaskList1;
5455 pxOverflowDelayedTaskList = &xDelayedTaskList2;
5457 /*-----------------------------------------------------------*/
5459 static void prvCheckTasksWaitingTermination( void )
5461 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
5463 #if ( INCLUDE_vTaskDelete == 1 )
5467 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
5468 * being called too often in the idle task. */
5469 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
5471 #if ( configNUMBER_OF_CORES == 1 )
5473 taskENTER_CRITICAL();
5476 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. */
5477 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5478 --uxCurrentNumberOfTasks;
5479 --uxDeletedTasksWaitingCleanUp;
5482 taskEXIT_CRITICAL();
5484 prvDeleteTCB( pxTCB );
5486 #else /* #if( configNUMBER_OF_CORES == 1 ) */
5490 taskENTER_CRITICAL();
5492 /* For SMP, multiple idles can be running simultaneously
5493 * and we need to check that other idles did not cleanup while we were
5494 * waiting to enter the critical section. */
5495 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
5497 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. */
5499 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
5501 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5502 --uxCurrentNumberOfTasks;
5503 --uxDeletedTasksWaitingCleanUp;
5507 /* The TCB to be deleted still has not yet been switched out
5508 * by the scheduler, so we will just exit this loop early and
5509 * try again next time. */
5510 taskEXIT_CRITICAL();
5515 taskEXIT_CRITICAL();
5519 prvDeleteTCB( pxTCB );
5522 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
5525 #endif /* INCLUDE_vTaskDelete */
5527 /*-----------------------------------------------------------*/
5529 #if ( configUSE_TRACE_FACILITY == 1 )
5531 void vTaskGetInfo( TaskHandle_t xTask,
5532 TaskStatus_t * pxTaskStatus,
5533 BaseType_t xGetFreeStackSpace,
5538 /* xTask is NULL then get the state of the calling task. */
5539 pxTCB = prvGetTCBFromHandle( xTask );
5541 pxTaskStatus->xHandle = pxTCB;
5542 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
5543 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
5544 pxTaskStatus->pxStackBase = pxTCB->pxStack;
5545 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
5546 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
5547 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
5549 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
5551 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
5553 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
5557 #if ( configUSE_MUTEXES == 1 )
5559 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
5563 pxTaskStatus->uxBasePriority = 0;
5567 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5569 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
5573 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
5577 /* Obtaining the task state is a little fiddly, so is only done if the
5578 * value of eState passed into this function is eInvalid - otherwise the
5579 * state is just set to whatever is passed in. */
5580 if( eState != eInvalid )
5582 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
5584 pxTaskStatus->eCurrentState = eRunning;
5588 pxTaskStatus->eCurrentState = eState;
5590 #if ( INCLUDE_vTaskSuspend == 1 )
5592 /* If the task is in the suspended list then there is a
5593 * chance it is actually just blocked indefinitely - so really
5594 * it should be reported as being in the Blocked state. */
5595 if( eState == eSuspended )
5599 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
5601 pxTaskStatus->eCurrentState = eBlocked;
5604 ( void ) xTaskResumeAll();
5607 #endif /* INCLUDE_vTaskSuspend */
5609 /* Tasks can be in pending ready list and other state list at the
5610 * same time. These tasks are in ready state no matter what state
5611 * list the task is in. */
5612 taskENTER_CRITICAL();
5614 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
5616 pxTaskStatus->eCurrentState = eReady;
5619 taskEXIT_CRITICAL();
5624 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
5627 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
5628 * parameter is provided to allow it to be skipped. */
5629 if( xGetFreeStackSpace != pdFALSE )
5631 #if ( portSTACK_GROWTH > 0 )
5633 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
5637 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
5643 pxTaskStatus->usStackHighWaterMark = 0;
5647 #endif /* configUSE_TRACE_FACILITY */
5648 /*-----------------------------------------------------------*/
5650 #if ( configUSE_TRACE_FACILITY == 1 )
5652 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
5656 configLIST_VOLATILE TCB_t * pxNextTCB;
5657 configLIST_VOLATILE TCB_t * pxFirstTCB;
5658 UBaseType_t uxTask = 0;
5660 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
5662 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. */
5664 /* Populate an TaskStatus_t structure within the
5665 * pxTaskStatusArray array for each task that is referenced from
5666 * pxList. See the definition of TaskStatus_t in task.h for the
5667 * meaning of each TaskStatus_t structure member. */
5670 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. */
5671 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
5673 } while( pxNextTCB != pxFirstTCB );
5677 mtCOVERAGE_TEST_MARKER();
5683 #endif /* configUSE_TRACE_FACILITY */
5684 /*-----------------------------------------------------------*/
5686 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
5688 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
5690 uint32_t ulCount = 0U;
5692 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
5694 pucStackByte -= portSTACK_GROWTH;
5698 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
5700 return ( configSTACK_DEPTH_TYPE ) ulCount;
5703 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
5704 /*-----------------------------------------------------------*/
5706 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
5708 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
5709 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
5710 * user to determine the return type. It gets around the problem of the value
5711 * overflowing on 8-bit types without breaking backward compatibility for
5712 * applications that expect an 8-bit return type. */
5713 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
5716 uint8_t * pucEndOfStack;
5717 configSTACK_DEPTH_TYPE uxReturn;
5719 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
5720 * the same except for their return type. Using configSTACK_DEPTH_TYPE
5721 * allows the user to determine the return type. It gets around the
5722 * problem of the value overflowing on 8-bit types without breaking
5723 * backward compatibility for applications that expect an 8-bit return
5726 pxTCB = prvGetTCBFromHandle( xTask );
5728 #if portSTACK_GROWTH < 0
5730 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
5734 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
5738 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
5743 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
5744 /*-----------------------------------------------------------*/
5746 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
5748 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
5751 uint8_t * pucEndOfStack;
5752 UBaseType_t uxReturn;
5754 pxTCB = prvGetTCBFromHandle( xTask );
5756 #if portSTACK_GROWTH < 0
5758 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
5762 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
5766 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
5771 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
5772 /*-----------------------------------------------------------*/
5774 #if ( INCLUDE_vTaskDelete == 1 )
5776 static void prvDeleteTCB( TCB_t * pxTCB )
5778 /* This call is required specifically for the TriCore port. It must be
5779 * above the vPortFree() calls. The call is also used by ports/demos that
5780 * want to allocate and clean RAM statically. */
5781 portCLEAN_UP_TCB( pxTCB );
5783 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
5785 /* Free up the memory allocated for the task's TLS Block. */
5786 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
5790 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
5792 /* The task can only have been allocated dynamically - free both
5793 * the stack and TCB. */
5794 vPortFreeStack( pxTCB->pxStack );
5797 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
5799 /* The task could have been allocated statically or dynamically, so
5800 * check what was statically allocated before trying to free the
5802 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
5804 /* Both the stack and TCB were allocated dynamically, so both
5806 vPortFreeStack( pxTCB->pxStack );
5809 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
5811 /* Only the stack was statically allocated, so the TCB is the
5812 * only memory that must be freed. */
5817 /* Neither the stack nor the TCB were allocated dynamically, so
5818 * nothing needs to be freed. */
5819 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
5820 mtCOVERAGE_TEST_MARKER();
5823 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
5826 #endif /* INCLUDE_vTaskDelete */
5827 /*-----------------------------------------------------------*/
5829 static void prvResetNextTaskUnblockTime( void )
5831 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
5833 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
5834 * the maximum possible value so it is extremely unlikely that the
5835 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
5836 * there is an item in the delayed list. */
5837 xNextTaskUnblockTime = portMAX_DELAY;
5841 /* The new current delayed list is not empty, get the value of
5842 * the item at the head of the delayed list. This is the time at
5843 * which the task at the head of the delayed list should be removed
5844 * from the Blocked state. */
5845 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
5848 /*-----------------------------------------------------------*/
5850 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
5852 #if ( configNUMBER_OF_CORES == 1 )
5853 TaskHandle_t xTaskGetCurrentTaskHandle( void )
5855 TaskHandle_t xReturn;
5857 /* A critical section is not required as this is not called from
5858 * an interrupt and the current TCB will always be the same for any
5859 * individual execution thread. */
5860 xReturn = pxCurrentTCB;
5864 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5865 TaskHandle_t xTaskGetCurrentTaskHandle( void )
5867 TaskHandle_t xReturn;
5868 UBaseType_t uxSavedInterruptStatus;
5870 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
5872 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
5874 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
5879 TaskHandle_t xTaskGetCurrentTaskHandleCPU( BaseType_t xCoreID )
5881 TaskHandle_t xReturn = NULL;
5883 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
5885 xReturn = pxCurrentTCBs[ xCoreID ];
5890 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5892 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
5893 /*-----------------------------------------------------------*/
5895 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
5897 BaseType_t xTaskGetSchedulerState( void )
5901 if( xSchedulerRunning == pdFALSE )
5903 xReturn = taskSCHEDULER_NOT_STARTED;
5907 #if ( configNUMBER_OF_CORES > 1 )
5908 taskENTER_CRITICAL();
5911 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5913 xReturn = taskSCHEDULER_RUNNING;
5917 xReturn = taskSCHEDULER_SUSPENDED;
5920 #if ( configNUMBER_OF_CORES > 1 )
5921 taskEXIT_CRITICAL();
5928 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
5929 /*-----------------------------------------------------------*/
5931 #if ( configUSE_MUTEXES == 1 )
5933 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
5935 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
5936 BaseType_t xReturn = pdFALSE;
5938 /* If the mutex was given back by an interrupt while the queue was
5939 * locked then the mutex holder might now be NULL. _RB_ Is this still
5940 * needed as interrupts can no longer use mutexes? */
5941 if( pxMutexHolder != NULL )
5943 /* If the holder of the mutex has a priority below the priority of
5944 * the task attempting to obtain the mutex then it will temporarily
5945 * inherit the priority of the task attempting to obtain the mutex. */
5946 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
5948 /* Adjust the mutex holder state to account for its new
5949 * priority. Only reset the event list item value if the value is
5950 * not being used for anything else. */
5951 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
5953 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. */
5957 mtCOVERAGE_TEST_MARKER();
5960 /* If the task being modified is in the ready state it will need
5961 * to be moved into a new list. */
5962 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
5964 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5966 /* It is known that the task is in its ready list so
5967 * there is no need to check again and the port level
5968 * reset macro can be called directly. */
5969 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
5973 mtCOVERAGE_TEST_MARKER();
5976 /* Inherit the priority before being moved into the new list. */
5977 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
5978 prvAddTaskToReadyList( pxMutexHolderTCB );
5979 #if ( configNUMBER_OF_CORES > 1 )
5981 /* The priority of the task is raised. Yield for this task
5982 * if it is not running. */
5983 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
5985 prvYieldForTask( pxMutexHolderTCB );
5988 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
5992 /* Just inherit the priority. */
5993 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
5996 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
5998 /* Inheritance occurred. */
6003 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6005 /* The base priority of the mutex holder is lower than the
6006 * priority of the task attempting to take the mutex, but the
6007 * current priority of the mutex holder is not lower than the
6008 * priority of the task attempting to take the mutex.
6009 * Therefore the mutex holder must have already inherited a
6010 * priority, but inheritance would have occurred if that had
6011 * not been the case. */
6016 mtCOVERAGE_TEST_MARKER();
6022 mtCOVERAGE_TEST_MARKER();
6028 #endif /* configUSE_MUTEXES */
6029 /*-----------------------------------------------------------*/
6031 #if ( configUSE_MUTEXES == 1 )
6033 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6035 TCB_t * const pxTCB = pxMutexHolder;
6036 BaseType_t xReturn = pdFALSE;
6038 if( pxMutexHolder != NULL )
6040 /* A task can only have an inherited priority if it holds the mutex.
6041 * If the mutex is held by a task then it cannot be given from an
6042 * interrupt, and if a mutex is given by the holding task then it must
6043 * be the running state task. */
6044 configASSERT( pxTCB == pxCurrentTCB );
6045 configASSERT( pxTCB->uxMutexesHeld );
6046 ( pxTCB->uxMutexesHeld )--;
6048 /* Has the holder of the mutex inherited the priority of another
6050 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6052 /* Only disinherit if no other mutexes are held. */
6053 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6055 /* A task can only have an inherited priority if it holds
6056 * the mutex. If the mutex is held by a task then it cannot be
6057 * given from an interrupt, and if a mutex is given by the
6058 * holding task then it must be the running state task. Remove
6059 * the holding task from the ready list. */
6060 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6062 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6066 mtCOVERAGE_TEST_MARKER();
6069 /* Disinherit the priority before adding the task into the
6070 * new ready list. */
6071 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6072 pxTCB->uxPriority = pxTCB->uxBasePriority;
6074 /* Reset the event list item value. It cannot be in use for
6075 * any other purpose if this task is running, and it must be
6076 * running to give back the mutex. */
6077 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. */
6078 prvAddTaskToReadyList( pxTCB );
6079 #if ( configNUMBER_OF_CORES > 1 )
6081 /* The priority of the task is dropped. Yield the core on
6082 * which the task is running. */
6083 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6085 prvYieldCore( pxTCB->xTaskRunState );
6088 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6090 /* Return true to indicate that a context switch is required.
6091 * This is only actually required in the corner case whereby
6092 * multiple mutexes were held and the mutexes were given back
6093 * in an order different to that in which they were taken.
6094 * If a context switch did not occur when the first mutex was
6095 * returned, even if a task was waiting on it, then a context
6096 * switch should occur when the last mutex is returned whether
6097 * a task is waiting on it or not. */
6102 mtCOVERAGE_TEST_MARKER();
6107 mtCOVERAGE_TEST_MARKER();
6112 mtCOVERAGE_TEST_MARKER();
6118 #endif /* configUSE_MUTEXES */
6119 /*-----------------------------------------------------------*/
6121 #if ( configUSE_MUTEXES == 1 )
6123 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6124 UBaseType_t uxHighestPriorityWaitingTask )
6126 TCB_t * const pxTCB = pxMutexHolder;
6127 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6128 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6130 if( pxMutexHolder != NULL )
6132 /* If pxMutexHolder is not NULL then the holder must hold at least
6134 configASSERT( pxTCB->uxMutexesHeld );
6136 /* Determine the priority to which the priority of the task that
6137 * holds the mutex should be set. This will be the greater of the
6138 * holding task's base priority and the priority of the highest
6139 * priority task that is waiting to obtain the mutex. */
6140 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6142 uxPriorityToUse = uxHighestPriorityWaitingTask;
6146 uxPriorityToUse = pxTCB->uxBasePriority;
6149 /* Does the priority need to change? */
6150 if( pxTCB->uxPriority != uxPriorityToUse )
6152 /* Only disinherit if no other mutexes are held. This is a
6153 * simplification in the priority inheritance implementation. If
6154 * the task that holds the mutex is also holding other mutexes then
6155 * the other mutexes may have caused the priority inheritance. */
6156 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6158 /* If a task has timed out because it already holds the
6159 * mutex it was trying to obtain then it cannot of inherited
6160 * its own priority. */
6161 configASSERT( pxTCB != pxCurrentTCB );
6163 /* Disinherit the priority, remembering the previous
6164 * priority to facilitate determining the subject task's
6166 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6167 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6168 pxTCB->uxPriority = uxPriorityToUse;
6170 /* Only reset the event list item value if the value is not
6171 * being used for anything else. */
6172 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
6174 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. */
6178 mtCOVERAGE_TEST_MARKER();
6181 /* If the running task is not the task that holds the mutex
6182 * then the task that holds the mutex could be in either the
6183 * Ready, Blocked or Suspended states. Only remove the task
6184 * from its current state list if it is in the Ready state as
6185 * the task's priority is going to change and there is one
6186 * Ready list per priority. */
6187 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6189 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6191 /* It is known that the task is in its ready list so
6192 * there is no need to check again and the port level
6193 * reset macro can be called directly. */
6194 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6198 mtCOVERAGE_TEST_MARKER();
6201 prvAddTaskToReadyList( pxTCB );
6202 #if ( configNUMBER_OF_CORES > 1 )
6204 /* The priority of the task is dropped. Yield the core on
6205 * which the task is running. */
6206 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6208 prvYieldCore( pxTCB->xTaskRunState );
6211 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6215 mtCOVERAGE_TEST_MARKER();
6220 mtCOVERAGE_TEST_MARKER();
6225 mtCOVERAGE_TEST_MARKER();
6230 mtCOVERAGE_TEST_MARKER();
6234 #endif /* configUSE_MUTEXES */
6235 /*-----------------------------------------------------------*/
6237 #if ( configNUMBER_OF_CORES > 1 )
6239 /* If not in a critical section then yield immediately.
6240 * Otherwise set xYieldPendings to true to wait to
6241 * yield until exiting the critical section.
6243 void vTaskYieldWithinAPI( void )
6245 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6251 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6254 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6256 /*-----------------------------------------------------------*/
6258 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6260 void vTaskEnterCritical( void )
6262 portDISABLE_INTERRUPTS();
6264 if( xSchedulerRunning != pdFALSE )
6266 ( pxCurrentTCB->uxCriticalNesting )++;
6268 /* This is not the interrupt safe version of the enter critical
6269 * function so assert() if it is being called from an interrupt
6270 * context. Only API functions that end in "FromISR" can be used in an
6271 * interrupt. Only assert if the critical nesting count is 1 to
6272 * protect against recursive calls if the assert function also uses a
6273 * critical section. */
6274 if( pxCurrentTCB->uxCriticalNesting == 1 )
6276 portASSERT_IF_IN_ISR();
6281 mtCOVERAGE_TEST_MARKER();
6285 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6286 /*-----------------------------------------------------------*/
6288 #if ( configNUMBER_OF_CORES > 1 )
6290 void vTaskEnterCritical( void )
6292 portDISABLE_INTERRUPTS();
6294 if( xSchedulerRunning != pdFALSE )
6296 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6298 portGET_TASK_LOCK();
6302 portINCREMENT_CRITICAL_NESTING_COUNT();
6304 /* This is not the interrupt safe version of the enter critical
6305 * function so assert() if it is being called from an interrupt
6306 * context. Only API functions that end in "FromISR" can be used in an
6307 * interrupt. Only assert if the critical nesting count is 1 to
6308 * protect against recursive calls if the assert function also uses a
6309 * critical section. */
6310 if( portGET_CRITICAL_NESTING_COUNT() == 1U )
6312 portASSERT_IF_IN_ISR();
6314 if( uxSchedulerSuspended == 0U )
6316 /* The only time there would be a problem is if this is called
6317 * before a context switch and vTaskExitCritical() is called
6318 * after pxCurrentTCB changes. Therefore this should not be
6319 * used within vTaskSwitchContext(). */
6320 prvCheckForRunStateChange();
6326 mtCOVERAGE_TEST_MARKER();
6330 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6332 /*-----------------------------------------------------------*/
6334 #if ( configNUMBER_OF_CORES > 1 )
6336 UBaseType_t vTaskEnterCriticalFromISR( void )
6338 UBaseType_t uxSavedInterruptStatus = 0;
6340 if( xSchedulerRunning != pdFALSE )
6342 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6344 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6349 portINCREMENT_CRITICAL_NESTING_COUNT();
6353 mtCOVERAGE_TEST_MARKER();
6356 return uxSavedInterruptStatus;
6359 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6360 /*-----------------------------------------------------------*/
6362 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6364 void vTaskExitCritical( void )
6366 if( xSchedulerRunning != pdFALSE )
6368 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
6369 * does not match a previous call to vTaskEnterCritical(). */
6370 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
6372 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
6373 * to exit critical section from ISR. */
6374 portASSERT_IF_IN_ISR();
6376 if( pxCurrentTCB->uxCriticalNesting > 0U )
6378 ( pxCurrentTCB->uxCriticalNesting )--;
6380 if( pxCurrentTCB->uxCriticalNesting == 0U )
6382 portENABLE_INTERRUPTS();
6386 mtCOVERAGE_TEST_MARKER();
6391 mtCOVERAGE_TEST_MARKER();
6396 mtCOVERAGE_TEST_MARKER();
6400 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6401 /*-----------------------------------------------------------*/
6403 #if ( configNUMBER_OF_CORES > 1 )
6405 void vTaskExitCritical( void )
6407 if( xSchedulerRunning != pdFALSE )
6409 /* If critical nesting count is zero then this function
6410 * does not match a previous call to vTaskEnterCritical(). */
6411 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
6413 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
6414 * to exit critical section from ISR. */
6415 portASSERT_IF_IN_ISR();
6417 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
6419 portDECREMENT_CRITICAL_NESTING_COUNT();
6421 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6423 BaseType_t xYieldCurrentTask;
6425 /* Get the xYieldPending stats inside the critical section. */
6426 xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
6428 portRELEASE_ISR_LOCK();
6429 portRELEASE_TASK_LOCK();
6430 portENABLE_INTERRUPTS();
6432 /* When a task yields in a critical section it just sets
6433 * xYieldPending to true. So now that we have exited the
6434 * critical section check if xYieldPending is true, and
6436 if( xYieldCurrentTask != pdFALSE )
6443 mtCOVERAGE_TEST_MARKER();
6448 mtCOVERAGE_TEST_MARKER();
6453 mtCOVERAGE_TEST_MARKER();
6457 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6458 /*-----------------------------------------------------------*/
6460 #if ( configNUMBER_OF_CORES > 1 )
6462 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
6464 if( xSchedulerRunning != pdFALSE )
6466 /* If critical nesting count is zero then this function
6467 * does not match a previous call to vTaskEnterCritical(). */
6468 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
6470 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
6472 portDECREMENT_CRITICAL_NESTING_COUNT();
6474 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6476 portRELEASE_ISR_LOCK();
6477 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6481 mtCOVERAGE_TEST_MARKER();
6486 mtCOVERAGE_TEST_MARKER();
6491 mtCOVERAGE_TEST_MARKER();
6495 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6496 /*-----------------------------------------------------------*/
6498 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
6500 static char * prvWriteNameToBuffer( char * pcBuffer,
6501 const char * pcTaskName )
6505 /* Start by copying the entire string. */
6506 ( void ) strcpy( pcBuffer, pcTaskName );
6508 /* Pad the end of the string with spaces to ensure columns line up when
6510 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
6512 pcBuffer[ x ] = ' ';
6516 pcBuffer[ x ] = ( char ) 0x00;
6518 /* Return the new end of string. */
6519 return &( pcBuffer[ x ] );
6522 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
6523 /*-----------------------------------------------------------*/
6525 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
6527 void vTaskList( char * pcWriteBuffer )
6529 TaskStatus_t * pxTaskStatusArray;
6530 UBaseType_t uxArraySize, x;
6536 * This function is provided for convenience only, and is used by many
6537 * of the demo applications. Do not consider it to be part of the
6540 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
6541 * uxTaskGetSystemState() output into a human readable table that
6542 * displays task: names, states, priority, stack usage and task number.
6543 * Stack usage specified as the number of unused StackType_t words stack can hold
6544 * on top of stack - not the number of bytes.
6546 * vTaskList() has a dependency on the sprintf() C library function that
6547 * might bloat the code size, use a lot of stack, and provide different
6548 * results on different platforms. An alternative, tiny, third party,
6549 * and limited functionality implementation of sprintf() is provided in
6550 * many of the FreeRTOS/Demo sub-directories in a file called
6551 * printf-stdarg.c (note printf-stdarg.c does not provide a full
6552 * snprintf() implementation!).
6554 * It is recommended that production systems call uxTaskGetSystemState()
6555 * directly to get access to raw stats data, rather than indirectly
6556 * through a call to vTaskList().
6560 /* Make sure the write buffer does not contain a string. */
6561 *pcWriteBuffer = ( char ) 0x00;
6563 /* Take a snapshot of the number of tasks in case it changes while this
6564 * function is executing. */
6565 uxArraySize = uxCurrentNumberOfTasks;
6567 /* Allocate an array index for each task. NOTE! if
6568 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
6569 * equate to NULL. */
6570 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. */
6572 if( pxTaskStatusArray != NULL )
6574 /* Generate the (binary) data. */
6575 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
6577 /* Create a human readable table from the binary data. */
6578 for( x = 0; x < uxArraySize; x++ )
6580 switch( pxTaskStatusArray[ x ].eCurrentState )
6583 cStatus = tskRUNNING_CHAR;
6587 cStatus = tskREADY_CHAR;
6591 cStatus = tskBLOCKED_CHAR;
6595 cStatus = tskSUSPENDED_CHAR;
6599 cStatus = tskDELETED_CHAR;
6602 case eInvalid: /* Fall through. */
6603 default: /* Should not get here, but it is included
6604 * to prevent static checking errors. */
6605 cStatus = ( char ) 0x00;
6609 /* Write the task name to the string, padding with spaces so it
6610 * can be printed in tabular form more easily. */
6611 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
6613 /* Write the rest of the string. */
6614 sprintf( pcWriteBuffer, "\t%c\t%u\t%u\t%u\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( 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. */
6615 pcWriteBuffer += strlen( pcWriteBuffer ); /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
6618 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
6619 * is 0 then vPortFree() will be #defined to nothing. */
6620 vPortFree( pxTaskStatusArray );
6624 mtCOVERAGE_TEST_MARKER();
6628 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
6629 /*----------------------------------------------------------*/
6631 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
6633 void vTaskGetRunTimeStats( char * pcWriteBuffer )
6635 TaskStatus_t * pxTaskStatusArray;
6636 UBaseType_t uxArraySize, x;
6637 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
6642 * This function is provided for convenience only, and is used by many
6643 * of the demo applications. Do not consider it to be part of the
6646 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
6647 * of the uxTaskGetSystemState() output into a human readable table that
6648 * displays the amount of time each task has spent in the Running state
6649 * in both absolute and percentage terms.
6651 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
6652 * function that might bloat the code size, use a lot of stack, and
6653 * provide different results on different platforms. An alternative,
6654 * tiny, third party, and limited functionality implementation of
6655 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
6656 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
6657 * a full snprintf() implementation!).
6659 * It is recommended that production systems call uxTaskGetSystemState()
6660 * directly to get access to raw stats data, rather than indirectly
6661 * through a call to vTaskGetRunTimeStats().
6664 /* Make sure the write buffer does not contain a string. */
6665 *pcWriteBuffer = ( char ) 0x00;
6667 /* Take a snapshot of the number of tasks in case it changes while this
6668 * function is executing. */
6669 uxArraySize = uxCurrentNumberOfTasks;
6671 /* Allocate an array index for each task. NOTE! If
6672 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
6673 * equate to NULL. */
6674 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. */
6676 if( pxTaskStatusArray != NULL )
6678 /* Generate the (binary) data. */
6679 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
6681 /* For percentage calculations. */
6682 ulTotalTime /= 100UL;
6684 /* Avoid divide by zero errors. */
6685 if( ulTotalTime > 0UL )
6687 /* Create a human readable table from the binary data. */
6688 for( x = 0; x < uxArraySize; x++ )
6690 /* What percentage of the total run time has the task used?
6691 * This will always be rounded down to the nearest integer.
6692 * ulTotalRunTime has already been divided by 100. */
6693 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
6695 /* Write the task name to the string, padding with
6696 * spaces so it can be printed in tabular form more
6698 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
6700 if( ulStatsAsPercentage > 0UL )
6702 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
6704 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
6708 /* sizeof( int ) == sizeof( long ) so a smaller
6709 * printf() library can be used. */
6710 sprintf( pcWriteBuffer, "\t%u\t\t%u%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter, ( 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. */
6716 /* If the percentage is zero here then the task has
6717 * consumed less than 1% of the total run time. */
6718 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
6720 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
6724 /* sizeof( int ) == sizeof( long ) so a smaller
6725 * printf() library can be used. */
6726 sprintf( pcWriteBuffer, "\t%u\t\t<1%%\r\n", ( 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. */
6731 pcWriteBuffer += strlen( pcWriteBuffer ); /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
6736 mtCOVERAGE_TEST_MARKER();
6739 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
6740 * is 0 then vPortFree() will be #defined to nothing. */
6741 vPortFree( pxTaskStatusArray );
6745 mtCOVERAGE_TEST_MARKER();
6749 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
6750 /*-----------------------------------------------------------*/
6752 TickType_t uxTaskResetEventItemValue( void )
6754 TickType_t uxReturn;
6756 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
6758 /* Reset the event list item to its normal value - so it can be used with
6759 * queues and semaphores. */
6760 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. */
6764 /*-----------------------------------------------------------*/
6766 #if ( configUSE_MUTEXES == 1 )
6768 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
6772 pxTCB = pxCurrentTCB;
6774 /* If xSemaphoreCreateMutex() is called before any tasks have been created
6775 * then pxCurrentTCB will be NULL. */
6778 ( pxTCB->uxMutexesHeld )++;
6784 #endif /* configUSE_MUTEXES */
6785 /*-----------------------------------------------------------*/
6787 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6789 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
6790 BaseType_t xClearCountOnExit,
6791 TickType_t xTicksToWait )
6795 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6797 taskENTER_CRITICAL();
6799 /* Only block if the notification count is not already non-zero. */
6800 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0UL )
6802 /* Mark this task as waiting for a notification. */
6803 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
6805 if( xTicksToWait > ( TickType_t ) 0 )
6807 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
6808 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
6810 /* All ports are written to allow a yield in a critical
6811 * section (some will yield immediately, others wait until the
6812 * critical section exits) - but it is not something that
6813 * application code should ever do. */
6814 #if ( configNUMBER_OF_CORES == 1 )
6816 portYIELD_WITHIN_API();
6820 vTaskYieldWithinAPI();
6826 mtCOVERAGE_TEST_MARKER();
6831 mtCOVERAGE_TEST_MARKER();
6834 taskEXIT_CRITICAL();
6836 taskENTER_CRITICAL();
6838 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
6839 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
6841 if( ulReturn != 0UL )
6843 if( xClearCountOnExit != pdFALSE )
6845 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = 0UL;
6849 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
6854 mtCOVERAGE_TEST_MARKER();
6857 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
6859 taskEXIT_CRITICAL();
6864 #endif /* configUSE_TASK_NOTIFICATIONS */
6865 /*-----------------------------------------------------------*/
6867 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6869 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
6870 uint32_t ulBitsToClearOnEntry,
6871 uint32_t ulBitsToClearOnExit,
6872 uint32_t * pulNotificationValue,
6873 TickType_t xTicksToWait )
6877 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6879 taskENTER_CRITICAL();
6881 /* Only block if a notification is not already pending. */
6882 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
6884 /* Clear bits in the task's notification value as bits may get
6885 * set by the notifying task or interrupt. This can be used to
6886 * clear the value to zero. */
6887 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
6889 /* Mark this task as waiting for a notification. */
6890 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
6892 if( xTicksToWait > ( TickType_t ) 0 )
6894 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
6895 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
6897 /* All ports are written to allow a yield in a critical
6898 * section (some will yield immediately, others wait until the
6899 * critical section exits) - but it is not something that
6900 * application code should ever do. */
6901 #if ( configNUMBER_OF_CORES == 1 )
6903 portYIELD_WITHIN_API();
6907 vTaskYieldWithinAPI();
6913 mtCOVERAGE_TEST_MARKER();
6918 mtCOVERAGE_TEST_MARKER();
6921 taskEXIT_CRITICAL();
6923 taskENTER_CRITICAL();
6925 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
6927 if( pulNotificationValue != NULL )
6929 /* Output the current notification value, which may or may not
6931 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
6934 /* If ucNotifyValue is set then either the task never entered the
6935 * blocked state (because a notification was already pending) or the
6936 * task unblocked because of a notification. Otherwise the task
6937 * unblocked because of a timeout. */
6938 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
6940 /* A notification was not received. */
6945 /* A notification was already pending or a notification was
6946 * received while the task was waiting. */
6947 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
6951 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
6953 taskEXIT_CRITICAL();
6958 #endif /* configUSE_TASK_NOTIFICATIONS */
6959 /*-----------------------------------------------------------*/
6961 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6963 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
6964 UBaseType_t uxIndexToNotify,
6966 eNotifyAction eAction,
6967 uint32_t * pulPreviousNotificationValue )
6970 BaseType_t xReturn = pdPASS;
6971 uint8_t ucOriginalNotifyState;
6973 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6974 configASSERT( xTaskToNotify );
6975 pxTCB = xTaskToNotify;
6977 taskENTER_CRITICAL();
6979 if( pulPreviousNotificationValue != NULL )
6981 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
6984 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
6986 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
6991 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
6995 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
6998 case eSetValueWithOverwrite:
6999 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7002 case eSetValueWithoutOverwrite:
7004 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7006 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7010 /* The value could not be written to the task. */
7018 /* The task is being notified without its notify value being
7024 /* Should not get here if all enums are handled.
7025 * Artificially force an assert by testing a value the
7026 * compiler can't assume is const. */
7027 configASSERT( xTickCount == ( TickType_t ) 0 );
7032 traceTASK_NOTIFY( uxIndexToNotify );
7034 /* If the task is in the blocked state specifically to wait for a
7035 * notification then unblock it now. */
7036 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7038 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7039 prvAddTaskToReadyList( pxTCB );
7041 /* The task should not have been on an event list. */
7042 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7044 #if ( configUSE_TICKLESS_IDLE != 0 )
7046 /* If a task is blocked waiting for a notification then
7047 * xNextTaskUnblockTime might be set to the blocked task's time
7048 * out time. If the task is unblocked for a reason other than
7049 * a timeout xNextTaskUnblockTime is normally left unchanged,
7050 * because it will automatically get reset to a new value when
7051 * the tick count equals xNextTaskUnblockTime. However if
7052 * tickless idling is used it might be more important to enter
7053 * sleep mode at the earliest possible time - so reset
7054 * xNextTaskUnblockTime here to ensure it is updated at the
7055 * earliest possible time. */
7056 prvResetNextTaskUnblockTime();
7060 /* Check if the notified task has a priority above the currently
7061 * executing task. */
7062 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7066 mtCOVERAGE_TEST_MARKER();
7069 taskEXIT_CRITICAL();
7074 #endif /* configUSE_TASK_NOTIFICATIONS */
7075 /*-----------------------------------------------------------*/
7077 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7079 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
7080 UBaseType_t uxIndexToNotify,
7082 eNotifyAction eAction,
7083 uint32_t * pulPreviousNotificationValue,
7084 BaseType_t * pxHigherPriorityTaskWoken )
7087 uint8_t ucOriginalNotifyState;
7088 BaseType_t xReturn = pdPASS;
7089 UBaseType_t uxSavedInterruptStatus;
7091 configASSERT( xTaskToNotify );
7092 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7094 /* RTOS ports that support interrupt nesting have the concept of a
7095 * maximum system call (or maximum API call) interrupt priority.
7096 * Interrupts that are above the maximum system call priority are keep
7097 * permanently enabled, even when the RTOS kernel is in a critical section,
7098 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7099 * is defined in FreeRTOSConfig.h then
7100 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7101 * failure if a FreeRTOS API function is called from an interrupt that has
7102 * been assigned a priority above the configured maximum system call
7103 * priority. Only FreeRTOS functions that end in FromISR can be called
7104 * from interrupts that have been assigned a priority at or (logically)
7105 * below the maximum system call interrupt priority. FreeRTOS maintains a
7106 * separate interrupt safe API to ensure interrupt entry is as fast and as
7107 * simple as possible. More information (albeit Cortex-M specific) is
7108 * provided on the following link:
7109 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7110 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7112 pxTCB = xTaskToNotify;
7114 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7116 if( pulPreviousNotificationValue != NULL )
7118 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7121 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7122 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7127 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7131 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7134 case eSetValueWithOverwrite:
7135 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7138 case eSetValueWithoutOverwrite:
7140 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7142 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7146 /* The value could not be written to the task. */
7154 /* The task is being notified without its notify value being
7160 /* Should not get here if all enums are handled.
7161 * Artificially force an assert by testing a value the
7162 * compiler can't assume is const. */
7163 configASSERT( xTickCount == ( TickType_t ) 0 );
7167 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
7169 /* If the task is in the blocked state specifically to wait for a
7170 * notification then unblock it now. */
7171 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7173 /* The task should not have been on an event list. */
7174 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7176 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
7178 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7179 prvAddTaskToReadyList( pxTCB );
7183 /* The delayed and ready lists cannot be accessed, so hold
7184 * this task pending until the scheduler is resumed. */
7185 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
7188 #if ( configNUMBER_OF_CORES == 1 )
7190 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7192 /* The notified task has a priority above the currently
7193 * executing task so a yield is required. */
7194 if( pxHigherPriorityTaskWoken != NULL )
7196 *pxHigherPriorityTaskWoken = pdTRUE;
7199 /* Mark that a yield is pending in case the user is not
7200 * using the "xHigherPriorityTaskWoken" parameter to an ISR
7201 * safe FreeRTOS function. */
7202 xYieldPendings[ 0 ] = pdTRUE;
7206 mtCOVERAGE_TEST_MARKER();
7209 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
7211 #if ( configUSE_PREEMPTION == 1 )
7213 prvYieldForTask( pxTCB );
7215 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
7217 if( pxHigherPriorityTaskWoken != NULL )
7219 *pxHigherPriorityTaskWoken = pdTRUE;
7223 #endif /* if ( configUSE_PREEMPTION == 1 ) */
7225 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7228 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
7233 #endif /* configUSE_TASK_NOTIFICATIONS */
7234 /*-----------------------------------------------------------*/
7236 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7238 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
7239 UBaseType_t uxIndexToNotify,
7240 BaseType_t * pxHigherPriorityTaskWoken )
7243 uint8_t ucOriginalNotifyState;
7244 UBaseType_t uxSavedInterruptStatus;
7246 configASSERT( xTaskToNotify );
7247 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7249 /* RTOS ports that support interrupt nesting have the concept of a
7250 * maximum system call (or maximum API call) interrupt priority.
7251 * Interrupts that are above the maximum system call priority are keep
7252 * permanently enabled, even when the RTOS kernel is in a critical section,
7253 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7254 * is defined in FreeRTOSConfig.h then
7255 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7256 * failure if a FreeRTOS API function is called from an interrupt that has
7257 * been assigned a priority above the configured maximum system call
7258 * priority. Only FreeRTOS functions that end in FromISR can be called
7259 * from interrupts that have been assigned a priority at or (logically)
7260 * below the maximum system call interrupt priority. FreeRTOS maintains a
7261 * separate interrupt safe API to ensure interrupt entry is as fast and as
7262 * simple as possible. More information (albeit Cortex-M specific) is
7263 * provided on the following link:
7264 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7265 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7267 pxTCB = xTaskToNotify;
7269 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7271 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7272 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7274 /* 'Giving' is equivalent to incrementing a count in a counting
7276 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7278 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
7280 /* If the task is in the blocked state specifically to wait for a
7281 * notification then unblock it now. */
7282 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7284 /* The task should not have been on an event list. */
7285 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7287 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
7289 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7290 prvAddTaskToReadyList( pxTCB );
7294 /* The delayed and ready lists cannot be accessed, so hold
7295 * this task pending until the scheduler is resumed. */
7296 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
7299 #if ( configNUMBER_OF_CORES == 1 )
7301 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7303 /* The notified task has a priority above the currently
7304 * executing task so a yield is required. */
7305 if( pxHigherPriorityTaskWoken != NULL )
7307 *pxHigherPriorityTaskWoken = pdTRUE;
7310 /* Mark that a yield is pending in case the user is not
7311 * using the "xHigherPriorityTaskWoken" parameter in an ISR
7312 * safe FreeRTOS function. */
7313 xYieldPendings[ 0 ] = pdTRUE;
7317 mtCOVERAGE_TEST_MARKER();
7320 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
7322 #if ( configUSE_PREEMPTION == 1 )
7324 prvYieldForTask( pxTCB );
7326 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
7328 if( pxHigherPriorityTaskWoken != NULL )
7330 *pxHigherPriorityTaskWoken = pdTRUE;
7334 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
7336 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7339 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
7342 #endif /* configUSE_TASK_NOTIFICATIONS */
7343 /*-----------------------------------------------------------*/
7345 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7347 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
7348 UBaseType_t uxIndexToClear )
7353 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7355 /* If null is passed in here then it is the calling task that is having
7356 * its notification state cleared. */
7357 pxTCB = prvGetTCBFromHandle( xTask );
7359 taskENTER_CRITICAL();
7361 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
7363 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
7371 taskEXIT_CRITICAL();
7376 #endif /* configUSE_TASK_NOTIFICATIONS */
7377 /*-----------------------------------------------------------*/
7379 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7381 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
7382 UBaseType_t uxIndexToClear,
7383 uint32_t ulBitsToClear )
7388 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7390 /* If null is passed in here then it is the calling task that is having
7391 * its notification state cleared. */
7392 pxTCB = prvGetTCBFromHandle( xTask );
7394 taskENTER_CRITICAL();
7396 /* Return the notification as it was before the bits were cleared,
7397 * then clear the bit mask. */
7398 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
7399 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
7401 taskEXIT_CRITICAL();
7406 #endif /* configUSE_TASK_NOTIFICATIONS */
7407 /*-----------------------------------------------------------*/
7409 #if ( configGENERATE_RUN_TIME_STATS == 1 )
7411 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
7415 pxTCB = prvGetTCBFromHandle( xTask );
7417 return pxTCB->ulRunTimeCounter;
7420 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
7421 /*-----------------------------------------------------------*/
7423 #if ( configGENERATE_RUN_TIME_STATS == 1 )
7425 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
7428 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
7430 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
7432 /* For percentage calculations. */
7433 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
7435 /* Avoid divide by zero errors. */
7436 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
7438 pxTCB = prvGetTCBFromHandle( xTask );
7439 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
7449 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
7450 /*-----------------------------------------------------------*/
7452 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
7454 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
7456 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
7459 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
7461 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
7467 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
7468 /*-----------------------------------------------------------*/
7470 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
7472 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
7474 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
7475 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
7478 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
7480 /* For percentage calculations. */
7481 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
7483 /* Avoid divide by zero errors. */
7484 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
7486 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
7488 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
7491 ulReturn = ulRunTimeCounter / ulTotalTime;
7501 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
7502 /*-----------------------------------------------------------*/
7504 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
7505 const BaseType_t xCanBlockIndefinitely )
7507 TickType_t xTimeToWake;
7508 const TickType_t xConstTickCount = xTickCount;
7510 #if ( INCLUDE_xTaskAbortDelay == 1 )
7512 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
7513 * reset to pdFALSE so it can be detected as having been set to pdTRUE
7514 * when the task leaves the Blocked state. */
7515 pxCurrentTCB->ucDelayAborted = pdFALSE;
7519 /* Remove the task from the ready list before adding it to the blocked list
7520 * as the same list item is used for both lists. */
7521 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
7523 /* The current task must be in a ready list, so there is no need to
7524 * check, and the port reset macro can be called directly. */
7525 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. */
7529 mtCOVERAGE_TEST_MARKER();
7532 #if ( INCLUDE_vTaskSuspend == 1 )
7534 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
7536 /* Add the task to the suspended task list instead of a delayed task
7537 * list to ensure it is not woken by a timing event. It will block
7539 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
7543 /* Calculate the time at which the task should be woken if the event
7544 * does not occur. This may overflow but this doesn't matter, the
7545 * kernel will manage it correctly. */
7546 xTimeToWake = xConstTickCount + xTicksToWait;
7548 /* The list item will be inserted in wake time order. */
7549 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
7551 if( xTimeToWake < xConstTickCount )
7553 /* Wake time has overflowed. Place this item in the overflow
7555 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
7556 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7560 /* The wake time has not overflowed, so the current block list
7562 traceMOVED_TASK_TO_DELAYED_LIST();
7563 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7565 /* If the task entering the blocked state was placed at the
7566 * head of the list of blocked tasks then xNextTaskUnblockTime
7567 * needs to be updated too. */
7568 if( xTimeToWake < xNextTaskUnblockTime )
7570 xNextTaskUnblockTime = xTimeToWake;
7574 mtCOVERAGE_TEST_MARKER();
7579 #else /* INCLUDE_vTaskSuspend */
7581 /* Calculate the time at which the task should be woken if the event
7582 * does not occur. This may overflow but this doesn't matter, the kernel
7583 * will manage it correctly. */
7584 xTimeToWake = xConstTickCount + xTicksToWait;
7586 /* The list item will be inserted in wake time order. */
7587 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
7589 if( xTimeToWake < xConstTickCount )
7591 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
7592 /* Wake time has overflowed. Place this item in the overflow list. */
7593 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7597 traceMOVED_TASK_TO_DELAYED_LIST();
7598 /* The wake time has not overflowed, so the current block list is used. */
7599 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7601 /* If the task entering the blocked state was placed at the head of the
7602 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
7604 if( xTimeToWake < xNextTaskUnblockTime )
7606 xNextTaskUnblockTime = xTimeToWake;
7610 mtCOVERAGE_TEST_MARKER();
7614 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
7615 ( void ) xCanBlockIndefinitely;
7617 #endif /* INCLUDE_vTaskSuspend */
7619 /*-----------------------------------------------------------*/
7621 #if ( portUSING_MPU_WRAPPERS == 1 )
7623 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
7627 pxTCB = prvGetTCBFromHandle( xTask );
7629 return &( pxTCB->xMPUSettings );
7632 #endif /* portUSING_MPU_WRAPPERS */
7633 /*-----------------------------------------------------------*/
7635 /* Code below here allows additional code to be inserted into this source file,
7636 * especially where access to file scope functions and data is needed (for example
7637 * when performing module tests). */
7639 #ifdef FREERTOS_MODULE_TEST
7640 #include "tasks_test_access_functions.h"
7644 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
7646 #include "freertos_tasks_c_additions.h"
7648 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
7649 static void freertos_tasks_c_additions_init( void )
7651 FREERTOS_TASKS_C_ADDITIONS_INIT();
7655 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */