2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /* Standard includes. */
33 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
34 * all the API functions to use the MPU wrappers. That should only be done when
35 * task.h is included from an application file. */
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
38 /* FreeRTOS includes. */
42 #include "stack_macros.h"
44 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
45 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
46 * for the header files above, but not in this file, in order to generate the
47 * correct privileged Vs unprivileged linkage and placement. */
48 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
50 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
51 * functions but without including stdio.h here. */
52 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
54 /* At the bottom of this file are two optional functions that can be used
55 * to generate human readable text from the raw data generated by the
56 * uxTaskGetSystemState() function. Note the formatting functions are provided
57 * for convenience only, and are NOT considered part of the kernel. */
59 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
61 #if ( configUSE_PREEMPTION == 0 )
63 /* If the cooperative scheduler is being used then a yield should not be
64 * performed just because a higher priority task has been woken. */
65 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB )
66 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB )
69 #if ( configNUMBER_OF_CORES == 1 )
71 /* This macro requests the running task pxTCB to yield. In single core
72 * scheduler, a running task always runs on core 0 and portYIELD_WITHIN_API()
73 * can be used to request the task running on core 0 to yield. Therefore, pxTCB
74 * is not used in this macro. */
75 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) \
78 portYIELD_WITHIN_API(); \
81 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \
83 if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \
85 portYIELD_WITHIN_API(); \
89 mtCOVERAGE_TEST_MARKER(); \
93 #else /* if ( configNUMBER_OF_CORES == 1 ) */
95 /* Yield the core on which this task is running. */
96 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldCore( ( pxTCB )->xTaskRunState )
98 /* Yield for the task if a running task has priority lower than this task. */
99 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldForTask( pxTCB )
101 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
103 #endif /* if ( configUSE_PREEMPTION == 0 ) */
105 /* Values that can be assigned to the ucNotifyState member of the TCB. */
106 #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
107 #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
108 #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
111 * The value used to fill the stack of a task when the task is created. This
112 * is used purely for checking the high water mark for tasks.
114 #define tskSTACK_FILL_BYTE ( 0xa5U )
116 /* Bits used to record how a task's stack and TCB were allocated. */
117 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
118 #define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
119 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
121 /* If any of the following are set then task stacks are filled with a known
122 * value so the high water mark can be determined. If none of the following are
123 * set then don't fill the stack so there is no unnecessary dependency on memset. */
124 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
125 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
127 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
131 * Macros used by vListTask to indicate which state a task is in.
133 #define tskRUNNING_CHAR ( 'X' )
134 #define tskBLOCKED_CHAR ( 'B' )
135 #define tskREADY_CHAR ( 'R' )
136 #define tskDELETED_CHAR ( 'D' )
137 #define tskSUSPENDED_CHAR ( 'S' )
140 * Some kernel aware debuggers require the data the debugger needs access to to
141 * be global, rather than file scope.
143 #ifdef portREMOVE_STATIC_QUALIFIER
147 /* The name allocated to the Idle task. This can be overridden by defining
148 * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
149 #ifndef configIDLE_TASK_NAME
150 #define configIDLE_TASK_NAME "IDLE"
153 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
155 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
156 * performed in a generic way that is not optimised to any particular
157 * microcontroller architecture. */
159 /* uxTopReadyPriority holds the priority of the highest priority ready
161 #define taskRECORD_READY_PRIORITY( uxPriority ) \
163 if( ( uxPriority ) > uxTopReadyPriority ) \
165 uxTopReadyPriority = ( uxPriority ); \
167 } while( 0 ) /* taskRECORD_READY_PRIORITY */
169 /*-----------------------------------------------------------*/
171 #if ( configNUMBER_OF_CORES == 1 )
172 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
174 UBaseType_t uxTopPriority = uxTopReadyPriority; \
176 /* Find the highest priority queue that contains ready tasks. */ \
177 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
179 configASSERT( uxTopPriority ); \
183 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
184 * the same priority get an equal share of the processor time. */ \
185 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
186 uxTopReadyPriority = uxTopPriority; \
187 } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */
188 #else /* if ( configNUMBER_OF_CORES == 1 ) */
190 #define taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID ) prvSelectHighestPriorityTask( xCoreID )
192 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
194 /*-----------------------------------------------------------*/
196 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
197 * they are only required when a port optimised method of task selection is
199 #define taskRESET_READY_PRIORITY( uxPriority )
200 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
202 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
204 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
205 * performed in a way that is tailored to the particular microcontroller
206 * architecture being used. */
208 /* A port optimised version is provided. Call the port defined macros. */
209 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( ( uxPriority ), uxTopReadyPriority )
211 /*-----------------------------------------------------------*/
213 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
215 UBaseType_t uxTopPriority; \
217 /* Find the highest priority list that contains ready tasks. */ \
218 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
219 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
220 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
223 /*-----------------------------------------------------------*/
225 /* A port optimised version is provided, call it only if the TCB being reset
226 * is being referenced from a ready list. If it is referenced from a delayed
227 * or suspended list then it won't be in a ready list. */
228 #define taskRESET_READY_PRIORITY( uxPriority ) \
230 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
232 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
236 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
238 /*-----------------------------------------------------------*/
240 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
241 * count overflows. */
242 #define taskSWITCH_DELAYED_LISTS() \
246 /* The delayed tasks list should be empty when the lists are switched. */ \
247 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
249 pxTemp = pxDelayedTaskList; \
250 pxDelayedTaskList = pxOverflowDelayedTaskList; \
251 pxOverflowDelayedTaskList = pxTemp; \
253 prvResetNextTaskUnblockTime(); \
256 /*-----------------------------------------------------------*/
259 * Place the task represented by pxTCB into the appropriate ready list for
260 * the task. It is inserted at the end of the list.
262 #define prvAddTaskToReadyList( pxTCB ) \
264 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
265 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
266 listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
267 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB ); \
269 /*-----------------------------------------------------------*/
272 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
273 * where NULL is used to indicate that the handle of the currently executing
274 * task should be used in place of the parameter. This macro simply checks to
275 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
277 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
279 /* The item value of the event list item is normally used to hold the priority
280 * of the task to which it belongs (coded to allow it to be held in reverse
281 * priority order). However, it is occasionally borrowed for other purposes. It
282 * is important its value is not updated due to a task priority change while it is
283 * being used for another purpose. The following bit definition is used to inform
284 * the scheduler that the value should not be changed - in which case it is the
285 * responsibility of whichever module is using the value to ensure it gets set back
286 * to its original value when it is released. */
287 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
288 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U
289 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
290 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
291 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
292 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000000000000000ULL
295 /* Indicates that the task is not actively running on any core. */
296 #define taskTASK_NOT_RUNNING ( ( BaseType_t ) ( -1 ) )
298 /* Indicates that the task is actively running but scheduled to yield. */
299 #define taskTASK_SCHEDULED_TO_YIELD ( ( BaseType_t ) ( -2 ) )
301 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
302 #if ( configNUMBER_OF_CORES == 1 )
303 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
304 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
306 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) )
307 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB )->xTaskRunState != taskTASK_NOT_RUNNING ) ? ( pdTRUE ) : ( pdFALSE ) )
310 /* Indicates that the task is an Idle task. */
311 #define taskATTRIBUTE_IS_IDLE ( UBaseType_t ) ( 1UL << 0UL )
313 #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) )
314 #define portGET_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting )
315 #define portSET_CRITICAL_NESTING_COUNT( x ) ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting = ( x ) )
316 #define portINCREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting++ )
317 #define portDECREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting-- )
318 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) */
320 #define taskBITS_PER_BYTE ( ( size_t ) 8 )
322 #if ( configNUMBER_OF_CORES > 1 )
324 /* Yields the given core. This must be called from a critical section and xCoreID
325 * must be valid. This macro is not required in single core since there is only
326 * one core to yield. */
327 #define prvYieldCore( xCoreID ) \
329 if( xCoreID == ( BaseType_t ) portGET_CORE_ID() ) \
331 /* Pending a yield for this core since it is in the critical section. */ \
332 xYieldPendings[ xCoreID ] = pdTRUE; \
336 /* Request other core to yield if it is not requested before. */ \
337 if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD ) \
339 portYIELD_CORE( xCoreID ); \
340 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_SCHEDULED_TO_YIELD; \
344 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
345 /*-----------------------------------------------------------*/
348 * Task control block. A task control block (TCB) is allocated for each task,
349 * and stores task state information, including a pointer to the task's context
350 * (the task's run time environment, including register values)
352 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
354 volatile StackType_t * pxTopOfStack; /**< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
356 #if ( portUSING_MPU_WRAPPERS == 1 )
357 xMPU_SETTINGS xMPUSettings; /**< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
360 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
361 UBaseType_t uxCoreAffinityMask; /**< Used to link the task to certain cores. UBaseType_t must have greater than or equal to the number of bits as configNUMBER_OF_CORES. */
364 ListItem_t xStateListItem; /**< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
365 ListItem_t xEventListItem; /**< Used to reference a task from an event list. */
366 UBaseType_t uxPriority; /**< The priority of the task. 0 is the lowest priority. */
367 StackType_t * pxStack; /**< Points to the start of the stack. */
368 #if ( configNUMBER_OF_CORES > 1 )
369 volatile BaseType_t xTaskRunState; /**< Used to identify the core the task is running on, if the task is running. Otherwise, identifies the task's state - not running or yielding. */
370 UBaseType_t uxTaskAttributes; /**< Task's attributes - currently used to identify the idle tasks. */
372 char pcTaskName[ configMAX_TASK_NAME_LEN ]; /**< Descriptive name given to the task when created. Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
374 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
375 BaseType_t xPreemptionDisable; /**< Used to prevent the task from being preempted. */
378 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
379 StackType_t * pxEndOfStack; /**< Points to the highest valid address for the stack. */
382 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
383 UBaseType_t uxCriticalNesting; /**< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
386 #if ( configUSE_TRACE_FACILITY == 1 )
387 UBaseType_t uxTCBNumber; /**< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
388 UBaseType_t uxTaskNumber; /**< Stores a number specifically for use by third party trace code. */
391 #if ( configUSE_MUTEXES == 1 )
392 UBaseType_t uxBasePriority; /**< The priority last assigned to the task - used by the priority inheritance mechanism. */
393 UBaseType_t uxMutexesHeld;
396 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
397 TaskHookFunction_t pxTaskTag;
400 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
401 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
404 #if ( configGENERATE_RUN_TIME_STATS == 1 )
405 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /**< Stores the amount of time the task has spent in the Running state. */
408 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
409 configTLS_BLOCK_TYPE xTLSBlock; /**< Memory block used as Thread Local Storage (TLS) Block for the task. */
412 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
413 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
414 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
417 /* See the comments in FreeRTOS.h with the definition of
418 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
419 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
420 uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
423 #if ( INCLUDE_xTaskAbortDelay == 1 )
424 uint8_t ucDelayAborted;
427 #if ( configUSE_POSIX_ERRNO == 1 )
432 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
433 * below to enable the use of older kernel aware debuggers. */
434 typedef tskTCB TCB_t;
436 /*lint -save -e956 A manual analysis and inspection has been used to determine
437 * which static variables must be declared volatile. */
438 #if ( configNUMBER_OF_CORES == 1 )
439 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
441 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
442 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
443 /* coverity[misra_c_2012_rule_8_4_violation] */
444 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ];
445 #define pxCurrentTCB xTaskGetCurrentTaskHandle()
448 /* Lists for ready and blocked tasks. --------------------
449 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
450 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
451 * the static qualifier. */
452 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
453 PRIVILEGED_DATA static List_t xDelayedTaskList1; /**< Delayed tasks. */
454 PRIVILEGED_DATA static List_t xDelayedTaskList2; /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
455 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /**< Points to the delayed task list currently being used. */
456 PRIVILEGED_DATA static List_t * volatile pxOverflowDelayedTaskList; /**< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
457 PRIVILEGED_DATA static List_t xPendingReadyList; /**< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready list when the scheduler is resumed. */
459 #if ( INCLUDE_vTaskDelete == 1 )
461 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
462 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
466 #if ( INCLUDE_vTaskSuspend == 1 )
468 PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
472 /* Global POSIX errno. Its value is changed upon context switching to match
473 * the errno of the currently running task. */
474 #if ( configUSE_POSIX_ERRNO == 1 )
475 int FreeRTOS_errno = 0;
478 /* Other file private variables. --------------------------------*/
479 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
480 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
481 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
482 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
483 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
484 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
485 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
486 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
487 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
488 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandles[ configNUMBER_OF_CORES ]; /**< Holds the handles of the idle tasks. The idle tasks are created automatically when the scheduler is started. */
490 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
491 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
492 * to determine the number of priority lists to read back from the remote target. */
493 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
495 /* Context switches are held pending while the scheduler is suspended. Also,
496 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
497 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
498 * If an interrupt needs to unblock a task while the scheduler is suspended then it
499 * moves the task's event list item into the xPendingReadyList, ready for the
500 * kernel to move the task from the pending ready list into the real ready list
501 * when the scheduler is unsuspended. The pending ready list itself can only be
502 * accessed from a critical section.
504 * Updates to uxSchedulerSuspended must be protected by both the task lock and the ISR lock
505 * and must not be done from an ISR. Reads must be protected by either lock and may be done
506 * from either an ISR or a task. */
507 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
509 #if ( configGENERATE_RUN_TIME_STATS == 1 )
511 /* Do not move these variables to function scope as doing so prevents the
512 * code working with debuggers that need to remove the static qualifier. */
513 PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the value of a timer/counter the last time a task was switched in. */
514 PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the total amount of execution time as defined by the run time counter clock. */
518 #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configNUMBER_OF_CORES > 1 )
520 /* Do not move these variables to function scope as doing so prevents the
521 * code working with debuggers that need to remove the static qualifier. */
522 static StaticTask_t xIdleTCBBuffers[ configNUMBER_OF_CORES - 1 ];
523 static StackType_t xIdleTaskStackBuffers[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
525 #endif /* #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
529 /*-----------------------------------------------------------*/
531 /* File private functions. --------------------------------*/
534 * Creates the idle tasks during scheduler start.
536 static BaseType_t prvCreateIdleTasks( void );
538 #if ( configNUMBER_OF_CORES > 1 )
541 * Checks to see if another task moved the current task out of the ready
542 * list while it was waiting to enter a critical section and yields, if so.
544 static void prvCheckForRunStateChange( void );
545 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
547 #if ( configNUMBER_OF_CORES > 1 )
550 * Yields a core, or cores if multiple priorities are not allowed to run
551 * simultaneously, to allow the task pxTCB to run.
553 static void prvYieldForTask( const TCB_t * pxTCB );
554 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
556 #if ( configNUMBER_OF_CORES > 1 )
559 * Selects the highest priority available task for the given core.
561 static void prvSelectHighestPriorityTask( BaseType_t xCoreID );
562 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
565 * Utility task that simply returns pdTRUE if the task referenced by xTask is
566 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
567 * is in any other state.
569 #if ( INCLUDE_vTaskSuspend == 1 )
571 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
573 #endif /* INCLUDE_vTaskSuspend */
576 * Utility to ready all the lists used by the scheduler. This is called
577 * automatically upon the creation of the first task.
579 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
582 * The idle task, which as all tasks is implemented as a never ending loop.
583 * The idle task is automatically created and added to the ready lists upon
584 * creation of the first user task.
586 * In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 minimal idle tasks are also
587 * created to ensure that each core has an idle task to run when no other
588 * task is available to run.
590 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
591 * language extensions. The equivalent prototype for these functions are:
593 * void prvIdleTask( void *pvParameters );
594 * void prvMinimalIdleTask( void *pvParameters );
597 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
598 #if ( configNUMBER_OF_CORES > 1 )
599 static portTASK_FUNCTION_PROTO( prvMinimalIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
603 * Utility to free all memory allocated by the scheduler to hold a TCB,
604 * including the stack pointed to by the TCB.
606 * This does not free memory allocated by the task itself (i.e. memory
607 * allocated by calls to pvPortMalloc from within the tasks application code).
609 #if ( INCLUDE_vTaskDelete == 1 )
611 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
616 * Used only by the idle task. This checks to see if anything has been placed
617 * in the list of tasks waiting to be deleted. If so the task is cleaned up
618 * and its TCB deleted.
620 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
623 * The currently executing task is entering the Blocked state. Add the task to
624 * either the current or the overflow delayed task list.
626 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
627 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
630 * Fills an TaskStatus_t structure with information on each task that is
631 * referenced from the pxList list (which may be a ready list, a delayed list,
632 * a suspended list, etc.).
634 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
635 * NORMAL APPLICATION CODE.
637 #if ( configUSE_TRACE_FACILITY == 1 )
639 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
641 eTaskState eState ) PRIVILEGED_FUNCTION;
646 * Searches pxList for a task with name pcNameToQuery - returning a handle to
647 * the task if it is found, or NULL if the task is not found.
649 #if ( INCLUDE_xTaskGetHandle == 1 )
651 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
652 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
657 * When a task is created, the stack of the task is filled with a known value.
658 * This function determines the 'high water mark' of the task stack by
659 * determining how much of the stack remains at the original preset value.
661 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
663 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
668 * Return the amount of time, in ticks, that will pass before the kernel will
669 * next move a task from the Blocked state to the Running state.
671 * This conditional compilation should use inequality to 0, not equality to 1.
672 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
673 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
674 * set to a value other than 1.
676 #if ( configUSE_TICKLESS_IDLE != 0 )
678 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
683 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
684 * will exit the Blocked state.
686 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
688 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
691 * Helper function used to pad task names with spaces when printing out
692 * human readable tables of task information.
694 static char * prvWriteNameToBuffer( char * pcBuffer,
695 const char * pcTaskName ) PRIVILEGED_FUNCTION;
700 * Called after a Task_t structure has been allocated either statically or
701 * dynamically to fill in the structure's members.
703 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
704 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
705 const uint32_t ulStackDepth,
706 void * const pvParameters,
707 UBaseType_t uxPriority,
708 TaskHandle_t * const pxCreatedTask,
710 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
713 * Called after a new task has been created and initialised to place the task
714 * under the control of the scheduler.
716 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
719 * freertos_tasks_c_additions_init() should only be called if the user definable
720 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
721 * called by the function.
723 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
725 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
729 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
730 extern void vApplicationMinimalIdleHook( void );
731 #endif /* #if ( configUSE_MINIMAL_IDLE_HOOK == 1 ) */
733 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
736 * Convert the snprintf return value to the number of characters
737 * written. The following are the possible cases:
739 * 1. The buffer supplied to snprintf is large enough to hold the
740 * generated string. The return value in this case is the number
741 * of characters actually written, not counting the terminating
743 * 2. The buffer supplied to snprintf is NOT large enough to hold
744 * the generated string. The return value in this case is the
745 * number of characters that would have been written if the
746 * buffer had been sufficiently large, not counting the
747 * terminating null character.
748 * 3. Encoding error. The return value in this case is a negative
751 * From 1 and 2 above ==> Only when the return value is non-negative
752 * and less than the supplied buffer length, the string has been
753 * completely written.
755 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
758 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
759 /*-----------------------------------------------------------*/
761 #if ( configNUMBER_OF_CORES > 1 )
762 static void prvCheckForRunStateChange( void )
764 UBaseType_t uxPrevCriticalNesting;
765 const TCB_t * pxThisTCB;
767 /* This must only be called from within a task. */
768 portASSERT_IF_IN_ISR();
770 /* This function is always called with interrupts disabled
771 * so this is safe. */
772 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
774 while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD )
776 /* We are only here if we just entered a critical section
777 * or if we just suspended the scheduler, and another task
778 * has requested that we yield.
780 * This is slightly complicated since we need to save and restore
781 * the suspension and critical nesting counts, as well as release
782 * and reacquire the correct locks. And then, do it all over again
783 * if our state changed again during the reacquisition. */
784 uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
786 if( uxPrevCriticalNesting > 0U )
788 portSET_CRITICAL_NESTING_COUNT( 0U );
789 portRELEASE_ISR_LOCK();
793 /* The scheduler is suspended. uxSchedulerSuspended is updated
794 * only when the task is not requested to yield. */
795 mtCOVERAGE_TEST_MARKER();
798 portRELEASE_TASK_LOCK();
799 portMEMORY_BARRIER();
800 configASSERT( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD );
802 portENABLE_INTERRUPTS();
804 /* Enabling interrupts should cause this core to immediately
805 * service the pending interrupt and yield. If the run state is still
806 * yielding here then that is a problem. */
807 configASSERT( pxThisTCB->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD );
809 portDISABLE_INTERRUPTS();
813 portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
815 if( uxPrevCriticalNesting == 0U )
817 portRELEASE_ISR_LOCK();
821 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
823 /*-----------------------------------------------------------*/
825 #if ( configNUMBER_OF_CORES > 1 )
826 static void prvYieldForTask( const TCB_t * pxTCB )
828 BaseType_t xLowestPriorityToPreempt;
829 BaseType_t xCurrentCoreTaskPriority;
830 BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
833 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
834 BaseType_t xYieldCount = 0;
835 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
837 /* This must be called from a critical section. */
838 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
840 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
842 /* No task should yield for this one if it is a lower priority
843 * than priority level of currently ready tasks. */
844 if( pxTCB->uxPriority >= uxTopReadyPriority )
846 /* Yield is not required for a task which is already running. */
847 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
850 xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
852 /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
853 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
854 --xLowestPriorityToPreempt;
856 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
858 xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
860 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
861 if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
863 xCurrentCoreTaskPriority = xCurrentCoreTaskPriority - 1;
866 if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
868 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
869 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
872 if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
874 #if ( configUSE_CORE_AFFINITY == 1 )
875 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
878 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
879 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
882 xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
883 xLowestPriorityCore = xCoreID;
889 mtCOVERAGE_TEST_MARKER();
893 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
895 /* Yield all currently running non-idle tasks with a priority lower than
896 * the task that needs to run. */
897 if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
898 ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
900 prvYieldCore( xCoreID );
905 mtCOVERAGE_TEST_MARKER();
908 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
912 mtCOVERAGE_TEST_MARKER();
916 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
917 if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
918 #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
919 if( xLowestPriorityCore >= 0 )
920 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
922 prvYieldCore( xLowestPriorityCore );
925 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
926 /* Verify that the calling core always yields to higher priority tasks. */
927 if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0 ) &&
928 ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
930 configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) ||
931 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) );
936 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
937 /*-----------------------------------------------------------*/
939 #if ( configNUMBER_OF_CORES > 1 )
940 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
942 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
943 BaseType_t xTaskScheduled = pdFALSE;
944 BaseType_t xDecrementTopPriority = pdTRUE;
946 #if ( configUSE_CORE_AFFINITY == 1 )
947 const TCB_t * pxPreviousTCB = NULL;
949 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
950 BaseType_t xPriorityDropped = pdFALSE;
953 /* This function should be called when scheduler is running. */
954 configASSERT( xSchedulerRunning == pdTRUE );
956 /* A new task is created and a running task with the same priority yields
957 * itself to run the new task. When a running task yields itself, it is still
958 * in the ready list. This running task will be selected before the new task
959 * since the new task is always added to the end of the ready list.
960 * The other problem is that the running task still in the same position of
961 * the ready list when it yields itself. It is possible that it will be selected
962 * earlier then other tasks which waits longer than this task.
964 * To fix these problems, the running task should be put to the end of the
965 * ready list before searching for the ready task in the ready list. */
966 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
967 &pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
969 ( void ) uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
970 vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
971 &pxCurrentTCBs[ xCoreID ]->xStateListItem );
974 while( xTaskScheduled == pdFALSE )
976 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
978 if( uxCurrentPriority < uxTopReadyPriority )
980 /* We can't schedule any tasks, other than idle, that have a
981 * priority lower than the priority of a task currently running
982 * on another core. */
983 uxCurrentPriority = tskIDLE_PRIORITY;
988 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
990 const List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
991 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxReadyList );
992 ListItem_t * pxIterator;
994 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
995 * must not be decremented any further. */
996 xDecrementTopPriority = pdFALSE;
998 for( pxIterator = listGET_HEAD_ENTRY( pxReadyList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
1000 TCB_t * pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
1002 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1004 /* When falling back to the idle priority because only one priority
1005 * level is allowed to run at a time, we should ONLY schedule the true
1006 * idle tasks, not user tasks at the idle priority. */
1007 if( uxCurrentPriority < uxTopReadyPriority )
1009 if( ( pxTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0 )
1015 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1017 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
1019 #if ( configUSE_CORE_AFFINITY == 1 )
1020 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1023 /* If the task is not being executed by any core swap it in. */
1024 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
1025 #if ( configUSE_CORE_AFFINITY == 1 )
1026 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
1028 pxTCB->xTaskRunState = xCoreID;
1029 pxCurrentTCBs[ xCoreID ] = pxTCB;
1030 xTaskScheduled = pdTRUE;
1033 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
1035 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) );
1037 #if ( configUSE_CORE_AFFINITY == 1 )
1038 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1041 /* The task is already running on this core, mark it as scheduled. */
1042 pxTCB->xTaskRunState = xCoreID;
1043 xTaskScheduled = pdTRUE;
1048 /* This task is running on the core other than xCoreID. */
1049 mtCOVERAGE_TEST_MARKER();
1052 if( xTaskScheduled != pdFALSE )
1054 /* A task has been selected to run on this core. */
1061 if( xDecrementTopPriority != pdFALSE )
1063 uxTopReadyPriority--;
1064 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1066 xPriorityDropped = pdTRUE;
1072 /* There are configNUMBER_OF_CORES Idle tasks created when scheduler started.
1073 * The scheduler should be able to select a task to run when uxCurrentPriority
1074 * is tskIDLE_PRIORITY. uxCurrentPriority is never decreased to value blow
1075 * tskIDLE_PRIORITY. */
1076 if( uxCurrentPriority > tskIDLE_PRIORITY )
1078 uxCurrentPriority--;
1082 /* This function is called when idle task is not created. Break the
1083 * loop to prevent uxCurrentPriority overrun. */
1088 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1090 if( xTaskScheduled == pdTRUE )
1092 if( xPriorityDropped != pdFALSE )
1094 /* There may be several ready tasks that were being prevented from running because there was
1095 * a higher priority task running. Now that the last of the higher priority tasks is no longer
1096 * running, make sure all the other idle tasks yield. */
1099 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUMBER_OF_CORES; x++ )
1101 if( ( pxCurrentTCBs[ x ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0 )
1109 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1111 #if ( configUSE_CORE_AFFINITY == 1 )
1113 if( xTaskScheduled == pdTRUE )
1115 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
1117 /* A ready task was just evicted from this core. See if it can be
1118 * scheduled on any other core. */
1119 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
1120 BaseType_t xLowestPriority = ( BaseType_t ) pxPreviousTCB->uxPriority;
1121 BaseType_t xLowestPriorityCore = -1;
1124 if( ( pxPreviousTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1126 xLowestPriority = xLowestPriority - 1;
1129 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1131 /* pxPreviousTCB was removed from this core and this core is not excluded
1132 * from it's core affinity mask.
1134 * pxPreviousTCB is preempted by the new higher priority task
1135 * pxCurrentTCBs[ xCoreID ]. When searching a new core for pxPreviousTCB,
1136 * we do not need to look at the cores on which pxCurrentTCBs[ xCoreID ]
1137 * is allowed to run. The reason is - when more than one cores are
1138 * eligible for an incoming task, we preempt the core with the minimum
1139 * priority task. Because this core (i.e. xCoreID) was preempted for
1140 * pxCurrentTCBs[ xCoreID ], this means that all the others cores
1141 * where pxCurrentTCBs[ xCoreID ] can run, are running tasks with priority
1142 * no lower than pxPreviousTCB's priority. Therefore, the only cores where
1143 * which can be preempted for pxPreviousTCB are the ones where
1144 * pxCurrentTCBs[ xCoreID ] is not allowed to run (and obviously,
1145 * pxPreviousTCB is allowed to run).
1147 * This is an optimization which reduces the number of cores needed to be
1148 * searched for pxPreviousTCB to run. */
1149 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1153 /* pxPreviousTCB's core affinity mask is changed and it is no longer
1154 * allowed to run on this core. Searching all the cores in pxPreviousTCB's
1155 * new core affinity mask to find a core on which it can run. */
1158 uxCoreMap &= ( ( 1U << configNUMBER_OF_CORES ) - 1U );
1160 for( x = ( ( BaseType_t ) configNUMBER_OF_CORES - 1 ); x >= ( BaseType_t ) 0; x-- )
1162 UBaseType_t uxCore = ( UBaseType_t ) x;
1163 BaseType_t xTaskPriority;
1165 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << uxCore ) ) != 0U )
1167 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority;
1169 if( ( pxCurrentTCBs[ uxCore ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1171 xTaskPriority = xTaskPriority - ( BaseType_t ) 1;
1174 uxCoreMap &= ~( ( UBaseType_t ) 1U << uxCore );
1176 if( ( xTaskPriority < xLowestPriority ) &&
1177 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ] ) != pdFALSE ) &&
1178 ( xYieldPendings[ uxCore ] == pdFALSE ) )
1180 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1181 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1184 xLowestPriority = xTaskPriority;
1185 xLowestPriorityCore = ( BaseType_t ) uxCore;
1191 if( xLowestPriorityCore >= 0 )
1193 prvYieldCore( xLowestPriorityCore );
1198 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) */
1201 #endif /* ( configNUMBER_OF_CORES > 1 ) */
1203 /*-----------------------------------------------------------*/
1205 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1207 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1208 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1209 const uint32_t ulStackDepth,
1210 void * const pvParameters,
1211 UBaseType_t uxPriority,
1212 StackType_t * const puxStackBuffer,
1213 StaticTask_t * const pxTaskBuffer )
1214 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1216 return xTaskCreateStaticAffinitySet( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, tskNO_AFFINITY );
1219 TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1220 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1221 const uint32_t ulStackDepth,
1222 void * const pvParameters,
1223 UBaseType_t uxPriority,
1224 StackType_t * const puxStackBuffer,
1225 StaticTask_t * const pxTaskBuffer,
1226 UBaseType_t uxCoreAffinityMask )
1227 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1230 TaskHandle_t xReturn;
1232 traceENTER_xTaskCreateStatic( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer );
1234 configASSERT( puxStackBuffer != NULL );
1235 configASSERT( pxTaskBuffer != NULL );
1237 #if ( configASSERT_DEFINED == 1 )
1239 /* Sanity check that the size of the structure used to declare a
1240 * variable of type StaticTask_t equals the size of the real task
1242 volatile size_t xSize = sizeof( StaticTask_t );
1243 configASSERT( xSize == sizeof( TCB_t ) );
1244 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1246 #endif /* configASSERT_DEFINED */
1248 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1250 /* The memory used for the task's TCB and stack are passed into this
1251 * function - use them. */
1252 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. */
1253 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1254 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1256 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1258 /* Tasks can be created statically or dynamically, so note this
1259 * task was created statically in case the task is later deleted. */
1260 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1262 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1264 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1266 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1268 /* Set the task's affinity before scheduling it. */
1269 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1273 prvAddNewTaskToReadyList( pxNewTCB );
1280 traceRETURN_xTaskCreateStatic( xReturn );
1285 #endif /* SUPPORT_STATIC_ALLOCATION */
1286 /*-----------------------------------------------------------*/
1288 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1290 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1291 TaskHandle_t * pxCreatedTask )
1292 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1294 return xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1297 BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1298 UBaseType_t uxCoreAffinityMask,
1299 TaskHandle_t * pxCreatedTask )
1300 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1303 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1305 traceENTER_xTaskCreateRestrictedStatic( pxTaskDefinition, pxCreatedTask );
1307 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1308 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1310 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1312 /* Allocate space for the TCB. Where the memory comes from depends
1313 * on the implementation of the port malloc function and whether or
1314 * not static allocation is being used. */
1315 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1316 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1318 /* Store the stack location in the TCB. */
1319 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1321 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1323 /* Tasks can be created statically or dynamically, so note this
1324 * task was created statically in case the task is later deleted. */
1325 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1327 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1329 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1330 pxTaskDefinition->pcName,
1331 ( uint32_t ) pxTaskDefinition->usStackDepth,
1332 pxTaskDefinition->pvParameters,
1333 pxTaskDefinition->uxPriority,
1334 pxCreatedTask, pxNewTCB,
1335 pxTaskDefinition->xRegions );
1337 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1339 /* Set the task's affinity before scheduling it. */
1340 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1344 prvAddNewTaskToReadyList( pxNewTCB );
1348 traceRETURN_xTaskCreateRestrictedStatic( xReturn );
1353 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1354 /*-----------------------------------------------------------*/
1356 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1358 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1359 TaskHandle_t * pxCreatedTask )
1360 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1362 return xTaskCreateRestrictedAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1365 BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1366 UBaseType_t uxCoreAffinityMask,
1367 TaskHandle_t * pxCreatedTask )
1368 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1371 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1373 traceENTER_xTaskCreateRestricted( pxTaskDefinition, pxCreatedTask );
1375 configASSERT( pxTaskDefinition->puxStackBuffer );
1377 if( pxTaskDefinition->puxStackBuffer != NULL )
1379 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1381 if( pxNewTCB != NULL )
1383 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1385 /* Store the stack location in the TCB. */
1386 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1388 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1390 /* Tasks can be created statically or dynamically, so note
1391 * this task had a statically allocated stack in case it is
1392 * later deleted. The TCB was allocated dynamically. */
1393 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1395 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1397 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1398 pxTaskDefinition->pcName,
1399 ( uint32_t ) pxTaskDefinition->usStackDepth,
1400 pxTaskDefinition->pvParameters,
1401 pxTaskDefinition->uxPriority,
1402 pxCreatedTask, pxNewTCB,
1403 pxTaskDefinition->xRegions );
1405 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1407 /* Set the task's affinity before scheduling it. */
1408 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1412 prvAddNewTaskToReadyList( pxNewTCB );
1417 traceRETURN_xTaskCreateRestricted( xReturn );
1422 #endif /* portUSING_MPU_WRAPPERS */
1423 /*-----------------------------------------------------------*/
1425 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1427 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1428 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1429 const configSTACK_DEPTH_TYPE usStackDepth,
1430 void * const pvParameters,
1431 UBaseType_t uxPriority,
1432 TaskHandle_t * const pxCreatedTask )
1433 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1435 return xTaskCreateAffinitySet( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, tskNO_AFFINITY, pxCreatedTask );
1438 BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1439 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1440 const configSTACK_DEPTH_TYPE usStackDepth,
1441 void * const pvParameters,
1442 UBaseType_t uxPriority,
1443 UBaseType_t uxCoreAffinityMask,
1444 TaskHandle_t * const pxCreatedTask )
1445 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1450 traceENTER_xTaskCreate( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask );
1452 /* If the stack grows down then allocate the stack then the TCB so the stack
1453 * does not grow into the TCB. Likewise if the stack grows up then allocate
1454 * the TCB then the stack. */
1455 #if ( portSTACK_GROWTH > 0 )
1457 /* Allocate space for the TCB. Where the memory comes from depends on
1458 * the implementation of the port malloc function and whether or not static
1459 * allocation is being used. */
1460 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1462 if( pxNewTCB != NULL )
1464 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1466 /* Allocate space for the stack used by the task being created.
1467 * The base of the stack memory stored in the TCB so the task can
1468 * be deleted later if required. */
1469 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1471 if( pxNewTCB->pxStack == NULL )
1473 /* Could not allocate the stack. Delete the allocated TCB. */
1474 vPortFree( pxNewTCB );
1479 #else /* portSTACK_GROWTH */
1481 StackType_t * pxStack;
1483 /* Allocate space for the stack used by the task being created. */
1484 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. */
1486 if( pxStack != NULL )
1488 /* Allocate space for the TCB. */
1489 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. */
1491 if( pxNewTCB != NULL )
1493 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1495 /* Store the stack location in the TCB. */
1496 pxNewTCB->pxStack = pxStack;
1500 /* The stack cannot be used as the TCB was not created. Free
1502 vPortFreeStack( pxStack );
1510 #endif /* portSTACK_GROWTH */
1512 if( pxNewTCB != NULL )
1514 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1516 /* Tasks can be created statically or dynamically, so note this
1517 * task was created dynamically in case it is later deleted. */
1518 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1520 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1522 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1524 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1526 /* Set the task's affinity before scheduling it. */
1527 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1531 prvAddNewTaskToReadyList( pxNewTCB );
1536 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1539 traceRETURN_xTaskCreate( xReturn );
1544 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1545 /*-----------------------------------------------------------*/
1547 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1548 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1549 const uint32_t ulStackDepth,
1550 void * const pvParameters,
1551 UBaseType_t uxPriority,
1552 TaskHandle_t * const pxCreatedTask,
1554 const MemoryRegion_t * const xRegions )
1556 StackType_t * pxTopOfStack;
1559 #if ( portUSING_MPU_WRAPPERS == 1 )
1560 /* Should the task be created in privileged mode? */
1561 BaseType_t xRunPrivileged;
1563 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1565 xRunPrivileged = pdTRUE;
1569 xRunPrivileged = pdFALSE;
1571 uxPriority &= ~portPRIVILEGE_BIT;
1572 #endif /* portUSING_MPU_WRAPPERS == 1 */
1574 /* Avoid dependency on memset() if it is not required. */
1575 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1577 /* Fill the stack with a known value to assist debugging. */
1578 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1580 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1582 /* Calculate the top of stack address. This depends on whether the stack
1583 * grows from high memory to low (as per the 80x86) or vice versa.
1584 * portSTACK_GROWTH is used to make the result positive or negative as required
1586 #if ( portSTACK_GROWTH < 0 )
1588 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1589 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(). */
1591 /* Check the alignment of the calculated top of stack is correct. */
1592 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1594 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1596 /* Also record the stack's high address, which may assist
1598 pxNewTCB->pxEndOfStack = pxTopOfStack;
1600 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1602 #else /* portSTACK_GROWTH */
1604 pxTopOfStack = pxNewTCB->pxStack;
1605 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(). */
1607 /* Check the alignment of the calculated top of stack is correct. */
1608 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1610 /* The other extreme of the stack space is required if stack checking is
1612 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1614 #endif /* portSTACK_GROWTH */
1616 /* Store the task name in the TCB. */
1617 if( pcName != NULL )
1619 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1621 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1623 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1624 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1625 * string is not accessible (extremely unlikely). */
1626 if( pcName[ x ] == ( char ) 0x00 )
1632 mtCOVERAGE_TEST_MARKER();
1636 /* Ensure the name string is terminated in the case that the string length
1637 * was greater or equal to configMAX_TASK_NAME_LEN. */
1638 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1642 mtCOVERAGE_TEST_MARKER();
1645 /* This is used as an array index so must ensure it's not too large. */
1646 configASSERT( uxPriority < configMAX_PRIORITIES );
1648 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1650 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1654 mtCOVERAGE_TEST_MARKER();
1657 pxNewTCB->uxPriority = uxPriority;
1658 #if ( configUSE_MUTEXES == 1 )
1660 pxNewTCB->uxBasePriority = uxPriority;
1662 #endif /* configUSE_MUTEXES */
1664 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1665 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1667 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1668 * back to the containing TCB from a generic item in a list. */
1669 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1671 /* Event lists are always in priority order. */
1672 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. */
1673 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1675 #if ( portUSING_MPU_WRAPPERS == 1 )
1677 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1681 /* Avoid compiler warning about unreferenced parameter. */
1686 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
1688 /* Allocate and initialize memory for the task's TLS Block. */
1689 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
1693 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1695 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1699 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1701 pxNewTCB->xPreemptionDisable = 0;
1705 /* Initialize the TCB stack to look as if the task was already running,
1706 * but had been interrupted by the scheduler. The return address is set
1707 * to the start of the task function. Once the stack has been initialised
1708 * the top of stack variable is updated. */
1709 #if ( portUSING_MPU_WRAPPERS == 1 )
1711 /* If the port has capability to detect stack overflow,
1712 * pass the stack end address to the stack initialization
1713 * function as well. */
1714 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1716 #if ( portSTACK_GROWTH < 0 )
1718 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1720 #else /* portSTACK_GROWTH */
1722 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1724 #endif /* portSTACK_GROWTH */
1726 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1728 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1730 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1732 #else /* portUSING_MPU_WRAPPERS */
1734 /* If the port has capability to detect stack overflow,
1735 * pass the stack end address to the stack initialization
1736 * function as well. */
1737 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1739 #if ( portSTACK_GROWTH < 0 )
1741 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1743 #else /* portSTACK_GROWTH */
1745 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1747 #endif /* portSTACK_GROWTH */
1749 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1751 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1753 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1755 #endif /* portUSING_MPU_WRAPPERS */
1757 /* Initialize task state and task attributes. */
1758 #if ( configNUMBER_OF_CORES > 1 )
1760 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1762 /* Is this an idle task? */
1763 if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvMinimalIdleTask ) )
1765 pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
1768 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
1770 if( pxCreatedTask != NULL )
1772 /* Pass the handle out in an anonymous way. The handle can be used to
1773 * change the created task's priority, delete the created task, etc.*/
1774 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1778 mtCOVERAGE_TEST_MARKER();
1781 /*-----------------------------------------------------------*/
1783 #if ( configNUMBER_OF_CORES == 1 )
1785 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1787 /* Ensure interrupts don't access the task lists while the lists are being
1789 taskENTER_CRITICAL();
1791 uxCurrentNumberOfTasks++;
1793 if( pxCurrentTCB == NULL )
1795 /* There are no other tasks, or all the other tasks are in
1796 * the suspended state - make this the current task. */
1797 pxCurrentTCB = pxNewTCB;
1799 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1801 /* This is the first task to be created so do the preliminary
1802 * initialisation required. We will not recover if this call
1803 * fails, but we will report the failure. */
1804 prvInitialiseTaskLists();
1808 mtCOVERAGE_TEST_MARKER();
1813 /* If the scheduler is not already running, make this task the
1814 * current task if it is the highest priority task to be created
1816 if( xSchedulerRunning == pdFALSE )
1818 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
1820 pxCurrentTCB = pxNewTCB;
1824 mtCOVERAGE_TEST_MARKER();
1829 mtCOVERAGE_TEST_MARKER();
1835 #if ( configUSE_TRACE_FACILITY == 1 )
1837 /* Add a counter into the TCB for tracing only. */
1838 pxNewTCB->uxTCBNumber = uxTaskNumber;
1840 #endif /* configUSE_TRACE_FACILITY */
1841 traceTASK_CREATE( pxNewTCB );
1843 prvAddTaskToReadyList( pxNewTCB );
1845 portSETUP_TCB( pxNewTCB );
1847 taskEXIT_CRITICAL();
1849 if( xSchedulerRunning != pdFALSE )
1851 /* If the created task is of a higher priority than the current task
1852 * then it should run now. */
1853 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
1857 mtCOVERAGE_TEST_MARKER();
1861 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
1863 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1865 /* Ensure interrupts don't access the task lists while the lists are being
1867 taskENTER_CRITICAL();
1869 uxCurrentNumberOfTasks++;
1871 if( xSchedulerRunning == pdFALSE )
1873 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1875 /* This is the first task to be created so do the preliminary
1876 * initialisation required. We will not recover if this call
1877 * fails, but we will report the failure. */
1878 prvInitialiseTaskLists();
1882 mtCOVERAGE_TEST_MARKER();
1885 if( ( pxNewTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1889 /* Check if a core is free. */
1890 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
1892 if( pxCurrentTCBs[ xCoreID ] == NULL )
1894 pxNewTCB->xTaskRunState = xCoreID;
1895 pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1900 mtCOVERAGE_TEST_MARKER();
1906 mtCOVERAGE_TEST_MARKER();
1912 #if ( configUSE_TRACE_FACILITY == 1 )
1914 /* Add a counter into the TCB for tracing only. */
1915 pxNewTCB->uxTCBNumber = uxTaskNumber;
1917 #endif /* configUSE_TRACE_FACILITY */
1918 traceTASK_CREATE( pxNewTCB );
1920 prvAddTaskToReadyList( pxNewTCB );
1922 portSETUP_TCB( pxNewTCB );
1924 if( xSchedulerRunning != pdFALSE )
1926 /* If the created task is of a higher priority than another
1927 * currently running task and preemption is on then it should
1929 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
1933 mtCOVERAGE_TEST_MARKER();
1936 taskEXIT_CRITICAL();
1939 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
1940 /*-----------------------------------------------------------*/
1942 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
1944 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
1947 size_t uxCharsWritten;
1949 if( iSnprintfReturnValue < 0 )
1951 /* Encoding error - Return 0 to indicate that nothing
1952 * was written to the buffer. */
1955 else if( iSnprintfReturnValue >= ( int ) n )
1957 /* This is the case when the supplied buffer is not
1958 * large to hold the generated string. Return the
1959 * number of characters actually written without
1960 * counting the terminating NULL character. */
1961 uxCharsWritten = n - 1;
1965 /* Complete string was written to the buffer. */
1966 uxCharsWritten = ( size_t ) iSnprintfReturnValue;
1969 return uxCharsWritten;
1972 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
1973 /*-----------------------------------------------------------*/
1975 #if ( INCLUDE_vTaskDelete == 1 )
1977 void vTaskDelete( TaskHandle_t xTaskToDelete )
1981 traceENTER_vTaskDelete( xTaskToDelete );
1983 taskENTER_CRITICAL();
1985 /* If null is passed in here then it is the calling task that is
1987 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1989 /* Remove task from the ready/delayed list. */
1990 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1992 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1996 mtCOVERAGE_TEST_MARKER();
1999 /* Is the task waiting on an event also? */
2000 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2002 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2006 mtCOVERAGE_TEST_MARKER();
2009 /* Increment the uxTaskNumber also so kernel aware debuggers can
2010 * detect that the task lists need re-generating. This is done before
2011 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
2015 /* If the task is running (or yielding), we must add it to the
2016 * termination list so that an idle task can delete it when it is
2017 * no longer running. */
2018 if( taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) != pdFALSE )
2020 /* A running task or a task which is scheduled to yield is being
2021 * deleted. This cannot complete when the task is still running
2022 * on a core, as a context switch to another task is required.
2023 * Place the task in the termination list. The idle task will check
2024 * the termination list and free up any memory allocated by the
2025 * scheduler for the TCB and stack of the deleted task. */
2026 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
2028 /* Increment the ucTasksDeleted variable so the idle task knows
2029 * there is a task that has been deleted and that it should therefore
2030 * check the xTasksWaitingTermination list. */
2031 ++uxDeletedTasksWaitingCleanUp;
2033 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
2034 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
2035 traceTASK_DELETE( pxTCB );
2037 /* The pre-delete hook is primarily for the Windows simulator,
2038 * in which Windows specific clean up operations are performed,
2039 * after which it is not possible to yield away from this task -
2040 * hence xYieldPending is used to latch that a context switch is
2042 #if ( configNUMBER_OF_CORES == 1 )
2043 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
2045 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
2050 --uxCurrentNumberOfTasks;
2051 traceTASK_DELETE( pxTCB );
2053 /* Reset the next expected unblock time in case it referred to
2054 * the task that has just been deleted. */
2055 prvResetNextTaskUnblockTime();
2059 #if ( configNUMBER_OF_CORES == 1 )
2061 taskEXIT_CRITICAL();
2063 /* If the task is not deleting itself, call prvDeleteTCB from outside of
2064 * critical section. If a task deletes itself, prvDeleteTCB is called
2065 * from prvCheckTasksWaitingTermination which is called from Idle task. */
2066 if( pxTCB != pxCurrentTCB )
2068 prvDeleteTCB( pxTCB );
2071 /* Force a reschedule if it is the currently running task that has just
2073 if( xSchedulerRunning != pdFALSE )
2075 if( pxTCB == pxCurrentTCB )
2077 configASSERT( uxSchedulerSuspended == 0 );
2078 portYIELD_WITHIN_API();
2082 mtCOVERAGE_TEST_MARKER();
2086 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2088 /* If a running task is not deleting itself, call prvDeleteTCB. If a running
2089 * task deletes itself, prvDeleteTCB is called from prvCheckTasksWaitingTermination
2090 * which is called from Idle task. */
2091 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
2093 prvDeleteTCB( pxTCB );
2096 /* Force a reschedule if the task that has just been deleted was running. */
2097 if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) )
2099 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2101 configASSERT( uxSchedulerSuspended == 0 );
2102 vTaskYieldWithinAPI();
2106 prvYieldCore( pxTCB->xTaskRunState );
2110 taskEXIT_CRITICAL();
2112 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2114 traceRETURN_vTaskDelete();
2117 #endif /* INCLUDE_vTaskDelete */
2118 /*-----------------------------------------------------------*/
2120 #if ( INCLUDE_xTaskDelayUntil == 1 )
2122 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2123 const TickType_t xTimeIncrement )
2125 TickType_t xTimeToWake;
2126 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2128 traceENTER_xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
2130 configASSERT( pxPreviousWakeTime );
2131 configASSERT( ( xTimeIncrement > 0U ) );
2135 /* Minor optimisation. The tick count cannot change in this
2137 const TickType_t xConstTickCount = xTickCount;
2139 configASSERT( uxSchedulerSuspended == 1U );
2141 /* Generate the tick time at which the task wants to wake. */
2142 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2144 if( xConstTickCount < *pxPreviousWakeTime )
2146 /* The tick count has overflowed since this function was
2147 * lasted called. In this case the only time we should ever
2148 * actually delay is if the wake time has also overflowed,
2149 * and the wake time is greater than the tick time. When this
2150 * is the case it is as if neither time had overflowed. */
2151 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2153 xShouldDelay = pdTRUE;
2157 mtCOVERAGE_TEST_MARKER();
2162 /* The tick time has not overflowed. In this case we will
2163 * delay if either the wake time has overflowed, and/or the
2164 * tick time is less than the wake time. */
2165 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2167 xShouldDelay = pdTRUE;
2171 mtCOVERAGE_TEST_MARKER();
2175 /* Update the wake time ready for the next call. */
2176 *pxPreviousWakeTime = xTimeToWake;
2178 if( xShouldDelay != pdFALSE )
2180 traceTASK_DELAY_UNTIL( xTimeToWake );
2182 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2183 * the time to wake, so subtract the current tick count. */
2184 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2188 mtCOVERAGE_TEST_MARKER();
2191 xAlreadyYielded = xTaskResumeAll();
2193 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2194 * have put ourselves to sleep. */
2195 if( xAlreadyYielded == pdFALSE )
2197 taskYIELD_WITHIN_API();
2201 mtCOVERAGE_TEST_MARKER();
2204 traceRETURN_xTaskDelayUntil( xShouldDelay );
2206 return xShouldDelay;
2209 #endif /* INCLUDE_xTaskDelayUntil */
2210 /*-----------------------------------------------------------*/
2212 #if ( INCLUDE_vTaskDelay == 1 )
2214 void vTaskDelay( const TickType_t xTicksToDelay )
2216 BaseType_t xAlreadyYielded = pdFALSE;
2218 traceENTER_vTaskDelay( xTicksToDelay );
2220 /* A delay time of zero just forces a reschedule. */
2221 if( xTicksToDelay > ( TickType_t ) 0U )
2225 configASSERT( uxSchedulerSuspended == 1U );
2229 /* A task that is removed from the event list while the
2230 * scheduler is suspended will not get placed in the ready
2231 * list or removed from the blocked list until the scheduler
2234 * This task cannot be in an event list as it is the currently
2235 * executing task. */
2236 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2238 xAlreadyYielded = xTaskResumeAll();
2242 mtCOVERAGE_TEST_MARKER();
2245 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2246 * have put ourselves to sleep. */
2247 if( xAlreadyYielded == pdFALSE )
2249 taskYIELD_WITHIN_API();
2253 mtCOVERAGE_TEST_MARKER();
2256 traceRETURN_vTaskDelay();
2259 #endif /* INCLUDE_vTaskDelay */
2260 /*-----------------------------------------------------------*/
2262 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2264 eTaskState eTaskGetState( TaskHandle_t xTask )
2267 List_t const * pxStateList;
2268 List_t const * pxEventList;
2269 List_t const * pxDelayedList;
2270 List_t const * pxOverflowedDelayedList;
2271 const TCB_t * const pxTCB = xTask;
2273 traceENTER_eTaskGetState( xTask );
2275 configASSERT( pxTCB );
2277 #if ( configNUMBER_OF_CORES == 1 )
2278 if( pxTCB == pxCurrentTCB )
2280 /* The task calling this function is querying its own state. */
2286 taskENTER_CRITICAL();
2288 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2289 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2290 pxDelayedList = pxDelayedTaskList;
2291 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2293 taskEXIT_CRITICAL();
2295 if( pxEventList == &xPendingReadyList )
2297 /* The task has been placed on the pending ready list, so its
2298 * state is eReady regardless of what list the task's state list
2299 * item is currently placed on. */
2302 else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2304 /* The task being queried is referenced from one of the Blocked
2309 #if ( INCLUDE_vTaskSuspend == 1 )
2310 else if( pxStateList == &xSuspendedTaskList )
2312 /* The task being queried is referenced from the suspended
2313 * list. Is it genuinely suspended or is it blocked
2315 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2317 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2321 /* The task does not appear on the event list item of
2322 * and of the RTOS objects, but could still be in the
2323 * blocked state if it is waiting on its notification
2324 * rather than waiting on an object. If not, is
2326 eReturn = eSuspended;
2328 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2330 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2337 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2339 eReturn = eSuspended;
2341 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2348 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2350 #if ( INCLUDE_vTaskDelete == 1 )
2351 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2353 /* The task being queried is referenced from the deleted
2354 * tasks list, or it is not referenced from any lists at
2360 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
2362 #if ( configNUMBER_OF_CORES == 1 )
2364 /* If the task is not in any other state, it must be in the
2365 * Ready (including pending ready) state. */
2368 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2370 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2372 /* Is it actively running on a core? */
2377 /* If the task is not in any other state, it must be in the
2378 * Ready (including pending ready) state. */
2382 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2386 traceRETURN_eTaskGetState( eReturn );
2389 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2391 #endif /* INCLUDE_eTaskGetState */
2392 /*-----------------------------------------------------------*/
2394 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2396 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2398 TCB_t const * pxTCB;
2399 UBaseType_t uxReturn;
2401 traceENTER_uxTaskPriorityGet( xTask );
2403 taskENTER_CRITICAL();
2405 /* If null is passed in here then it is the priority of the task
2406 * that called uxTaskPriorityGet() that is being queried. */
2407 pxTCB = prvGetTCBFromHandle( xTask );
2408 uxReturn = pxTCB->uxPriority;
2410 taskEXIT_CRITICAL();
2412 traceRETURN_uxTaskPriorityGet( uxReturn );
2417 #endif /* INCLUDE_uxTaskPriorityGet */
2418 /*-----------------------------------------------------------*/
2420 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2422 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2424 TCB_t const * pxTCB;
2425 UBaseType_t uxReturn;
2426 UBaseType_t uxSavedInterruptStatus;
2428 traceENTER_uxTaskPriorityGetFromISR( xTask );
2430 /* RTOS ports that support interrupt nesting have the concept of a
2431 * maximum system call (or maximum API call) interrupt priority.
2432 * Interrupts that are above the maximum system call priority are keep
2433 * permanently enabled, even when the RTOS kernel is in a critical section,
2434 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2435 * is defined in FreeRTOSConfig.h then
2436 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2437 * failure if a FreeRTOS API function is called from an interrupt that has
2438 * been assigned a priority above the configured maximum system call
2439 * priority. Only FreeRTOS functions that end in FromISR can be called
2440 * from interrupts that have been assigned a priority at or (logically)
2441 * below the maximum system call interrupt priority. FreeRTOS maintains a
2442 * separate interrupt safe API to ensure interrupt entry is as fast and as
2443 * simple as possible. More information (albeit Cortex-M specific) is
2444 * provided on the following link:
2445 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2446 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2448 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
2450 /* If null is passed in here then it is the priority of the calling
2451 * task that is being queried. */
2452 pxTCB = prvGetTCBFromHandle( xTask );
2453 uxReturn = pxTCB->uxPriority;
2455 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2457 traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
2462 #endif /* INCLUDE_uxTaskPriorityGet */
2463 /*-----------------------------------------------------------*/
2465 #if ( INCLUDE_vTaskPrioritySet == 1 )
2467 void vTaskPrioritySet( TaskHandle_t xTask,
2468 UBaseType_t uxNewPriority )
2471 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2472 BaseType_t xYieldRequired = pdFALSE;
2474 #if ( configNUMBER_OF_CORES > 1 )
2475 BaseType_t xYieldForTask = pdFALSE;
2478 traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
2480 configASSERT( uxNewPriority < configMAX_PRIORITIES );
2482 /* Ensure the new priority is valid. */
2483 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2485 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2489 mtCOVERAGE_TEST_MARKER();
2492 taskENTER_CRITICAL();
2494 /* If null is passed in here then it is the priority of the calling
2495 * task that is being changed. */
2496 pxTCB = prvGetTCBFromHandle( xTask );
2498 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2500 #if ( configUSE_MUTEXES == 1 )
2502 uxCurrentBasePriority = pxTCB->uxBasePriority;
2506 uxCurrentBasePriority = pxTCB->uxPriority;
2510 if( uxCurrentBasePriority != uxNewPriority )
2512 /* The priority change may have readied a task of higher
2513 * priority than a running task. */
2514 if( uxNewPriority > uxCurrentBasePriority )
2516 #if ( configNUMBER_OF_CORES == 1 )
2518 if( pxTCB != pxCurrentTCB )
2520 /* The priority of a task other than the currently
2521 * running task is being raised. Is the priority being
2522 * raised above that of the running task? */
2523 if( uxNewPriority > pxCurrentTCB->uxPriority )
2525 xYieldRequired = pdTRUE;
2529 mtCOVERAGE_TEST_MARKER();
2534 /* The priority of the running task is being raised,
2535 * but the running task must already be the highest
2536 * priority task able to run so no yield is required. */
2539 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2541 /* The priority of a task is being raised so
2542 * perform a yield for this task later. */
2543 xYieldForTask = pdTRUE;
2545 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2547 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2549 /* Setting the priority of a running task down means
2550 * there may now be another task of higher priority that
2551 * is ready to execute. */
2552 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2553 if( pxTCB->xPreemptionDisable == pdFALSE )
2556 xYieldRequired = pdTRUE;
2561 /* Setting the priority of any other task down does not
2562 * require a yield as the running task must be above the
2563 * new priority of the task being modified. */
2566 /* Remember the ready list the task might be referenced from
2567 * before its uxPriority member is changed so the
2568 * taskRESET_READY_PRIORITY() macro can function correctly. */
2569 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2571 #if ( configUSE_MUTEXES == 1 )
2573 /* Only change the priority being used if the task is not
2574 * currently using an inherited priority or the new priority
2575 * is bigger than the inherited priority. */
2576 if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2578 pxTCB->uxPriority = uxNewPriority;
2582 mtCOVERAGE_TEST_MARKER();
2585 /* The base priority gets set whatever. */
2586 pxTCB->uxBasePriority = uxNewPriority;
2588 #else /* if ( configUSE_MUTEXES == 1 ) */
2590 pxTCB->uxPriority = uxNewPriority;
2592 #endif /* if ( configUSE_MUTEXES == 1 ) */
2594 /* Only reset the event list item value if the value is not
2595 * being used for anything else. */
2596 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2598 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. */
2602 mtCOVERAGE_TEST_MARKER();
2605 /* If the task is in the blocked or suspended list we need do
2606 * nothing more than change its priority variable. However, if
2607 * the task is in a ready list it needs to be removed and placed
2608 * in the list appropriate to its new priority. */
2609 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2611 /* The task is currently in its ready list - remove before
2612 * adding it to its new ready list. As we are in a critical
2613 * section we can do this even if the scheduler is suspended. */
2614 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2616 /* It is known that the task is in its ready list so
2617 * there is no need to check again and the port level
2618 * reset macro can be called directly. */
2619 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2623 mtCOVERAGE_TEST_MARKER();
2626 prvAddTaskToReadyList( pxTCB );
2630 #if ( configNUMBER_OF_CORES == 1 )
2632 mtCOVERAGE_TEST_MARKER();
2636 /* It's possible that xYieldForTask was already set to pdTRUE because
2637 * its priority is being raised. However, since it is not in a ready list
2638 * we don't actually need to yield for it. */
2639 xYieldForTask = pdFALSE;
2644 if( xYieldRequired != pdFALSE )
2646 /* The running task priority is set down. Request the task to yield. */
2647 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2651 #if ( configNUMBER_OF_CORES > 1 )
2652 if( xYieldForTask != pdFALSE )
2654 /* The priority of the task is being raised. If a running
2655 * task has priority lower than this task, it should yield
2657 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2660 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2662 mtCOVERAGE_TEST_MARKER();
2666 /* Remove compiler warning about unused variables when the port
2667 * optimised task selection is not being used. */
2668 ( void ) uxPriorityUsedOnEntry;
2671 taskEXIT_CRITICAL();
2673 traceRETURN_vTaskPrioritySet();
2676 #endif /* INCLUDE_vTaskPrioritySet */
2677 /*-----------------------------------------------------------*/
2679 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2680 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2681 UBaseType_t uxCoreAffinityMask )
2685 UBaseType_t uxPrevCoreAffinityMask;
2687 #if ( configUSE_PREEMPTION == 1 )
2688 UBaseType_t uxPrevNotAllowedCores;
2691 traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
2693 taskENTER_CRITICAL();
2695 pxTCB = prvGetTCBFromHandle( xTask );
2697 uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2698 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2700 if( xSchedulerRunning != pdFALSE )
2702 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2704 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2706 /* If the task can no longer run on the core it was running,
2707 * request the core to yield. */
2708 if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
2710 prvYieldCore( xCoreID );
2715 #if ( configUSE_PREEMPTION == 1 )
2717 /* Calculate the cores on which this task was not allowed to
2718 * run previously. */
2719 uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
2721 /* Does the new core mask enables this task to run on any of the
2722 * previously not allowed cores? If yes, check if this task can be
2723 * scheduled on any of those cores. */
2724 if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
2726 prvYieldForTask( pxTCB );
2729 #else /* #if( configUSE_PREEMPTION == 1 ) */
2731 mtCOVERAGE_TEST_MARKER();
2733 #endif /* #if( configUSE_PREEMPTION == 1 ) */
2737 taskEXIT_CRITICAL();
2739 traceRETURN_vTaskCoreAffinitySet();
2741 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
2742 /*-----------------------------------------------------------*/
2744 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2745 UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
2747 const TCB_t * pxTCB;
2748 UBaseType_t uxCoreAffinityMask;
2750 traceENTER_vTaskCoreAffinityGet( xTask );
2752 taskENTER_CRITICAL();
2754 pxTCB = prvGetTCBFromHandle( xTask );
2755 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2757 taskEXIT_CRITICAL();
2759 traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
2761 return uxCoreAffinityMask;
2763 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
2765 /*-----------------------------------------------------------*/
2767 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2769 void vTaskPreemptionDisable( const TaskHandle_t xTask )
2773 traceENTER_vTaskPreemptionDisable( xTask );
2775 taskENTER_CRITICAL();
2777 pxTCB = prvGetTCBFromHandle( xTask );
2779 pxTCB->xPreemptionDisable = pdTRUE;
2781 taskEXIT_CRITICAL();
2783 traceRETURN_vTaskPreemptionDisable();
2786 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
2787 /*-----------------------------------------------------------*/
2789 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2791 void vTaskPreemptionEnable( const TaskHandle_t xTask )
2796 traceENTER_vTaskPreemptionEnable( xTask );
2798 taskENTER_CRITICAL();
2800 pxTCB = prvGetTCBFromHandle( xTask );
2802 pxTCB->xPreemptionDisable = pdFALSE;
2804 if( xSchedulerRunning != pdFALSE )
2806 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2808 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2809 prvYieldCore( xCoreID );
2813 taskEXIT_CRITICAL();
2815 traceRETURN_vTaskPreemptionEnable();
2818 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
2819 /*-----------------------------------------------------------*/
2821 #if ( INCLUDE_vTaskSuspend == 1 )
2823 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2827 #if ( configNUMBER_OF_CORES > 1 )
2828 BaseType_t xTaskRunningOnCore;
2831 traceENTER_vTaskSuspend( xTaskToSuspend );
2833 taskENTER_CRITICAL();
2835 /* If null is passed in here then it is the running task that is
2836 * being suspended. */
2837 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2839 traceTASK_SUSPEND( pxTCB );
2841 #if ( configNUMBER_OF_CORES > 1 )
2842 xTaskRunningOnCore = pxTCB->xTaskRunState;
2845 /* Remove task from the ready/delayed list and place in the
2846 * suspended list. */
2847 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2849 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2853 mtCOVERAGE_TEST_MARKER();
2856 /* Is the task waiting on an event also? */
2857 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2859 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2863 mtCOVERAGE_TEST_MARKER();
2866 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2868 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2872 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2874 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2876 /* The task was blocked to wait for a notification, but is
2877 * now suspended, so no notification was received. */
2878 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2882 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2885 #if ( configNUMBER_OF_CORES == 1 )
2887 taskEXIT_CRITICAL();
2889 if( xSchedulerRunning != pdFALSE )
2891 /* Reset the next expected unblock time in case it referred to the
2892 * task that is now in the Suspended state. */
2893 taskENTER_CRITICAL();
2895 prvResetNextTaskUnblockTime();
2897 taskEXIT_CRITICAL();
2901 mtCOVERAGE_TEST_MARKER();
2904 if( pxTCB == pxCurrentTCB )
2906 if( xSchedulerRunning != pdFALSE )
2908 /* The current task has just been suspended. */
2909 configASSERT( uxSchedulerSuspended == 0 );
2910 portYIELD_WITHIN_API();
2914 /* The scheduler is not running, but the task that was pointed
2915 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
2916 * must be adjusted to point to a different task. */
2917 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
2919 /* No other tasks are ready, so set pxCurrentTCB back to
2920 * NULL so when the next task is created pxCurrentTCB will
2921 * be set to point to it no matter what its relative priority
2923 pxCurrentTCB = NULL;
2927 vTaskSwitchContext();
2933 mtCOVERAGE_TEST_MARKER();
2936 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2938 if( xSchedulerRunning != pdFALSE )
2940 /* Reset the next expected unblock time in case it referred to the
2941 * task that is now in the Suspended state. */
2942 prvResetNextTaskUnblockTime();
2946 mtCOVERAGE_TEST_MARKER();
2949 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2951 if( xSchedulerRunning != pdFALSE )
2953 if( xTaskRunningOnCore == ( BaseType_t ) portGET_CORE_ID() )
2955 /* The current task has just been suspended. */
2956 configASSERT( uxSchedulerSuspended == 0 );
2957 vTaskYieldWithinAPI();
2961 prvYieldCore( xTaskRunningOnCore );
2966 /* This code path is not possible because only Idle tasks are
2967 * assigned a core before the scheduler is started ( i.e.
2968 * taskTASK_IS_RUNNING is only true for idle tasks before
2969 * the scheduler is started ) and idle tasks cannot be
2971 mtCOVERAGE_TEST_MARKER();
2976 mtCOVERAGE_TEST_MARKER();
2979 taskEXIT_CRITICAL();
2981 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2983 traceRETURN_vTaskSuspend();
2986 #endif /* INCLUDE_vTaskSuspend */
2987 /*-----------------------------------------------------------*/
2989 #if ( INCLUDE_vTaskSuspend == 1 )
2991 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
2993 BaseType_t xReturn = pdFALSE;
2994 const TCB_t * const pxTCB = xTask;
2996 /* Accesses xPendingReadyList so must be called from a critical
2999 /* It does not make sense to check if the calling task is suspended. */
3000 configASSERT( xTask );
3002 /* Is the task being resumed actually in the suspended list? */
3003 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
3005 /* Has the task already been resumed from within an ISR? */
3006 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
3008 /* Is it in the suspended list because it is in the Suspended
3009 * state, or because is is blocked with no timeout? */
3010 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
3016 mtCOVERAGE_TEST_MARKER();
3021 mtCOVERAGE_TEST_MARKER();
3026 mtCOVERAGE_TEST_MARKER();
3030 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
3032 #endif /* INCLUDE_vTaskSuspend */
3033 /*-----------------------------------------------------------*/
3035 #if ( INCLUDE_vTaskSuspend == 1 )
3037 void vTaskResume( TaskHandle_t xTaskToResume )
3039 TCB_t * const pxTCB = xTaskToResume;
3041 traceENTER_vTaskResume( xTaskToResume );
3043 /* It does not make sense to resume the calling task. */
3044 configASSERT( xTaskToResume );
3046 #if ( configNUMBER_OF_CORES == 1 )
3048 /* The parameter cannot be NULL as it is impossible to resume the
3049 * currently executing task. */
3050 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
3053 /* The parameter cannot be NULL as it is impossible to resume the
3054 * currently executing task. It is also impossible to resume a task
3055 * that is actively running on another core but it is not safe
3056 * to check their run state here. Therefore, we get into a critical
3057 * section and check if the task is actually suspended or not. */
3061 taskENTER_CRITICAL();
3063 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3065 traceTASK_RESUME( pxTCB );
3067 /* The ready list can be accessed even if the scheduler is
3068 * suspended because this is inside a critical section. */
3069 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3070 prvAddTaskToReadyList( pxTCB );
3072 /* This yield may not cause the task just resumed to run,
3073 * but will leave the lists in the correct state for the
3075 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
3079 mtCOVERAGE_TEST_MARKER();
3082 taskEXIT_CRITICAL();
3086 mtCOVERAGE_TEST_MARKER();
3089 traceRETURN_vTaskResume();
3092 #endif /* INCLUDE_vTaskSuspend */
3094 /*-----------------------------------------------------------*/
3096 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
3098 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
3100 BaseType_t xYieldRequired = pdFALSE;
3101 TCB_t * const pxTCB = xTaskToResume;
3102 UBaseType_t uxSavedInterruptStatus;
3104 traceENTER_xTaskResumeFromISR( xTaskToResume );
3106 configASSERT( xTaskToResume );
3108 /* RTOS ports that support interrupt nesting have the concept of a
3109 * maximum system call (or maximum API call) interrupt priority.
3110 * Interrupts that are above the maximum system call priority are keep
3111 * permanently enabled, even when the RTOS kernel is in a critical section,
3112 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
3113 * is defined in FreeRTOSConfig.h then
3114 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3115 * failure if a FreeRTOS API function is called from an interrupt that has
3116 * been assigned a priority above the configured maximum system call
3117 * priority. Only FreeRTOS functions that end in FromISR can be called
3118 * from interrupts that have been assigned a priority at or (logically)
3119 * below the maximum system call interrupt priority. FreeRTOS maintains a
3120 * separate interrupt safe API to ensure interrupt entry is as fast and as
3121 * simple as possible. More information (albeit Cortex-M specific) is
3122 * provided on the following link:
3123 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3124 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3126 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3128 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3130 traceTASK_RESUME_FROM_ISR( pxTCB );
3132 /* Check the ready lists can be accessed. */
3133 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3135 #if ( configNUMBER_OF_CORES == 1 )
3137 /* Ready lists can be accessed so move the task from the
3138 * suspended list to the ready list directly. */
3139 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3141 xYieldRequired = pdTRUE;
3143 /* Mark that a yield is pending in case the user is not
3144 * using the return value to initiate a context switch
3145 * from the ISR using portYIELD_FROM_ISR. */
3146 xYieldPendings[ 0 ] = pdTRUE;
3150 mtCOVERAGE_TEST_MARKER();
3153 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3155 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3156 prvAddTaskToReadyList( pxTCB );
3160 /* The delayed or ready lists cannot be accessed so the task
3161 * is held in the pending ready list until the scheduler is
3163 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3166 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3168 prvYieldForTask( pxTCB );
3170 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3172 xYieldRequired = pdTRUE;
3175 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3179 mtCOVERAGE_TEST_MARKER();
3182 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3184 traceRETURN_xTaskResumeFromISR( xYieldRequired );
3186 return xYieldRequired;
3189 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3190 /*-----------------------------------------------------------*/
3192 static BaseType_t prvCreateIdleTasks( void )
3194 BaseType_t xReturn = pdPASS;
3196 #if ( configNUMBER_OF_CORES == 1 )
3198 /* Add the idle task at the lowest priority. */
3199 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3201 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3202 StackType_t * pxIdleTaskStackBuffer = NULL;
3203 uint32_t ulIdleTaskStackSize;
3205 /* The Idle task is created using user provided RAM - obtain the
3206 * address of the RAM then create the idle task. */
3207 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3208 xIdleTaskHandles[ 0 ] = xTaskCreateStatic( prvIdleTask,
3209 configIDLE_TASK_NAME,
3210 ulIdleTaskStackSize,
3211 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3212 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3213 pxIdleTaskStackBuffer,
3214 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3216 if( xIdleTaskHandles[ 0 ] != NULL )
3225 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3227 /* The Idle task is being created using dynamically allocated RAM. */
3228 xReturn = xTaskCreate( prvIdleTask,
3229 configIDLE_TASK_NAME,
3230 configMINIMAL_STACK_SIZE,
3232 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3233 &xIdleTaskHandles[ 0 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3235 #endif /* configSUPPORT_STATIC_ALLOCATION */
3237 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3240 char cIdleName[ configMAX_TASK_NAME_LEN ];
3242 /* Add each idle task at the lowest priority. */
3243 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3247 if( xReturn == pdFAIL )
3253 mtCOVERAGE_TEST_MARKER();
3256 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3258 cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
3260 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3261 * configMAX_TASK_NAME_LEN characters just in case the memory after the
3262 * string is not accessible (extremely unlikely). */
3263 if( cIdleName[ x ] == ( char ) 0x00 )
3269 mtCOVERAGE_TEST_MARKER();
3273 /* Append the idle task number to the end of the name if there is space. */
3274 if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3276 cIdleName[ x ] = ( char ) ( xCoreID + '0' );
3279 /* And append a null character if there is space. */
3280 if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3282 cIdleName[ x ] = '\0';
3286 mtCOVERAGE_TEST_MARKER();
3291 mtCOVERAGE_TEST_MARKER();
3294 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3298 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3299 StackType_t * pxIdleTaskStackBuffer = NULL;
3300 uint32_t ulIdleTaskStackSize;
3302 /* The Idle task is created using user provided RAM - obtain the
3303 * address of the RAM then create the idle task. */
3304 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3305 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
3307 ulIdleTaskStackSize,
3308 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3309 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3310 pxIdleTaskStackBuffer,
3311 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3315 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( prvMinimalIdleTask,
3317 configMINIMAL_STACK_SIZE,
3318 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3319 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3320 xIdleTaskStackBuffers[ xCoreID - 1 ],
3321 &xIdleTCBBuffers[ xCoreID - 1 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3324 if( xIdleTaskHandles[ xCoreID ] != NULL )
3333 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3337 /* The Idle task is being created using dynamically allocated RAM. */
3338 xReturn = xTaskCreate( prvIdleTask,
3340 configMINIMAL_STACK_SIZE,
3342 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3343 &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3347 xReturn = xTaskCreate( prvMinimalIdleTask,
3349 configMINIMAL_STACK_SIZE,
3351 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3352 &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3355 #endif /* configSUPPORT_STATIC_ALLOCATION */
3358 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3363 /*-----------------------------------------------------------*/
3365 void vTaskStartScheduler( void )
3369 traceENTER_vTaskStartScheduler();
3371 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3373 /* Sanity check that the UBaseType_t must have greater than or equal to
3374 * the number of bits as confNUMBER_OF_CORES. */
3375 configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3377 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3379 xReturn = prvCreateIdleTasks();
3381 #if ( configUSE_TIMERS == 1 )
3383 if( xReturn == pdPASS )
3385 xReturn = xTimerCreateTimerTask();
3389 mtCOVERAGE_TEST_MARKER();
3392 #endif /* configUSE_TIMERS */
3394 if( xReturn == pdPASS )
3396 /* freertos_tasks_c_additions_init() should only be called if the user
3397 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3398 * the only macro called by the function. */
3399 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3401 freertos_tasks_c_additions_init();
3405 /* Interrupts are turned off here, to ensure a tick does not occur
3406 * before or during the call to xPortStartScheduler(). The stacks of
3407 * the created tasks contain a status word with interrupts switched on
3408 * so interrupts will automatically get re-enabled when the first task
3410 portDISABLE_INTERRUPTS();
3412 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3414 /* Switch C-Runtime's TLS Block to point to the TLS
3415 * block specific to the task that will run first. */
3416 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3420 xNextTaskUnblockTime = portMAX_DELAY;
3421 xSchedulerRunning = pdTRUE;
3422 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3424 /* If configGENERATE_RUN_TIME_STATS is defined then the following
3425 * macro must be defined to configure the timer/counter used to generate
3426 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
3427 * is set to 0 and the following line fails to build then ensure you do not
3428 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3429 * FreeRTOSConfig.h file. */
3430 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3432 traceTASK_SWITCHED_IN();
3434 /* Setting up the timer tick is hardware specific and thus in the
3435 * portable interface. */
3436 xPortStartScheduler();
3438 /* In most cases, xPortStartScheduler() will not return. If it
3439 * returns pdTRUE then there was not enough heap memory available
3440 * to create either the Idle or the Timer task. If it returned
3441 * pdFALSE, then the application called xTaskEndScheduler().
3442 * Most ports don't implement xTaskEndScheduler() as there is
3443 * nothing to return to. */
3447 /* This line will only be reached if the kernel could not be started,
3448 * because there was not enough FreeRTOS heap to create the idle task
3449 * or the timer task. */
3450 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3453 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3454 * meaning xIdleTaskHandles are not used anywhere else. */
3455 ( void ) xIdleTaskHandles;
3457 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3458 * from getting optimized out as it is no longer used by the kernel. */
3459 ( void ) uxTopUsedPriority;
3461 traceRETURN_vTaskStartScheduler();
3463 /*-----------------------------------------------------------*/
3465 void vTaskEndScheduler( void )
3467 traceENTER_vTaskEndScheduler();
3469 /* Stop the scheduler interrupts and call the portable scheduler end
3470 * routine so the original ISRs can be restored if necessary. The port
3471 * layer must ensure interrupts enable bit is left in the correct state. */
3472 portDISABLE_INTERRUPTS();
3473 xSchedulerRunning = pdFALSE;
3474 vPortEndScheduler();
3476 traceRETURN_vTaskEndScheduler();
3478 /*----------------------------------------------------------*/
3480 void vTaskSuspendAll( void )
3482 traceENTER_vTaskSuspendAll();
3484 #if ( configNUMBER_OF_CORES == 1 )
3486 /* A critical section is not required as the variable is of type
3487 * BaseType_t. Please read Richard Barry's reply in the following link to a
3488 * post in the FreeRTOS support forum before reporting this as a bug! -
3489 * https://goo.gl/wu4acr */
3491 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3492 * do not otherwise exhibit real time behaviour. */
3493 portSOFTWARE_BARRIER();
3495 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3496 * is used to allow calls to vTaskSuspendAll() to nest. */
3497 ++uxSchedulerSuspended;
3499 /* Enforces ordering for ports and optimised compilers that may otherwise place
3500 * the above increment elsewhere. */
3501 portMEMORY_BARRIER();
3503 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3505 UBaseType_t ulState;
3507 /* This must only be called from within a task. */
3508 portASSERT_IF_IN_ISR();
3510 if( xSchedulerRunning != pdFALSE )
3512 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3513 * We must disable interrupts before we grab the locks in the event that this task is
3514 * interrupted and switches context before incrementing uxSchedulerSuspended.
3515 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3516 * uxSchedulerSuspended since that will prevent context switches. */
3517 ulState = portSET_INTERRUPT_MASK();
3519 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3520 * do not otherwise exhibit real time behaviour. */
3521 portSOFTWARE_BARRIER();
3523 portGET_TASK_LOCK();
3525 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3526 * purpose is to prevent altering the variable when fromISR APIs are readying
3528 if( uxSchedulerSuspended == 0U )
3530 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
3532 prvCheckForRunStateChange();
3536 mtCOVERAGE_TEST_MARKER();
3541 mtCOVERAGE_TEST_MARKER();
3546 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3547 * is used to allow calls to vTaskSuspendAll() to nest. */
3548 ++uxSchedulerSuspended;
3549 portRELEASE_ISR_LOCK();
3551 portCLEAR_INTERRUPT_MASK( ulState );
3555 mtCOVERAGE_TEST_MARKER();
3558 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3560 traceRETURN_vTaskSuspendAll();
3563 /*----------------------------------------------------------*/
3565 #if ( configUSE_TICKLESS_IDLE != 0 )
3567 static TickType_t prvGetExpectedIdleTime( void )
3570 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
3572 /* uxHigherPriorityReadyTasks takes care of the case where
3573 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3574 * task that are in the Ready state, even though the idle task is
3576 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3578 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3580 uxHigherPriorityReadyTasks = pdTRUE;
3585 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3587 /* When port optimised task selection is used the uxTopReadyPriority
3588 * variable is used as a bit map. If bits other than the least
3589 * significant bit are set then there are tasks that have a priority
3590 * above the idle priority that are in the Ready state. This takes
3591 * care of the case where the co-operative scheduler is in use. */
3592 if( uxTopReadyPriority > uxLeastSignificantBit )
3594 uxHigherPriorityReadyTasks = pdTRUE;
3597 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3599 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3603 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
3605 /* There are other idle priority tasks in the ready state. If
3606 * time slicing is used then the very next tick interrupt must be
3610 else if( uxHigherPriorityReadyTasks != pdFALSE )
3612 /* There are tasks in the Ready state that have a priority above the
3613 * idle priority. This path can only be reached if
3614 * configUSE_PREEMPTION is 0. */
3619 xReturn = xNextTaskUnblockTime - xTickCount;
3625 #endif /* configUSE_TICKLESS_IDLE */
3626 /*----------------------------------------------------------*/
3628 BaseType_t xTaskResumeAll( void )
3630 TCB_t * pxTCB = NULL;
3631 BaseType_t xAlreadyYielded = pdFALSE;
3633 traceENTER_xTaskResumeAll();
3635 #if ( configNUMBER_OF_CORES > 1 )
3636 if( xSchedulerRunning != pdFALSE )
3639 /* It is possible that an ISR caused a task to be removed from an event
3640 * list while the scheduler was suspended. If this was the case then the
3641 * removed task will have been added to the xPendingReadyList. Once the
3642 * scheduler has been resumed it is safe to move all the pending ready
3643 * tasks from this list into their appropriate ready list. */
3644 taskENTER_CRITICAL();
3647 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3649 /* If uxSchedulerSuspended is zero then this function does not match a
3650 * previous call to vTaskSuspendAll(). */
3651 configASSERT( uxSchedulerSuspended != 0U );
3653 --uxSchedulerSuspended;
3654 portRELEASE_TASK_LOCK();
3656 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3658 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3660 /* Move any readied tasks from the pending list into the
3661 * appropriate ready list. */
3662 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3664 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. */
3665 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
3666 portMEMORY_BARRIER();
3667 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
3668 prvAddTaskToReadyList( pxTCB );
3670 #if ( configNUMBER_OF_CORES == 1 )
3672 /* If the moved task has a priority higher than the current
3673 * task then a yield must be performed. */
3674 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3676 xYieldPendings[ xCoreID ] = pdTRUE;
3680 mtCOVERAGE_TEST_MARKER();
3683 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3685 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
3686 * If the current core yielded then vTaskSwitchContext() has already been called
3687 * which sets xYieldPendings for the current core to pdTRUE. */
3689 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3694 /* A task was unblocked while the scheduler was suspended,
3695 * which may have prevented the next unblock time from being
3696 * re-calculated, in which case re-calculate it now. Mainly
3697 * important for low power tickless implementations, where
3698 * this can prevent an unnecessary exit from low power
3700 prvResetNextTaskUnblockTime();
3703 /* If any ticks occurred while the scheduler was suspended then
3704 * they should be processed now. This ensures the tick count does
3705 * not slip, and that any delayed tasks are resumed at the correct
3708 * It should be safe to call xTaskIncrementTick here from any core
3709 * since we are in a critical section and xTaskIncrementTick itself
3710 * protects itself within a critical section. Suspending the scheduler
3711 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
3713 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3715 if( xPendedCounts > ( TickType_t ) 0U )
3719 if( xTaskIncrementTick() != pdFALSE )
3721 /* Other cores are interrupted from
3722 * within xTaskIncrementTick(). */
3723 xYieldPendings[ xCoreID ] = pdTRUE;
3727 mtCOVERAGE_TEST_MARKER();
3731 } while( xPendedCounts > ( TickType_t ) 0U );
3737 mtCOVERAGE_TEST_MARKER();
3741 if( xYieldPendings[ xCoreID ] != pdFALSE )
3743 #if ( configUSE_PREEMPTION != 0 )
3745 xAlreadyYielded = pdTRUE;
3747 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
3749 #if ( configNUMBER_OF_CORES == 1 )
3751 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
3753 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3757 mtCOVERAGE_TEST_MARKER();
3763 mtCOVERAGE_TEST_MARKER();
3766 taskEXIT_CRITICAL();
3769 traceRETURN_xTaskResumeAll( xAlreadyYielded );
3771 return xAlreadyYielded;
3773 /*-----------------------------------------------------------*/
3775 TickType_t xTaskGetTickCount( void )
3779 traceENTER_xTaskGetTickCount();
3781 /* Critical section required if running on a 16 bit processor. */
3782 portTICK_TYPE_ENTER_CRITICAL();
3784 xTicks = xTickCount;
3786 portTICK_TYPE_EXIT_CRITICAL();
3788 traceRETURN_xTaskGetTickCount( xTicks );
3792 /*-----------------------------------------------------------*/
3794 TickType_t xTaskGetTickCountFromISR( void )
3797 UBaseType_t uxSavedInterruptStatus;
3799 traceENTER_xTaskGetTickCountFromISR();
3801 /* RTOS ports that support interrupt nesting have the concept of a maximum
3802 * system call (or maximum API call) interrupt priority. Interrupts that are
3803 * above the maximum system call priority are kept permanently enabled, even
3804 * when the RTOS kernel is in a critical section, but cannot make any calls to
3805 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
3806 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3807 * failure if a FreeRTOS API function is called from an interrupt that has been
3808 * assigned a priority above the configured maximum system call priority.
3809 * Only FreeRTOS functions that end in FromISR can be called from interrupts
3810 * that have been assigned a priority at or (logically) below the maximum
3811 * system call interrupt priority. FreeRTOS maintains a separate interrupt
3812 * safe API to ensure interrupt entry is as fast and as simple as possible.
3813 * More information (albeit Cortex-M specific) is provided on the following
3814 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3815 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3817 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
3819 xReturn = xTickCount;
3821 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3823 traceRETURN_xTaskGetTickCountFromISR( xReturn );
3827 /*-----------------------------------------------------------*/
3829 UBaseType_t uxTaskGetNumberOfTasks( void )
3831 traceENTER_uxTaskGetNumberOfTasks();
3833 /* A critical section is not required because the variables are of type
3835 traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
3837 return uxCurrentNumberOfTasks;
3839 /*-----------------------------------------------------------*/
3841 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3845 traceENTER_pcTaskGetName( xTaskToQuery );
3847 /* If null is passed in here then the name of the calling task is being
3849 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3850 configASSERT( pxTCB );
3852 traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
3854 return &( pxTCB->pcTaskName[ 0 ] );
3856 /*-----------------------------------------------------------*/
3858 #if ( INCLUDE_xTaskGetHandle == 1 )
3860 #if ( configNUMBER_OF_CORES == 1 )
3861 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3862 const char pcNameToQuery[] )
3866 TCB_t * pxReturn = NULL;
3869 BaseType_t xBreakLoop;
3871 /* This function is called with the scheduler suspended. */
3873 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3875 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. */
3879 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. */
3881 /* Check each character in the name looking for a match or
3883 xBreakLoop = pdFALSE;
3885 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3887 cNextChar = pxNextTCB->pcTaskName[ x ];
3889 if( cNextChar != pcNameToQuery[ x ] )
3891 /* Characters didn't match. */
3892 xBreakLoop = pdTRUE;
3894 else if( cNextChar == ( char ) 0x00 )
3896 /* Both strings terminated, a match must have been
3898 pxReturn = pxNextTCB;
3899 xBreakLoop = pdTRUE;
3903 mtCOVERAGE_TEST_MARKER();
3906 if( xBreakLoop != pdFALSE )
3912 if( pxReturn != NULL )
3914 /* The handle has been found. */
3917 } while( pxNextTCB != pxFirstTCB );
3921 mtCOVERAGE_TEST_MARKER();
3926 #else /* if ( configNUMBER_OF_CORES == 1 ) */
3927 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3928 const char pcNameToQuery[] )
3930 TCB_t * pxReturn = NULL;
3933 BaseType_t xBreakLoop;
3934 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
3935 ListItem_t * pxIterator;
3937 /* This function is called with the scheduler suspended. */
3939 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3941 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
3943 TCB_t * pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
3945 /* Check each character in the name looking for a match or
3947 xBreakLoop = pdFALSE;
3949 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3951 cNextChar = pxTCB->pcTaskName[ x ];
3953 if( cNextChar != pcNameToQuery[ x ] )
3955 /* Characters didn't match. */
3956 xBreakLoop = pdTRUE;
3958 else if( cNextChar == ( char ) 0x00 )
3960 /* Both strings terminated, a match must have been
3963 xBreakLoop = pdTRUE;
3967 mtCOVERAGE_TEST_MARKER();
3970 if( xBreakLoop != pdFALSE )
3976 if( pxReturn != NULL )
3978 /* The handle has been found. */
3985 mtCOVERAGE_TEST_MARKER();
3990 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3992 #endif /* INCLUDE_xTaskGetHandle */
3993 /*-----------------------------------------------------------*/
3995 #if ( INCLUDE_xTaskGetHandle == 1 )
3997 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3999 UBaseType_t uxQueue = configMAX_PRIORITIES;
4002 traceENTER_xTaskGetHandle( pcNameToQuery );
4004 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
4005 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
4009 /* Search the ready lists. */
4013 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
4017 /* Found the handle. */
4020 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4022 /* Search the delayed lists. */
4025 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
4030 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
4033 #if ( INCLUDE_vTaskSuspend == 1 )
4037 /* Search the suspended list. */
4038 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
4043 #if ( INCLUDE_vTaskDelete == 1 )
4047 /* Search the deleted list. */
4048 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
4053 ( void ) xTaskResumeAll();
4055 traceRETURN_xTaskGetHandle( pxTCB );
4060 #endif /* INCLUDE_xTaskGetHandle */
4061 /*-----------------------------------------------------------*/
4063 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4065 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4066 StackType_t ** ppuxStackBuffer,
4067 StaticTask_t ** ppxTaskBuffer )
4072 traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4074 configASSERT( ppuxStackBuffer != NULL );
4075 configASSERT( ppxTaskBuffer != NULL );
4077 pxTCB = prvGetTCBFromHandle( xTask );
4079 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4081 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4083 *ppuxStackBuffer = pxTCB->pxStack;
4084 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4087 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4089 *ppuxStackBuffer = pxTCB->pxStack;
4090 *ppxTaskBuffer = NULL;
4098 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4100 *ppuxStackBuffer = pxTCB->pxStack;
4101 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4104 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4106 traceRETURN_xTaskGetStaticBuffers( xReturn );
4111 #endif /* configSUPPORT_STATIC_ALLOCATION */
4112 /*-----------------------------------------------------------*/
4114 #if ( configUSE_TRACE_FACILITY == 1 )
4116 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4117 const UBaseType_t uxArraySize,
4118 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4120 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4122 traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4126 /* Is there a space in the array for each task in the system? */
4127 if( uxArraySize >= uxCurrentNumberOfTasks )
4129 /* Fill in an TaskStatus_t structure with information on each
4130 * task in the Ready state. */
4134 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4135 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4137 /* Fill in an TaskStatus_t structure with information on each
4138 * task in the Blocked state. */
4139 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4140 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4142 #if ( INCLUDE_vTaskDelete == 1 )
4144 /* Fill in an TaskStatus_t structure with information on
4145 * each task that has been deleted but not yet cleaned up. */
4146 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4150 #if ( INCLUDE_vTaskSuspend == 1 )
4152 /* Fill in an TaskStatus_t structure with information on
4153 * each task in the Suspended state. */
4154 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4158 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4160 if( pulTotalRunTime != NULL )
4162 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4163 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4165 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4169 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4171 if( pulTotalRunTime != NULL )
4173 *pulTotalRunTime = 0;
4176 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4180 mtCOVERAGE_TEST_MARKER();
4183 ( void ) xTaskResumeAll();
4185 traceRETURN_uxTaskGetSystemState( uxTask );
4190 #endif /* configUSE_TRACE_FACILITY */
4191 /*----------------------------------------------------------*/
4193 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4195 /* SMP_TODO : This function returns only idle task handle for core 0.
4196 * Consider to add another function to return the idle task handles. */
4197 TaskHandle_t xTaskGetIdleTaskHandle( void )
4199 traceENTER_xTaskGetIdleTaskHandle();
4201 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4202 * started, then xIdleTaskHandles will be NULL. */
4203 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4205 traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4207 return xIdleTaskHandles[ 0 ];
4210 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4211 /*----------------------------------------------------------*/
4213 /* This conditional compilation should use inequality to 0, not equality to 1.
4214 * This is to ensure vTaskStepTick() is available when user defined low power mode
4215 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4217 #if ( configUSE_TICKLESS_IDLE != 0 )
4219 void vTaskStepTick( TickType_t xTicksToJump )
4221 traceENTER_vTaskStepTick( xTicksToJump );
4223 /* Correct the tick count value after a period during which the tick
4224 * was suppressed. Note this does *not* call the tick hook function for
4225 * each stepped tick. */
4226 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
4228 if( ( xTickCount + xTicksToJump ) == xNextTaskUnblockTime )
4230 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4231 * xTaskIncrementTick() when the scheduler resumes. This ensures
4232 * that any delayed tasks are resumed at the correct time. */
4233 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4234 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4236 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4237 taskENTER_CRITICAL();
4241 taskEXIT_CRITICAL();
4246 mtCOVERAGE_TEST_MARKER();
4249 xTickCount += xTicksToJump;
4251 traceINCREASE_TICK_COUNT( xTicksToJump );
4252 traceRETURN_vTaskStepTick();
4255 #endif /* configUSE_TICKLESS_IDLE */
4256 /*----------------------------------------------------------*/
4258 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4260 BaseType_t xYieldOccurred;
4262 traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4264 /* Must not be called with the scheduler suspended as the implementation
4265 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4266 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4268 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4269 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4272 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4273 taskENTER_CRITICAL();
4275 xPendedTicks += xTicksToCatchUp;
4277 taskEXIT_CRITICAL();
4278 xYieldOccurred = xTaskResumeAll();
4280 traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4282 return xYieldOccurred;
4284 /*----------------------------------------------------------*/
4286 #if ( INCLUDE_xTaskAbortDelay == 1 )
4288 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4290 TCB_t * pxTCB = xTask;
4293 traceENTER_xTaskAbortDelay( xTask );
4295 configASSERT( pxTCB );
4299 /* A task can only be prematurely removed from the Blocked state if
4300 * it is actually in the Blocked state. */
4301 if( eTaskGetState( xTask ) == eBlocked )
4305 /* Remove the reference to the task from the blocked list. An
4306 * interrupt won't touch the xStateListItem because the
4307 * scheduler is suspended. */
4308 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4310 /* Is the task waiting on an event also? If so remove it from
4311 * the event list too. Interrupts can touch the event list item,
4312 * even though the scheduler is suspended, so a critical section
4314 taskENTER_CRITICAL();
4316 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4318 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4320 /* This lets the task know it was forcibly removed from the
4321 * blocked state so it should not re-evaluate its block time and
4322 * then block again. */
4323 pxTCB->ucDelayAborted = pdTRUE;
4327 mtCOVERAGE_TEST_MARKER();
4330 taskEXIT_CRITICAL();
4332 /* Place the unblocked task into the appropriate ready list. */
4333 prvAddTaskToReadyList( pxTCB );
4335 /* A task being unblocked cannot cause an immediate context
4336 * switch if preemption is turned off. */
4337 #if ( configUSE_PREEMPTION == 1 )
4339 #if ( configNUMBER_OF_CORES == 1 )
4341 /* Preemption is on, but a context switch should only be
4342 * performed if the unblocked task has a priority that is
4343 * higher than the currently executing task. */
4344 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4346 /* Pend the yield to be performed when the scheduler
4347 * is unsuspended. */
4348 xYieldPendings[ 0 ] = pdTRUE;
4352 mtCOVERAGE_TEST_MARKER();
4355 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4357 taskENTER_CRITICAL();
4359 prvYieldForTask( pxTCB );
4361 taskEXIT_CRITICAL();
4363 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4365 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4372 ( void ) xTaskResumeAll();
4374 traceRETURN_xTaskAbortDelay( xReturn );
4379 #endif /* INCLUDE_xTaskAbortDelay */
4380 /*----------------------------------------------------------*/
4382 BaseType_t xTaskIncrementTick( void )
4385 TickType_t xItemValue;
4386 BaseType_t xSwitchRequired = pdFALSE;
4388 #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4389 BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4390 #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4392 traceENTER_xTaskIncrementTick();
4394 /* Called by the portable layer each time a tick interrupt occurs.
4395 * Increments the tick then checks to see if the new tick value will cause any
4396 * tasks to be unblocked. */
4397 traceTASK_INCREMENT_TICK( xTickCount );
4399 /* Tick increment should occur on every kernel timer event. Core 0 has the
4400 * responsibility to increment the tick, or increment the pended ticks if the
4401 * scheduler is suspended. If pended ticks is greater than zero, the core that
4402 * calls xTaskResumeAll has the responsibility to increment the tick. */
4403 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4405 /* Minor optimisation. The tick count cannot change in this
4407 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4409 /* Increment the RTOS tick, switching the delayed and overflowed
4410 * delayed lists if it wraps to 0. */
4411 xTickCount = xConstTickCount;
4413 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
4415 taskSWITCH_DELAYED_LISTS();
4419 mtCOVERAGE_TEST_MARKER();
4422 /* See if this tick has made a timeout expire. Tasks are stored in
4423 * the queue in the order of their wake time - meaning once one task
4424 * has been found whose block time has not expired there is no need to
4425 * look any further down the list. */
4426 if( xConstTickCount >= xNextTaskUnblockTime )
4430 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4432 /* The delayed list is empty. Set xNextTaskUnblockTime
4433 * to the maximum possible value so it is extremely
4435 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4436 * next time through. */
4437 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4442 /* The delayed list is not empty, get the value of the
4443 * item at the head of the delayed list. This is the time
4444 * at which the task at the head of the delayed list must
4445 * be removed from the Blocked state. */
4446 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. */
4447 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4449 if( xConstTickCount < xItemValue )
4451 /* It is not time to unblock this item yet, but the
4452 * item value is the time at which the task at the head
4453 * of the blocked list must be removed from the Blocked
4454 * state - so record the item value in
4455 * xNextTaskUnblockTime. */
4456 xNextTaskUnblockTime = xItemValue;
4457 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
4461 mtCOVERAGE_TEST_MARKER();
4464 /* It is time to remove the item from the Blocked state. */
4465 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4467 /* Is the task waiting on an event also? If so remove
4468 * it from the event list. */
4469 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4471 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4475 mtCOVERAGE_TEST_MARKER();
4478 /* Place the unblocked task into the appropriate ready
4480 prvAddTaskToReadyList( pxTCB );
4482 /* A task being unblocked cannot cause an immediate
4483 * context switch if preemption is turned off. */
4484 #if ( configUSE_PREEMPTION == 1 )
4486 #if ( configNUMBER_OF_CORES == 1 )
4488 /* Preemption is on, but a context switch should
4489 * only be performed if the unblocked task's
4490 * priority is higher than the currently executing
4492 * The case of equal priority tasks sharing
4493 * processing time (which happens when both
4494 * preemption and time slicing are on) is
4496 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4498 xSwitchRequired = pdTRUE;
4502 mtCOVERAGE_TEST_MARKER();
4505 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4507 prvYieldForTask( pxTCB );
4509 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4511 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4516 /* Tasks of equal priority to the currently running task will share
4517 * processing time (time slice) if preemption is on, and the application
4518 * writer has not explicitly turned time slicing off. */
4519 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4521 #if ( configNUMBER_OF_CORES == 1 )
4523 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 )
4525 xSwitchRequired = pdTRUE;
4529 mtCOVERAGE_TEST_MARKER();
4532 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4536 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4538 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1 )
4540 xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4544 mtCOVERAGE_TEST_MARKER();
4548 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4550 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4552 #if ( configUSE_TICK_HOOK == 1 )
4554 /* Guard against the tick hook being called when the pended tick
4555 * count is being unwound (when the scheduler is being unlocked). */
4556 if( xPendedTicks == ( TickType_t ) 0 )
4558 vApplicationTickHook();
4562 mtCOVERAGE_TEST_MARKER();
4565 #endif /* configUSE_TICK_HOOK */
4567 #if ( configUSE_PREEMPTION == 1 )
4569 #if ( configNUMBER_OF_CORES == 1 )
4571 /* For single core the core ID is always 0. */
4572 if( xYieldPendings[ 0 ] != pdFALSE )
4574 xSwitchRequired = pdTRUE;
4578 mtCOVERAGE_TEST_MARKER();
4581 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4583 BaseType_t xCoreID, xCurrentCoreID;
4584 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4586 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4588 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4589 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4592 if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4594 if( xCoreID == xCurrentCoreID )
4596 xSwitchRequired = pdTRUE;
4600 prvYieldCore( xCoreID );
4605 mtCOVERAGE_TEST_MARKER();
4610 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4612 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4618 /* The tick hook gets called at regular intervals, even if the
4619 * scheduler is locked. */
4620 #if ( configUSE_TICK_HOOK == 1 )
4622 vApplicationTickHook();
4627 traceRETURN_xTaskIncrementTick( xSwitchRequired );
4629 return xSwitchRequired;
4631 /*-----------------------------------------------------------*/
4633 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4635 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4636 TaskHookFunction_t pxHookFunction )
4640 traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4642 /* If xTask is NULL then it is the task hook of the calling task that is
4646 xTCB = ( TCB_t * ) pxCurrentTCB;
4653 /* Save the hook function in the TCB. A critical section is required as
4654 * the value can be accessed from an interrupt. */
4655 taskENTER_CRITICAL();
4657 xTCB->pxTaskTag = pxHookFunction;
4659 taskEXIT_CRITICAL();
4661 traceRETURN_vTaskSetApplicationTaskTag();
4664 #endif /* configUSE_APPLICATION_TASK_TAG */
4665 /*-----------------------------------------------------------*/
4667 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4669 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4672 TaskHookFunction_t xReturn;
4674 traceENTER_xTaskGetApplicationTaskTag( xTask );
4676 /* If xTask is NULL then set the calling task's hook. */
4677 pxTCB = prvGetTCBFromHandle( xTask );
4679 /* Save the hook function in the TCB. A critical section is required as
4680 * the value can be accessed from an interrupt. */
4681 taskENTER_CRITICAL();
4683 xReturn = pxTCB->pxTaskTag;
4685 taskEXIT_CRITICAL();
4687 traceRETURN_xTaskGetApplicationTaskTag( xReturn );
4692 #endif /* configUSE_APPLICATION_TASK_TAG */
4693 /*-----------------------------------------------------------*/
4695 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4697 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
4700 TaskHookFunction_t xReturn;
4701 UBaseType_t uxSavedInterruptStatus;
4703 traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
4705 /* If xTask is NULL then set the calling task's hook. */
4706 pxTCB = prvGetTCBFromHandle( xTask );
4708 /* Save the hook function in the TCB. A critical section is required as
4709 * the value can be accessed from an interrupt. */
4710 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
4712 xReturn = pxTCB->pxTaskTag;
4714 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
4716 traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
4721 #endif /* configUSE_APPLICATION_TASK_TAG */
4722 /*-----------------------------------------------------------*/
4724 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4726 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
4727 void * pvParameter )
4732 traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
4734 /* If xTask is NULL then we are calling our own task hook. */
4737 xTCB = pxCurrentTCB;
4744 if( xTCB->pxTaskTag != NULL )
4746 xReturn = xTCB->pxTaskTag( pvParameter );
4753 traceRETURN_xTaskCallApplicationTaskHook( xReturn );
4758 #endif /* configUSE_APPLICATION_TASK_TAG */
4759 /*-----------------------------------------------------------*/
4761 #if ( configNUMBER_OF_CORES == 1 )
4762 void vTaskSwitchContext( void )
4764 traceENTER_vTaskSwitchContext();
4766 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
4768 /* The scheduler is currently suspended - do not allow a context
4770 xYieldPendings[ 0 ] = pdTRUE;
4774 xYieldPendings[ 0 ] = pdFALSE;
4775 traceTASK_SWITCHED_OUT();
4777 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4779 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4780 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
4782 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
4785 /* Add the amount of time the task has been running to the
4786 * accumulated time so far. The time the task started running was
4787 * stored in ulTaskSwitchedInTime. Note that there is no overflow
4788 * protection here so count values are only valid until the timer
4789 * overflows. The guard against negative values is to protect
4790 * against suspect run time stat counter implementations - which
4791 * are provided by the application, not the kernel. */
4792 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
4794 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
4798 mtCOVERAGE_TEST_MARKER();
4801 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
4803 #endif /* configGENERATE_RUN_TIME_STATS */
4805 /* Check for stack overflow, if configured. */
4806 taskCHECK_FOR_STACK_OVERFLOW();
4808 /* Before the currently running task is switched out, save its errno. */
4809 #if ( configUSE_POSIX_ERRNO == 1 )
4811 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
4815 /* Select a new task to run using either the generic C or port
4816 * optimised asm code. */
4817 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. */
4818 traceTASK_SWITCHED_IN();
4820 /* After the new task is switched in, update the global errno. */
4821 #if ( configUSE_POSIX_ERRNO == 1 )
4823 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
4827 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
4829 /* Switch C-Runtime's TLS Block to point to the TLS
4830 * Block specific to this task. */
4831 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
4836 traceRETURN_vTaskSwitchContext();
4838 #else /* if ( configNUMBER_OF_CORES == 1 ) */
4839 void vTaskSwitchContext( BaseType_t xCoreID )
4841 traceENTER_vTaskSwitchContext();
4843 /* Acquire both locks:
4844 * - The ISR lock protects the ready list from simultaneous access by
4845 * both other ISRs and tasks.
4846 * - We also take the task lock to pause here in case another core has
4847 * suspended the scheduler. We don't want to simply set xYieldPending
4848 * and move on if another core suspended the scheduler. We should only
4849 * do that if the current core has suspended the scheduler. */
4851 portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
4854 /* vTaskSwitchContext() must never be called from within a critical section.
4855 * This is not necessarily true for single core FreeRTOS, but it is for this
4857 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
4859 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
4861 /* The scheduler is currently suspended - do not allow a context
4863 xYieldPendings[ xCoreID ] = pdTRUE;
4867 xYieldPendings[ xCoreID ] = pdFALSE;
4868 traceTASK_SWITCHED_OUT();
4870 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4872 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4873 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
4875 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
4878 /* Add the amount of time the task has been running to the
4879 * accumulated time so far. The time the task started running was
4880 * stored in ulTaskSwitchedInTime. Note that there is no overflow
4881 * protection here so count values are only valid until the timer
4882 * overflows. The guard against negative values is to protect
4883 * against suspect run time stat counter implementations - which
4884 * are provided by the application, not the kernel. */
4885 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
4887 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
4891 mtCOVERAGE_TEST_MARKER();
4894 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
4896 #endif /* configGENERATE_RUN_TIME_STATS */
4898 /* Check for stack overflow, if configured. */
4899 taskCHECK_FOR_STACK_OVERFLOW();
4901 /* Before the currently running task is switched out, save its errno. */
4902 #if ( configUSE_POSIX_ERRNO == 1 )
4904 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
4908 /* Select a new task to run. */
4909 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
4910 traceTASK_SWITCHED_IN();
4912 /* After the new task is switched in, update the global errno. */
4913 #if ( configUSE_POSIX_ERRNO == 1 )
4915 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
4919 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
4921 /* Switch C-Runtime's TLS Block to point to the TLS
4922 * Block specific to this task. */
4923 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
4928 portRELEASE_ISR_LOCK();
4929 portRELEASE_TASK_LOCK();
4931 traceRETURN_vTaskSwitchContext();
4933 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
4934 /*-----------------------------------------------------------*/
4936 void vTaskPlaceOnEventList( List_t * const pxEventList,
4937 const TickType_t xTicksToWait )
4939 traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
4941 configASSERT( pxEventList );
4943 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
4944 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
4946 /* Place the event list item of the TCB in the appropriate event list.
4947 * This is placed in the list in priority order so the highest priority task
4948 * is the first to be woken by the event.
4950 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
4951 * Normally, the xItemValue of a TCB's ListItem_t members is:
4952 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
4953 * Therefore, the event list is sorted in descending priority order.
4955 * The queue that contains the event list is locked, preventing
4956 * simultaneous access from interrupts. */
4957 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4959 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4961 traceRETURN_vTaskPlaceOnEventList();
4963 /*-----------------------------------------------------------*/
4965 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
4966 const TickType_t xItemValue,
4967 const TickType_t xTicksToWait )
4969 traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
4971 configASSERT( pxEventList );
4973 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
4974 * the event groups implementation. */
4975 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4977 /* Store the item value in the event list item. It is safe to access the
4978 * event list item here as interrupts won't access the event list item of a
4979 * task that is not in the Blocked state. */
4980 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4982 /* Place the event list item of the TCB at the end of the appropriate event
4983 * list. It is safe to access the event list here because it is part of an
4984 * event group implementation - and interrupts don't access event groups
4985 * directly (instead they access them indirectly by pending function calls to
4986 * the task level). */
4987 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4989 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4991 traceRETURN_vTaskPlaceOnUnorderedEventList();
4993 /*-----------------------------------------------------------*/
4995 #if ( configUSE_TIMERS == 1 )
4997 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
4998 TickType_t xTicksToWait,
4999 const BaseType_t xWaitIndefinitely )
5001 traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
5003 configASSERT( pxEventList );
5005 /* This function should not be called by application code hence the
5006 * 'Restricted' in its name. It is not part of the public API. It is
5007 * designed for use by kernel code, and has special calling requirements -
5008 * it should be called with the scheduler suspended. */
5011 /* Place the event list item of the TCB in the appropriate event list.
5012 * In this case it is assume that this is the only task that is going to
5013 * be waiting on this event list, so the faster vListInsertEnd() function
5014 * can be used in place of vListInsert. */
5015 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5017 /* If the task should block indefinitely then set the block time to a
5018 * value that will be recognised as an indefinite delay inside the
5019 * prvAddCurrentTaskToDelayedList() function. */
5020 if( xWaitIndefinitely != pdFALSE )
5022 xTicksToWait = portMAX_DELAY;
5025 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
5026 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
5028 traceRETURN_vTaskPlaceOnEventListRestricted();
5031 #endif /* configUSE_TIMERS */
5032 /*-----------------------------------------------------------*/
5034 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
5036 TCB_t * pxUnblockedTCB;
5039 traceENTER_xTaskRemoveFromEventList( pxEventList );
5041 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
5042 * called from a critical section within an ISR. */
5044 /* The event list is sorted in priority order, so the first in the list can
5045 * be removed as it is known to be the highest priority. Remove the TCB from
5046 * the delayed list, and add it to the ready list.
5048 * If an event is for a queue that is locked then this function will never
5049 * get called - the lock count on the queue will get modified instead. This
5050 * means exclusive access to the event list is guaranteed here.
5052 * This function assumes that a check has already been made to ensure that
5053 * pxEventList is not empty. */
5054 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. */
5055 configASSERT( pxUnblockedTCB );
5056 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5058 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5060 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5061 prvAddTaskToReadyList( pxUnblockedTCB );
5063 #if ( configUSE_TICKLESS_IDLE != 0 )
5065 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5066 * might be set to the blocked task's time out time. If the task is
5067 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5068 * normally left unchanged, because it is automatically reset to a new
5069 * value when the tick count equals xNextTaskUnblockTime. However if
5070 * tickless idling is used it might be more important to enter sleep mode
5071 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5072 * ensure it is updated at the earliest possible time. */
5073 prvResetNextTaskUnblockTime();
5079 /* The delayed and ready lists cannot be accessed, so hold this task
5080 * pending until the scheduler is resumed. */
5081 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5084 #if ( configNUMBER_OF_CORES == 1 )
5086 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5088 /* Return true if the task removed from the event list has a higher
5089 * priority than the calling task. This allows the calling task to know if
5090 * it should force a context switch now. */
5093 /* Mark that a yield is pending in case the user is not using the
5094 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5095 xYieldPendings[ 0 ] = pdTRUE;
5102 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5106 #if ( configUSE_PREEMPTION == 1 )
5108 prvYieldForTask( pxUnblockedTCB );
5110 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5115 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5117 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5119 traceRETURN_xTaskRemoveFromEventList( xReturn );
5122 /*-----------------------------------------------------------*/
5124 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5125 const TickType_t xItemValue )
5127 TCB_t * pxUnblockedTCB;
5129 traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5131 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5132 * the event flags implementation. */
5133 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5135 /* Store the new item value in the event list. */
5136 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5138 /* Remove the event list form the event flag. Interrupts do not access
5140 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. */
5141 configASSERT( pxUnblockedTCB );
5142 listREMOVE_ITEM( pxEventListItem );
5144 #if ( configUSE_TICKLESS_IDLE != 0 )
5146 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5147 * might be set to the blocked task's time out time. If the task is
5148 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5149 * normally left unchanged, because it is automatically reset to a new
5150 * value when the tick count equals xNextTaskUnblockTime. However if
5151 * tickless idling is used it might be more important to enter sleep mode
5152 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5153 * ensure it is updated at the earliest possible time. */
5154 prvResetNextTaskUnblockTime();
5158 /* Remove the task from the delayed list and add it to the ready list. The
5159 * scheduler is suspended so interrupts will not be accessing the ready
5161 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5162 prvAddTaskToReadyList( pxUnblockedTCB );
5164 #if ( configNUMBER_OF_CORES == 1 )
5166 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5168 /* The unblocked task has a priority above that of the calling task, so
5169 * a context switch is required. This function is called with the
5170 * scheduler suspended so xYieldPending is set so the context switch
5171 * occurs immediately that the scheduler is resumed (unsuspended). */
5172 xYieldPendings[ 0 ] = pdTRUE;
5175 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5177 #if ( configUSE_PREEMPTION == 1 )
5179 taskENTER_CRITICAL();
5181 prvYieldForTask( pxUnblockedTCB );
5183 taskEXIT_CRITICAL();
5187 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5189 traceRETURN_vTaskRemoveFromUnorderedEventList();
5191 /*-----------------------------------------------------------*/
5193 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5195 traceENTER_vTaskSetTimeOutState( pxTimeOut );
5197 configASSERT( pxTimeOut );
5198 taskENTER_CRITICAL();
5200 pxTimeOut->xOverflowCount = xNumOfOverflows;
5201 pxTimeOut->xTimeOnEntering = xTickCount;
5203 taskEXIT_CRITICAL();
5205 traceRETURN_vTaskSetTimeOutState();
5207 /*-----------------------------------------------------------*/
5209 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5211 traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5213 /* For internal use only as it does not use a critical section. */
5214 pxTimeOut->xOverflowCount = xNumOfOverflows;
5215 pxTimeOut->xTimeOnEntering = xTickCount;
5217 traceRETURN_vTaskInternalSetTimeOutState();
5219 /*-----------------------------------------------------------*/
5221 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5222 TickType_t * const pxTicksToWait )
5226 traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5228 configASSERT( pxTimeOut );
5229 configASSERT( pxTicksToWait );
5231 taskENTER_CRITICAL();
5233 /* Minor optimisation. The tick count cannot change in this block. */
5234 const TickType_t xConstTickCount = xTickCount;
5235 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5237 #if ( INCLUDE_xTaskAbortDelay == 1 )
5238 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5240 /* The delay was aborted, which is not the same as a time out,
5241 * but has the same result. */
5242 pxCurrentTCB->ucDelayAborted = pdFALSE;
5248 #if ( INCLUDE_vTaskSuspend == 1 )
5249 if( *pxTicksToWait == portMAX_DELAY )
5251 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5252 * specified is the maximum block time then the task should block
5253 * indefinitely, and therefore never time out. */
5259 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
5261 /* The tick count is greater than the time at which
5262 * vTaskSetTimeout() was called, but has also overflowed since
5263 * vTaskSetTimeOut() was called. It must have wrapped all the way
5264 * around and gone past again. This passed since vTaskSetTimeout()
5267 *pxTicksToWait = ( TickType_t ) 0;
5269 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
5271 /* Not a genuine timeout. Adjust parameters for time remaining. */
5272 *pxTicksToWait -= xElapsedTime;
5273 vTaskInternalSetTimeOutState( pxTimeOut );
5278 *pxTicksToWait = ( TickType_t ) 0;
5282 taskEXIT_CRITICAL();
5284 traceRETURN_xTaskCheckForTimeOut( xReturn );
5288 /*-----------------------------------------------------------*/
5290 void vTaskMissedYield( void )
5292 traceENTER_vTaskMissedYield();
5294 /* Must be called from within a critical section. */
5295 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5297 traceRETURN_vTaskMissedYield();
5299 /*-----------------------------------------------------------*/
5301 #if ( configUSE_TRACE_FACILITY == 1 )
5303 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5305 UBaseType_t uxReturn;
5306 TCB_t const * pxTCB;
5308 traceENTER_uxTaskGetTaskNumber( xTask );
5313 uxReturn = pxTCB->uxTaskNumber;
5320 traceRETURN_uxTaskGetTaskNumber( uxReturn );
5325 #endif /* configUSE_TRACE_FACILITY */
5326 /*-----------------------------------------------------------*/
5328 #if ( configUSE_TRACE_FACILITY == 1 )
5330 void vTaskSetTaskNumber( TaskHandle_t xTask,
5331 const UBaseType_t uxHandle )
5335 traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5340 pxTCB->uxTaskNumber = uxHandle;
5343 traceRETURN_vTaskSetTaskNumber();
5346 #endif /* configUSE_TRACE_FACILITY */
5347 /*-----------------------------------------------------------*/
5350 * -----------------------------------------------------------
5351 * The MinimalIdle task.
5352 * ----------------------------------------------------------
5354 * The minimal idle task is used for all the additional cores in a SMP
5355 * system. There must be only 1 idle task and the rest are minimal idle
5358 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5359 * language extensions. The equivalent prototype for this function is:
5361 * void prvMinimalIdleTask( void *pvParameters );
5364 #if ( configNUMBER_OF_CORES > 1 )
5365 static portTASK_FUNCTION( prvMinimalIdleTask, pvParameters )
5367 ( void ) pvParameters;
5371 for( ; configCONTROL_INFINITE_LOOP(); )
5373 #if ( configUSE_PREEMPTION == 0 )
5375 /* If we are not using preemption we keep forcing a task switch to
5376 * see if any other task has become available. If we are using
5377 * preemption we don't need to do this as any task becoming available
5378 * will automatically get the processor anyway. */
5381 #endif /* configUSE_PREEMPTION */
5383 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5385 /* When using preemption tasks of equal priority will be
5386 * timesliced. If a task that is sharing the idle priority is ready
5387 * to run then the idle task should yield before the end of the
5390 * A critical region is not required here as we are just reading from
5391 * the list, and an occasional incorrect value will not matter. If
5392 * the ready list at the idle priority contains one more task than the
5393 * number of idle tasks, which is equal to the configured numbers of cores
5394 * then a task other than the idle task is ready to execute. */
5395 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5401 mtCOVERAGE_TEST_MARKER();
5404 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5406 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
5408 /* Call the user defined function from within the idle task. This
5409 * allows the application designer to add background functionality
5410 * without the overhead of a separate task.
5412 * This hook is intended to manage core activity such as disabling cores that go idle.
5414 * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5415 * CALL A FUNCTION THAT MIGHT BLOCK. */
5416 vApplicationMinimalIdleHook();
5418 #endif /* configUSE_MINIMAL_IDLE_HOOK */
5421 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5424 * -----------------------------------------------------------
5426 * ----------------------------------------------------------
5428 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5429 * language extensions. The equivalent prototype for this function is:
5431 * void prvIdleTask( void *pvParameters );
5435 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5437 /* Stop warnings. */
5438 ( void ) pvParameters;
5440 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5441 * SCHEDULER IS STARTED. **/
5443 /* In case a task that has a secure context deletes itself, in which case
5444 * the idle task is responsible for deleting the task's secure context, if
5446 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5448 #if ( configNUMBER_OF_CORES > 1 )
5450 /* SMP all cores start up in the idle task. This initial yield gets the application
5454 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5456 for( ; configCONTROL_INFINITE_LOOP(); )
5458 /* See if any tasks have deleted themselves - if so then the idle task
5459 * is responsible for freeing the deleted task's TCB and stack. */
5460 prvCheckTasksWaitingTermination();
5462 #if ( configUSE_PREEMPTION == 0 )
5464 /* If we are not using preemption we keep forcing a task switch to
5465 * see if any other task has become available. If we are using
5466 * preemption we don't need to do this as any task becoming available
5467 * will automatically get the processor anyway. */
5470 #endif /* configUSE_PREEMPTION */
5472 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5474 /* When using preemption tasks of equal priority will be
5475 * timesliced. If a task that is sharing the idle priority is ready
5476 * to run then the idle task should yield before the end of the
5479 * A critical region is not required here as we are just reading from
5480 * the list, and an occasional incorrect value will not matter. If
5481 * the ready list at the idle priority contains one more task than the
5482 * number of idle tasks, which is equal to the configured numbers of cores
5483 * then a task other than the idle task is ready to execute. */
5484 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5490 mtCOVERAGE_TEST_MARKER();
5493 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5495 #if ( configUSE_IDLE_HOOK == 1 )
5497 /* Call the user defined function from within the idle task. */
5498 vApplicationIdleHook();
5500 #endif /* configUSE_IDLE_HOOK */
5502 /* This conditional compilation should use inequality to 0, not equality
5503 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5504 * user defined low power mode implementations require
5505 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5506 #if ( configUSE_TICKLESS_IDLE != 0 )
5508 TickType_t xExpectedIdleTime;
5510 /* It is not desirable to suspend then resume the scheduler on
5511 * each iteration of the idle task. Therefore, a preliminary
5512 * test of the expected idle time is performed without the
5513 * scheduler suspended. The result here is not necessarily
5515 xExpectedIdleTime = prvGetExpectedIdleTime();
5517 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5521 /* Now the scheduler is suspended, the expected idle
5522 * time can be sampled again, and this time its value can
5524 configASSERT( xNextTaskUnblockTime >= xTickCount );
5525 xExpectedIdleTime = prvGetExpectedIdleTime();
5527 /* Define the following macro to set xExpectedIdleTime to 0
5528 * if the application does not want
5529 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5530 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5532 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5534 traceLOW_POWER_IDLE_BEGIN();
5535 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5536 traceLOW_POWER_IDLE_END();
5540 mtCOVERAGE_TEST_MARKER();
5543 ( void ) xTaskResumeAll();
5547 mtCOVERAGE_TEST_MARKER();
5550 #endif /* configUSE_TICKLESS_IDLE */
5552 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_MINIMAL_IDLE_HOOK == 1 ) )
5554 /* Call the user defined function from within the idle task. This
5555 * allows the application designer to add background functionality
5556 * without the overhead of a separate task.
5558 * This hook is intended to manage core activity such as disabling cores that go idle.
5560 * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5561 * CALL A FUNCTION THAT MIGHT BLOCK. */
5562 vApplicationMinimalIdleHook();
5564 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_MINIMAL_IDLE_HOOK == 1 ) ) */
5567 /*-----------------------------------------------------------*/
5569 #if ( configUSE_TICKLESS_IDLE != 0 )
5571 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5573 #if ( INCLUDE_vTaskSuspend == 1 )
5574 /* The idle task exists in addition to the application tasks. */
5575 const UBaseType_t uxNonApplicationTasks = 1;
5576 #endif /* INCLUDE_vTaskSuspend */
5578 eSleepModeStatus eReturn = eStandardSleep;
5580 traceENTER_eTaskConfirmSleepModeStatus();
5582 /* This function must be called from a critical section. */
5584 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
5586 /* A task was made ready while the scheduler was suspended. */
5587 eReturn = eAbortSleep;
5589 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5591 /* A yield was pended while the scheduler was suspended. */
5592 eReturn = eAbortSleep;
5594 else if( xPendedTicks != 0 )
5596 /* A tick interrupt has already occurred but was held pending
5597 * because the scheduler is suspended. */
5598 eReturn = eAbortSleep;
5601 #if ( INCLUDE_vTaskSuspend == 1 )
5602 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5604 /* If all the tasks are in the suspended list (which might mean they
5605 * have an infinite block time rather than actually being suspended)
5606 * then it is safe to turn all clocks off and just wait for external
5608 eReturn = eNoTasksWaitingTimeout;
5610 #endif /* INCLUDE_vTaskSuspend */
5613 mtCOVERAGE_TEST_MARKER();
5616 traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5621 #endif /* configUSE_TICKLESS_IDLE */
5622 /*-----------------------------------------------------------*/
5624 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5626 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5632 traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
5634 if( ( xIndex >= 0 ) &&
5635 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5637 pxTCB = prvGetTCBFromHandle( xTaskToSet );
5638 configASSERT( pxTCB != NULL );
5639 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5642 traceRETURN_vTaskSetThreadLocalStoragePointer();
5645 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5646 /*-----------------------------------------------------------*/
5648 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5650 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5653 void * pvReturn = NULL;
5656 traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
5658 if( ( xIndex >= 0 ) &&
5659 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5661 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
5662 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
5669 traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
5674 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5675 /*-----------------------------------------------------------*/
5677 #if ( portUSING_MPU_WRAPPERS == 1 )
5679 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
5680 const MemoryRegion_t * const pxRegions )
5684 traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
5686 /* If null is passed in here then we are modifying the MPU settings of
5687 * the calling task. */
5688 pxTCB = prvGetTCBFromHandle( xTaskToModify );
5690 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
5692 traceRETURN_vTaskAllocateMPURegions();
5695 #endif /* portUSING_MPU_WRAPPERS */
5696 /*-----------------------------------------------------------*/
5698 static void prvInitialiseTaskLists( void )
5700 UBaseType_t uxPriority;
5702 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
5704 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
5707 vListInitialise( &xDelayedTaskList1 );
5708 vListInitialise( &xDelayedTaskList2 );
5709 vListInitialise( &xPendingReadyList );
5711 #if ( INCLUDE_vTaskDelete == 1 )
5713 vListInitialise( &xTasksWaitingTermination );
5715 #endif /* INCLUDE_vTaskDelete */
5717 #if ( INCLUDE_vTaskSuspend == 1 )
5719 vListInitialise( &xSuspendedTaskList );
5721 #endif /* INCLUDE_vTaskSuspend */
5723 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
5725 pxDelayedTaskList = &xDelayedTaskList1;
5726 pxOverflowDelayedTaskList = &xDelayedTaskList2;
5728 /*-----------------------------------------------------------*/
5730 static void prvCheckTasksWaitingTermination( void )
5732 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
5734 #if ( INCLUDE_vTaskDelete == 1 )
5738 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
5739 * being called too often in the idle task. */
5740 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
5742 #if ( configNUMBER_OF_CORES == 1 )
5744 taskENTER_CRITICAL();
5747 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. */
5748 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5749 --uxCurrentNumberOfTasks;
5750 --uxDeletedTasksWaitingCleanUp;
5753 taskEXIT_CRITICAL();
5755 prvDeleteTCB( pxTCB );
5757 #else /* #if( configNUMBER_OF_CORES == 1 ) */
5761 taskENTER_CRITICAL();
5763 /* For SMP, multiple idles can be running simultaneously
5764 * and we need to check that other idles did not cleanup while we were
5765 * waiting to enter the critical section. */
5766 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
5768 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. */
5770 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
5772 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5773 --uxCurrentNumberOfTasks;
5774 --uxDeletedTasksWaitingCleanUp;
5778 /* The TCB to be deleted still has not yet been switched out
5779 * by the scheduler, so we will just exit this loop early and
5780 * try again next time. */
5781 taskEXIT_CRITICAL();
5786 taskEXIT_CRITICAL();
5790 prvDeleteTCB( pxTCB );
5793 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
5796 #endif /* INCLUDE_vTaskDelete */
5798 /*-----------------------------------------------------------*/
5800 #if ( configUSE_TRACE_FACILITY == 1 )
5802 void vTaskGetInfo( TaskHandle_t xTask,
5803 TaskStatus_t * pxTaskStatus,
5804 BaseType_t xGetFreeStackSpace,
5809 traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
5811 /* xTask is NULL then get the state of the calling task. */
5812 pxTCB = prvGetTCBFromHandle( xTask );
5814 pxTaskStatus->xHandle = pxTCB;
5815 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
5816 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
5817 pxTaskStatus->pxStackBase = pxTCB->pxStack;
5818 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
5819 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
5820 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
5822 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
5824 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
5826 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
5830 #if ( configUSE_MUTEXES == 1 )
5832 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
5836 pxTaskStatus->uxBasePriority = 0;
5840 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5842 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
5846 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
5850 /* Obtaining the task state is a little fiddly, so is only done if the
5851 * value of eState passed into this function is eInvalid - otherwise the
5852 * state is just set to whatever is passed in. */
5853 if( eState != eInvalid )
5855 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
5857 pxTaskStatus->eCurrentState = eRunning;
5861 pxTaskStatus->eCurrentState = eState;
5863 #if ( INCLUDE_vTaskSuspend == 1 )
5865 /* If the task is in the suspended list then there is a
5866 * chance it is actually just blocked indefinitely - so really
5867 * it should be reported as being in the Blocked state. */
5868 if( eState == eSuspended )
5872 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
5874 pxTaskStatus->eCurrentState = eBlocked;
5877 ( void ) xTaskResumeAll();
5880 #endif /* INCLUDE_vTaskSuspend */
5882 /* Tasks can be in pending ready list and other state list at the
5883 * same time. These tasks are in ready state no matter what state
5884 * list the task is in. */
5885 taskENTER_CRITICAL();
5887 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
5889 pxTaskStatus->eCurrentState = eReady;
5892 taskEXIT_CRITICAL();
5897 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
5900 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
5901 * parameter is provided to allow it to be skipped. */
5902 if( xGetFreeStackSpace != pdFALSE )
5904 #if ( portSTACK_GROWTH > 0 )
5906 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
5910 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
5916 pxTaskStatus->usStackHighWaterMark = 0;
5919 traceRETURN_vTaskGetInfo();
5922 #endif /* configUSE_TRACE_FACILITY */
5923 /*-----------------------------------------------------------*/
5925 #if ( configUSE_TRACE_FACILITY == 1 )
5927 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
5931 configLIST_VOLATILE TCB_t * pxNextTCB;
5932 configLIST_VOLATILE TCB_t * pxFirstTCB;
5933 UBaseType_t uxTask = 0;
5935 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
5937 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. */
5939 /* Populate an TaskStatus_t structure within the
5940 * pxTaskStatusArray array for each task that is referenced from
5941 * pxList. See the definition of TaskStatus_t in task.h for the
5942 * meaning of each TaskStatus_t structure member. */
5945 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. */
5946 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
5948 } while( pxNextTCB != pxFirstTCB );
5952 mtCOVERAGE_TEST_MARKER();
5958 #endif /* configUSE_TRACE_FACILITY */
5959 /*-----------------------------------------------------------*/
5961 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
5963 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
5965 uint32_t ulCount = 0U;
5967 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
5969 pucStackByte -= portSTACK_GROWTH;
5973 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
5975 return ( configSTACK_DEPTH_TYPE ) ulCount;
5978 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
5979 /*-----------------------------------------------------------*/
5981 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
5983 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
5984 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
5985 * user to determine the return type. It gets around the problem of the value
5986 * overflowing on 8-bit types without breaking backward compatibility for
5987 * applications that expect an 8-bit return type. */
5988 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
5991 uint8_t * pucEndOfStack;
5992 configSTACK_DEPTH_TYPE uxReturn;
5994 traceENTER_uxTaskGetStackHighWaterMark2( xTask );
5996 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
5997 * the same except for their return type. Using configSTACK_DEPTH_TYPE
5998 * allows the user to determine the return type. It gets around the
5999 * problem of the value overflowing on 8-bit types without breaking
6000 * backward compatibility for applications that expect an 8-bit return
6003 pxTCB = prvGetTCBFromHandle( xTask );
6005 #if portSTACK_GROWTH < 0
6007 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6011 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6015 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
6017 traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
6022 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
6023 /*-----------------------------------------------------------*/
6025 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
6027 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
6030 uint8_t * pucEndOfStack;
6031 UBaseType_t uxReturn;
6033 traceENTER_uxTaskGetStackHighWaterMark( xTask );
6035 pxTCB = prvGetTCBFromHandle( xTask );
6037 #if portSTACK_GROWTH < 0
6039 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6043 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6047 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
6049 traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6054 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6055 /*-----------------------------------------------------------*/
6057 #if ( INCLUDE_vTaskDelete == 1 )
6059 static void prvDeleteTCB( TCB_t * pxTCB )
6061 /* This call is required specifically for the TriCore port. It must be
6062 * above the vPortFree() calls. The call is also used by ports/demos that
6063 * want to allocate and clean RAM statically. */
6064 portCLEAN_UP_TCB( pxTCB );
6066 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6068 /* Free up the memory allocated for the task's TLS Block. */
6069 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6073 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6075 /* The task can only have been allocated dynamically - free both
6076 * the stack and TCB. */
6077 vPortFreeStack( pxTCB->pxStack );
6080 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
6082 /* The task could have been allocated statically or dynamically, so
6083 * check what was statically allocated before trying to free the
6085 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6087 /* Both the stack and TCB were allocated dynamically, so both
6089 vPortFreeStack( pxTCB->pxStack );
6092 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6094 /* Only the stack was statically allocated, so the TCB is the
6095 * only memory that must be freed. */
6100 /* Neither the stack nor the TCB were allocated dynamically, so
6101 * nothing needs to be freed. */
6102 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6103 mtCOVERAGE_TEST_MARKER();
6106 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6109 #endif /* INCLUDE_vTaskDelete */
6110 /*-----------------------------------------------------------*/
6112 static void prvResetNextTaskUnblockTime( void )
6114 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6116 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
6117 * the maximum possible value so it is extremely unlikely that the
6118 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6119 * there is an item in the delayed list. */
6120 xNextTaskUnblockTime = portMAX_DELAY;
6124 /* The new current delayed list is not empty, get the value of
6125 * the item at the head of the delayed list. This is the time at
6126 * which the task at the head of the delayed list should be removed
6127 * from the Blocked state. */
6128 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6131 /*-----------------------------------------------------------*/
6133 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6135 #if ( configNUMBER_OF_CORES == 1 )
6136 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6138 TaskHandle_t xReturn;
6140 traceENTER_xTaskGetCurrentTaskHandle();
6142 /* A critical section is not required as this is not called from
6143 * an interrupt and the current TCB will always be the same for any
6144 * individual execution thread. */
6145 xReturn = pxCurrentTCB;
6147 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6151 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6152 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6154 TaskHandle_t xReturn;
6155 UBaseType_t uxSavedInterruptStatus;
6157 traceENTER_xTaskGetCurrentTaskHandle();
6159 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6161 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6163 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6165 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6170 TaskHandle_t xTaskGetCurrentTaskHandleCPU( BaseType_t xCoreID )
6172 TaskHandle_t xReturn = NULL;
6174 traceENTER_xTaskGetCurrentTaskHandleCPU( xCoreID );
6176 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6178 xReturn = pxCurrentTCBs[ xCoreID ];
6181 traceRETURN_xTaskGetCurrentTaskHandleCPU( xReturn );
6185 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6187 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
6188 /*-----------------------------------------------------------*/
6190 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6192 BaseType_t xTaskGetSchedulerState( void )
6196 traceENTER_xTaskGetSchedulerState();
6198 if( xSchedulerRunning == pdFALSE )
6200 xReturn = taskSCHEDULER_NOT_STARTED;
6204 #if ( configNUMBER_OF_CORES > 1 )
6205 taskENTER_CRITICAL();
6208 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6210 xReturn = taskSCHEDULER_RUNNING;
6214 xReturn = taskSCHEDULER_SUSPENDED;
6217 #if ( configNUMBER_OF_CORES > 1 )
6218 taskEXIT_CRITICAL();
6222 traceRETURN_xTaskGetSchedulerState( xReturn );
6227 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6228 /*-----------------------------------------------------------*/
6230 #if ( configUSE_MUTEXES == 1 )
6232 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6234 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6235 BaseType_t xReturn = pdFALSE;
6237 traceENTER_xTaskPriorityInherit( pxMutexHolder );
6239 /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6240 * inheritance is not applied in this scenario. */
6241 if( pxMutexHolder != NULL )
6243 /* If the holder of the mutex has a priority below the priority of
6244 * the task attempting to obtain the mutex then it will temporarily
6245 * inherit the priority of the task attempting to obtain the mutex. */
6246 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6248 /* Adjust the mutex holder state to account for its new
6249 * priority. Only reset the event list item value if the value is
6250 * not being used for anything else. */
6251 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
6253 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. */
6257 mtCOVERAGE_TEST_MARKER();
6260 /* If the task being modified is in the ready state it will need
6261 * to be moved into a new list. */
6262 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6264 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6266 /* It is known that the task is in its ready list so
6267 * there is no need to check again and the port level
6268 * reset macro can be called directly. */
6269 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6273 mtCOVERAGE_TEST_MARKER();
6276 /* Inherit the priority before being moved into the new list. */
6277 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6278 prvAddTaskToReadyList( pxMutexHolderTCB );
6279 #if ( configNUMBER_OF_CORES > 1 )
6281 /* The priority of the task is raised. Yield for this task
6282 * if it is not running. */
6283 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6285 prvYieldForTask( pxMutexHolderTCB );
6288 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6292 /* Just inherit the priority. */
6293 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6296 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6298 /* Inheritance occurred. */
6303 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6305 /* The base priority of the mutex holder is lower than the
6306 * priority of the task attempting to take the mutex, but the
6307 * current priority of the mutex holder is not lower than the
6308 * priority of the task attempting to take the mutex.
6309 * Therefore the mutex holder must have already inherited a
6310 * priority, but inheritance would have occurred if that had
6311 * not been the case. */
6316 mtCOVERAGE_TEST_MARKER();
6322 mtCOVERAGE_TEST_MARKER();
6325 traceRETURN_xTaskPriorityInherit( xReturn );
6330 #endif /* configUSE_MUTEXES */
6331 /*-----------------------------------------------------------*/
6333 #if ( configUSE_MUTEXES == 1 )
6335 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6337 TCB_t * const pxTCB = pxMutexHolder;
6338 BaseType_t xReturn = pdFALSE;
6340 traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6342 if( pxMutexHolder != NULL )
6344 /* A task can only have an inherited priority if it holds the mutex.
6345 * If the mutex is held by a task then it cannot be given from an
6346 * interrupt, and if a mutex is given by the holding task then it must
6347 * be the running state task. */
6348 configASSERT( pxTCB == pxCurrentTCB );
6349 configASSERT( pxTCB->uxMutexesHeld );
6350 ( pxTCB->uxMutexesHeld )--;
6352 /* Has the holder of the mutex inherited the priority of another
6354 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6356 /* Only disinherit if no other mutexes are held. */
6357 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6359 /* A task can only have an inherited priority if it holds
6360 * the mutex. If the mutex is held by a task then it cannot be
6361 * given from an interrupt, and if a mutex is given by the
6362 * holding task then it must be the running state task. Remove
6363 * the holding task from the ready list. */
6364 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6366 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6370 mtCOVERAGE_TEST_MARKER();
6373 /* Disinherit the priority before adding the task into the
6374 * new ready list. */
6375 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6376 pxTCB->uxPriority = pxTCB->uxBasePriority;
6378 /* Reset the event list item value. It cannot be in use for
6379 * any other purpose if this task is running, and it must be
6380 * running to give back the mutex. */
6381 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. */
6382 prvAddTaskToReadyList( pxTCB );
6383 #if ( configNUMBER_OF_CORES > 1 )
6385 /* The priority of the task is dropped. Yield the core on
6386 * which the task is running. */
6387 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6389 prvYieldCore( pxTCB->xTaskRunState );
6392 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6394 /* Return true to indicate that a context switch is required.
6395 * This is only actually required in the corner case whereby
6396 * multiple mutexes were held and the mutexes were given back
6397 * in an order different to that in which they were taken.
6398 * If a context switch did not occur when the first mutex was
6399 * returned, even if a task was waiting on it, then a context
6400 * switch should occur when the last mutex is returned whether
6401 * a task is waiting on it or not. */
6406 mtCOVERAGE_TEST_MARKER();
6411 mtCOVERAGE_TEST_MARKER();
6416 mtCOVERAGE_TEST_MARKER();
6419 traceRETURN_xTaskPriorityDisinherit( xReturn );
6424 #endif /* configUSE_MUTEXES */
6425 /*-----------------------------------------------------------*/
6427 #if ( configUSE_MUTEXES == 1 )
6429 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6430 UBaseType_t uxHighestPriorityWaitingTask )
6432 TCB_t * const pxTCB = pxMutexHolder;
6433 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6434 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6436 traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6438 if( pxMutexHolder != NULL )
6440 /* If pxMutexHolder is not NULL then the holder must hold at least
6442 configASSERT( pxTCB->uxMutexesHeld );
6444 /* Determine the priority to which the priority of the task that
6445 * holds the mutex should be set. This will be the greater of the
6446 * holding task's base priority and the priority of the highest
6447 * priority task that is waiting to obtain the mutex. */
6448 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6450 uxPriorityToUse = uxHighestPriorityWaitingTask;
6454 uxPriorityToUse = pxTCB->uxBasePriority;
6457 /* Does the priority need to change? */
6458 if( pxTCB->uxPriority != uxPriorityToUse )
6460 /* Only disinherit if no other mutexes are held. This is a
6461 * simplification in the priority inheritance implementation. If
6462 * the task that holds the mutex is also holding other mutexes then
6463 * the other mutexes may have caused the priority inheritance. */
6464 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6466 /* If a task has timed out because it already holds the
6467 * mutex it was trying to obtain then it cannot of inherited
6468 * its own priority. */
6469 configASSERT( pxTCB != pxCurrentTCB );
6471 /* Disinherit the priority, remembering the previous
6472 * priority to facilitate determining the subject task's
6474 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6475 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6476 pxTCB->uxPriority = uxPriorityToUse;
6478 /* Only reset the event list item value if the value is not
6479 * being used for anything else. */
6480 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
6482 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. */
6486 mtCOVERAGE_TEST_MARKER();
6489 /* If the running task is not the task that holds the mutex
6490 * then the task that holds the mutex could be in either the
6491 * Ready, Blocked or Suspended states. Only remove the task
6492 * from its current state list if it is in the Ready state as
6493 * the task's priority is going to change and there is one
6494 * Ready list per priority. */
6495 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6497 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6499 /* It is known that the task is in its ready list so
6500 * there is no need to check again and the port level
6501 * reset macro can be called directly. */
6502 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6506 mtCOVERAGE_TEST_MARKER();
6509 prvAddTaskToReadyList( pxTCB );
6510 #if ( configNUMBER_OF_CORES > 1 )
6512 /* The priority of the task is dropped. Yield the core on
6513 * which the task is running. */
6514 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6516 prvYieldCore( pxTCB->xTaskRunState );
6519 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6523 mtCOVERAGE_TEST_MARKER();
6528 mtCOVERAGE_TEST_MARKER();
6533 mtCOVERAGE_TEST_MARKER();
6538 mtCOVERAGE_TEST_MARKER();
6541 traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6544 #endif /* configUSE_MUTEXES */
6545 /*-----------------------------------------------------------*/
6547 #if ( configNUMBER_OF_CORES > 1 )
6549 /* If not in a critical section then yield immediately.
6550 * Otherwise set xYieldPendings to true to wait to
6551 * yield until exiting the critical section.
6553 void vTaskYieldWithinAPI( void )
6555 traceENTER_vTaskYieldWithinAPI();
6557 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6563 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6566 traceRETURN_vTaskYieldWithinAPI();
6568 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6570 /*-----------------------------------------------------------*/
6572 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6574 void vTaskEnterCritical( void )
6576 traceENTER_vTaskEnterCritical();
6578 portDISABLE_INTERRUPTS();
6580 if( xSchedulerRunning != pdFALSE )
6582 ( pxCurrentTCB->uxCriticalNesting )++;
6584 /* This is not the interrupt safe version of the enter critical
6585 * function so assert() if it is being called from an interrupt
6586 * context. Only API functions that end in "FromISR" can be used in an
6587 * interrupt. Only assert if the critical nesting count is 1 to
6588 * protect against recursive calls if the assert function also uses a
6589 * critical section. */
6590 if( pxCurrentTCB->uxCriticalNesting == 1 )
6592 portASSERT_IF_IN_ISR();
6597 mtCOVERAGE_TEST_MARKER();
6600 traceRETURN_vTaskEnterCritical();
6603 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6604 /*-----------------------------------------------------------*/
6606 #if ( configNUMBER_OF_CORES > 1 )
6608 void vTaskEnterCritical( void )
6610 traceENTER_vTaskEnterCritical();
6612 portDISABLE_INTERRUPTS();
6614 if( xSchedulerRunning != pdFALSE )
6616 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6618 portGET_TASK_LOCK();
6622 portINCREMENT_CRITICAL_NESTING_COUNT();
6624 /* This is not the interrupt safe version of the enter critical
6625 * function so assert() if it is being called from an interrupt
6626 * context. Only API functions that end in "FromISR" can be used in an
6627 * interrupt. Only assert if the critical nesting count is 1 to
6628 * protect against recursive calls if the assert function also uses a
6629 * critical section. */
6630 if( portGET_CRITICAL_NESTING_COUNT() == 1U )
6632 portASSERT_IF_IN_ISR();
6634 if( uxSchedulerSuspended == 0U )
6636 /* The only time there would be a problem is if this is called
6637 * before a context switch and vTaskExitCritical() is called
6638 * after pxCurrentTCB changes. Therefore this should not be
6639 * used within vTaskSwitchContext(). */
6640 prvCheckForRunStateChange();
6646 mtCOVERAGE_TEST_MARKER();
6649 traceRETURN_vTaskEnterCritical();
6652 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6654 /*-----------------------------------------------------------*/
6656 #if ( configNUMBER_OF_CORES > 1 )
6658 UBaseType_t vTaskEnterCriticalFromISR( void )
6660 UBaseType_t uxSavedInterruptStatus = 0;
6662 traceENTER_vTaskEnterCriticalFromISR();
6664 if( xSchedulerRunning != pdFALSE )
6666 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6668 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6673 portINCREMENT_CRITICAL_NESTING_COUNT();
6677 mtCOVERAGE_TEST_MARKER();
6680 traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
6682 return uxSavedInterruptStatus;
6685 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6686 /*-----------------------------------------------------------*/
6688 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6690 void vTaskExitCritical( void )
6692 traceENTER_vTaskExitCritical();
6694 if( xSchedulerRunning != pdFALSE )
6696 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
6697 * does not match a previous call to vTaskEnterCritical(). */
6698 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
6700 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
6701 * to exit critical section from ISR. */
6702 portASSERT_IF_IN_ISR();
6704 if( pxCurrentTCB->uxCriticalNesting > 0U )
6706 ( pxCurrentTCB->uxCriticalNesting )--;
6708 if( pxCurrentTCB->uxCriticalNesting == 0U )
6710 portENABLE_INTERRUPTS();
6714 mtCOVERAGE_TEST_MARKER();
6719 mtCOVERAGE_TEST_MARKER();
6724 mtCOVERAGE_TEST_MARKER();
6727 traceRETURN_vTaskExitCritical();
6730 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6731 /*-----------------------------------------------------------*/
6733 #if ( configNUMBER_OF_CORES > 1 )
6735 void vTaskExitCritical( void )
6737 traceENTER_vTaskExitCritical();
6739 if( xSchedulerRunning != pdFALSE )
6741 /* If critical nesting count is zero then this function
6742 * does not match a previous call to vTaskEnterCritical(). */
6743 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
6745 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
6746 * to exit critical section from ISR. */
6747 portASSERT_IF_IN_ISR();
6749 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
6751 portDECREMENT_CRITICAL_NESTING_COUNT();
6753 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6755 BaseType_t xYieldCurrentTask;
6757 /* Get the xYieldPending stats inside the critical section. */
6758 xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
6760 portRELEASE_ISR_LOCK();
6761 portRELEASE_TASK_LOCK();
6762 portENABLE_INTERRUPTS();
6764 /* When a task yields in a critical section it just sets
6765 * xYieldPending to true. So now that we have exited the
6766 * critical section check if xYieldPending is true, and
6768 if( xYieldCurrentTask != pdFALSE )
6775 mtCOVERAGE_TEST_MARKER();
6780 mtCOVERAGE_TEST_MARKER();
6785 mtCOVERAGE_TEST_MARKER();
6788 traceRETURN_vTaskExitCritical();
6791 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6792 /*-----------------------------------------------------------*/
6794 #if ( configNUMBER_OF_CORES > 1 )
6796 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
6798 traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
6800 if( xSchedulerRunning != pdFALSE )
6802 /* If critical nesting count is zero then this function
6803 * does not match a previous call to vTaskEnterCritical(). */
6804 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
6806 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
6808 portDECREMENT_CRITICAL_NESTING_COUNT();
6810 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6812 portRELEASE_ISR_LOCK();
6813 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6817 mtCOVERAGE_TEST_MARKER();
6822 mtCOVERAGE_TEST_MARKER();
6827 mtCOVERAGE_TEST_MARKER();
6830 traceRETURN_vTaskExitCriticalFromISR();
6833 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6834 /*-----------------------------------------------------------*/
6836 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
6838 static char * prvWriteNameToBuffer( char * pcBuffer,
6839 const char * pcTaskName )
6843 /* Start by copying the entire string. */
6844 ( void ) strcpy( pcBuffer, pcTaskName );
6846 /* Pad the end of the string with spaces to ensure columns line up when
6848 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
6850 pcBuffer[ x ] = ' ';
6854 pcBuffer[ x ] = ( char ) 0x00;
6856 /* Return the new end of string. */
6857 return &( pcBuffer[ x ] );
6860 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
6861 /*-----------------------------------------------------------*/
6863 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
6865 void vTaskListTasks( char * pcWriteBuffer,
6866 size_t uxBufferLength )
6868 TaskStatus_t * pxTaskStatusArray;
6869 size_t uxConsumedBufferLength = 0;
6870 size_t uxCharsWrittenBySnprintf;
6871 int iSnprintfReturnValue;
6872 BaseType_t xOutputBufferFull = pdFALSE;
6873 UBaseType_t uxArraySize, x;
6876 traceENTER_vTaskListTasks( pcWriteBuffer, uxBufferLength );
6881 * This function is provided for convenience only, and is used by many
6882 * of the demo applications. Do not consider it to be part of the
6885 * vTaskListTasks() calls uxTaskGetSystemState(), then formats part of the
6886 * uxTaskGetSystemState() output into a human readable table that
6887 * displays task: names, states, priority, stack usage and task number.
6888 * Stack usage specified as the number of unused StackType_t words stack can hold
6889 * on top of stack - not the number of bytes.
6891 * vTaskListTasks() has a dependency on the snprintf() C library function that
6892 * might bloat the code size, use a lot of stack, and provide different
6893 * results on different platforms. An alternative, tiny, third party,
6894 * and limited functionality implementation of snprintf() is provided in
6895 * many of the FreeRTOS/Demo sub-directories in a file called
6896 * printf-stdarg.c (note printf-stdarg.c does not provide a full
6897 * snprintf() implementation!).
6899 * It is recommended that production systems call uxTaskGetSystemState()
6900 * directly to get access to raw stats data, rather than indirectly
6901 * through a call to vTaskListTasks().
6905 /* Make sure the write buffer does not contain a string. */
6906 *pcWriteBuffer = ( char ) 0x00;
6908 /* Take a snapshot of the number of tasks in case it changes while this
6909 * function is executing. */
6910 uxArraySize = uxCurrentNumberOfTasks;
6912 /* Allocate an array index for each task. NOTE! if
6913 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
6914 * equate to NULL. */
6915 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. */
6917 if( pxTaskStatusArray != NULL )
6919 /* Generate the (binary) data. */
6920 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
6922 /* Create a human readable table from the binary data. */
6923 for( x = 0; ( x < uxArraySize ) && ( xOutputBufferFull == pdFALSE ); x++ )
6925 switch( pxTaskStatusArray[ x ].eCurrentState )
6928 cStatus = tskRUNNING_CHAR;
6932 cStatus = tskREADY_CHAR;
6936 cStatus = tskBLOCKED_CHAR;
6940 cStatus = tskSUSPENDED_CHAR;
6944 cStatus = tskDELETED_CHAR;
6947 case eInvalid: /* Fall through. */
6948 default: /* Should not get here, but it is included
6949 * to prevent static checking errors. */
6950 cStatus = ( char ) 0x00;
6954 /* Is there enough space in the buffer to hold task name? */
6955 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
6957 /* Write the task name to the string, padding with spaces so it
6958 * can be printed in tabular form more easily. */
6959 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
6960 /* Do not count the terminating null character. */
6961 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1 );
6963 /* Is there space left in the buffer? -1 is done because snprintf
6964 * writes a terminating null character. So we are essentially
6965 * checking if the buffer has space to write at least one non-null
6967 if( uxConsumedBufferLength < ( uxBufferLength - 1 ) )
6969 /* Write the rest of the string. */
6970 iSnprintfReturnValue = snprintf( pcWriteBuffer,
6971 uxBufferLength - uxConsumedBufferLength,
6972 "\t%c\t%u\t%u\t%u\r\n",
6974 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
6975 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
6976 ( 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. */
6977 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
6979 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
6980 pcWriteBuffer += uxCharsWrittenBySnprintf; /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
6984 xOutputBufferFull = pdTRUE;
6989 xOutputBufferFull = pdTRUE;
6993 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
6994 * is 0 then vPortFree() will be #defined to nothing. */
6995 vPortFree( pxTaskStatusArray );
6999 mtCOVERAGE_TEST_MARKER();
7002 traceRETURN_vTaskListTasks();
7005 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7006 /*----------------------------------------------------------*/
7008 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
7010 void vTaskGetRunTimeStatistics( char * pcWriteBuffer,
7011 size_t uxBufferLength )
7013 TaskStatus_t * pxTaskStatusArray;
7014 size_t uxConsumedBufferLength = 0;
7015 size_t uxCharsWrittenBySnprintf;
7016 int iSnprintfReturnValue;
7017 BaseType_t xOutputBufferFull = pdFALSE;
7018 UBaseType_t uxArraySize, x;
7019 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
7021 traceENTER_vTaskGetRunTimeStatistics( pcWriteBuffer, uxBufferLength );
7026 * This function is provided for convenience only, and is used by many
7027 * of the demo applications. Do not consider it to be part of the
7030 * vTaskGetRunTimeStatistics() calls uxTaskGetSystemState(), then formats part
7031 * of the uxTaskGetSystemState() output into a human readable table that
7032 * displays the amount of time each task has spent in the Running state
7033 * in both absolute and percentage terms.
7035 * vTaskGetRunTimeStatistics() has a dependency on the snprintf() C library
7036 * function that might bloat the code size, use a lot of stack, and
7037 * provide different results on different platforms. An alternative,
7038 * tiny, third party, and limited functionality implementation of
7039 * snprintf() is provided in many of the FreeRTOS/Demo sub-directories in
7040 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
7041 * a full snprintf() implementation!).
7043 * It is recommended that production systems call uxTaskGetSystemState()
7044 * directly to get access to raw stats data, rather than indirectly
7045 * through a call to vTaskGetRunTimeStatistics().
7048 /* Make sure the write buffer does not contain a string. */
7049 *pcWriteBuffer = ( char ) 0x00;
7051 /* Take a snapshot of the number of tasks in case it changes while this
7052 * function is executing. */
7053 uxArraySize = uxCurrentNumberOfTasks;
7055 /* Allocate an array index for each task. NOTE! If
7056 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7057 * equate to NULL. */
7058 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. */
7060 if( pxTaskStatusArray != NULL )
7062 /* Generate the (binary) data. */
7063 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
7065 /* For percentage calculations. */
7066 ulTotalTime /= 100UL;
7068 /* Avoid divide by zero errors. */
7069 if( ulTotalTime > 0UL )
7071 /* Create a human readable table from the binary data. */
7072 for( x = 0; ( x < uxArraySize ) && ( xOutputBufferFull == pdFALSE ); x++ )
7074 /* What percentage of the total run time has the task used?
7075 * This will always be rounded down to the nearest integer.
7076 * ulTotalRunTime has already been divided by 100. */
7077 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
7079 /* Is there enough space in the buffer to hold task name? */
7080 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7082 /* Write the task name to the string, padding with
7083 * spaces so it can be printed in tabular form more
7085 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7086 /* Do not count the terminating null character. */
7087 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1 );
7089 /* Is there space left in the buffer? -1 is done because snprintf
7090 * writes a terminating null character. So we are essentially
7091 * checking if the buffer has space to write at least one non-null
7093 if( uxConsumedBufferLength < ( uxBufferLength - 1 ) )
7095 if( ulStatsAsPercentage > 0UL )
7097 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7099 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7100 uxBufferLength - uxConsumedBufferLength,
7101 "\t%lu\t\t%lu%%\r\n",
7102 pxTaskStatusArray[ x ].ulRunTimeCounter,
7103 ulStatsAsPercentage );
7107 /* sizeof( int ) == sizeof( long ) so a smaller
7108 * printf() library can be used. */
7109 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7110 uxBufferLength - uxConsumedBufferLength,
7112 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter,
7113 ( 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. */
7115 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7119 /* If the percentage is zero here then the task has
7120 * consumed less than 1% of the total run time. */
7121 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7123 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7124 uxBufferLength - uxConsumedBufferLength,
7125 "\t%lu\t\t<1%%\r\n",
7126 pxTaskStatusArray[ x ].ulRunTimeCounter );
7130 /* sizeof( int ) == sizeof( long ) so a smaller
7131 * printf() library can be used. */
7132 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7133 uxBufferLength - uxConsumedBufferLength,
7135 ( 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. */
7137 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7140 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7141 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7142 pcWriteBuffer += uxCharsWrittenBySnprintf; /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
7146 xOutputBufferFull = pdTRUE;
7151 xOutputBufferFull = pdTRUE;
7157 mtCOVERAGE_TEST_MARKER();
7160 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7161 * is 0 then vPortFree() will be #defined to nothing. */
7162 vPortFree( pxTaskStatusArray );
7166 mtCOVERAGE_TEST_MARKER();
7169 traceRETURN_vTaskGetRunTimeStatistics();
7172 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7173 /*-----------------------------------------------------------*/
7175 TickType_t uxTaskResetEventItemValue( void )
7177 TickType_t uxReturn;
7179 traceENTER_uxTaskResetEventItemValue();
7181 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7183 /* Reset the event list item to its normal value - so it can be used with
7184 * queues and semaphores. */
7185 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. */
7187 traceRETURN_uxTaskResetEventItemValue( uxReturn );
7191 /*-----------------------------------------------------------*/
7193 #if ( configUSE_MUTEXES == 1 )
7195 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7199 traceENTER_pvTaskIncrementMutexHeldCount();
7201 pxTCB = pxCurrentTCB;
7203 /* If xSemaphoreCreateMutex() is called before any tasks have been created
7204 * then pxCurrentTCB will be NULL. */
7207 ( pxTCB->uxMutexesHeld )++;
7210 traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7215 #endif /* configUSE_MUTEXES */
7216 /*-----------------------------------------------------------*/
7218 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7220 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7221 BaseType_t xClearCountOnExit,
7222 TickType_t xTicksToWait )
7226 traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7228 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7230 taskENTER_CRITICAL();
7232 /* Only block if the notification count is not already non-zero. */
7233 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0UL )
7235 /* Mark this task as waiting for a notification. */
7236 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7238 if( xTicksToWait > ( TickType_t ) 0 )
7240 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7241 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7243 /* All ports are written to allow a yield in a critical
7244 * section (some will yield immediately, others wait until the
7245 * critical section exits) - but it is not something that
7246 * application code should ever do. */
7247 taskYIELD_WITHIN_API();
7251 mtCOVERAGE_TEST_MARKER();
7256 mtCOVERAGE_TEST_MARKER();
7259 taskEXIT_CRITICAL();
7261 taskENTER_CRITICAL();
7263 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7264 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7266 if( ulReturn != 0UL )
7268 if( xClearCountOnExit != pdFALSE )
7270 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = 0UL;
7274 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7279 mtCOVERAGE_TEST_MARKER();
7282 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7284 taskEXIT_CRITICAL();
7286 traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7291 #endif /* configUSE_TASK_NOTIFICATIONS */
7292 /*-----------------------------------------------------------*/
7294 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7296 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7297 uint32_t ulBitsToClearOnEntry,
7298 uint32_t ulBitsToClearOnExit,
7299 uint32_t * pulNotificationValue,
7300 TickType_t xTicksToWait )
7304 traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7306 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7308 taskENTER_CRITICAL();
7310 /* Only block if a notification is not already pending. */
7311 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7313 /* Clear bits in the task's notification value as bits may get
7314 * set by the notifying task or interrupt. This can be used to
7315 * clear the value to zero. */
7316 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7318 /* Mark this task as waiting for a notification. */
7319 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7321 if( xTicksToWait > ( TickType_t ) 0 )
7323 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7324 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7326 /* All ports are written to allow a yield in a critical
7327 * section (some will yield immediately, others wait until the
7328 * critical section exits) - but it is not something that
7329 * application code should ever do. */
7330 taskYIELD_WITHIN_API();
7334 mtCOVERAGE_TEST_MARKER();
7339 mtCOVERAGE_TEST_MARKER();
7342 taskEXIT_CRITICAL();
7344 taskENTER_CRITICAL();
7346 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7348 if( pulNotificationValue != NULL )
7350 /* Output the current notification value, which may or may not
7352 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7355 /* If ucNotifyValue is set then either the task never entered the
7356 * blocked state (because a notification was already pending) or the
7357 * task unblocked because of a notification. Otherwise the task
7358 * unblocked because of a timeout. */
7359 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7361 /* A notification was not received. */
7366 /* A notification was already pending or a notification was
7367 * received while the task was waiting. */
7368 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7372 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7374 taskEXIT_CRITICAL();
7376 traceRETURN_xTaskGenericNotifyWait( xReturn );
7381 #endif /* configUSE_TASK_NOTIFICATIONS */
7382 /*-----------------------------------------------------------*/
7384 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7386 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7387 UBaseType_t uxIndexToNotify,
7389 eNotifyAction eAction,
7390 uint32_t * pulPreviousNotificationValue )
7393 BaseType_t xReturn = pdPASS;
7394 uint8_t ucOriginalNotifyState;
7396 traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7398 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7399 configASSERT( xTaskToNotify );
7400 pxTCB = xTaskToNotify;
7402 taskENTER_CRITICAL();
7404 if( pulPreviousNotificationValue != NULL )
7406 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7409 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7411 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7416 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7420 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7423 case eSetValueWithOverwrite:
7424 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7427 case eSetValueWithoutOverwrite:
7429 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7431 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7435 /* The value could not be written to the task. */
7443 /* The task is being notified without its notify value being
7449 /* Should not get here if all enums are handled.
7450 * Artificially force an assert by testing a value the
7451 * compiler can't assume is const. */
7452 configASSERT( xTickCount == ( TickType_t ) 0 );
7457 traceTASK_NOTIFY( uxIndexToNotify );
7459 /* If the task is in the blocked state specifically to wait for a
7460 * notification then unblock it now. */
7461 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7463 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7464 prvAddTaskToReadyList( pxTCB );
7466 /* The task should not have been on an event list. */
7467 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7469 #if ( configUSE_TICKLESS_IDLE != 0 )
7471 /* If a task is blocked waiting for a notification then
7472 * xNextTaskUnblockTime might be set to the blocked task's time
7473 * out time. If the task is unblocked for a reason other than
7474 * a timeout xNextTaskUnblockTime is normally left unchanged,
7475 * because it will automatically get reset to a new value when
7476 * the tick count equals xNextTaskUnblockTime. However if
7477 * tickless idling is used it might be more important to enter
7478 * sleep mode at the earliest possible time - so reset
7479 * xNextTaskUnblockTime here to ensure it is updated at the
7480 * earliest possible time. */
7481 prvResetNextTaskUnblockTime();
7485 /* Check if the notified task has a priority above the currently
7486 * executing task. */
7487 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7491 mtCOVERAGE_TEST_MARKER();
7494 taskEXIT_CRITICAL();
7496 traceRETURN_xTaskGenericNotify( xReturn );
7501 #endif /* configUSE_TASK_NOTIFICATIONS */
7502 /*-----------------------------------------------------------*/
7504 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7506 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
7507 UBaseType_t uxIndexToNotify,
7509 eNotifyAction eAction,
7510 uint32_t * pulPreviousNotificationValue,
7511 BaseType_t * pxHigherPriorityTaskWoken )
7514 uint8_t ucOriginalNotifyState;
7515 BaseType_t xReturn = pdPASS;
7516 UBaseType_t uxSavedInterruptStatus;
7518 traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
7520 configASSERT( xTaskToNotify );
7521 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7523 /* RTOS ports that support interrupt nesting have the concept of a
7524 * maximum system call (or maximum API call) interrupt priority.
7525 * Interrupts that are above the maximum system call priority are keep
7526 * permanently enabled, even when the RTOS kernel is in a critical section,
7527 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7528 * is defined in FreeRTOSConfig.h then
7529 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7530 * failure if a FreeRTOS API function is called from an interrupt that has
7531 * been assigned a priority above the configured maximum system call
7532 * priority. Only FreeRTOS functions that end in FromISR can be called
7533 * from interrupts that have been assigned a priority at or (logically)
7534 * below the maximum system call interrupt priority. FreeRTOS maintains a
7535 * separate interrupt safe API to ensure interrupt entry is as fast and as
7536 * simple as possible. More information (albeit Cortex-M specific) is
7537 * provided on the following link:
7538 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7539 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7541 pxTCB = xTaskToNotify;
7543 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7545 if( pulPreviousNotificationValue != NULL )
7547 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7550 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7551 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7556 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7560 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7563 case eSetValueWithOverwrite:
7564 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7567 case eSetValueWithoutOverwrite:
7569 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7571 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7575 /* The value could not be written to the task. */
7583 /* The task is being notified without its notify value being
7589 /* Should not get here if all enums are handled.
7590 * Artificially force an assert by testing a value the
7591 * compiler can't assume is const. */
7592 configASSERT( xTickCount == ( TickType_t ) 0 );
7596 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
7598 /* If the task is in the blocked state specifically to wait for a
7599 * notification then unblock it now. */
7600 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7602 /* The task should not have been on an event list. */
7603 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7605 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
7607 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7608 prvAddTaskToReadyList( pxTCB );
7612 /* The delayed and ready lists cannot be accessed, so hold
7613 * this task pending until the scheduler is resumed. */
7614 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
7617 #if ( configNUMBER_OF_CORES == 1 )
7619 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7621 /* The notified task has a priority above the currently
7622 * executing task so a yield is required. */
7623 if( pxHigherPriorityTaskWoken != NULL )
7625 *pxHigherPriorityTaskWoken = pdTRUE;
7628 /* Mark that a yield is pending in case the user is not
7629 * using the "xHigherPriorityTaskWoken" parameter to an ISR
7630 * safe FreeRTOS function. */
7631 xYieldPendings[ 0 ] = pdTRUE;
7635 mtCOVERAGE_TEST_MARKER();
7638 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
7640 #if ( configUSE_PREEMPTION == 1 )
7642 prvYieldForTask( pxTCB );
7644 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
7646 if( pxHigherPriorityTaskWoken != NULL )
7648 *pxHigherPriorityTaskWoken = pdTRUE;
7652 #endif /* if ( configUSE_PREEMPTION == 1 ) */
7654 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7657 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
7659 traceRETURN_xTaskGenericNotifyFromISR( xReturn );
7664 #endif /* configUSE_TASK_NOTIFICATIONS */
7665 /*-----------------------------------------------------------*/
7667 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7669 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
7670 UBaseType_t uxIndexToNotify,
7671 BaseType_t * pxHigherPriorityTaskWoken )
7674 uint8_t ucOriginalNotifyState;
7675 UBaseType_t uxSavedInterruptStatus;
7677 traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
7679 configASSERT( xTaskToNotify );
7680 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7682 /* RTOS ports that support interrupt nesting have the concept of a
7683 * maximum system call (or maximum API call) interrupt priority.
7684 * Interrupts that are above the maximum system call priority are keep
7685 * permanently enabled, even when the RTOS kernel is in a critical section,
7686 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7687 * is defined in FreeRTOSConfig.h then
7688 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7689 * failure if a FreeRTOS API function is called from an interrupt that has
7690 * been assigned a priority above the configured maximum system call
7691 * priority. Only FreeRTOS functions that end in FromISR can be called
7692 * from interrupts that have been assigned a priority at or (logically)
7693 * below the maximum system call interrupt priority. FreeRTOS maintains a
7694 * separate interrupt safe API to ensure interrupt entry is as fast and as
7695 * simple as possible. More information (albeit Cortex-M specific) is
7696 * provided on the following link:
7697 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7698 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7700 pxTCB = xTaskToNotify;
7702 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7704 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7705 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7707 /* 'Giving' is equivalent to incrementing a count in a counting
7709 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7711 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
7713 /* If the task is in the blocked state specifically to wait for a
7714 * notification then unblock it now. */
7715 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7717 /* The task should not have been on an event list. */
7718 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7720 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
7722 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7723 prvAddTaskToReadyList( pxTCB );
7727 /* The delayed and ready lists cannot be accessed, so hold
7728 * this task pending until the scheduler is resumed. */
7729 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
7732 #if ( configNUMBER_OF_CORES == 1 )
7734 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7736 /* The notified task has a priority above the currently
7737 * executing task so a yield is required. */
7738 if( pxHigherPriorityTaskWoken != NULL )
7740 *pxHigherPriorityTaskWoken = pdTRUE;
7743 /* Mark that a yield is pending in case the user is not
7744 * using the "xHigherPriorityTaskWoken" parameter in an ISR
7745 * safe FreeRTOS function. */
7746 xYieldPendings[ 0 ] = pdTRUE;
7750 mtCOVERAGE_TEST_MARKER();
7753 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
7755 #if ( configUSE_PREEMPTION == 1 )
7757 prvYieldForTask( pxTCB );
7759 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
7761 if( pxHigherPriorityTaskWoken != NULL )
7763 *pxHigherPriorityTaskWoken = pdTRUE;
7767 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
7769 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7772 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
7774 traceRETURN_vTaskGenericNotifyGiveFromISR();
7777 #endif /* configUSE_TASK_NOTIFICATIONS */
7778 /*-----------------------------------------------------------*/
7780 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7782 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
7783 UBaseType_t uxIndexToClear )
7788 traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
7790 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7792 /* If null is passed in here then it is the calling task that is having
7793 * its notification state cleared. */
7794 pxTCB = prvGetTCBFromHandle( xTask );
7796 taskENTER_CRITICAL();
7798 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
7800 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
7808 taskEXIT_CRITICAL();
7810 traceRETURN_xTaskGenericNotifyStateClear( xReturn );
7815 #endif /* configUSE_TASK_NOTIFICATIONS */
7816 /*-----------------------------------------------------------*/
7818 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7820 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
7821 UBaseType_t uxIndexToClear,
7822 uint32_t ulBitsToClear )
7827 traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
7829 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7831 /* If null is passed in here then it is the calling task that is having
7832 * its notification state cleared. */
7833 pxTCB = prvGetTCBFromHandle( xTask );
7835 taskENTER_CRITICAL();
7837 /* Return the notification as it was before the bits were cleared,
7838 * then clear the bit mask. */
7839 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
7840 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
7842 taskEXIT_CRITICAL();
7844 traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
7849 #endif /* configUSE_TASK_NOTIFICATIONS */
7850 /*-----------------------------------------------------------*/
7852 #if ( configGENERATE_RUN_TIME_STATS == 1 )
7854 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
7858 traceENTER_ulTaskGetRunTimeCounter( xTask );
7860 pxTCB = prvGetTCBFromHandle( xTask );
7862 traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
7864 return pxTCB->ulRunTimeCounter;
7867 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
7868 /*-----------------------------------------------------------*/
7870 #if ( configGENERATE_RUN_TIME_STATS == 1 )
7872 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
7875 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
7877 traceENTER_ulTaskGetRunTimePercent( xTask );
7879 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
7881 /* For percentage calculations. */
7882 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
7884 /* Avoid divide by zero errors. */
7885 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
7887 pxTCB = prvGetTCBFromHandle( xTask );
7888 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
7895 traceRETURN_ulTaskGetRunTimePercent( ulReturn );
7900 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
7901 /*-----------------------------------------------------------*/
7903 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
7905 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
7907 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
7910 traceENTER_ulTaskGetIdleRunTimeCounter();
7912 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
7914 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
7917 traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
7922 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
7923 /*-----------------------------------------------------------*/
7925 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
7927 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
7929 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
7930 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
7933 traceENTER_ulTaskGetIdleRunTimePercent();
7935 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
7937 /* For percentage calculations. */
7938 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
7940 /* Avoid divide by zero errors. */
7941 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
7943 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
7945 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
7948 ulReturn = ulRunTimeCounter / ulTotalTime;
7955 traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
7960 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
7961 /*-----------------------------------------------------------*/
7963 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
7964 const BaseType_t xCanBlockIndefinitely )
7966 TickType_t xTimeToWake;
7967 const TickType_t xConstTickCount = xTickCount;
7969 #if ( INCLUDE_xTaskAbortDelay == 1 )
7971 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
7972 * reset to pdFALSE so it can be detected as having been set to pdTRUE
7973 * when the task leaves the Blocked state. */
7974 pxCurrentTCB->ucDelayAborted = pdFALSE;
7978 /* Remove the task from the ready list before adding it to the blocked list
7979 * as the same list item is used for both lists. */
7980 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
7982 /* The current task must be in a ready list, so there is no need to
7983 * check, and the port reset macro can be called directly. */
7984 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. */
7988 mtCOVERAGE_TEST_MARKER();
7991 #if ( INCLUDE_vTaskSuspend == 1 )
7993 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
7995 /* Add the task to the suspended task list instead of a delayed task
7996 * list to ensure it is not woken by a timing event. It will block
7998 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
8002 /* Calculate the time at which the task should be woken if the event
8003 * does not occur. This may overflow but this doesn't matter, the
8004 * kernel will manage it correctly. */
8005 xTimeToWake = xConstTickCount + xTicksToWait;
8007 /* The list item will be inserted in wake time order. */
8008 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8010 if( xTimeToWake < xConstTickCount )
8012 /* Wake time has overflowed. Place this item in the overflow
8014 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8015 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8019 /* The wake time has not overflowed, so the current block list
8021 traceMOVED_TASK_TO_DELAYED_LIST();
8022 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8024 /* If the task entering the blocked state was placed at the
8025 * head of the list of blocked tasks then xNextTaskUnblockTime
8026 * needs to be updated too. */
8027 if( xTimeToWake < xNextTaskUnblockTime )
8029 xNextTaskUnblockTime = xTimeToWake;
8033 mtCOVERAGE_TEST_MARKER();
8038 #else /* INCLUDE_vTaskSuspend */
8040 /* Calculate the time at which the task should be woken if the event
8041 * does not occur. This may overflow but this doesn't matter, the kernel
8042 * will manage it correctly. */
8043 xTimeToWake = xConstTickCount + xTicksToWait;
8045 /* The list item will be inserted in wake time order. */
8046 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8048 if( xTimeToWake < xConstTickCount )
8050 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8051 /* Wake time has overflowed. Place this item in the overflow list. */
8052 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8056 traceMOVED_TASK_TO_DELAYED_LIST();
8057 /* The wake time has not overflowed, so the current block list is used. */
8058 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8060 /* If the task entering the blocked state was placed at the head of the
8061 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
8063 if( xTimeToWake < xNextTaskUnblockTime )
8065 xNextTaskUnblockTime = xTimeToWake;
8069 mtCOVERAGE_TEST_MARKER();
8073 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
8074 ( void ) xCanBlockIndefinitely;
8076 #endif /* INCLUDE_vTaskSuspend */
8078 /*-----------------------------------------------------------*/
8080 #if ( portUSING_MPU_WRAPPERS == 1 )
8082 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
8086 traceENTER_xTaskGetMPUSettings( xTask );
8088 pxTCB = prvGetTCBFromHandle( xTask );
8090 traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
8092 return &( pxTCB->xMPUSettings );
8095 #endif /* portUSING_MPU_WRAPPERS */
8096 /*-----------------------------------------------------------*/
8098 /* Code below here allows additional code to be inserted into this source file,
8099 * especially where access to file scope functions and data is needed (for example
8100 * when performing module tests). */
8102 #ifdef FREERTOS_MODULE_TEST
8103 #include "tasks_test_access_functions.h"
8107 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
8109 #include "freertos_tasks_c_additions.h"
8111 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8112 static void freertos_tasks_c_additions_init( void )
8114 FREERTOS_TASKS_C_ADDITIONS_INIT();
8118 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8119 /*-----------------------------------------------------------*/
8121 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8124 * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8125 * to provide the memory that is used by the Idle task. It is used when
8126 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8127 * it's own implementation of vApplicationGetIdleTaskMemory by setting
8128 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8130 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8131 StackType_t ** ppxIdleTaskStackBuffer,
8132 uint32_t * pulIdleTaskStackSize )
8134 static StaticTask_t xIdleTaskTCB;
8135 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8137 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8138 *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8139 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8142 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8143 /*-----------------------------------------------------------*/
8145 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8148 * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8149 * to provide the memory that is used by the Timer service task. It is used when
8150 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8151 * it's own implementation of vApplicationGetTimerTaskMemory by setting
8152 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8154 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8155 StackType_t ** ppxTimerTaskStackBuffer,
8156 uint32_t * pulTimerTaskStackSize )
8158 static StaticTask_t xTimerTaskTCB;
8159 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8161 *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8162 *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8163 *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8166 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8167 /*-----------------------------------------------------------*/