2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /* Standard includes. */
33 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
34 * all the API functions to use the MPU wrappers. That should only be done when
35 * task.h is included from an application file. */
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
38 /* FreeRTOS includes. */
42 #include "stack_macros.h"
44 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
45 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
46 * for the header files above, but not in this file, in order to generate the
47 * correct privileged Vs unprivileged linkage and placement. */
48 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
50 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
51 * functions but without including stdio.h here. */
52 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
54 /* At the bottom of this file are two optional functions that can be used
55 * to generate human readable text from the raw data generated by the
56 * uxTaskGetSystemState() function. Note the formatting functions are provided
57 * for convenience only, and are NOT considered part of the kernel. */
59 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
61 #if ( configUSE_PREEMPTION == 0 )
63 /* If the cooperative scheduler is being used then a yield should not be
64 * performed just because a higher priority task has been woken. */
65 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB )
66 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB )
69 #if ( configNUMBER_OF_CORES == 1 )
71 /* This macro requests the running task pxTCB to yield. In single core
72 * scheduler, a running task always runs on core 0 and portYIELD_WITHIN_API()
73 * can be used to request the task running on core 0 to yield. Therefore, pxTCB
74 * is not used in this macro. */
75 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) \
78 portYIELD_WITHIN_API(); \
81 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \
83 if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \
85 portYIELD_WITHIN_API(); \
89 mtCOVERAGE_TEST_MARKER(); \
93 #else /* if ( configNUMBER_OF_CORES == 1 ) */
95 /* Yield the core on which this task is running. */
96 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldCore( ( pxTCB )->xTaskRunState )
98 /* Yield for the task if a running task has priority lower than this task. */
99 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldForTask( pxTCB )
101 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
103 #endif /* if ( configUSE_PREEMPTION == 0 ) */
105 /* Values that can be assigned to the ucNotifyState member of the TCB. */
106 #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
107 #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
108 #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
111 * The value used to fill the stack of a task when the task is created. This
112 * is used purely for checking the high water mark for tasks.
114 #define tskSTACK_FILL_BYTE ( 0xa5U )
116 /* Bits used to record how a task's stack and TCB were allocated. */
117 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
118 #define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
119 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
121 /* If any of the following are set then task stacks are filled with a known
122 * value so the high water mark can be determined. If none of the following are
123 * set then don't fill the stack so there is no unnecessary dependency on memset. */
124 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
125 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
127 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
131 * Macros used by vListTask to indicate which state a task is in.
133 #define tskRUNNING_CHAR ( 'X' )
134 #define tskBLOCKED_CHAR ( 'B' )
135 #define tskREADY_CHAR ( 'R' )
136 #define tskDELETED_CHAR ( 'D' )
137 #define tskSUSPENDED_CHAR ( 'S' )
140 * Some kernel aware debuggers require the data the debugger needs access to to
141 * be global, rather than file scope.
143 #ifdef portREMOVE_STATIC_QUALIFIER
147 /* The name allocated to the Idle task. This can be overridden by defining
148 * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
149 #ifndef configIDLE_TASK_NAME
150 #define configIDLE_TASK_NAME "IDLE"
153 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
155 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
156 * performed in a generic way that is not optimised to any particular
157 * microcontroller architecture. */
159 /* uxTopReadyPriority holds the priority of the highest priority ready
161 #define taskRECORD_READY_PRIORITY( uxPriority ) \
163 if( ( uxPriority ) > uxTopReadyPriority ) \
165 uxTopReadyPriority = ( uxPriority ); \
167 } while( 0 ) /* taskRECORD_READY_PRIORITY */
169 /*-----------------------------------------------------------*/
171 #if ( configNUMBER_OF_CORES == 1 )
172 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
174 UBaseType_t uxTopPriority = uxTopReadyPriority; \
176 /* Find the highest priority queue that contains ready tasks. */ \
177 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
179 configASSERT( uxTopPriority ); \
183 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
184 * the same priority get an equal share of the processor time. */ \
185 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
186 uxTopReadyPriority = uxTopPriority; \
187 } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */
188 #else /* if ( configNUMBER_OF_CORES == 1 ) */
190 #define taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID ) prvSelectHighestPriorityTask( xCoreID )
192 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
194 /*-----------------------------------------------------------*/
196 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
197 * they are only required when a port optimised method of task selection is
199 #define taskRESET_READY_PRIORITY( uxPriority )
200 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
202 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
204 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
205 * performed in a way that is tailored to the particular microcontroller
206 * architecture being used. */
208 /* A port optimised version is provided. Call the port defined macros. */
209 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( ( uxPriority ), uxTopReadyPriority )
211 /*-----------------------------------------------------------*/
213 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
215 UBaseType_t uxTopPriority; \
217 /* Find the highest priority list that contains ready tasks. */ \
218 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
219 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
220 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
223 /*-----------------------------------------------------------*/
225 /* A port optimised version is provided, call it only if the TCB being reset
226 * is being referenced from a ready list. If it is referenced from a delayed
227 * or suspended list then it won't be in a ready list. */
228 #define taskRESET_READY_PRIORITY( uxPriority ) \
230 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
232 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
236 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
238 /*-----------------------------------------------------------*/
240 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
241 * count overflows. */
242 #define taskSWITCH_DELAYED_LISTS() \
246 /* The delayed tasks list should be empty when the lists are switched. */ \
247 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
249 pxTemp = pxDelayedTaskList; \
250 pxDelayedTaskList = pxOverflowDelayedTaskList; \
251 pxOverflowDelayedTaskList = pxTemp; \
253 prvResetNextTaskUnblockTime(); \
256 /*-----------------------------------------------------------*/
259 * Place the task represented by pxTCB into the appropriate ready list for
260 * the task. It is inserted at the end of the list.
262 #define prvAddTaskToReadyList( pxTCB ) \
264 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
265 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
266 listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
267 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB ); \
269 /*-----------------------------------------------------------*/
272 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
273 * where NULL is used to indicate that the handle of the currently executing
274 * task should be used in place of the parameter. This macro simply checks to
275 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
277 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
279 /* The item value of the event list item is normally used to hold the priority
280 * of the task to which it belongs (coded to allow it to be held in reverse
281 * priority order). However, it is occasionally borrowed for other purposes. It
282 * is important its value is not updated due to a task priority change while it is
283 * being used for another purpose. The following bit definition is used to inform
284 * the scheduler that the value should not be changed - in which case it is the
285 * responsibility of whichever module is using the value to ensure it gets set back
286 * to its original value when it is released. */
287 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
288 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U
289 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
290 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
291 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
292 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000000000000000ULL
295 /* Indicates that the task is not actively running on any core. */
296 #define taskTASK_NOT_RUNNING ( ( BaseType_t ) ( -1 ) )
298 /* Indicates that the task is actively running but scheduled to yield. */
299 #define taskTASK_SCHEDULED_TO_YIELD ( ( BaseType_t ) ( -2 ) )
301 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
302 #if ( configNUMBER_OF_CORES == 1 )
303 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
304 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
306 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) )
307 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB )->xTaskRunState != taskTASK_NOT_RUNNING ) ? ( pdTRUE ) : ( pdFALSE ) )
310 /* Indicates that the task is an Idle task. */
311 #define taskATTRIBUTE_IS_IDLE ( UBaseType_t ) ( 1UL << 0UL )
313 #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) )
314 #define portGET_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting )
315 #define portSET_CRITICAL_NESTING_COUNT( x ) ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting = ( x ) )
316 #define portINCREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting++ )
317 #define portDECREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting-- )
318 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) */
320 /* Code below here allows infinite loop controlling, especially for the infinite loop
321 * in idle task function (for example when performing unit tests). */
322 #ifndef INFINITE_LOOP
323 #define INFINITE_LOOP() 1
326 #define taskBITS_PER_BYTE ( ( size_t ) 8 )
329 * Task control block. A task control block (TCB) is allocated for each task,
330 * and stores task state information, including a pointer to the task's context
331 * (the task's run time environment, including register values)
333 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
335 volatile StackType_t * pxTopOfStack; /**< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
337 #if ( portUSING_MPU_WRAPPERS == 1 )
338 xMPU_SETTINGS xMPUSettings; /**< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
341 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
342 UBaseType_t uxCoreAffinityMask; /**< Used to link the task to certain cores. UBaseType_t must have greater than or equal to the number of bits as configNUMBER_OF_CORES. */
345 ListItem_t xStateListItem; /**< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
346 ListItem_t xEventListItem; /**< Used to reference a task from an event list. */
347 UBaseType_t uxPriority; /**< The priority of the task. 0 is the lowest priority. */
348 StackType_t * pxStack; /**< Points to the start of the stack. */
349 #if ( configNUMBER_OF_CORES > 1 )
350 volatile BaseType_t xTaskRunState; /**< Used to identify the core the task is running on, if the task is running. Otherwise, identifies the task's state - not running or yielding. */
351 UBaseType_t uxTaskAttributes; /**< Task's attributes - currently used to identify the idle tasks. */
353 char pcTaskName[ configMAX_TASK_NAME_LEN ]; /**< Descriptive name given to the task when created. Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
355 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
356 BaseType_t xPreemptionDisable; /**< Used to prevent the task from being preempted. */
359 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
360 StackType_t * pxEndOfStack; /**< Points to the highest valid address for the stack. */
363 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
364 UBaseType_t uxCriticalNesting; /**< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
367 #if ( configUSE_TRACE_FACILITY == 1 )
368 UBaseType_t uxTCBNumber; /**< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
369 UBaseType_t uxTaskNumber; /**< Stores a number specifically for use by third party trace code. */
372 #if ( configUSE_MUTEXES == 1 )
373 UBaseType_t uxBasePriority; /**< The priority last assigned to the task - used by the priority inheritance mechanism. */
374 UBaseType_t uxMutexesHeld;
377 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
378 TaskHookFunction_t pxTaskTag;
381 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
382 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
385 #if ( configGENERATE_RUN_TIME_STATS == 1 )
386 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /**< Stores the amount of time the task has spent in the Running state. */
389 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
390 configTLS_BLOCK_TYPE xTLSBlock; /**< Memory block used as Thread Local Storage (TLS) Block for the task. */
393 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
394 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
395 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
398 /* See the comments in FreeRTOS.h with the definition of
399 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
400 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
401 uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
404 #if ( INCLUDE_xTaskAbortDelay == 1 )
405 uint8_t ucDelayAborted;
408 #if ( configUSE_POSIX_ERRNO == 1 )
413 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
414 * below to enable the use of older kernel aware debuggers. */
415 typedef tskTCB TCB_t;
417 /*lint -save -e956 A manual analysis and inspection has been used to determine
418 * which static variables must be declared volatile. */
419 #if ( configNUMBER_OF_CORES == 1 )
420 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
422 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
423 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
424 /* coverity[misra_c_2012_rule_8_4_violation] */
425 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ];
426 #define pxCurrentTCB xTaskGetCurrentTaskHandle()
429 /* Lists for ready and blocked tasks. --------------------
430 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
431 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
432 * the static qualifier. */
433 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
434 PRIVILEGED_DATA static List_t xDelayedTaskList1; /**< Delayed tasks. */
435 PRIVILEGED_DATA static List_t xDelayedTaskList2; /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
436 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /**< Points to the delayed task list currently being used. */
437 PRIVILEGED_DATA static List_t * volatile pxOverflowDelayedTaskList; /**< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
438 PRIVILEGED_DATA static List_t xPendingReadyList; /**< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready list when the scheduler is resumed. */
440 #if ( INCLUDE_vTaskDelete == 1 )
442 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
443 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
447 #if ( INCLUDE_vTaskSuspend == 1 )
449 PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
453 /* Global POSIX errno. Its value is changed upon context switching to match
454 * the errno of the currently running task. */
455 #if ( configUSE_POSIX_ERRNO == 1 )
456 int FreeRTOS_errno = 0;
459 /* Other file private variables. --------------------------------*/
460 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
461 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
462 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
463 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
464 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
465 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
466 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
467 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
468 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
469 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandles[ configNUMBER_OF_CORES ]; /**< Holds the handles of the idle tasks. The idle tasks are created automatically when the scheduler is started. */
471 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
472 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
473 * to determine the number of priority lists to read back from the remote target. */
474 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
476 /* Context switches are held pending while the scheduler is suspended. Also,
477 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
478 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
479 * If an interrupt needs to unblock a task while the scheduler is suspended then it
480 * moves the task's event list item into the xPendingReadyList, ready for the
481 * kernel to move the task from the pending ready list into the real ready list
482 * when the scheduler is unsuspended. The pending ready list itself can only be
483 * accessed from a critical section.
485 * Updates to uxSchedulerSuspended must be protected by both the task lock and the ISR lock
486 * and must not be done from an ISR. Reads must be protected by either lock and may be done
487 * from either an ISR or a task. */
488 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
490 #if ( configGENERATE_RUN_TIME_STATS == 1 )
492 /* Do not move these variables to function scope as doing so prevents the
493 * code working with debuggers that need to remove the static qualifier. */
494 PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the value of a timer/counter the last time a task was switched in. */
495 PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the total amount of execution time as defined by the run time counter clock. */
499 #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configNUMBER_OF_CORES > 1 )
501 /* Do not move these variables to function scope as doing so prevents the
502 * code working with debuggers that need to remove the static qualifier. */
503 static StaticTask_t xIdleTCBBuffers[ configNUMBER_OF_CORES - 1 ];
504 static StackType_t xIdleTaskStackBuffers[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
506 #endif /* #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
510 /*-----------------------------------------------------------*/
512 /* File private functions. --------------------------------*/
515 * Creates the idle tasks during scheduler start.
517 static BaseType_t prvCreateIdleTasks( void );
519 #if ( configNUMBER_OF_CORES > 1 )
522 * Checks to see if another task moved the current task out of the ready
523 * list while it was waiting to enter a critical section and yields, if so.
525 static void prvCheckForRunStateChange( void );
526 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
528 #if ( configNUMBER_OF_CORES > 1 )
531 * Yields the given core.
533 static void prvYieldCore( BaseType_t xCoreID );
534 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
536 #if ( configNUMBER_OF_CORES > 1 )
539 * Yields a core, or cores if multiple priorities are not allowed to run
540 * simultaneously, to allow the task pxTCB to run.
542 static void prvYieldForTask( const TCB_t * pxTCB );
543 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
545 #if ( configNUMBER_OF_CORES > 1 )
548 * Selects the highest priority available task for the given core.
550 static void prvSelectHighestPriorityTask( BaseType_t xCoreID );
551 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
554 * Utility task that simply returns pdTRUE if the task referenced by xTask is
555 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
556 * is in any other state.
558 #if ( INCLUDE_vTaskSuspend == 1 )
560 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
562 #endif /* INCLUDE_vTaskSuspend */
565 * Utility to ready all the lists used by the scheduler. This is called
566 * automatically upon the creation of the first task.
568 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
571 * The idle task, which as all tasks is implemented as a never ending loop.
572 * The idle task is automatically created and added to the ready lists upon
573 * creation of the first user task.
575 * In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 minimal idle tasks are also
576 * created to ensure that each core has an idle task to run when no other
577 * task is available to run.
579 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
580 * language extensions. The equivalent prototype for these functions are:
582 * void prvIdleTask( void *pvParameters );
583 * void prvMinimalIdleTask( void *pvParameters );
586 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
587 #if ( configNUMBER_OF_CORES > 1 )
588 static portTASK_FUNCTION_PROTO( prvMinimalIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
592 * Utility to free all memory allocated by the scheduler to hold a TCB,
593 * including the stack pointed to by the TCB.
595 * This does not free memory allocated by the task itself (i.e. memory
596 * allocated by calls to pvPortMalloc from within the tasks application code).
598 #if ( INCLUDE_vTaskDelete == 1 )
600 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
605 * Used only by the idle task. This checks to see if anything has been placed
606 * in the list of tasks waiting to be deleted. If so the task is cleaned up
607 * and its TCB deleted.
609 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
612 * The currently executing task is entering the Blocked state. Add the task to
613 * either the current or the overflow delayed task list.
615 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
616 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
619 * Fills an TaskStatus_t structure with information on each task that is
620 * referenced from the pxList list (which may be a ready list, a delayed list,
621 * a suspended list, etc.).
623 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
624 * NORMAL APPLICATION CODE.
626 #if ( configUSE_TRACE_FACILITY == 1 )
628 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
630 eTaskState eState ) PRIVILEGED_FUNCTION;
635 * Searches pxList for a task with name pcNameToQuery - returning a handle to
636 * the task if it is found, or NULL if the task is not found.
638 #if ( INCLUDE_xTaskGetHandle == 1 )
640 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
641 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
646 * When a task is created, the stack of the task is filled with a known value.
647 * This function determines the 'high water mark' of the task stack by
648 * determining how much of the stack remains at the original preset value.
650 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
652 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
657 * Return the amount of time, in ticks, that will pass before the kernel will
658 * next move a task from the Blocked state to the Running state.
660 * This conditional compilation should use inequality to 0, not equality to 1.
661 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
662 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
663 * set to a value other than 1.
665 #if ( configUSE_TICKLESS_IDLE != 0 )
667 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
672 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
673 * will exit the Blocked state.
675 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
677 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
680 * Helper function used to pad task names with spaces when printing out
681 * human readable tables of task information.
683 static char * prvWriteNameToBuffer( char * pcBuffer,
684 const char * pcTaskName ) PRIVILEGED_FUNCTION;
689 * Called after a Task_t structure has been allocated either statically or
690 * dynamically to fill in the structure's members.
692 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
693 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
694 const uint32_t ulStackDepth,
695 void * const pvParameters,
696 UBaseType_t uxPriority,
697 TaskHandle_t * const pxCreatedTask,
699 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
702 * Called after a new task has been created and initialised to place the task
703 * under the control of the scheduler.
705 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
708 * freertos_tasks_c_additions_init() should only be called if the user definable
709 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
710 * called by the function.
712 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
714 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
718 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
719 extern void vApplicationMinimalIdleHook( void );
720 #endif /* #if ( configUSE_MINIMAL_IDLE_HOOK == 1 ) */
722 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
725 * Convert the snprintf return value to the number of characters
726 * written. The following are the possible cases:
728 * 1. The buffer supplied to snprintf is large enough to hold the
729 * generated string. The return value in this case is the number
730 * of characters actually written, not counting the terminating
732 * 2. The buffer supplied to snprintf is NOT large enough to hold
733 * the generated string. The return value in this case is the
734 * number of characters that would have been written if the
735 * buffer had been sufficiently large, not counting the
736 * terminating null character.
737 * 3. Encoding error. The return value in this case is a negative
740 * From 1 and 2 above ==> Only when the return value is non-negative
741 * and less than the supplied buffer length, the string has been
742 * completely written.
744 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
747 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
748 /*-----------------------------------------------------------*/
750 #if ( configNUMBER_OF_CORES > 1 )
751 static void prvCheckForRunStateChange( void )
753 UBaseType_t uxPrevCriticalNesting;
754 const TCB_t * pxThisTCB;
756 /* This must only be called from within a task. */
757 portASSERT_IF_IN_ISR();
759 /* This function is always called with interrupts disabled
760 * so this is safe. */
761 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
763 while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD )
765 /* We are only here if we just entered a critical section
766 * or if we just suspended the scheduler, and another task
767 * has requested that we yield.
769 * This is slightly complicated since we need to save and restore
770 * the suspension and critical nesting counts, as well as release
771 * and reacquire the correct locks. And then, do it all over again
772 * if our state changed again during the reacquisition. */
773 uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
775 if( uxPrevCriticalNesting > 0U )
777 portSET_CRITICAL_NESTING_COUNT( 0U );
778 portRELEASE_ISR_LOCK();
782 /* The scheduler is suspended. uxSchedulerSuspended is updated
783 * only when the task is not requested to yield. */
784 mtCOVERAGE_TEST_MARKER();
787 portRELEASE_TASK_LOCK();
788 portMEMORY_BARRIER();
789 configASSERT( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD );
791 portENABLE_INTERRUPTS();
793 /* Enabling interrupts should cause this core to immediately
794 * service the pending interrupt and yield. If the run state is still
795 * yielding here then that is a problem. */
796 configASSERT( pxThisTCB->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD );
798 portDISABLE_INTERRUPTS();
802 portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
804 if( uxPrevCriticalNesting == 0U )
806 portRELEASE_ISR_LOCK();
810 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
812 /*-----------------------------------------------------------*/
814 #if ( configNUMBER_OF_CORES > 1 )
815 static void prvYieldCore( BaseType_t xCoreID )
817 /* This must be called from a critical section and xCoreID must be valid. */
818 if( ( portCHECK_IF_IN_ISR() == pdTRUE ) && ( xCoreID == ( BaseType_t ) portGET_CORE_ID() ) )
820 xYieldPendings[ xCoreID ] = pdTRUE;
824 if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD )
826 if( xCoreID == ( BaseType_t ) portGET_CORE_ID() )
828 xYieldPendings[ xCoreID ] = pdTRUE;
832 portYIELD_CORE( xCoreID );
833 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_SCHEDULED_TO_YIELD;
838 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
839 /*-----------------------------------------------------------*/
841 #if ( configNUMBER_OF_CORES > 1 )
842 static void prvYieldForTask( const TCB_t * pxTCB )
844 BaseType_t xLowestPriorityToPreempt;
845 BaseType_t xCurrentCoreTaskPriority;
846 BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
849 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
850 BaseType_t xYieldCount = 0;
851 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
853 /* This must be called from a critical section. */
854 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
856 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
858 /* No task should yield for this one if it is a lower priority
859 * than priority level of currently ready tasks. */
860 if( pxTCB->uxPriority >= uxTopReadyPriority )
862 /* Yield is not required for a task which is already running. */
863 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
866 xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
868 /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
869 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
870 --xLowestPriorityToPreempt;
872 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
874 xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
876 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
877 if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
879 xCurrentCoreTaskPriority = xCurrentCoreTaskPriority - 1;
882 if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
884 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
885 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
888 if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
890 #if ( configUSE_CORE_AFFINITY == 1 )
891 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
894 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
895 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
898 xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
899 xLowestPriorityCore = xCoreID;
905 mtCOVERAGE_TEST_MARKER();
909 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
911 /* Yield all currently running non-idle tasks with a priority lower than
912 * the task that needs to run. */
913 if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
914 ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
916 prvYieldCore( xCoreID );
921 mtCOVERAGE_TEST_MARKER();
924 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
928 mtCOVERAGE_TEST_MARKER();
932 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
933 if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
934 #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
935 if( xLowestPriorityCore >= 0 )
936 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
938 prvYieldCore( xLowestPriorityCore );
941 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
942 /* Verify that the calling core always yields to higher priority tasks. */
943 if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0 ) &&
944 ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
946 configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) ||
947 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) );
952 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
953 /*-----------------------------------------------------------*/
955 #if ( configNUMBER_OF_CORES > 1 )
956 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
958 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
959 BaseType_t xTaskScheduled = pdFALSE;
960 BaseType_t xDecrementTopPriority = pdTRUE;
962 #if ( configUSE_CORE_AFFINITY == 1 )
963 const TCB_t * pxPreviousTCB = NULL;
965 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
966 BaseType_t xPriorityDropped = pdFALSE;
969 /* This function should be called when scheduler is running. */
970 configASSERT( xSchedulerRunning == pdTRUE );
972 /* A new task is created and a running task with the same priority yields
973 * itself to run the new task. When a running task yields itself, it is still
974 * in the ready list. This running task will be selected before the new task
975 * since the new task is always added to the end of the ready list.
976 * The other problem is that the running task still in the same position of
977 * the ready list when it yields itself. It is possible that it will be selected
978 * earlier then other tasks which waits longer than this task.
980 * To fix these problems, the running task should be put to the end of the
981 * ready list before searching for the ready task in the ready list. */
982 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
983 &pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
985 ( void ) uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
986 vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
987 &pxCurrentTCBs[ xCoreID ]->xStateListItem );
990 while( xTaskScheduled == pdFALSE )
992 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
994 if( uxCurrentPriority < uxTopReadyPriority )
996 /* We can't schedule any tasks, other than idle, that have a
997 * priority lower than the priority of a task currently running
998 * on another core. */
999 uxCurrentPriority = tskIDLE_PRIORITY;
1004 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
1006 const List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
1007 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxReadyList );
1008 ListItem_t * pxIterator;
1010 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
1011 * must not be decremented any further. */
1012 xDecrementTopPriority = pdFALSE;
1014 for( pxIterator = listGET_HEAD_ENTRY( pxReadyList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
1016 TCB_t * pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
1018 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1020 /* When falling back to the idle priority because only one priority
1021 * level is allowed to run at a time, we should ONLY schedule the true
1022 * idle tasks, not user tasks at the idle priority. */
1023 if( uxCurrentPriority < uxTopReadyPriority )
1025 if( ( pxTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0 )
1031 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1033 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
1035 #if ( configUSE_CORE_AFFINITY == 1 )
1036 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1039 /* If the task is not being executed by any core swap it in. */
1040 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
1041 #if ( configUSE_CORE_AFFINITY == 1 )
1042 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
1044 pxTCB->xTaskRunState = xCoreID;
1045 pxCurrentTCBs[ xCoreID ] = pxTCB;
1046 xTaskScheduled = pdTRUE;
1049 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
1051 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) );
1053 #if ( configUSE_CORE_AFFINITY == 1 )
1054 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1057 /* The task is already running on this core, mark it as scheduled. */
1058 pxTCB->xTaskRunState = xCoreID;
1059 xTaskScheduled = pdTRUE;
1064 /* This task is running on the core other than xCoreID. */
1065 mtCOVERAGE_TEST_MARKER();
1068 if( xTaskScheduled != pdFALSE )
1070 /* A task has been selected to run on this core. */
1077 if( xDecrementTopPriority != pdFALSE )
1079 uxTopReadyPriority--;
1080 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1082 xPriorityDropped = pdTRUE;
1088 /* There are configNUMBER_OF_CORES Idle tasks created when scheduler started.
1089 * The scheduler should be able to select a task to run when uxCurrentPriority
1090 * is tskIDLE_PRIORITY. uxCurrentPriority is never decreased to value blow
1091 * tskIDLE_PRIORITY. */
1092 if( uxCurrentPriority > tskIDLE_PRIORITY )
1094 uxCurrentPriority--;
1098 /* This function is called when idle task is not created. Break the
1099 * loop to prevent uxCurrentPriority overrun. */
1104 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1106 if( xTaskScheduled == pdTRUE )
1108 if( xPriorityDropped != pdFALSE )
1110 /* There may be several ready tasks that were being prevented from running because there was
1111 * a higher priority task running. Now that the last of the higher priority tasks is no longer
1112 * running, make sure all the other idle tasks yield. */
1115 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUMBER_OF_CORES; x++ )
1117 if( ( pxCurrentTCBs[ x ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0 )
1125 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1127 #if ( configUSE_CORE_AFFINITY == 1 )
1129 if( xTaskScheduled == pdTRUE )
1131 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
1133 /* A ready task was just evicted from this core. See if it can be
1134 * scheduled on any other core. */
1135 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
1136 BaseType_t xLowestPriority = ( BaseType_t ) pxPreviousTCB->uxPriority;
1137 BaseType_t xLowestPriorityCore = -1;
1140 if( ( pxPreviousTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1142 xLowestPriority = xLowestPriority - 1;
1145 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1147 /* The ready task that was removed from this core is not excluded from it.
1148 * Only look at the intersection of the cores the removed task is allowed to run
1149 * on with the cores that the new task is excluded from. It is possible that the
1150 * new task was only placed onto this core because it is excluded from another.
1151 * Check to see if the previous task could run on one of those cores. */
1152 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1156 /* The ready task that was removed from this core is excluded from it. */
1159 uxCoreMap &= ( ( 1U << configNUMBER_OF_CORES ) - 1U );
1161 for( x = ( ( BaseType_t ) configNUMBER_OF_CORES - 1 ); x >= ( BaseType_t ) 0; x-- )
1163 UBaseType_t uxCore = ( UBaseType_t ) x;
1164 BaseType_t xTaskPriority;
1166 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << uxCore ) ) != 0U )
1168 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority;
1170 if( ( pxCurrentTCBs[ uxCore ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1172 xTaskPriority = xTaskPriority - ( BaseType_t ) 1;
1175 uxCoreMap &= ~( ( UBaseType_t ) 1U << uxCore );
1177 if( ( xTaskPriority < xLowestPriority ) &&
1178 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ] ) != pdFALSE ) &&
1179 ( xYieldPendings[ uxCore ] == pdFALSE ) )
1181 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1182 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1185 xLowestPriority = xTaskPriority;
1186 xLowestPriorityCore = ( BaseType_t ) uxCore;
1192 if( xLowestPriorityCore >= 0 )
1194 prvYieldCore( xLowestPriorityCore );
1199 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) */
1202 #endif /* ( configNUMBER_OF_CORES > 1 ) */
1204 /*-----------------------------------------------------------*/
1206 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1208 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1209 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1210 const uint32_t ulStackDepth,
1211 void * const pvParameters,
1212 UBaseType_t uxPriority,
1213 StackType_t * const puxStackBuffer,
1214 StaticTask_t * const pxTaskBuffer )
1215 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1217 return xTaskCreateStaticAffinitySet( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, tskNO_AFFINITY );
1220 TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1221 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1222 const uint32_t ulStackDepth,
1223 void * const pvParameters,
1224 UBaseType_t uxPriority,
1225 StackType_t * const puxStackBuffer,
1226 StaticTask_t * const pxTaskBuffer,
1227 UBaseType_t uxCoreAffinityMask )
1228 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1231 TaskHandle_t xReturn;
1233 traceENTER_xTaskCreateStatic( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer );
1235 configASSERT( puxStackBuffer != NULL );
1236 configASSERT( pxTaskBuffer != NULL );
1238 #if ( configASSERT_DEFINED == 1 )
1240 /* Sanity check that the size of the structure used to declare a
1241 * variable of type StaticTask_t equals the size of the real task
1243 volatile size_t xSize = sizeof( StaticTask_t );
1244 configASSERT( xSize == sizeof( TCB_t ) );
1245 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1247 #endif /* configASSERT_DEFINED */
1249 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1251 /* The memory used for the task's TCB and stack are passed into this
1252 * function - use them. */
1253 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. */
1254 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1255 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1257 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1259 /* Tasks can be created statically or dynamically, so note this
1260 * task was created statically in case the task is later deleted. */
1261 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1263 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1265 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1267 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1269 /* Set the task's affinity before scheduling it. */
1270 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1274 prvAddNewTaskToReadyList( pxNewTCB );
1281 traceRETURN_xTaskCreateStatic( xReturn );
1286 #endif /* SUPPORT_STATIC_ALLOCATION */
1287 /*-----------------------------------------------------------*/
1289 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1291 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1292 TaskHandle_t * pxCreatedTask )
1293 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1295 return xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1298 BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1299 UBaseType_t uxCoreAffinityMask,
1300 TaskHandle_t * pxCreatedTask )
1301 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1304 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1306 traceENTER_xTaskCreateRestrictedStatic( pxTaskDefinition, pxCreatedTask );
1308 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1309 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1311 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1313 /* Allocate space for the TCB. Where the memory comes from depends
1314 * on the implementation of the port malloc function and whether or
1315 * not static allocation is being used. */
1316 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1317 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1319 /* Store the stack location in the TCB. */
1320 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1322 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1324 /* Tasks can be created statically or dynamically, so note this
1325 * task was created statically in case the task is later deleted. */
1326 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1328 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1330 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1331 pxTaskDefinition->pcName,
1332 ( uint32_t ) pxTaskDefinition->usStackDepth,
1333 pxTaskDefinition->pvParameters,
1334 pxTaskDefinition->uxPriority,
1335 pxCreatedTask, pxNewTCB,
1336 pxTaskDefinition->xRegions );
1338 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1340 /* Set the task's affinity before scheduling it. */
1341 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1345 prvAddNewTaskToReadyList( pxNewTCB );
1349 traceRETURN_xTaskCreateRestrictedStatic( xReturn );
1354 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1355 /*-----------------------------------------------------------*/
1357 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1359 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1360 TaskHandle_t * pxCreatedTask )
1361 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1363 return xTaskCreateRestrictedAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1366 BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1367 UBaseType_t uxCoreAffinityMask,
1368 TaskHandle_t * pxCreatedTask )
1369 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1372 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1374 traceENTER_xTaskCreateRestricted( pxTaskDefinition, pxCreatedTask );
1376 configASSERT( pxTaskDefinition->puxStackBuffer );
1378 if( pxTaskDefinition->puxStackBuffer != NULL )
1380 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1382 if( pxNewTCB != NULL )
1384 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1386 /* Store the stack location in the TCB. */
1387 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1389 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1391 /* Tasks can be created statically or dynamically, so note
1392 * this task had a statically allocated stack in case it is
1393 * later deleted. The TCB was allocated dynamically. */
1394 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1396 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1398 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1399 pxTaskDefinition->pcName,
1400 ( uint32_t ) pxTaskDefinition->usStackDepth,
1401 pxTaskDefinition->pvParameters,
1402 pxTaskDefinition->uxPriority,
1403 pxCreatedTask, pxNewTCB,
1404 pxTaskDefinition->xRegions );
1406 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1408 /* Set the task's affinity before scheduling it. */
1409 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1413 prvAddNewTaskToReadyList( pxNewTCB );
1418 traceRETURN_xTaskCreateRestricted( xReturn );
1423 #endif /* portUSING_MPU_WRAPPERS */
1424 /*-----------------------------------------------------------*/
1426 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1428 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1429 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1430 const configSTACK_DEPTH_TYPE usStackDepth,
1431 void * const pvParameters,
1432 UBaseType_t uxPriority,
1433 TaskHandle_t * const pxCreatedTask )
1434 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1436 return xTaskCreateAffinitySet( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, tskNO_AFFINITY, pxCreatedTask );
1439 BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1440 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1441 const configSTACK_DEPTH_TYPE usStackDepth,
1442 void * const pvParameters,
1443 UBaseType_t uxPriority,
1444 UBaseType_t uxCoreAffinityMask,
1445 TaskHandle_t * const pxCreatedTask )
1446 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1451 traceENTER_xTaskCreate( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask );
1453 /* If the stack grows down then allocate the stack then the TCB so the stack
1454 * does not grow into the TCB. Likewise if the stack grows up then allocate
1455 * the TCB then the stack. */
1456 #if ( portSTACK_GROWTH > 0 )
1458 /* Allocate space for the TCB. Where the memory comes from depends on
1459 * the implementation of the port malloc function and whether or not static
1460 * allocation is being used. */
1461 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1463 if( pxNewTCB != NULL )
1465 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1467 /* Allocate space for the stack used by the task being created.
1468 * The base of the stack memory stored in the TCB so the task can
1469 * be deleted later if required. */
1470 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1472 if( pxNewTCB->pxStack == NULL )
1474 /* Could not allocate the stack. Delete the allocated TCB. */
1475 vPortFree( pxNewTCB );
1480 #else /* portSTACK_GROWTH */
1482 StackType_t * pxStack;
1484 /* Allocate space for the stack used by the task being created. */
1485 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. */
1487 if( pxStack != NULL )
1489 /* Allocate space for the TCB. */
1490 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. */
1492 if( pxNewTCB != NULL )
1494 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1496 /* Store the stack location in the TCB. */
1497 pxNewTCB->pxStack = pxStack;
1501 /* The stack cannot be used as the TCB was not created. Free
1503 vPortFreeStack( pxStack );
1511 #endif /* portSTACK_GROWTH */
1513 if( pxNewTCB != NULL )
1515 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1517 /* Tasks can be created statically or dynamically, so note this
1518 * task was created dynamically in case it is later deleted. */
1519 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1521 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1523 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1525 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1527 /* Set the task's affinity before scheduling it. */
1528 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1532 prvAddNewTaskToReadyList( pxNewTCB );
1537 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1540 traceRETURN_xTaskCreate( xReturn );
1545 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1546 /*-----------------------------------------------------------*/
1548 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1549 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1550 const uint32_t ulStackDepth,
1551 void * const pvParameters,
1552 UBaseType_t uxPriority,
1553 TaskHandle_t * const pxCreatedTask,
1555 const MemoryRegion_t * const xRegions )
1557 StackType_t * pxTopOfStack;
1560 #if ( portUSING_MPU_WRAPPERS == 1 )
1561 /* Should the task be created in privileged mode? */
1562 BaseType_t xRunPrivileged;
1564 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1566 xRunPrivileged = pdTRUE;
1570 xRunPrivileged = pdFALSE;
1572 uxPriority &= ~portPRIVILEGE_BIT;
1573 #endif /* portUSING_MPU_WRAPPERS == 1 */
1575 /* Avoid dependency on memset() if it is not required. */
1576 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1578 /* Fill the stack with a known value to assist debugging. */
1579 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1581 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1583 /* Calculate the top of stack address. This depends on whether the stack
1584 * grows from high memory to low (as per the 80x86) or vice versa.
1585 * portSTACK_GROWTH is used to make the result positive or negative as required
1587 #if ( portSTACK_GROWTH < 0 )
1589 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1590 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(). */
1592 /* Check the alignment of the calculated top of stack is correct. */
1593 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1595 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1597 /* Also record the stack's high address, which may assist
1599 pxNewTCB->pxEndOfStack = pxTopOfStack;
1601 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1603 #else /* portSTACK_GROWTH */
1605 pxTopOfStack = pxNewTCB->pxStack;
1606 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(). */
1608 /* Check the alignment of the calculated top of stack is correct. */
1609 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1611 /* The other extreme of the stack space is required if stack checking is
1613 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1615 #endif /* portSTACK_GROWTH */
1617 /* Store the task name in the TCB. */
1618 if( pcName != NULL )
1620 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1622 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1624 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1625 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1626 * string is not accessible (extremely unlikely). */
1627 if( pcName[ x ] == ( char ) 0x00 )
1633 mtCOVERAGE_TEST_MARKER();
1637 /* Ensure the name string is terminated in the case that the string length
1638 * was greater or equal to configMAX_TASK_NAME_LEN. */
1639 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1643 mtCOVERAGE_TEST_MARKER();
1646 /* This is used as an array index so must ensure it's not too large. */
1647 configASSERT( uxPriority < configMAX_PRIORITIES );
1649 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1651 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1655 mtCOVERAGE_TEST_MARKER();
1658 pxNewTCB->uxPriority = uxPriority;
1659 #if ( configUSE_MUTEXES == 1 )
1661 pxNewTCB->uxBasePriority = uxPriority;
1663 #endif /* configUSE_MUTEXES */
1665 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1666 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1668 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1669 * back to the containing TCB from a generic item in a list. */
1670 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1672 /* Event lists are always in priority order. */
1673 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. */
1674 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1676 #if ( portUSING_MPU_WRAPPERS == 1 )
1678 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1682 /* Avoid compiler warning about unreferenced parameter. */
1687 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
1689 /* Allocate and initialize memory for the task's TLS Block. */
1690 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
1694 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1696 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1700 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1702 pxNewTCB->xPreemptionDisable = 0;
1706 /* Initialize the TCB stack to look as if the task was already running,
1707 * but had been interrupted by the scheduler. The return address is set
1708 * to the start of the task function. Once the stack has been initialised
1709 * the top of stack variable is updated. */
1710 #if ( portUSING_MPU_WRAPPERS == 1 )
1712 /* If the port has capability to detect stack overflow,
1713 * pass the stack end address to the stack initialization
1714 * function as well. */
1715 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1717 #if ( portSTACK_GROWTH < 0 )
1719 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1721 #else /* portSTACK_GROWTH */
1723 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1725 #endif /* portSTACK_GROWTH */
1727 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1729 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1731 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1733 #else /* portUSING_MPU_WRAPPERS */
1735 /* If the port has capability to detect stack overflow,
1736 * pass the stack end address to the stack initialization
1737 * function as well. */
1738 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1740 #if ( portSTACK_GROWTH < 0 )
1742 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1744 #else /* portSTACK_GROWTH */
1746 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1748 #endif /* portSTACK_GROWTH */
1750 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1752 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1754 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1756 #endif /* portUSING_MPU_WRAPPERS */
1758 /* Initialize task state and task attributes. */
1759 #if ( configNUMBER_OF_CORES > 1 )
1761 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1763 /* Is this an idle task? */
1764 if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvMinimalIdleTask ) )
1766 pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
1769 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
1771 if( pxCreatedTask != NULL )
1773 /* Pass the handle out in an anonymous way. The handle can be used to
1774 * change the created task's priority, delete the created task, etc.*/
1775 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1779 mtCOVERAGE_TEST_MARKER();
1782 /*-----------------------------------------------------------*/
1784 #if ( configNUMBER_OF_CORES == 1 )
1786 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1788 /* Ensure interrupts don't access the task lists while the lists are being
1790 taskENTER_CRITICAL();
1792 uxCurrentNumberOfTasks++;
1794 if( pxCurrentTCB == NULL )
1796 /* There are no other tasks, or all the other tasks are in
1797 * the suspended state - make this the current task. */
1798 pxCurrentTCB = pxNewTCB;
1800 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1802 /* This is the first task to be created so do the preliminary
1803 * initialisation required. We will not recover if this call
1804 * fails, but we will report the failure. */
1805 prvInitialiseTaskLists();
1809 mtCOVERAGE_TEST_MARKER();
1814 /* If the scheduler is not already running, make this task the
1815 * current task if it is the highest priority task to be created
1817 if( xSchedulerRunning == pdFALSE )
1819 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
1821 pxCurrentTCB = pxNewTCB;
1825 mtCOVERAGE_TEST_MARKER();
1830 mtCOVERAGE_TEST_MARKER();
1836 #if ( configUSE_TRACE_FACILITY == 1 )
1838 /* Add a counter into the TCB for tracing only. */
1839 pxNewTCB->uxTCBNumber = uxTaskNumber;
1841 #endif /* configUSE_TRACE_FACILITY */
1842 traceTASK_CREATE( pxNewTCB );
1844 prvAddTaskToReadyList( pxNewTCB );
1846 portSETUP_TCB( pxNewTCB );
1848 taskEXIT_CRITICAL();
1850 if( xSchedulerRunning != pdFALSE )
1852 /* If the created task is of a higher priority than the current task
1853 * then it should run now. */
1854 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
1858 mtCOVERAGE_TEST_MARKER();
1862 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
1864 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1866 /* Ensure interrupts don't access the task lists while the lists are being
1868 taskENTER_CRITICAL();
1870 uxCurrentNumberOfTasks++;
1872 if( xSchedulerRunning == pdFALSE )
1874 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1876 /* This is the first task to be created so do the preliminary
1877 * initialisation required. We will not recover if this call
1878 * fails, but we will report the failure. */
1879 prvInitialiseTaskLists();
1883 mtCOVERAGE_TEST_MARKER();
1886 if( ( pxNewTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1890 /* Check if a core is free. */
1891 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
1893 if( pxCurrentTCBs[ xCoreID ] == NULL )
1895 pxNewTCB->xTaskRunState = xCoreID;
1896 pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1901 mtCOVERAGE_TEST_MARKER();
1907 mtCOVERAGE_TEST_MARKER();
1913 #if ( configUSE_TRACE_FACILITY == 1 )
1915 /* Add a counter into the TCB for tracing only. */
1916 pxNewTCB->uxTCBNumber = uxTaskNumber;
1918 #endif /* configUSE_TRACE_FACILITY */
1919 traceTASK_CREATE( pxNewTCB );
1921 prvAddTaskToReadyList( pxNewTCB );
1923 portSETUP_TCB( pxNewTCB );
1925 if( xSchedulerRunning != pdFALSE )
1927 /* If the created task is of a higher priority than another
1928 * currently running task and preemption is on then it should
1930 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
1934 mtCOVERAGE_TEST_MARKER();
1937 taskEXIT_CRITICAL();
1940 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
1941 /*-----------------------------------------------------------*/
1943 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
1945 static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
1948 size_t uxCharsWritten;
1950 if( iSnprintfReturnValue < 0 )
1952 /* Encoding error - Return 0 to indicate that nothing
1953 * was written to the buffer. */
1956 else if( iSnprintfReturnValue >= ( int ) n )
1958 /* This is the case when the supplied buffer is not
1959 * large to hold the generated string. Return the
1960 * number of characters actually written without
1961 * counting the terminating NULL character. */
1962 uxCharsWritten = n - 1;
1966 /* Complete string was written to the buffer. */
1967 uxCharsWritten = ( size_t ) iSnprintfReturnValue;
1970 return uxCharsWritten;
1973 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
1974 /*-----------------------------------------------------------*/
1976 #if ( INCLUDE_vTaskDelete == 1 )
1978 void vTaskDelete( TaskHandle_t xTaskToDelete )
1982 traceENTER_vTaskDelete( xTaskToDelete );
1984 taskENTER_CRITICAL();
1986 /* If null is passed in here then it is the calling task that is
1988 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1990 /* Remove task from the ready/delayed list. */
1991 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1993 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1997 mtCOVERAGE_TEST_MARKER();
2000 /* Is the task waiting on an event also? */
2001 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2003 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2007 mtCOVERAGE_TEST_MARKER();
2010 /* Increment the uxTaskNumber also so kernel aware debuggers can
2011 * detect that the task lists need re-generating. This is done before
2012 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
2016 /* If the task is running (or yielding), we must add it to the
2017 * termination list so that an idle task can delete it when it is
2018 * no longer running. */
2019 if( taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) != pdFALSE )
2021 /* A running task or a task which is scheduled to yield is being
2022 * deleted. This cannot complete when the task is still running
2023 * on a core, as a context switch to another task is required.
2024 * Place the task in the termination list. The idle task will check
2025 * the termination list and free up any memory allocated by the
2026 * scheduler for the TCB and stack of the deleted task. */
2027 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
2029 /* Increment the ucTasksDeleted variable so the idle task knows
2030 * there is a task that has been deleted and that it should therefore
2031 * check the xTasksWaitingTermination list. */
2032 ++uxDeletedTasksWaitingCleanUp;
2034 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
2035 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
2036 traceTASK_DELETE( pxTCB );
2038 /* The pre-delete hook is primarily for the Windows simulator,
2039 * in which Windows specific clean up operations are performed,
2040 * after which it is not possible to yield away from this task -
2041 * hence xYieldPending is used to latch that a context switch is
2043 #if ( configNUMBER_OF_CORES == 1 )
2044 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
2046 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
2051 --uxCurrentNumberOfTasks;
2052 traceTASK_DELETE( pxTCB );
2054 /* Reset the next expected unblock time in case it referred to
2055 * the task that has just been deleted. */
2056 prvResetNextTaskUnblockTime();
2060 #if ( configNUMBER_OF_CORES == 1 )
2062 taskEXIT_CRITICAL();
2064 /* If the task is not deleting itself, call prvDeleteTCB from outside of
2065 * critical section. If a task deletes itself, prvDeleteTCB is called
2066 * from prvCheckTasksWaitingTermination which is called from Idle task. */
2067 if( pxTCB != pxCurrentTCB )
2069 prvDeleteTCB( pxTCB );
2072 /* Force a reschedule if it is the currently running task that has just
2074 if( xSchedulerRunning != pdFALSE )
2076 if( pxTCB == pxCurrentTCB )
2078 configASSERT( uxSchedulerSuspended == 0 );
2079 portYIELD_WITHIN_API();
2083 mtCOVERAGE_TEST_MARKER();
2087 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2089 /* If a running task is not deleting itself, call prvDeleteTCB. If a running
2090 * task deletes itself, prvDeleteTCB is called from prvCheckTasksWaitingTermination
2091 * which is called from Idle task. */
2092 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
2094 prvDeleteTCB( pxTCB );
2097 /* Force a reschedule if the task that has just been deleted was running. */
2098 if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) )
2100 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2102 configASSERT( uxSchedulerSuspended == 0 );
2103 vTaskYieldWithinAPI();
2107 prvYieldCore( pxTCB->xTaskRunState );
2111 taskEXIT_CRITICAL();
2113 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2115 traceRETURN_vTaskDelete();
2118 #endif /* INCLUDE_vTaskDelete */
2119 /*-----------------------------------------------------------*/
2121 #if ( INCLUDE_xTaskDelayUntil == 1 )
2123 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2124 const TickType_t xTimeIncrement )
2126 TickType_t xTimeToWake;
2127 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2129 traceENTER_xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
2131 configASSERT( pxPreviousWakeTime );
2132 configASSERT( ( xTimeIncrement > 0U ) );
2136 /* Minor optimisation. The tick count cannot change in this
2138 const TickType_t xConstTickCount = xTickCount;
2140 configASSERT( uxSchedulerSuspended == 1U );
2142 /* Generate the tick time at which the task wants to wake. */
2143 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2145 if( xConstTickCount < *pxPreviousWakeTime )
2147 /* The tick count has overflowed since this function was
2148 * lasted called. In this case the only time we should ever
2149 * actually delay is if the wake time has also overflowed,
2150 * and the wake time is greater than the tick time. When this
2151 * is the case it is as if neither time had overflowed. */
2152 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2154 xShouldDelay = pdTRUE;
2158 mtCOVERAGE_TEST_MARKER();
2163 /* The tick time has not overflowed. In this case we will
2164 * delay if either the wake time has overflowed, and/or the
2165 * tick time is less than the wake time. */
2166 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2168 xShouldDelay = pdTRUE;
2172 mtCOVERAGE_TEST_MARKER();
2176 /* Update the wake time ready for the next call. */
2177 *pxPreviousWakeTime = xTimeToWake;
2179 if( xShouldDelay != pdFALSE )
2181 traceTASK_DELAY_UNTIL( xTimeToWake );
2183 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2184 * the time to wake, so subtract the current tick count. */
2185 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2189 mtCOVERAGE_TEST_MARKER();
2192 xAlreadyYielded = xTaskResumeAll();
2194 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2195 * have put ourselves to sleep. */
2196 if( xAlreadyYielded == pdFALSE )
2198 #if ( configNUMBER_OF_CORES == 1 )
2199 portYIELD_WITHIN_API();
2201 vTaskYieldWithinAPI();
2206 mtCOVERAGE_TEST_MARKER();
2209 traceRETURN_xTaskDelayUntil( xShouldDelay );
2211 return xShouldDelay;
2214 #endif /* INCLUDE_xTaskDelayUntil */
2215 /*-----------------------------------------------------------*/
2217 #if ( INCLUDE_vTaskDelay == 1 )
2219 void vTaskDelay( const TickType_t xTicksToDelay )
2221 BaseType_t xAlreadyYielded = pdFALSE;
2223 traceENTER_vTaskDelay( xTicksToDelay );
2225 /* A delay time of zero just forces a reschedule. */
2226 if( xTicksToDelay > ( TickType_t ) 0U )
2230 configASSERT( uxSchedulerSuspended == 1U );
2234 /* A task that is removed from the event list while the
2235 * scheduler is suspended will not get placed in the ready
2236 * list or removed from the blocked list until the scheduler
2239 * This task cannot be in an event list as it is the currently
2240 * executing task. */
2241 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2243 xAlreadyYielded = xTaskResumeAll();
2247 mtCOVERAGE_TEST_MARKER();
2250 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2251 * have put ourselves to sleep. */
2252 if( xAlreadyYielded == pdFALSE )
2254 #if ( configNUMBER_OF_CORES == 1 )
2255 portYIELD_WITHIN_API();
2257 vTaskYieldWithinAPI();
2262 mtCOVERAGE_TEST_MARKER();
2265 traceRETURN_vTaskDelay();
2268 #endif /* INCLUDE_vTaskDelay */
2269 /*-----------------------------------------------------------*/
2271 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2273 eTaskState eTaskGetState( TaskHandle_t xTask )
2276 List_t const * pxStateList;
2277 List_t const * pxEventList;
2278 List_t const * pxDelayedList;
2279 List_t const * pxOverflowedDelayedList;
2280 const TCB_t * const pxTCB = xTask;
2282 traceENTER_eTaskGetState( xTask );
2284 configASSERT( pxTCB );
2286 #if ( configNUMBER_OF_CORES == 1 )
2287 if( pxTCB == pxCurrentTCB )
2289 /* The task calling this function is querying its own state. */
2295 taskENTER_CRITICAL();
2297 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2298 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2299 pxDelayedList = pxDelayedTaskList;
2300 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2302 taskEXIT_CRITICAL();
2304 if( pxEventList == &xPendingReadyList )
2306 /* The task has been placed on the pending ready list, so its
2307 * state is eReady regardless of what list the task's state list
2308 * item is currently placed on. */
2311 else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2313 /* The task being queried is referenced from one of the Blocked
2318 #if ( INCLUDE_vTaskSuspend == 1 )
2319 else if( pxStateList == &xSuspendedTaskList )
2321 /* The task being queried is referenced from the suspended
2322 * list. Is it genuinely suspended or is it blocked
2324 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2326 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2330 /* The task does not appear on the event list item of
2331 * and of the RTOS objects, but could still be in the
2332 * blocked state if it is waiting on its notification
2333 * rather than waiting on an object. If not, is
2335 eReturn = eSuspended;
2337 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2339 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2346 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2348 eReturn = eSuspended;
2350 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2357 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2359 #if ( INCLUDE_vTaskDelete == 1 )
2360 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2362 /* The task being queried is referenced from the deleted
2363 * tasks list, or it is not referenced from any lists at
2369 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
2371 #if ( configNUMBER_OF_CORES == 1 )
2373 /* If the task is not in any other state, it must be in the
2374 * Ready (including pending ready) state. */
2377 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2379 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2381 /* Is it actively running on a core? */
2386 /* If the task is not in any other state, it must be in the
2387 * Ready (including pending ready) state. */
2391 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2395 traceRETURN_eTaskGetState( eReturn );
2398 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2400 #endif /* INCLUDE_eTaskGetState */
2401 /*-----------------------------------------------------------*/
2403 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2405 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2407 TCB_t const * pxTCB;
2408 UBaseType_t uxReturn;
2410 traceENTER_uxTaskPriorityGet( xTask );
2412 taskENTER_CRITICAL();
2414 /* If null is passed in here then it is the priority of the task
2415 * that called uxTaskPriorityGet() that is being queried. */
2416 pxTCB = prvGetTCBFromHandle( xTask );
2417 uxReturn = pxTCB->uxPriority;
2419 taskEXIT_CRITICAL();
2421 traceRETURN_uxTaskPriorityGet( uxReturn );
2426 #endif /* INCLUDE_uxTaskPriorityGet */
2427 /*-----------------------------------------------------------*/
2429 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2431 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2433 TCB_t const * pxTCB;
2434 UBaseType_t uxReturn;
2435 UBaseType_t uxSavedInterruptStatus;
2437 traceENTER_uxTaskPriorityGetFromISR( xTask );
2439 /* RTOS ports that support interrupt nesting have the concept of a
2440 * maximum system call (or maximum API call) interrupt priority.
2441 * Interrupts that are above the maximum system call priority are keep
2442 * permanently enabled, even when the RTOS kernel is in a critical section,
2443 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2444 * is defined in FreeRTOSConfig.h then
2445 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2446 * failure if a FreeRTOS API function is called from an interrupt that has
2447 * been assigned a priority above the configured maximum system call
2448 * priority. Only FreeRTOS functions that end in FromISR can be called
2449 * from interrupts that have been assigned a priority at or (logically)
2450 * below the maximum system call interrupt priority. FreeRTOS maintains a
2451 * separate interrupt safe API to ensure interrupt entry is as fast and as
2452 * simple as possible. More information (albeit Cortex-M specific) is
2453 * provided on the following link:
2454 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2455 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2457 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
2459 /* If null is passed in here then it is the priority of the calling
2460 * task that is being queried. */
2461 pxTCB = prvGetTCBFromHandle( xTask );
2462 uxReturn = pxTCB->uxPriority;
2464 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2466 traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
2471 #endif /* INCLUDE_uxTaskPriorityGet */
2472 /*-----------------------------------------------------------*/
2474 #if ( INCLUDE_vTaskPrioritySet == 1 )
2476 void vTaskPrioritySet( TaskHandle_t xTask,
2477 UBaseType_t uxNewPriority )
2480 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2481 BaseType_t xYieldRequired = pdFALSE;
2483 traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
2485 #if ( configNUMBER_OF_CORES > 1 )
2486 BaseType_t xYieldForTask = pdFALSE;
2489 configASSERT( uxNewPriority < configMAX_PRIORITIES );
2491 /* Ensure the new priority is valid. */
2492 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2494 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2498 mtCOVERAGE_TEST_MARKER();
2501 taskENTER_CRITICAL();
2503 /* If null is passed in here then it is the priority of the calling
2504 * task that is being changed. */
2505 pxTCB = prvGetTCBFromHandle( xTask );
2507 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2509 #if ( configUSE_MUTEXES == 1 )
2511 uxCurrentBasePriority = pxTCB->uxBasePriority;
2515 uxCurrentBasePriority = pxTCB->uxPriority;
2519 if( uxCurrentBasePriority != uxNewPriority )
2521 /* The priority change may have readied a task of higher
2522 * priority than a running task. */
2523 if( uxNewPriority > uxCurrentBasePriority )
2525 #if ( configNUMBER_OF_CORES == 1 )
2527 if( pxTCB != pxCurrentTCB )
2529 /* The priority of a task other than the currently
2530 * running task is being raised. Is the priority being
2531 * raised above that of the running task? */
2532 if( uxNewPriority > pxCurrentTCB->uxPriority )
2534 xYieldRequired = pdTRUE;
2538 mtCOVERAGE_TEST_MARKER();
2543 /* The priority of the running task is being raised,
2544 * but the running task must already be the highest
2545 * priority task able to run so no yield is required. */
2548 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2550 /* The priority of a task is being raised so
2551 * perform a yield for this task later. */
2552 xYieldForTask = pdTRUE;
2554 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2556 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2558 /* Setting the priority of a running task down means
2559 * there may now be another task of higher priority that
2560 * is ready to execute. */
2561 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2562 if( pxTCB->xPreemptionDisable == pdFALSE )
2565 xYieldRequired = pdTRUE;
2570 /* Setting the priority of any other task down does not
2571 * require a yield as the running task must be above the
2572 * new priority of the task being modified. */
2575 /* Remember the ready list the task might be referenced from
2576 * before its uxPriority member is changed so the
2577 * taskRESET_READY_PRIORITY() macro can function correctly. */
2578 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2580 #if ( configUSE_MUTEXES == 1 )
2582 /* Only change the priority being used if the task is not
2583 * currently using an inherited priority or the new priority
2584 * is bigger than the inherited priority. */
2585 if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2587 pxTCB->uxPriority = uxNewPriority;
2591 mtCOVERAGE_TEST_MARKER();
2594 /* The base priority gets set whatever. */
2595 pxTCB->uxBasePriority = uxNewPriority;
2597 #else /* if ( configUSE_MUTEXES == 1 ) */
2599 pxTCB->uxPriority = uxNewPriority;
2601 #endif /* if ( configUSE_MUTEXES == 1 ) */
2603 /* Only reset the event list item value if the value is not
2604 * being used for anything else. */
2605 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2607 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. */
2611 mtCOVERAGE_TEST_MARKER();
2614 /* If the task is in the blocked or suspended list we need do
2615 * nothing more than change its priority variable. However, if
2616 * the task is in a ready list it needs to be removed and placed
2617 * in the list appropriate to its new priority. */
2618 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2620 /* The task is currently in its ready list - remove before
2621 * adding it to its new ready list. As we are in a critical
2622 * section we can do this even if the scheduler is suspended. */
2623 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2625 /* It is known that the task is in its ready list so
2626 * there is no need to check again and the port level
2627 * reset macro can be called directly. */
2628 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2632 mtCOVERAGE_TEST_MARKER();
2635 prvAddTaskToReadyList( pxTCB );
2639 #if ( configNUMBER_OF_CORES == 1 )
2641 mtCOVERAGE_TEST_MARKER();
2645 /* It's possible that xYieldForTask was already set to pdTRUE because
2646 * its priority is being raised. However, since it is not in a ready list
2647 * we don't actually need to yield for it. */
2648 xYieldForTask = pdFALSE;
2653 if( xYieldRequired != pdFALSE )
2655 /* The running task priority is set down. Request the task to yield. */
2656 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2660 #if ( configNUMBER_OF_CORES > 1 )
2661 if( xYieldForTask != pdFALSE )
2663 /* The priority of the task is being raised. If a running
2664 * task has priority lower than this task, it should yield
2666 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2669 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2671 mtCOVERAGE_TEST_MARKER();
2675 /* Remove compiler warning about unused variables when the port
2676 * optimised task selection is not being used. */
2677 ( void ) uxPriorityUsedOnEntry;
2680 taskEXIT_CRITICAL();
2682 traceRETURN_vTaskPrioritySet();
2685 #endif /* INCLUDE_vTaskPrioritySet */
2686 /*-----------------------------------------------------------*/
2688 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2689 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2690 UBaseType_t uxCoreAffinityMask )
2694 UBaseType_t uxPrevCoreAffinityMask;
2696 #if ( configUSE_PREEMPTION == 1 )
2697 UBaseType_t uxPrevNotAllowedCores;
2700 traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
2702 taskENTER_CRITICAL();
2704 pxTCB = prvGetTCBFromHandle( xTask );
2706 uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2707 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2709 if( xSchedulerRunning != pdFALSE )
2711 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2713 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2715 /* If the task can no longer run on the core it was running,
2716 * request the core to yield. */
2717 if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
2719 prvYieldCore( xCoreID );
2724 #if ( configUSE_PREEMPTION == 1 )
2726 /* Calculate the cores on which this task was not allowed to
2727 * run previously. */
2728 uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
2730 /* Does the new core mask enables this task to run on any of the
2731 * previously not allowed cores? If yes, check if this task can be
2732 * scheduled on any of those cores. */
2733 if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
2735 prvYieldForTask( pxTCB );
2738 #else /* #if( configUSE_PREEMPTION == 1 ) */
2740 mtCOVERAGE_TEST_MARKER();
2742 #endif /* #if( configUSE_PREEMPTION == 1 ) */
2746 taskEXIT_CRITICAL();
2748 traceRETURN_vTaskCoreAffinitySet();
2750 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
2751 /*-----------------------------------------------------------*/
2753 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2754 UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
2756 const TCB_t * pxTCB;
2757 UBaseType_t uxCoreAffinityMask;
2759 traceENTER_vTaskCoreAffinityGet( xTask );
2761 taskENTER_CRITICAL();
2763 pxTCB = prvGetTCBFromHandle( xTask );
2764 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2766 taskEXIT_CRITICAL();
2768 traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
2770 return uxCoreAffinityMask;
2772 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
2774 /*-----------------------------------------------------------*/
2776 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2778 void vTaskPreemptionDisable( const TaskHandle_t xTask )
2782 traceENTER_vTaskPreemptionDisable( xTask );
2784 taskENTER_CRITICAL();
2786 pxTCB = prvGetTCBFromHandle( xTask );
2788 pxTCB->xPreemptionDisable = pdTRUE;
2790 taskEXIT_CRITICAL();
2792 traceRETURN_vTaskPreemptionDisable();
2795 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
2796 /*-----------------------------------------------------------*/
2798 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2800 void vTaskPreemptionEnable( const TaskHandle_t xTask )
2805 traceENTER_vTaskPreemptionEnable( xTask );
2807 taskENTER_CRITICAL();
2809 pxTCB = prvGetTCBFromHandle( xTask );
2811 pxTCB->xPreemptionDisable = pdFALSE;
2813 if( xSchedulerRunning != pdFALSE )
2815 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2817 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2818 prvYieldCore( xCoreID );
2822 taskEXIT_CRITICAL();
2824 traceRETURN_vTaskPreemptionEnable();
2827 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
2828 /*-----------------------------------------------------------*/
2830 #if ( INCLUDE_vTaskSuspend == 1 )
2832 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2836 #if ( configNUMBER_OF_CORES > 1 )
2837 BaseType_t xTaskRunningOnCore;
2840 traceENTER_vTaskSuspend( xTaskToSuspend );
2842 taskENTER_CRITICAL();
2844 /* If null is passed in here then it is the running task that is
2845 * being suspended. */
2846 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2848 traceTASK_SUSPEND( pxTCB );
2850 #if ( configNUMBER_OF_CORES > 1 )
2851 xTaskRunningOnCore = pxTCB->xTaskRunState;
2854 /* Remove task from the ready/delayed list and place in the
2855 * suspended list. */
2856 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2858 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2862 mtCOVERAGE_TEST_MARKER();
2865 /* Is the task waiting on an event also? */
2866 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2868 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2872 mtCOVERAGE_TEST_MARKER();
2875 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2877 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2881 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2883 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2885 /* The task was blocked to wait for a notification, but is
2886 * now suspended, so no notification was received. */
2887 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2891 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2894 #if ( configNUMBER_OF_CORES == 1 )
2896 taskEXIT_CRITICAL();
2898 if( xSchedulerRunning != pdFALSE )
2900 /* Reset the next expected unblock time in case it referred to the
2901 * task that is now in the Suspended state. */
2902 taskENTER_CRITICAL();
2904 prvResetNextTaskUnblockTime();
2906 taskEXIT_CRITICAL();
2910 mtCOVERAGE_TEST_MARKER();
2913 if( pxTCB == pxCurrentTCB )
2915 if( xSchedulerRunning != pdFALSE )
2917 /* The current task has just been suspended. */
2918 configASSERT( uxSchedulerSuspended == 0 );
2919 portYIELD_WITHIN_API();
2923 /* The scheduler is not running, but the task that was pointed
2924 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
2925 * must be adjusted to point to a different task. */
2926 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
2928 /* No other tasks are ready, so set pxCurrentTCB back to
2929 * NULL so when the next task is created pxCurrentTCB will
2930 * be set to point to it no matter what its relative priority
2932 pxCurrentTCB = NULL;
2936 vTaskSwitchContext();
2942 mtCOVERAGE_TEST_MARKER();
2945 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2947 if( xSchedulerRunning != pdFALSE )
2949 /* Reset the next expected unblock time in case it referred to the
2950 * task that is now in the Suspended state. */
2951 prvResetNextTaskUnblockTime();
2955 mtCOVERAGE_TEST_MARKER();
2958 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2960 if( xSchedulerRunning != pdFALSE )
2962 if( xTaskRunningOnCore == ( BaseType_t ) portGET_CORE_ID() )
2964 /* The current task has just been suspended. */
2965 configASSERT( uxSchedulerSuspended == 0 );
2966 vTaskYieldWithinAPI();
2970 prvYieldCore( xTaskRunningOnCore );
2975 /* This code path is not possible because only Idle tasks are
2976 * assigned a core before the scheduler is started ( i.e.
2977 * taskTASK_IS_RUNNING is only true for idle tasks before
2978 * the scheduler is started ) and idle tasks cannot be
2980 mtCOVERAGE_TEST_MARKER();
2985 mtCOVERAGE_TEST_MARKER();
2988 taskEXIT_CRITICAL();
2990 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2992 traceRETURN_vTaskSuspend();
2995 #endif /* INCLUDE_vTaskSuspend */
2996 /*-----------------------------------------------------------*/
2998 #if ( INCLUDE_vTaskSuspend == 1 )
3000 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
3002 BaseType_t xReturn = pdFALSE;
3003 const TCB_t * const pxTCB = xTask;
3005 /* Accesses xPendingReadyList so must be called from a critical
3008 /* It does not make sense to check if the calling task is suspended. */
3009 configASSERT( xTask );
3011 /* Is the task being resumed actually in the suspended list? */
3012 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
3014 /* Has the task already been resumed from within an ISR? */
3015 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
3017 /* Is it in the suspended list because it is in the Suspended
3018 * state, or because is is blocked with no timeout? */
3019 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
3025 mtCOVERAGE_TEST_MARKER();
3030 mtCOVERAGE_TEST_MARKER();
3035 mtCOVERAGE_TEST_MARKER();
3039 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
3041 #endif /* INCLUDE_vTaskSuspend */
3042 /*-----------------------------------------------------------*/
3044 #if ( INCLUDE_vTaskSuspend == 1 )
3046 void vTaskResume( TaskHandle_t xTaskToResume )
3048 TCB_t * const pxTCB = xTaskToResume;
3050 traceENTER_vTaskResume( xTaskToResume );
3052 /* It does not make sense to resume the calling task. */
3053 configASSERT( xTaskToResume );
3055 #if ( configNUMBER_OF_CORES == 1 )
3057 /* The parameter cannot be NULL as it is impossible to resume the
3058 * currently executing task. */
3059 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
3062 /* The parameter cannot be NULL as it is impossible to resume the
3063 * currently executing task. It is also impossible to resume a task
3064 * that is actively running on another core but it is not safe
3065 * to check their run state here. Therefore, we get into a critical
3066 * section and check if the task is actually suspended or not. */
3070 taskENTER_CRITICAL();
3072 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3074 traceTASK_RESUME( pxTCB );
3076 /* The ready list can be accessed even if the scheduler is
3077 * suspended because this is inside a critical section. */
3078 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3079 prvAddTaskToReadyList( pxTCB );
3081 /* This yield may not cause the task just resumed to run,
3082 * but will leave the lists in the correct state for the
3084 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
3088 mtCOVERAGE_TEST_MARKER();
3091 taskEXIT_CRITICAL();
3095 mtCOVERAGE_TEST_MARKER();
3098 traceRETURN_vTaskResume();
3101 #endif /* INCLUDE_vTaskSuspend */
3103 /*-----------------------------------------------------------*/
3105 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
3107 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
3109 BaseType_t xYieldRequired = pdFALSE;
3110 TCB_t * const pxTCB = xTaskToResume;
3111 UBaseType_t uxSavedInterruptStatus;
3113 traceENTER_xTaskResumeFromISR( xTaskToResume );
3115 configASSERT( xTaskToResume );
3117 /* RTOS ports that support interrupt nesting have the concept of a
3118 * maximum system call (or maximum API call) interrupt priority.
3119 * Interrupts that are above the maximum system call priority are keep
3120 * permanently enabled, even when the RTOS kernel is in a critical section,
3121 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
3122 * is defined in FreeRTOSConfig.h then
3123 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3124 * failure if a FreeRTOS API function is called from an interrupt that has
3125 * been assigned a priority above the configured maximum system call
3126 * priority. Only FreeRTOS functions that end in FromISR can be called
3127 * from interrupts that have been assigned a priority at or (logically)
3128 * below the maximum system call interrupt priority. FreeRTOS maintains a
3129 * separate interrupt safe API to ensure interrupt entry is as fast and as
3130 * simple as possible. More information (albeit Cortex-M specific) is
3131 * provided on the following link:
3132 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3133 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3135 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3137 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3139 traceTASK_RESUME_FROM_ISR( pxTCB );
3141 /* Check the ready lists can be accessed. */
3142 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3144 #if ( configNUMBER_OF_CORES == 1 )
3146 /* Ready lists can be accessed so move the task from the
3147 * suspended list to the ready list directly. */
3148 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3150 xYieldRequired = pdTRUE;
3152 /* Mark that a yield is pending in case the user is not
3153 * using the return value to initiate a context switch
3154 * from the ISR using portYIELD_FROM_ISR. */
3155 xYieldPendings[ 0 ] = pdTRUE;
3159 mtCOVERAGE_TEST_MARKER();
3162 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3164 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3165 prvAddTaskToReadyList( pxTCB );
3169 /* The delayed or ready lists cannot be accessed so the task
3170 * is held in the pending ready list until the scheduler is
3172 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3175 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3177 prvYieldForTask( pxTCB );
3179 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3181 xYieldRequired = pdTRUE;
3184 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3188 mtCOVERAGE_TEST_MARKER();
3191 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3193 traceRETURN_xTaskResumeFromISR( xYieldRequired );
3195 return xYieldRequired;
3198 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3199 /*-----------------------------------------------------------*/
3201 static BaseType_t prvCreateIdleTasks( void )
3203 BaseType_t xReturn = pdPASS;
3205 #if ( configNUMBER_OF_CORES == 1 )
3207 /* Add the idle task at the lowest priority. */
3208 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3210 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3211 StackType_t * pxIdleTaskStackBuffer = NULL;
3212 uint32_t ulIdleTaskStackSize;
3214 /* The Idle task is created using user provided RAM - obtain the
3215 * address of the RAM then create the idle task. */
3216 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3217 xIdleTaskHandles[ 0 ] = xTaskCreateStatic( prvIdleTask,
3218 configIDLE_TASK_NAME,
3219 ulIdleTaskStackSize,
3220 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3221 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3222 pxIdleTaskStackBuffer,
3223 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3225 if( xIdleTaskHandles[ 0 ] != NULL )
3234 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3236 /* The Idle task is being created using dynamically allocated RAM. */
3237 xReturn = xTaskCreate( prvIdleTask,
3238 configIDLE_TASK_NAME,
3239 configMINIMAL_STACK_SIZE,
3241 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3242 &xIdleTaskHandles[ 0 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3244 #endif /* configSUPPORT_STATIC_ALLOCATION */
3246 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3249 char cIdleName[ configMAX_TASK_NAME_LEN ];
3251 /* Add each idle task at the lowest priority. */
3252 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3256 if( xReturn == pdFAIL )
3262 mtCOVERAGE_TEST_MARKER();
3265 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3267 cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
3269 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3270 * configMAX_TASK_NAME_LEN characters just in case the memory after the
3271 * string is not accessible (extremely unlikely). */
3272 if( cIdleName[ x ] == ( char ) 0x00 )
3278 mtCOVERAGE_TEST_MARKER();
3282 /* Append the idle task number to the end of the name if there is space. */
3283 if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3285 cIdleName[ x ] = ( char ) ( xCoreID + '0' );
3288 /* And append a null character if there is space. */
3289 if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3291 cIdleName[ x ] = '\0';
3295 mtCOVERAGE_TEST_MARKER();
3300 mtCOVERAGE_TEST_MARKER();
3303 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3307 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3308 StackType_t * pxIdleTaskStackBuffer = NULL;
3309 uint32_t ulIdleTaskStackSize;
3311 /* The Idle task is created using user provided RAM - obtain the
3312 * address of the RAM then create the idle task. */
3313 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3314 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
3316 ulIdleTaskStackSize,
3317 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3318 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3319 pxIdleTaskStackBuffer,
3320 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3324 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( prvMinimalIdleTask,
3326 configMINIMAL_STACK_SIZE,
3327 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3328 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3329 xIdleTaskStackBuffers[ xCoreID - 1 ],
3330 &xIdleTCBBuffers[ xCoreID - 1 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3333 if( xIdleTaskHandles[ xCoreID ] != NULL )
3342 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3346 /* The Idle task is being created using dynamically allocated RAM. */
3347 xReturn = xTaskCreate( prvIdleTask,
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. */
3356 xReturn = xTaskCreate( prvMinimalIdleTask,
3358 configMINIMAL_STACK_SIZE,
3360 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3361 &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3364 #endif /* configSUPPORT_STATIC_ALLOCATION */
3367 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3372 /*-----------------------------------------------------------*/
3374 void vTaskStartScheduler( void )
3378 traceENTER_vTaskStartScheduler();
3380 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3382 /* Sanity check that the UBaseType_t must have greater than or equal to
3383 * the number of bits as confNUMBER_OF_CORES. */
3384 configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3386 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3388 xReturn = prvCreateIdleTasks();
3390 #if ( configUSE_TIMERS == 1 )
3392 if( xReturn == pdPASS )
3394 xReturn = xTimerCreateTimerTask();
3398 mtCOVERAGE_TEST_MARKER();
3401 #endif /* configUSE_TIMERS */
3403 if( xReturn == pdPASS )
3405 /* freertos_tasks_c_additions_init() should only be called if the user
3406 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3407 * the only macro called by the function. */
3408 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3410 freertos_tasks_c_additions_init();
3414 /* Interrupts are turned off here, to ensure a tick does not occur
3415 * before or during the call to xPortStartScheduler(). The stacks of
3416 * the created tasks contain a status word with interrupts switched on
3417 * so interrupts will automatically get re-enabled when the first task
3419 portDISABLE_INTERRUPTS();
3421 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3423 /* Switch C-Runtime's TLS Block to point to the TLS
3424 * block specific to the task that will run first. */
3425 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3429 xNextTaskUnblockTime = portMAX_DELAY;
3430 xSchedulerRunning = pdTRUE;
3431 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3433 /* If configGENERATE_RUN_TIME_STATS is defined then the following
3434 * macro must be defined to configure the timer/counter used to generate
3435 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
3436 * is set to 0 and the following line fails to build then ensure you do not
3437 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3438 * FreeRTOSConfig.h file. */
3439 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3441 traceTASK_SWITCHED_IN();
3443 /* Setting up the timer tick is hardware specific and thus in the
3444 * portable interface. */
3445 xPortStartScheduler();
3447 /* In most cases, xPortStartScheduler() will not return. If it
3448 * returns pdTRUE then there was not enough heap memory available
3449 * to create either the Idle or the Timer task. If it returned
3450 * pdFALSE, then the application called xTaskEndScheduler().
3451 * Most ports don't implement xTaskEndScheduler() as there is
3452 * nothing to return to. */
3456 /* This line will only be reached if the kernel could not be started,
3457 * because there was not enough FreeRTOS heap to create the idle task
3458 * or the timer task. */
3459 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3462 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3463 * meaning xIdleTaskHandles are not used anywhere else. */
3464 ( void ) xIdleTaskHandles;
3466 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3467 * from getting optimized out as it is no longer used by the kernel. */
3468 ( void ) uxTopUsedPriority;
3470 traceRETURN_vTaskStartScheduler();
3472 /*-----------------------------------------------------------*/
3474 void vTaskEndScheduler( void )
3476 traceENTER_vTaskEndScheduler();
3478 /* Stop the scheduler interrupts and call the portable scheduler end
3479 * routine so the original ISRs can be restored if necessary. The port
3480 * layer must ensure interrupts enable bit is left in the correct state. */
3481 portDISABLE_INTERRUPTS();
3482 xSchedulerRunning = pdFALSE;
3483 vPortEndScheduler();
3485 traceRETURN_vTaskEndScheduler();
3487 /*----------------------------------------------------------*/
3489 void vTaskSuspendAll( void )
3491 traceENTER_vTaskSuspendAll();
3493 #if ( configNUMBER_OF_CORES == 1 )
3495 /* A critical section is not required as the variable is of type
3496 * BaseType_t. Please read Richard Barry's reply in the following link to a
3497 * post in the FreeRTOS support forum before reporting this as a bug! -
3498 * https://goo.gl/wu4acr */
3500 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3501 * do not otherwise exhibit real time behaviour. */
3502 portSOFTWARE_BARRIER();
3504 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3505 * is used to allow calls to vTaskSuspendAll() to nest. */
3506 ++uxSchedulerSuspended;
3508 /* Enforces ordering for ports and optimised compilers that may otherwise place
3509 * the above increment elsewhere. */
3510 portMEMORY_BARRIER();
3512 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3514 UBaseType_t ulState;
3516 /* This must only be called from within a task. */
3517 portASSERT_IF_IN_ISR();
3519 if( xSchedulerRunning != pdFALSE )
3521 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3522 * We must disable interrupts before we grab the locks in the event that this task is
3523 * interrupted and switches context before incrementing uxSchedulerSuspended.
3524 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3525 * uxSchedulerSuspended since that will prevent context switches. */
3526 ulState = portSET_INTERRUPT_MASK();
3528 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3529 * do not otherwise exhibit real time behaviour. */
3530 portSOFTWARE_BARRIER();
3532 portGET_TASK_LOCK();
3534 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3535 * purpose is to prevent altering the variable when fromISR APIs are readying
3537 if( uxSchedulerSuspended == 0U )
3539 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
3541 prvCheckForRunStateChange();
3545 mtCOVERAGE_TEST_MARKER();
3550 mtCOVERAGE_TEST_MARKER();
3555 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3556 * is used to allow calls to vTaskSuspendAll() to nest. */
3557 ++uxSchedulerSuspended;
3558 portRELEASE_ISR_LOCK();
3560 portCLEAR_INTERRUPT_MASK( ulState );
3564 mtCOVERAGE_TEST_MARKER();
3567 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3569 traceRETURN_vTaskSuspendAll();
3572 /*----------------------------------------------------------*/
3574 #if ( configUSE_TICKLESS_IDLE != 0 )
3576 static TickType_t prvGetExpectedIdleTime( void )
3579 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
3581 /* uxHigherPriorityReadyTasks takes care of the case where
3582 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3583 * task that are in the Ready state, even though the idle task is
3585 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3587 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3589 uxHigherPriorityReadyTasks = pdTRUE;
3594 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3596 /* When port optimised task selection is used the uxTopReadyPriority
3597 * variable is used as a bit map. If bits other than the least
3598 * significant bit are set then there are tasks that have a priority
3599 * above the idle priority that are in the Ready state. This takes
3600 * care of the case where the co-operative scheduler is in use. */
3601 if( uxTopReadyPriority > uxLeastSignificantBit )
3603 uxHigherPriorityReadyTasks = pdTRUE;
3606 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3608 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3612 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
3614 /* There are other idle priority tasks in the ready state. If
3615 * time slicing is used then the very next tick interrupt must be
3619 else if( uxHigherPriorityReadyTasks != pdFALSE )
3621 /* There are tasks in the Ready state that have a priority above the
3622 * idle priority. This path can only be reached if
3623 * configUSE_PREEMPTION is 0. */
3628 xReturn = xNextTaskUnblockTime - xTickCount;
3634 #endif /* configUSE_TICKLESS_IDLE */
3635 /*----------------------------------------------------------*/
3637 BaseType_t xTaskResumeAll( void )
3639 TCB_t * pxTCB = NULL;
3640 BaseType_t xAlreadyYielded = pdFALSE;
3642 traceENTER_xTaskResumeAll();
3644 #if ( configNUMBER_OF_CORES > 1 )
3645 if( xSchedulerRunning != pdFALSE )
3648 /* It is possible that an ISR caused a task to be removed from an event
3649 * list while the scheduler was suspended. If this was the case then the
3650 * removed task will have been added to the xPendingReadyList. Once the
3651 * scheduler has been resumed it is safe to move all the pending ready
3652 * tasks from this list into their appropriate ready list. */
3653 taskENTER_CRITICAL();
3656 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3658 /* If uxSchedulerSuspended is zero then this function does not match a
3659 * previous call to vTaskSuspendAll(). */
3660 configASSERT( uxSchedulerSuspended != 0U );
3662 --uxSchedulerSuspended;
3663 portRELEASE_TASK_LOCK();
3665 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3667 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3669 /* Move any readied tasks from the pending list into the
3670 * appropriate ready list. */
3671 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3673 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. */
3674 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
3675 portMEMORY_BARRIER();
3676 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
3677 prvAddTaskToReadyList( pxTCB );
3679 #if ( configNUMBER_OF_CORES == 1 )
3681 /* If the moved task has a priority higher than the current
3682 * task then a yield must be performed. */
3683 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3685 xYieldPendings[ xCoreID ] = pdTRUE;
3689 mtCOVERAGE_TEST_MARKER();
3692 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3694 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
3695 * If the current core yielded then vTaskSwitchContext() has already been called
3696 * which sets xYieldPendings for the current core to pdTRUE. */
3698 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3703 /* A task was unblocked while the scheduler was suspended,
3704 * which may have prevented the next unblock time from being
3705 * re-calculated, in which case re-calculate it now. Mainly
3706 * important for low power tickless implementations, where
3707 * this can prevent an unnecessary exit from low power
3709 prvResetNextTaskUnblockTime();
3712 /* If any ticks occurred while the scheduler was suspended then
3713 * they should be processed now. This ensures the tick count does
3714 * not slip, and that any delayed tasks are resumed at the correct
3717 * It should be safe to call xTaskIncrementTick here from any core
3718 * since we are in a critical section and xTaskIncrementTick itself
3719 * protects itself within a critical section. Suspending the scheduler
3720 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
3722 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3724 if( xPendedCounts > ( TickType_t ) 0U )
3728 if( xTaskIncrementTick() != pdFALSE )
3730 /* Other cores are interrupted from
3731 * within xTaskIncrementTick(). */
3732 xYieldPendings[ xCoreID ] = pdTRUE;
3736 mtCOVERAGE_TEST_MARKER();
3740 } while( xPendedCounts > ( TickType_t ) 0U );
3746 mtCOVERAGE_TEST_MARKER();
3750 if( xYieldPendings[ xCoreID ] != pdFALSE )
3752 #if ( configUSE_PREEMPTION != 0 )
3754 xAlreadyYielded = pdTRUE;
3756 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
3758 #if ( configNUMBER_OF_CORES == 1 )
3760 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
3762 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3766 mtCOVERAGE_TEST_MARKER();
3772 mtCOVERAGE_TEST_MARKER();
3775 taskEXIT_CRITICAL();
3778 traceRETURN_xTaskResumeAll( xAlreadyYielded );
3780 return xAlreadyYielded;
3782 /*-----------------------------------------------------------*/
3784 TickType_t xTaskGetTickCount( void )
3788 traceENTER_xTaskGetTickCount();
3790 /* Critical section required if running on a 16 bit processor. */
3791 portTICK_TYPE_ENTER_CRITICAL();
3793 xTicks = xTickCount;
3795 portTICK_TYPE_EXIT_CRITICAL();
3797 traceRETURN_xTaskGetTickCount( xTicks );
3801 /*-----------------------------------------------------------*/
3803 TickType_t xTaskGetTickCountFromISR( void )
3806 UBaseType_t uxSavedInterruptStatus;
3808 traceENTER_xTaskGetTickCountFromISR();
3810 /* RTOS ports that support interrupt nesting have the concept of a maximum
3811 * system call (or maximum API call) interrupt priority. Interrupts that are
3812 * above the maximum system call priority are kept permanently enabled, even
3813 * when the RTOS kernel is in a critical section, but cannot make any calls to
3814 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
3815 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3816 * failure if a FreeRTOS API function is called from an interrupt that has been
3817 * assigned a priority above the configured maximum system call priority.
3818 * Only FreeRTOS functions that end in FromISR can be called from interrupts
3819 * that have been assigned a priority at or (logically) below the maximum
3820 * system call interrupt priority. FreeRTOS maintains a separate interrupt
3821 * safe API to ensure interrupt entry is as fast and as simple as possible.
3822 * More information (albeit Cortex-M specific) is provided on the following
3823 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3824 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3826 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
3828 xReturn = xTickCount;
3830 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3832 traceRETURN_xTaskGetTickCountFromISR( xReturn );
3836 /*-----------------------------------------------------------*/
3838 UBaseType_t uxTaskGetNumberOfTasks( void )
3840 traceENTER_uxTaskGetNumberOfTasks();
3842 /* A critical section is not required because the variables are of type
3844 traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
3846 return uxCurrentNumberOfTasks;
3848 /*-----------------------------------------------------------*/
3850 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3854 traceENTER_pcTaskGetName( xTaskToQuery );
3856 /* If null is passed in here then the name of the calling task is being
3858 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3859 configASSERT( pxTCB );
3861 traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
3863 return &( pxTCB->pcTaskName[ 0 ] );
3865 /*-----------------------------------------------------------*/
3867 #if ( INCLUDE_xTaskGetHandle == 1 )
3869 #if ( configNUMBER_OF_CORES == 1 )
3870 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3871 const char pcNameToQuery[] )
3875 TCB_t * pxReturn = NULL;
3878 BaseType_t xBreakLoop;
3880 /* This function is called with the scheduler suspended. */
3882 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3884 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. */
3888 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. */
3890 /* Check each character in the name looking for a match or
3892 xBreakLoop = pdFALSE;
3894 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3896 cNextChar = pxNextTCB->pcTaskName[ x ];
3898 if( cNextChar != pcNameToQuery[ x ] )
3900 /* Characters didn't match. */
3901 xBreakLoop = pdTRUE;
3903 else if( cNextChar == ( char ) 0x00 )
3905 /* Both strings terminated, a match must have been
3907 pxReturn = pxNextTCB;
3908 xBreakLoop = pdTRUE;
3912 mtCOVERAGE_TEST_MARKER();
3915 if( xBreakLoop != pdFALSE )
3921 if( pxReturn != NULL )
3923 /* The handle has been found. */
3926 } while( pxNextTCB != pxFirstTCB );
3930 mtCOVERAGE_TEST_MARKER();
3935 #else /* if ( configNUMBER_OF_CORES == 1 ) */
3936 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3937 const char pcNameToQuery[] )
3939 TCB_t * pxReturn = NULL;
3942 BaseType_t xBreakLoop;
3943 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
3944 ListItem_t * pxIterator;
3946 /* This function is called with the scheduler suspended. */
3948 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3950 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
3952 TCB_t * pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
3954 /* Check each character in the name looking for a match or
3956 xBreakLoop = pdFALSE;
3958 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3960 cNextChar = pxTCB->pcTaskName[ x ];
3962 if( cNextChar != pcNameToQuery[ x ] )
3964 /* Characters didn't match. */
3965 xBreakLoop = pdTRUE;
3967 else if( cNextChar == ( char ) 0x00 )
3969 /* Both strings terminated, a match must have been
3972 xBreakLoop = pdTRUE;
3976 mtCOVERAGE_TEST_MARKER();
3979 if( xBreakLoop != pdFALSE )
3985 if( pxReturn != NULL )
3987 /* The handle has been found. */
3994 mtCOVERAGE_TEST_MARKER();
3999 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4001 #endif /* INCLUDE_xTaskGetHandle */
4002 /*-----------------------------------------------------------*/
4004 #if ( INCLUDE_xTaskGetHandle == 1 )
4006 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
4008 UBaseType_t uxQueue = configMAX_PRIORITIES;
4011 traceENTER_xTaskGetHandle( pcNameToQuery );
4013 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
4014 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
4018 /* Search the ready lists. */
4022 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
4026 /* Found the handle. */
4029 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4031 /* Search the delayed lists. */
4034 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
4039 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
4042 #if ( INCLUDE_vTaskSuspend == 1 )
4046 /* Search the suspended list. */
4047 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
4052 #if ( INCLUDE_vTaskDelete == 1 )
4056 /* Search the deleted list. */
4057 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
4062 ( void ) xTaskResumeAll();
4064 traceRETURN_xTaskGetHandle( pxTCB );
4069 #endif /* INCLUDE_xTaskGetHandle */
4070 /*-----------------------------------------------------------*/
4072 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4074 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4075 StackType_t ** ppuxStackBuffer,
4076 StaticTask_t ** ppxTaskBuffer )
4081 traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4083 configASSERT( ppuxStackBuffer != NULL );
4084 configASSERT( ppxTaskBuffer != NULL );
4086 pxTCB = prvGetTCBFromHandle( xTask );
4088 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4090 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4092 *ppuxStackBuffer = pxTCB->pxStack;
4093 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4096 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4098 *ppuxStackBuffer = pxTCB->pxStack;
4099 *ppxTaskBuffer = NULL;
4107 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4109 *ppuxStackBuffer = pxTCB->pxStack;
4110 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4113 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4115 traceRETURN_xTaskGetStaticBuffers( xReturn );
4120 #endif /* configSUPPORT_STATIC_ALLOCATION */
4121 /*-----------------------------------------------------------*/
4123 #if ( configUSE_TRACE_FACILITY == 1 )
4125 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4126 const UBaseType_t uxArraySize,
4127 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4129 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4131 traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4135 /* Is there a space in the array for each task in the system? */
4136 if( uxArraySize >= uxCurrentNumberOfTasks )
4138 /* Fill in an TaskStatus_t structure with information on each
4139 * task in the Ready state. */
4143 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4144 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4146 /* Fill in an TaskStatus_t structure with information on each
4147 * task in the Blocked state. */
4148 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4149 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4151 #if ( INCLUDE_vTaskDelete == 1 )
4153 /* Fill in an TaskStatus_t structure with information on
4154 * each task that has been deleted but not yet cleaned up. */
4155 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4159 #if ( INCLUDE_vTaskSuspend == 1 )
4161 /* Fill in an TaskStatus_t structure with information on
4162 * each task in the Suspended state. */
4163 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4167 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4169 if( pulTotalRunTime != NULL )
4171 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4172 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4174 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4178 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4180 if( pulTotalRunTime != NULL )
4182 *pulTotalRunTime = 0;
4185 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4189 mtCOVERAGE_TEST_MARKER();
4192 ( void ) xTaskResumeAll();
4194 traceRETURN_uxTaskGetSystemState( uxTask );
4199 #endif /* configUSE_TRACE_FACILITY */
4200 /*----------------------------------------------------------*/
4202 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4204 /* SMP_TODO : This function returns only idle task handle for core 0.
4205 * Consider to add another function to return the idle task handles. */
4206 TaskHandle_t xTaskGetIdleTaskHandle( void )
4208 traceENTER_xTaskGetIdleTaskHandle();
4210 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4211 * started, then xIdleTaskHandles will be NULL. */
4212 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4214 traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4216 return xIdleTaskHandles[ 0 ];
4219 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4220 /*----------------------------------------------------------*/
4222 /* This conditional compilation should use inequality to 0, not equality to 1.
4223 * This is to ensure vTaskStepTick() is available when user defined low power mode
4224 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4226 #if ( configUSE_TICKLESS_IDLE != 0 )
4228 void vTaskStepTick( TickType_t xTicksToJump )
4230 traceENTER_vTaskStepTick( xTicksToJump );
4232 /* Correct the tick count value after a period during which the tick
4233 * was suppressed. Note this does *not* call the tick hook function for
4234 * each stepped tick. */
4235 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
4237 if( ( xTickCount + xTicksToJump ) == xNextTaskUnblockTime )
4239 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4240 * xTaskIncrementTick() when the scheduler resumes. This ensures
4241 * that any delayed tasks are resumed at the correct time. */
4242 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4243 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4245 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4246 taskENTER_CRITICAL();
4250 taskEXIT_CRITICAL();
4255 mtCOVERAGE_TEST_MARKER();
4258 xTickCount += xTicksToJump;
4260 traceINCREASE_TICK_COUNT( xTicksToJump );
4261 traceRETURN_vTaskStepTick();
4264 #endif /* configUSE_TICKLESS_IDLE */
4265 /*----------------------------------------------------------*/
4267 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4269 BaseType_t xYieldOccurred;
4271 traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4273 /* Must not be called with the scheduler suspended as the implementation
4274 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4275 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4277 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4278 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4281 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4282 taskENTER_CRITICAL();
4284 xPendedTicks += xTicksToCatchUp;
4286 taskEXIT_CRITICAL();
4287 xYieldOccurred = xTaskResumeAll();
4289 traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4291 return xYieldOccurred;
4293 /*----------------------------------------------------------*/
4295 #if ( INCLUDE_xTaskAbortDelay == 1 )
4297 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4299 TCB_t * pxTCB = xTask;
4302 traceENTER_xTaskAbortDelay( xTask );
4304 configASSERT( pxTCB );
4308 /* A task can only be prematurely removed from the Blocked state if
4309 * it is actually in the Blocked state. */
4310 if( eTaskGetState( xTask ) == eBlocked )
4314 /* Remove the reference to the task from the blocked list. An
4315 * interrupt won't touch the xStateListItem because the
4316 * scheduler is suspended. */
4317 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4319 /* Is the task waiting on an event also? If so remove it from
4320 * the event list too. Interrupts can touch the event list item,
4321 * even though the scheduler is suspended, so a critical section
4323 taskENTER_CRITICAL();
4325 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4327 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4329 /* This lets the task know it was forcibly removed from the
4330 * blocked state so it should not re-evaluate its block time and
4331 * then block again. */
4332 pxTCB->ucDelayAborted = pdTRUE;
4336 mtCOVERAGE_TEST_MARKER();
4339 taskEXIT_CRITICAL();
4341 /* Place the unblocked task into the appropriate ready list. */
4342 prvAddTaskToReadyList( pxTCB );
4344 /* A task being unblocked cannot cause an immediate context
4345 * switch if preemption is turned off. */
4346 #if ( configUSE_PREEMPTION == 1 )
4348 #if ( configNUMBER_OF_CORES == 1 )
4350 /* Preemption is on, but a context switch should only be
4351 * performed if the unblocked task has a priority that is
4352 * higher than the currently executing task. */
4353 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4355 /* Pend the yield to be performed when the scheduler
4356 * is unsuspended. */
4357 xYieldPendings[ 0 ] = pdTRUE;
4361 mtCOVERAGE_TEST_MARKER();
4364 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4366 taskENTER_CRITICAL();
4368 prvYieldForTask( pxTCB );
4370 taskEXIT_CRITICAL();
4372 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4374 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4381 ( void ) xTaskResumeAll();
4383 traceRETURN_xTaskAbortDelay( xReturn );
4388 #endif /* INCLUDE_xTaskAbortDelay */
4389 /*----------------------------------------------------------*/
4391 BaseType_t xTaskIncrementTick( void )
4394 TickType_t xItemValue;
4395 BaseType_t xSwitchRequired = pdFALSE;
4397 traceENTER_xTaskIncrementTick();
4399 #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4400 BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4401 #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4403 /* Called by the portable layer each time a tick interrupt occurs.
4404 * Increments the tick then checks to see if the new tick value will cause any
4405 * tasks to be unblocked. */
4406 traceTASK_INCREMENT_TICK( xTickCount );
4408 /* Tick increment should occur on every kernel timer event. Core 0 has the
4409 * responsibility to increment the tick, or increment the pended ticks if the
4410 * scheduler is suspended. If pended ticks is greater than zero, the core that
4411 * calls xTaskResumeAll has the responsibility to increment the tick. */
4412 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4414 /* Minor optimisation. The tick count cannot change in this
4416 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4418 /* Increment the RTOS tick, switching the delayed and overflowed
4419 * delayed lists if it wraps to 0. */
4420 xTickCount = xConstTickCount;
4422 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
4424 taskSWITCH_DELAYED_LISTS();
4428 mtCOVERAGE_TEST_MARKER();
4431 /* See if this tick has made a timeout expire. Tasks are stored in
4432 * the queue in the order of their wake time - meaning once one task
4433 * has been found whose block time has not expired there is no need to
4434 * look any further down the list. */
4435 if( xConstTickCount >= xNextTaskUnblockTime )
4439 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4441 /* The delayed list is empty. Set xNextTaskUnblockTime
4442 * to the maximum possible value so it is extremely
4444 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4445 * next time through. */
4446 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4451 /* The delayed list is not empty, get the value of the
4452 * item at the head of the delayed list. This is the time
4453 * at which the task at the head of the delayed list must
4454 * be removed from the Blocked state. */
4455 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. */
4456 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4458 if( xConstTickCount < xItemValue )
4460 /* It is not time to unblock this item yet, but the
4461 * item value is the time at which the task at the head
4462 * of the blocked list must be removed from the Blocked
4463 * state - so record the item value in
4464 * xNextTaskUnblockTime. */
4465 xNextTaskUnblockTime = xItemValue;
4466 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
4470 mtCOVERAGE_TEST_MARKER();
4473 /* It is time to remove the item from the Blocked state. */
4474 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4476 /* Is the task waiting on an event also? If so remove
4477 * it from the event list. */
4478 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4480 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4484 mtCOVERAGE_TEST_MARKER();
4487 /* Place the unblocked task into the appropriate ready
4489 prvAddTaskToReadyList( pxTCB );
4491 /* A task being unblocked cannot cause an immediate
4492 * context switch if preemption is turned off. */
4493 #if ( configUSE_PREEMPTION == 1 )
4495 #if ( configNUMBER_OF_CORES == 1 )
4497 /* Preemption is on, but a context switch should
4498 * only be performed if the unblocked task's
4499 * priority is higher than the currently executing
4501 * The case of equal priority tasks sharing
4502 * processing time (which happens when both
4503 * preemption and time slicing are on) is
4505 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4507 xSwitchRequired = pdTRUE;
4511 mtCOVERAGE_TEST_MARKER();
4514 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4516 prvYieldForTask( pxTCB );
4518 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4520 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4525 /* Tasks of equal priority to the currently running task will share
4526 * processing time (time slice) if preemption is on, and the application
4527 * writer has not explicitly turned time slicing off. */
4528 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4530 #if ( configNUMBER_OF_CORES == 1 )
4532 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 )
4534 xSwitchRequired = pdTRUE;
4538 mtCOVERAGE_TEST_MARKER();
4541 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4545 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4547 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1 )
4549 xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4553 mtCOVERAGE_TEST_MARKER();
4557 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4559 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4561 #if ( configUSE_TICK_HOOK == 1 )
4563 /* Guard against the tick hook being called when the pended tick
4564 * count is being unwound (when the scheduler is being unlocked). */
4565 if( xPendedTicks == ( TickType_t ) 0 )
4567 vApplicationTickHook();
4571 mtCOVERAGE_TEST_MARKER();
4574 #endif /* configUSE_TICK_HOOK */
4576 #if ( configUSE_PREEMPTION == 1 )
4578 #if ( configNUMBER_OF_CORES == 1 )
4580 /* For single core the core ID is always 0. */
4581 if( xYieldPendings[ 0 ] != pdFALSE )
4583 xSwitchRequired = pdTRUE;
4587 mtCOVERAGE_TEST_MARKER();
4590 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4592 BaseType_t xCoreID, xCurrentCoreID;
4593 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4595 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4597 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4598 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4601 if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4603 if( xCoreID == xCurrentCoreID )
4605 xSwitchRequired = pdTRUE;
4609 prvYieldCore( xCoreID );
4614 mtCOVERAGE_TEST_MARKER();
4619 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4621 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4627 /* The tick hook gets called at regular intervals, even if the
4628 * scheduler is locked. */
4629 #if ( configUSE_TICK_HOOK == 1 )
4631 vApplicationTickHook();
4636 traceRETURN_xTaskIncrementTick( xSwitchRequired );
4638 return xSwitchRequired;
4640 /*-----------------------------------------------------------*/
4642 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4644 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4645 TaskHookFunction_t pxHookFunction )
4649 traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4651 /* If xTask is NULL then it is the task hook of the calling task that is
4655 xTCB = ( TCB_t * ) pxCurrentTCB;
4662 /* Save the hook function in the TCB. A critical section is required as
4663 * the value can be accessed from an interrupt. */
4664 taskENTER_CRITICAL();
4666 xTCB->pxTaskTag = pxHookFunction;
4668 taskEXIT_CRITICAL();
4670 traceRETURN_vTaskSetApplicationTaskTag();
4673 #endif /* configUSE_APPLICATION_TASK_TAG */
4674 /*-----------------------------------------------------------*/
4676 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4678 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4681 TaskHookFunction_t xReturn;
4683 traceENTER_xTaskGetApplicationTaskTag( xTask );
4685 /* If xTask is NULL then set the calling task's hook. */
4686 pxTCB = prvGetTCBFromHandle( xTask );
4688 /* Save the hook function in the TCB. A critical section is required as
4689 * the value can be accessed from an interrupt. */
4690 taskENTER_CRITICAL();
4692 xReturn = pxTCB->pxTaskTag;
4694 taskEXIT_CRITICAL();
4696 traceRETURN_xTaskGetApplicationTaskTag( xReturn );
4701 #endif /* configUSE_APPLICATION_TASK_TAG */
4702 /*-----------------------------------------------------------*/
4704 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4706 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
4709 TaskHookFunction_t xReturn;
4710 UBaseType_t uxSavedInterruptStatus;
4712 traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
4714 /* If xTask is NULL then set the calling task's hook. */
4715 pxTCB = prvGetTCBFromHandle( xTask );
4717 /* Save the hook function in the TCB. A critical section is required as
4718 * the value can be accessed from an interrupt. */
4719 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
4721 xReturn = pxTCB->pxTaskTag;
4723 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
4725 traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
4730 #endif /* configUSE_APPLICATION_TASK_TAG */
4731 /*-----------------------------------------------------------*/
4733 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4735 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
4736 void * pvParameter )
4741 traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
4743 /* If xTask is NULL then we are calling our own task hook. */
4746 xTCB = pxCurrentTCB;
4753 if( xTCB->pxTaskTag != NULL )
4755 xReturn = xTCB->pxTaskTag( pvParameter );
4762 traceRETURN_xTaskCallApplicationTaskHook( xReturn );
4767 #endif /* configUSE_APPLICATION_TASK_TAG */
4768 /*-----------------------------------------------------------*/
4770 #if ( configNUMBER_OF_CORES == 1 )
4771 void vTaskSwitchContext( void )
4773 traceENTER_vTaskSwitchContext();
4775 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
4777 /* The scheduler is currently suspended - do not allow a context
4779 xYieldPendings[ 0 ] = pdTRUE;
4783 xYieldPendings[ 0 ] = pdFALSE;
4784 traceTASK_SWITCHED_OUT();
4786 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4788 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4789 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
4791 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
4794 /* Add the amount of time the task has been running to the
4795 * accumulated time so far. The time the task started running was
4796 * stored in ulTaskSwitchedInTime. Note that there is no overflow
4797 * protection here so count values are only valid until the timer
4798 * overflows. The guard against negative values is to protect
4799 * against suspect run time stat counter implementations - which
4800 * are provided by the application, not the kernel. */
4801 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
4803 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
4807 mtCOVERAGE_TEST_MARKER();
4810 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
4812 #endif /* configGENERATE_RUN_TIME_STATS */
4814 /* Check for stack overflow, if configured. */
4815 taskCHECK_FOR_STACK_OVERFLOW();
4817 /* Before the currently running task is switched out, save its errno. */
4818 #if ( configUSE_POSIX_ERRNO == 1 )
4820 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
4824 /* Select a new task to run using either the generic C or port
4825 * optimised asm code. */
4826 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. */
4827 traceTASK_SWITCHED_IN();
4829 /* After the new task is switched in, update the global errno. */
4830 #if ( configUSE_POSIX_ERRNO == 1 )
4832 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
4836 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
4838 /* Switch C-Runtime's TLS Block to point to the TLS
4839 * Block specific to this task. */
4840 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
4845 traceRETURN_vTaskSwitchContext();
4847 #else /* if ( configNUMBER_OF_CORES == 1 ) */
4848 void vTaskSwitchContext( BaseType_t xCoreID )
4850 traceENTER_vTaskSwitchContext();
4852 /* Acquire both locks:
4853 * - The ISR lock protects the ready list from simultaneous access by
4854 * both other ISRs and tasks.
4855 * - We also take the task lock to pause here in case another core has
4856 * suspended the scheduler. We don't want to simply set xYieldPending
4857 * and move on if another core suspended the scheduler. We should only
4858 * do that if the current core has suspended the scheduler. */
4860 portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
4863 /* vTaskSwitchContext() must never be called from within a critical section.
4864 * This is not necessarily true for single core FreeRTOS, but it is for this
4866 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
4868 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
4870 /* The scheduler is currently suspended - do not allow a context
4872 xYieldPendings[ xCoreID ] = pdTRUE;
4876 xYieldPendings[ xCoreID ] = pdFALSE;
4877 traceTASK_SWITCHED_OUT();
4879 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4881 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4882 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
4884 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
4887 /* Add the amount of time the task has been running to the
4888 * accumulated time so far. The time the task started running was
4889 * stored in ulTaskSwitchedInTime. Note that there is no overflow
4890 * protection here so count values are only valid until the timer
4891 * overflows. The guard against negative values is to protect
4892 * against suspect run time stat counter implementations - which
4893 * are provided by the application, not the kernel. */
4894 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
4896 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
4900 mtCOVERAGE_TEST_MARKER();
4903 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
4905 #endif /* configGENERATE_RUN_TIME_STATS */
4907 /* Check for stack overflow, if configured. */
4908 taskCHECK_FOR_STACK_OVERFLOW();
4910 /* Before the currently running task is switched out, save its errno. */
4911 #if ( configUSE_POSIX_ERRNO == 1 )
4913 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
4917 /* Select a new task to run. */
4918 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
4919 traceTASK_SWITCHED_IN();
4921 /* After the new task is switched in, update the global errno. */
4922 #if ( configUSE_POSIX_ERRNO == 1 )
4924 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
4928 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
4930 /* Switch C-Runtime's TLS Block to point to the TLS
4931 * Block specific to this task. */
4932 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
4937 portRELEASE_ISR_LOCK();
4938 portRELEASE_TASK_LOCK();
4940 traceRETURN_vTaskSwitchContext();
4942 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
4943 /*-----------------------------------------------------------*/
4945 void vTaskPlaceOnEventList( List_t * const pxEventList,
4946 const TickType_t xTicksToWait )
4948 traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
4950 configASSERT( pxEventList );
4952 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
4953 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
4955 /* Place the event list item of the TCB in the appropriate event list.
4956 * This is placed in the list in priority order so the highest priority task
4957 * is the first to be woken by the event.
4959 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
4960 * Normally, the xItemValue of a TCB's ListItem_t members is:
4961 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
4962 * Therefore, the event list is sorted in descending priority order.
4964 * The queue that contains the event list is locked, preventing
4965 * simultaneous access from interrupts. */
4966 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4968 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4970 traceRETURN_vTaskPlaceOnEventList();
4972 /*-----------------------------------------------------------*/
4974 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
4975 const TickType_t xItemValue,
4976 const TickType_t xTicksToWait )
4978 traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
4980 configASSERT( pxEventList );
4982 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
4983 * the event groups implementation. */
4984 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4986 /* Store the item value in the event list item. It is safe to access the
4987 * event list item here as interrupts won't access the event list item of a
4988 * task that is not in the Blocked state. */
4989 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4991 /* Place the event list item of the TCB at the end of the appropriate event
4992 * list. It is safe to access the event list here because it is part of an
4993 * event group implementation - and interrupts don't access event groups
4994 * directly (instead they access them indirectly by pending function calls to
4995 * the task level). */
4996 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4998 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5000 traceRETURN_vTaskPlaceOnUnorderedEventList();
5002 /*-----------------------------------------------------------*/
5004 #if ( configUSE_TIMERS == 1 )
5006 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
5007 TickType_t xTicksToWait,
5008 const BaseType_t xWaitIndefinitely )
5010 traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
5012 configASSERT( pxEventList );
5014 /* This function should not be called by application code hence the
5015 * 'Restricted' in its name. It is not part of the public API. It is
5016 * designed for use by kernel code, and has special calling requirements -
5017 * it should be called with the scheduler suspended. */
5020 /* Place the event list item of the TCB in the appropriate event list.
5021 * In this case it is assume that this is the only task that is going to
5022 * be waiting on this event list, so the faster vListInsertEnd() function
5023 * can be used in place of vListInsert. */
5024 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5026 /* If the task should block indefinitely then set the block time to a
5027 * value that will be recognised as an indefinite delay inside the
5028 * prvAddCurrentTaskToDelayedList() function. */
5029 if( xWaitIndefinitely != pdFALSE )
5031 xTicksToWait = portMAX_DELAY;
5034 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
5035 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
5037 traceRETURN_vTaskPlaceOnEventListRestricted();
5040 #endif /* configUSE_TIMERS */
5041 /*-----------------------------------------------------------*/
5043 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
5045 TCB_t * pxUnblockedTCB;
5048 traceENTER_xTaskRemoveFromEventList( pxEventList );
5050 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
5051 * called from a critical section within an ISR. */
5053 /* The event list is sorted in priority order, so the first in the list can
5054 * be removed as it is known to be the highest priority. Remove the TCB from
5055 * the delayed list, and add it to the ready list.
5057 * If an event is for a queue that is locked then this function will never
5058 * get called - the lock count on the queue will get modified instead. This
5059 * means exclusive access to the event list is guaranteed here.
5061 * This function assumes that a check has already been made to ensure that
5062 * pxEventList is not empty. */
5063 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. */
5064 configASSERT( pxUnblockedTCB );
5065 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5067 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5069 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5070 prvAddTaskToReadyList( pxUnblockedTCB );
5072 #if ( configUSE_TICKLESS_IDLE != 0 )
5074 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5075 * might be set to the blocked task's time out time. If the task is
5076 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5077 * normally left unchanged, because it is automatically reset to a new
5078 * value when the tick count equals xNextTaskUnblockTime. However if
5079 * tickless idling is used it might be more important to enter sleep mode
5080 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5081 * ensure it is updated at the earliest possible time. */
5082 prvResetNextTaskUnblockTime();
5088 /* The delayed and ready lists cannot be accessed, so hold this task
5089 * pending until the scheduler is resumed. */
5090 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5093 #if ( configNUMBER_OF_CORES == 1 )
5095 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5097 /* Return true if the task removed from the event list has a higher
5098 * priority than the calling task. This allows the calling task to know if
5099 * it should force a context switch now. */
5102 /* Mark that a yield is pending in case the user is not using the
5103 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5104 xYieldPendings[ 0 ] = pdTRUE;
5111 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5115 #if ( configUSE_PREEMPTION == 1 )
5117 prvYieldForTask( pxUnblockedTCB );
5119 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5124 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5126 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5128 traceRETURN_xTaskRemoveFromEventList( xReturn );
5131 /*-----------------------------------------------------------*/
5133 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5134 const TickType_t xItemValue )
5136 TCB_t * pxUnblockedTCB;
5138 traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5140 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5141 * the event flags implementation. */
5142 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5144 /* Store the new item value in the event list. */
5145 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5147 /* Remove the event list form the event flag. Interrupts do not access
5149 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. */
5150 configASSERT( pxUnblockedTCB );
5151 listREMOVE_ITEM( pxEventListItem );
5153 #if ( configUSE_TICKLESS_IDLE != 0 )
5155 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5156 * might be set to the blocked task's time out time. If the task is
5157 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5158 * normally left unchanged, because it is automatically reset to a new
5159 * value when the tick count equals xNextTaskUnblockTime. However if
5160 * tickless idling is used it might be more important to enter sleep mode
5161 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5162 * ensure it is updated at the earliest possible time. */
5163 prvResetNextTaskUnblockTime();
5167 /* Remove the task from the delayed list and add it to the ready list. The
5168 * scheduler is suspended so interrupts will not be accessing the ready
5170 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5171 prvAddTaskToReadyList( pxUnblockedTCB );
5173 #if ( configNUMBER_OF_CORES == 1 )
5175 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5177 /* The unblocked task has a priority above that of the calling task, so
5178 * a context switch is required. This function is called with the
5179 * scheduler suspended so xYieldPending is set so the context switch
5180 * occurs immediately that the scheduler is resumed (unsuspended). */
5181 xYieldPendings[ 0 ] = pdTRUE;
5184 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5186 #if ( configUSE_PREEMPTION == 1 )
5188 taskENTER_CRITICAL();
5190 prvYieldForTask( pxUnblockedTCB );
5192 taskEXIT_CRITICAL();
5196 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5198 traceRETURN_vTaskRemoveFromUnorderedEventList();
5200 /*-----------------------------------------------------------*/
5202 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5204 traceENTER_vTaskSetTimeOutState( pxTimeOut );
5206 configASSERT( pxTimeOut );
5207 taskENTER_CRITICAL();
5209 pxTimeOut->xOverflowCount = xNumOfOverflows;
5210 pxTimeOut->xTimeOnEntering = xTickCount;
5212 taskEXIT_CRITICAL();
5214 traceRETURN_vTaskSetTimeOutState();
5216 /*-----------------------------------------------------------*/
5218 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5220 traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5222 /* For internal use only as it does not use a critical section. */
5223 pxTimeOut->xOverflowCount = xNumOfOverflows;
5224 pxTimeOut->xTimeOnEntering = xTickCount;
5226 traceRETURN_vTaskInternalSetTimeOutState();
5228 /*-----------------------------------------------------------*/
5230 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5231 TickType_t * const pxTicksToWait )
5235 traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5237 configASSERT( pxTimeOut );
5238 configASSERT( pxTicksToWait );
5240 taskENTER_CRITICAL();
5242 /* Minor optimisation. The tick count cannot change in this block. */
5243 const TickType_t xConstTickCount = xTickCount;
5244 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5246 #if ( INCLUDE_xTaskAbortDelay == 1 )
5247 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5249 /* The delay was aborted, which is not the same as a time out,
5250 * but has the same result. */
5251 pxCurrentTCB->ucDelayAborted = pdFALSE;
5257 #if ( INCLUDE_vTaskSuspend == 1 )
5258 if( *pxTicksToWait == portMAX_DELAY )
5260 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5261 * specified is the maximum block time then the task should block
5262 * indefinitely, and therefore never time out. */
5268 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
5270 /* The tick count is greater than the time at which
5271 * vTaskSetTimeout() was called, but has also overflowed since
5272 * vTaskSetTimeOut() was called. It must have wrapped all the way
5273 * around and gone past again. This passed since vTaskSetTimeout()
5276 *pxTicksToWait = ( TickType_t ) 0;
5278 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
5280 /* Not a genuine timeout. Adjust parameters for time remaining. */
5281 *pxTicksToWait -= xElapsedTime;
5282 vTaskInternalSetTimeOutState( pxTimeOut );
5287 *pxTicksToWait = ( TickType_t ) 0;
5291 taskEXIT_CRITICAL();
5293 traceRETURN_xTaskCheckForTimeOut( xReturn );
5297 /*-----------------------------------------------------------*/
5299 void vTaskMissedYield( void )
5301 traceENTER_vTaskMissedYield();
5303 /* Must be called from within a critical section. */
5304 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5306 traceRETURN_vTaskMissedYield();
5308 /*-----------------------------------------------------------*/
5310 #if ( configUSE_TRACE_FACILITY == 1 )
5312 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5314 UBaseType_t uxReturn;
5315 TCB_t const * pxTCB;
5317 traceENTER_uxTaskGetTaskNumber( xTask );
5322 uxReturn = pxTCB->uxTaskNumber;
5329 traceRETURN_uxTaskGetTaskNumber( uxReturn );
5334 #endif /* configUSE_TRACE_FACILITY */
5335 /*-----------------------------------------------------------*/
5337 #if ( configUSE_TRACE_FACILITY == 1 )
5339 void vTaskSetTaskNumber( TaskHandle_t xTask,
5340 const UBaseType_t uxHandle )
5344 traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5349 pxTCB->uxTaskNumber = uxHandle;
5352 traceRETURN_vTaskSetTaskNumber();
5355 #endif /* configUSE_TRACE_FACILITY */
5356 /*-----------------------------------------------------------*/
5359 * -----------------------------------------------------------
5360 * The MinimalIdle task.
5361 * ----------------------------------------------------------
5363 * The minimal idle task is used for all the additional cores in a SMP
5364 * system. There must be only 1 idle task and the rest are minimal idle
5367 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5368 * language extensions. The equivalent prototype for this function is:
5370 * void prvMinimalIdleTask( void *pvParameters );
5373 #if ( configNUMBER_OF_CORES > 1 )
5374 static portTASK_FUNCTION( prvMinimalIdleTask, pvParameters )
5376 ( void ) pvParameters;
5380 for( ; INFINITE_LOOP(); )
5382 #if ( configUSE_PREEMPTION == 0 )
5384 /* If we are not using preemption we keep forcing a task switch to
5385 * see if any other task has become available. If we are using
5386 * preemption we don't need to do this as any task becoming available
5387 * will automatically get the processor anyway. */
5390 #endif /* configUSE_PREEMPTION */
5392 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5394 /* When using preemption tasks of equal priority will be
5395 * timesliced. If a task that is sharing the idle priority is ready
5396 * to run then the idle task should yield before the end of the
5399 * A critical region is not required here as we are just reading from
5400 * the list, and an occasional incorrect value will not matter. If
5401 * the ready list at the idle priority contains one more task than the
5402 * number of idle tasks, which is equal to the configured numbers of cores
5403 * then a task other than the idle task is ready to execute. */
5404 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5410 mtCOVERAGE_TEST_MARKER();
5413 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5415 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
5417 /* Call the user defined function from within the idle task. This
5418 * allows the application designer to add background functionality
5419 * without the overhead of a separate task.
5421 * This hook is intended to manage core activity such as disabling cores that go idle.
5423 * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5424 * CALL A FUNCTION THAT MIGHT BLOCK. */
5425 vApplicationMinimalIdleHook();
5427 #endif /* configUSE_MINIMAL_IDLE_HOOK */
5430 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5433 * -----------------------------------------------------------
5435 * ----------------------------------------------------------
5437 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5438 * language extensions. The equivalent prototype for this function is:
5440 * void prvIdleTask( void *pvParameters );
5444 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5446 /* Stop warnings. */
5447 ( void ) pvParameters;
5449 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5450 * SCHEDULER IS STARTED. **/
5452 /* In case a task that has a secure context deletes itself, in which case
5453 * the idle task is responsible for deleting the task's secure context, if
5455 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5457 #if ( configNUMBER_OF_CORES > 1 )
5459 /* SMP all cores start up in the idle task. This initial yield gets the application
5463 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5465 for( ; INFINITE_LOOP(); )
5467 /* See if any tasks have deleted themselves - if so then the idle task
5468 * is responsible for freeing the deleted task's TCB and stack. */
5469 prvCheckTasksWaitingTermination();
5471 #if ( configUSE_PREEMPTION == 0 )
5473 /* If we are not using preemption we keep forcing a task switch to
5474 * see if any other task has become available. If we are using
5475 * preemption we don't need to do this as any task becoming available
5476 * will automatically get the processor anyway. */
5479 #endif /* configUSE_PREEMPTION */
5481 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5483 /* When using preemption tasks of equal priority will be
5484 * timesliced. If a task that is sharing the idle priority is ready
5485 * to run then the idle task should yield before the end of the
5488 * A critical region is not required here as we are just reading from
5489 * the list, and an occasional incorrect value will not matter. If
5490 * the ready list at the idle priority contains one more task than the
5491 * number of idle tasks, which is equal to the configured numbers of cores
5492 * then a task other than the idle task is ready to execute. */
5493 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5499 mtCOVERAGE_TEST_MARKER();
5502 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5504 #if ( configUSE_IDLE_HOOK == 1 )
5506 /* Call the user defined function from within the idle task. */
5507 vApplicationIdleHook();
5509 #endif /* configUSE_IDLE_HOOK */
5511 /* This conditional compilation should use inequality to 0, not equality
5512 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5513 * user defined low power mode implementations require
5514 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5515 #if ( configUSE_TICKLESS_IDLE != 0 )
5517 TickType_t xExpectedIdleTime;
5519 /* It is not desirable to suspend then resume the scheduler on
5520 * each iteration of the idle task. Therefore, a preliminary
5521 * test of the expected idle time is performed without the
5522 * scheduler suspended. The result here is not necessarily
5524 xExpectedIdleTime = prvGetExpectedIdleTime();
5526 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5530 /* Now the scheduler is suspended, the expected idle
5531 * time can be sampled again, and this time its value can
5533 configASSERT( xNextTaskUnblockTime >= xTickCount );
5534 xExpectedIdleTime = prvGetExpectedIdleTime();
5536 /* Define the following macro to set xExpectedIdleTime to 0
5537 * if the application does not want
5538 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5539 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5541 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5543 traceLOW_POWER_IDLE_BEGIN();
5544 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5545 traceLOW_POWER_IDLE_END();
5549 mtCOVERAGE_TEST_MARKER();
5552 ( void ) xTaskResumeAll();
5556 mtCOVERAGE_TEST_MARKER();
5559 #endif /* configUSE_TICKLESS_IDLE */
5561 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_MINIMAL_IDLE_HOOK == 1 ) )
5563 /* Call the user defined function from within the idle task. This
5564 * allows the application designer to add background functionality
5565 * without the overhead of a separate task.
5567 * This hook is intended to manage core activity such as disabling cores that go idle.
5569 * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5570 * CALL A FUNCTION THAT MIGHT BLOCK. */
5571 vApplicationMinimalIdleHook();
5573 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_MINIMAL_IDLE_HOOK == 1 ) ) */
5576 /*-----------------------------------------------------------*/
5578 #if ( configUSE_TICKLESS_IDLE != 0 )
5580 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5582 #if ( INCLUDE_vTaskSuspend == 1 )
5583 /* The idle task exists in addition to the application tasks. */
5584 const UBaseType_t uxNonApplicationTasks = 1;
5585 #endif /* INCLUDE_vTaskSuspend */
5587 traceENTER_eTaskConfirmSleepModeStatus();
5589 eSleepModeStatus eReturn = eStandardSleep;
5591 /* This function must be called from a critical section. */
5593 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
5595 /* A task was made ready while the scheduler was suspended. */
5596 eReturn = eAbortSleep;
5598 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5600 /* A yield was pended while the scheduler was suspended. */
5601 eReturn = eAbortSleep;
5603 else if( xPendedTicks != 0 )
5605 /* A tick interrupt has already occurred but was held pending
5606 * because the scheduler is suspended. */
5607 eReturn = eAbortSleep;
5610 #if ( INCLUDE_vTaskSuspend == 1 )
5611 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5613 /* If all the tasks are in the suspended list (which might mean they
5614 * have an infinite block time rather than actually being suspended)
5615 * then it is safe to turn all clocks off and just wait for external
5617 eReturn = eNoTasksWaitingTimeout;
5619 #endif /* INCLUDE_vTaskSuspend */
5622 mtCOVERAGE_TEST_MARKER();
5625 traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5630 #endif /* configUSE_TICKLESS_IDLE */
5631 /*-----------------------------------------------------------*/
5633 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5635 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5641 traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
5643 if( ( xIndex >= 0 ) &&
5644 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5646 pxTCB = prvGetTCBFromHandle( xTaskToSet );
5647 configASSERT( pxTCB != NULL );
5648 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5651 traceRETURN_vTaskSetThreadLocalStoragePointer();
5654 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5655 /*-----------------------------------------------------------*/
5657 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5659 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5662 void * pvReturn = NULL;
5665 traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
5667 if( ( xIndex >= 0 ) &&
5668 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5670 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
5671 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
5678 traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
5683 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5684 /*-----------------------------------------------------------*/
5686 #if ( portUSING_MPU_WRAPPERS == 1 )
5688 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
5689 const MemoryRegion_t * const pxRegions )
5693 traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
5695 /* If null is passed in here then we are modifying the MPU settings of
5696 * the calling task. */
5697 pxTCB = prvGetTCBFromHandle( xTaskToModify );
5699 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
5701 traceRETURN_vTaskAllocateMPURegions();
5704 #endif /* portUSING_MPU_WRAPPERS */
5705 /*-----------------------------------------------------------*/
5707 static void prvInitialiseTaskLists( void )
5709 UBaseType_t uxPriority;
5711 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
5713 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
5716 vListInitialise( &xDelayedTaskList1 );
5717 vListInitialise( &xDelayedTaskList2 );
5718 vListInitialise( &xPendingReadyList );
5720 #if ( INCLUDE_vTaskDelete == 1 )
5722 vListInitialise( &xTasksWaitingTermination );
5724 #endif /* INCLUDE_vTaskDelete */
5726 #if ( INCLUDE_vTaskSuspend == 1 )
5728 vListInitialise( &xSuspendedTaskList );
5730 #endif /* INCLUDE_vTaskSuspend */
5732 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
5734 pxDelayedTaskList = &xDelayedTaskList1;
5735 pxOverflowDelayedTaskList = &xDelayedTaskList2;
5737 /*-----------------------------------------------------------*/
5739 static void prvCheckTasksWaitingTermination( void )
5741 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
5743 #if ( INCLUDE_vTaskDelete == 1 )
5747 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
5748 * being called too often in the idle task. */
5749 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
5751 #if ( configNUMBER_OF_CORES == 1 )
5753 taskENTER_CRITICAL();
5756 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. */
5757 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5758 --uxCurrentNumberOfTasks;
5759 --uxDeletedTasksWaitingCleanUp;
5762 taskEXIT_CRITICAL();
5764 prvDeleteTCB( pxTCB );
5766 #else /* #if( configNUMBER_OF_CORES == 1 ) */
5770 taskENTER_CRITICAL();
5772 /* For SMP, multiple idles can be running simultaneously
5773 * and we need to check that other idles did not cleanup while we were
5774 * waiting to enter the critical section. */
5775 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
5777 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. */
5779 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
5781 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5782 --uxCurrentNumberOfTasks;
5783 --uxDeletedTasksWaitingCleanUp;
5787 /* The TCB to be deleted still has not yet been switched out
5788 * by the scheduler, so we will just exit this loop early and
5789 * try again next time. */
5790 taskEXIT_CRITICAL();
5795 taskEXIT_CRITICAL();
5799 prvDeleteTCB( pxTCB );
5802 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
5805 #endif /* INCLUDE_vTaskDelete */
5807 /*-----------------------------------------------------------*/
5809 #if ( configUSE_TRACE_FACILITY == 1 )
5811 void vTaskGetInfo( TaskHandle_t xTask,
5812 TaskStatus_t * pxTaskStatus,
5813 BaseType_t xGetFreeStackSpace,
5818 traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
5820 /* xTask is NULL then get the state of the calling task. */
5821 pxTCB = prvGetTCBFromHandle( xTask );
5823 pxTaskStatus->xHandle = pxTCB;
5824 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
5825 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
5826 pxTaskStatus->pxStackBase = pxTCB->pxStack;
5827 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
5828 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
5829 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
5831 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
5833 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
5835 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
5839 #if ( configUSE_MUTEXES == 1 )
5841 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
5845 pxTaskStatus->uxBasePriority = 0;
5849 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5851 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
5855 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
5859 /* Obtaining the task state is a little fiddly, so is only done if the
5860 * value of eState passed into this function is eInvalid - otherwise the
5861 * state is just set to whatever is passed in. */
5862 if( eState != eInvalid )
5864 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
5866 pxTaskStatus->eCurrentState = eRunning;
5870 pxTaskStatus->eCurrentState = eState;
5872 #if ( INCLUDE_vTaskSuspend == 1 )
5874 /* If the task is in the suspended list then there is a
5875 * chance it is actually just blocked indefinitely - so really
5876 * it should be reported as being in the Blocked state. */
5877 if( eState == eSuspended )
5881 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
5883 pxTaskStatus->eCurrentState = eBlocked;
5886 ( void ) xTaskResumeAll();
5889 #endif /* INCLUDE_vTaskSuspend */
5891 /* Tasks can be in pending ready list and other state list at the
5892 * same time. These tasks are in ready state no matter what state
5893 * list the task is in. */
5894 taskENTER_CRITICAL();
5896 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
5898 pxTaskStatus->eCurrentState = eReady;
5901 taskEXIT_CRITICAL();
5906 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
5909 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
5910 * parameter is provided to allow it to be skipped. */
5911 if( xGetFreeStackSpace != pdFALSE )
5913 #if ( portSTACK_GROWTH > 0 )
5915 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
5919 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
5925 pxTaskStatus->usStackHighWaterMark = 0;
5928 traceRETURN_vTaskGetInfo();
5931 #endif /* configUSE_TRACE_FACILITY */
5932 /*-----------------------------------------------------------*/
5934 #if ( configUSE_TRACE_FACILITY == 1 )
5936 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
5940 configLIST_VOLATILE TCB_t * pxNextTCB;
5941 configLIST_VOLATILE TCB_t * pxFirstTCB;
5942 UBaseType_t uxTask = 0;
5944 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
5946 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. */
5948 /* Populate an TaskStatus_t structure within the
5949 * pxTaskStatusArray array for each task that is referenced from
5950 * pxList. See the definition of TaskStatus_t in task.h for the
5951 * meaning of each TaskStatus_t structure member. */
5954 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. */
5955 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
5957 } while( pxNextTCB != pxFirstTCB );
5961 mtCOVERAGE_TEST_MARKER();
5967 #endif /* configUSE_TRACE_FACILITY */
5968 /*-----------------------------------------------------------*/
5970 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
5972 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
5974 uint32_t ulCount = 0U;
5976 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
5978 pucStackByte -= portSTACK_GROWTH;
5982 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
5984 return ( configSTACK_DEPTH_TYPE ) ulCount;
5987 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
5988 /*-----------------------------------------------------------*/
5990 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
5992 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
5993 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
5994 * user to determine the return type. It gets around the problem of the value
5995 * overflowing on 8-bit types without breaking backward compatibility for
5996 * applications that expect an 8-bit return type. */
5997 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
6000 uint8_t * pucEndOfStack;
6001 configSTACK_DEPTH_TYPE uxReturn;
6003 traceENTER_uxTaskGetStackHighWaterMark2( xTask );
6005 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
6006 * the same except for their return type. Using configSTACK_DEPTH_TYPE
6007 * allows the user to determine the return type. It gets around the
6008 * problem of the value overflowing on 8-bit types without breaking
6009 * backward compatibility for applications that expect an 8-bit return
6012 pxTCB = prvGetTCBFromHandle( xTask );
6014 #if portSTACK_GROWTH < 0
6016 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6020 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6024 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
6026 traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
6031 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
6032 /*-----------------------------------------------------------*/
6034 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
6036 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
6039 uint8_t * pucEndOfStack;
6040 UBaseType_t uxReturn;
6042 traceENTER_uxTaskGetStackHighWaterMark( xTask );
6044 pxTCB = prvGetTCBFromHandle( xTask );
6046 #if portSTACK_GROWTH < 0
6048 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6052 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6056 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
6058 traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6063 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6064 /*-----------------------------------------------------------*/
6066 #if ( INCLUDE_vTaskDelete == 1 )
6068 static void prvDeleteTCB( TCB_t * pxTCB )
6070 /* This call is required specifically for the TriCore port. It must be
6071 * above the vPortFree() calls. The call is also used by ports/demos that
6072 * want to allocate and clean RAM statically. */
6073 portCLEAN_UP_TCB( pxTCB );
6075 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6077 /* Free up the memory allocated for the task's TLS Block. */
6078 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6082 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6084 /* The task can only have been allocated dynamically - free both
6085 * the stack and TCB. */
6086 vPortFreeStack( pxTCB->pxStack );
6089 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
6091 /* The task could have been allocated statically or dynamically, so
6092 * check what was statically allocated before trying to free the
6094 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6096 /* Both the stack and TCB were allocated dynamically, so both
6098 vPortFreeStack( pxTCB->pxStack );
6101 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6103 /* Only the stack was statically allocated, so the TCB is the
6104 * only memory that must be freed. */
6109 /* Neither the stack nor the TCB were allocated dynamically, so
6110 * nothing needs to be freed. */
6111 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6112 mtCOVERAGE_TEST_MARKER();
6115 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6118 #endif /* INCLUDE_vTaskDelete */
6119 /*-----------------------------------------------------------*/
6121 static void prvResetNextTaskUnblockTime( void )
6123 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6125 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
6126 * the maximum possible value so it is extremely unlikely that the
6127 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6128 * there is an item in the delayed list. */
6129 xNextTaskUnblockTime = portMAX_DELAY;
6133 /* The new current delayed list is not empty, get the value of
6134 * the item at the head of the delayed list. This is the time at
6135 * which the task at the head of the delayed list should be removed
6136 * from the Blocked state. */
6137 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6140 /*-----------------------------------------------------------*/
6142 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6144 #if ( configNUMBER_OF_CORES == 1 )
6145 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6147 TaskHandle_t xReturn;
6149 traceENTER_xTaskGetCurrentTaskHandle();
6151 /* A critical section is not required as this is not called from
6152 * an interrupt and the current TCB will always be the same for any
6153 * individual execution thread. */
6154 xReturn = pxCurrentTCB;
6156 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6160 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6161 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6163 TaskHandle_t xReturn;
6164 UBaseType_t uxSavedInterruptStatus;
6166 traceENTER_xTaskGetCurrentTaskHandle();
6168 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6170 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6172 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6174 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6179 TaskHandle_t xTaskGetCurrentTaskHandleCPU( BaseType_t xCoreID )
6181 TaskHandle_t xReturn = NULL;
6183 traceENTER_xTaskGetCurrentTaskHandleCPU( xCoreID );
6185 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6187 xReturn = pxCurrentTCBs[ xCoreID ];
6190 traceRETURN_xTaskGetCurrentTaskHandleCPU( xReturn );
6194 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6196 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
6197 /*-----------------------------------------------------------*/
6199 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6201 BaseType_t xTaskGetSchedulerState( void )
6205 traceENTER_xTaskGetSchedulerState();
6207 if( xSchedulerRunning == pdFALSE )
6209 xReturn = taskSCHEDULER_NOT_STARTED;
6213 #if ( configNUMBER_OF_CORES > 1 )
6214 taskENTER_CRITICAL();
6217 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6219 xReturn = taskSCHEDULER_RUNNING;
6223 xReturn = taskSCHEDULER_SUSPENDED;
6226 #if ( configNUMBER_OF_CORES > 1 )
6227 taskEXIT_CRITICAL();
6231 traceRETURN_xTaskGetSchedulerState( xReturn );
6236 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6237 /*-----------------------------------------------------------*/
6239 #if ( configUSE_MUTEXES == 1 )
6241 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6243 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6244 BaseType_t xReturn = pdFALSE;
6246 traceENTER_xTaskPriorityInherit( pxMutexHolder );
6248 /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6249 * inheritance is not applied in this scenario. */
6250 if( pxMutexHolder != NULL )
6252 /* If the holder of the mutex has a priority below the priority of
6253 * the task attempting to obtain the mutex then it will temporarily
6254 * inherit the priority of the task attempting to obtain the mutex. */
6255 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6257 /* Adjust the mutex holder state to account for its new
6258 * priority. Only reset the event list item value if the value is
6259 * not being used for anything else. */
6260 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
6262 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. */
6266 mtCOVERAGE_TEST_MARKER();
6269 /* If the task being modified is in the ready state it will need
6270 * to be moved into a new list. */
6271 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6273 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6275 /* It is known that the task is in its ready list so
6276 * there is no need to check again and the port level
6277 * reset macro can be called directly. */
6278 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6282 mtCOVERAGE_TEST_MARKER();
6285 /* Inherit the priority before being moved into the new list. */
6286 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6287 prvAddTaskToReadyList( pxMutexHolderTCB );
6288 #if ( configNUMBER_OF_CORES > 1 )
6290 /* The priority of the task is raised. Yield for this task
6291 * if it is not running. */
6292 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6294 prvYieldForTask( pxMutexHolderTCB );
6297 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6301 /* Just inherit the priority. */
6302 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6305 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6307 /* Inheritance occurred. */
6312 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6314 /* The base priority of the mutex holder is lower than the
6315 * priority of the task attempting to take the mutex, but the
6316 * current priority of the mutex holder is not lower than the
6317 * priority of the task attempting to take the mutex.
6318 * Therefore the mutex holder must have already inherited a
6319 * priority, but inheritance would have occurred if that had
6320 * not been the case. */
6325 mtCOVERAGE_TEST_MARKER();
6331 mtCOVERAGE_TEST_MARKER();
6334 traceRETURN_xTaskPriorityInherit( xReturn );
6339 #endif /* configUSE_MUTEXES */
6340 /*-----------------------------------------------------------*/
6342 #if ( configUSE_MUTEXES == 1 )
6344 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6346 TCB_t * const pxTCB = pxMutexHolder;
6347 BaseType_t xReturn = pdFALSE;
6349 traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6351 if( pxMutexHolder != NULL )
6353 /* A task can only have an inherited priority if it holds the mutex.
6354 * If the mutex is held by a task then it cannot be given from an
6355 * interrupt, and if a mutex is given by the holding task then it must
6356 * be the running state task. */
6357 configASSERT( pxTCB == pxCurrentTCB );
6358 configASSERT( pxTCB->uxMutexesHeld );
6359 ( pxTCB->uxMutexesHeld )--;
6361 /* Has the holder of the mutex inherited the priority of another
6363 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6365 /* Only disinherit if no other mutexes are held. */
6366 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6368 /* A task can only have an inherited priority if it holds
6369 * the mutex. If the mutex is held by a task then it cannot be
6370 * given from an interrupt, and if a mutex is given by the
6371 * holding task then it must be the running state task. Remove
6372 * the holding task from the ready list. */
6373 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6375 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6379 mtCOVERAGE_TEST_MARKER();
6382 /* Disinherit the priority before adding the task into the
6383 * new ready list. */
6384 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6385 pxTCB->uxPriority = pxTCB->uxBasePriority;
6387 /* Reset the event list item value. It cannot be in use for
6388 * any other purpose if this task is running, and it must be
6389 * running to give back the mutex. */
6390 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. */
6391 prvAddTaskToReadyList( pxTCB );
6392 #if ( configNUMBER_OF_CORES > 1 )
6394 /* The priority of the task is dropped. Yield the core on
6395 * which the task is running. */
6396 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6398 prvYieldCore( pxTCB->xTaskRunState );
6401 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6403 /* Return true to indicate that a context switch is required.
6404 * This is only actually required in the corner case whereby
6405 * multiple mutexes were held and the mutexes were given back
6406 * in an order different to that in which they were taken.
6407 * If a context switch did not occur when the first mutex was
6408 * returned, even if a task was waiting on it, then a context
6409 * switch should occur when the last mutex is returned whether
6410 * a task is waiting on it or not. */
6415 mtCOVERAGE_TEST_MARKER();
6420 mtCOVERAGE_TEST_MARKER();
6425 mtCOVERAGE_TEST_MARKER();
6428 traceRETURN_xTaskPriorityDisinherit( xReturn );
6433 #endif /* configUSE_MUTEXES */
6434 /*-----------------------------------------------------------*/
6436 #if ( configUSE_MUTEXES == 1 )
6438 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6439 UBaseType_t uxHighestPriorityWaitingTask )
6441 TCB_t * const pxTCB = pxMutexHolder;
6442 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6443 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6445 traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6447 if( pxMutexHolder != NULL )
6449 /* If pxMutexHolder is not NULL then the holder must hold at least
6451 configASSERT( pxTCB->uxMutexesHeld );
6453 /* Determine the priority to which the priority of the task that
6454 * holds the mutex should be set. This will be the greater of the
6455 * holding task's base priority and the priority of the highest
6456 * priority task that is waiting to obtain the mutex. */
6457 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6459 uxPriorityToUse = uxHighestPriorityWaitingTask;
6463 uxPriorityToUse = pxTCB->uxBasePriority;
6466 /* Does the priority need to change? */
6467 if( pxTCB->uxPriority != uxPriorityToUse )
6469 /* Only disinherit if no other mutexes are held. This is a
6470 * simplification in the priority inheritance implementation. If
6471 * the task that holds the mutex is also holding other mutexes then
6472 * the other mutexes may have caused the priority inheritance. */
6473 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6475 /* If a task has timed out because it already holds the
6476 * mutex it was trying to obtain then it cannot of inherited
6477 * its own priority. */
6478 configASSERT( pxTCB != pxCurrentTCB );
6480 /* Disinherit the priority, remembering the previous
6481 * priority to facilitate determining the subject task's
6483 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6484 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6485 pxTCB->uxPriority = uxPriorityToUse;
6487 /* Only reset the event list item value if the value is not
6488 * being used for anything else. */
6489 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
6491 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. */
6495 mtCOVERAGE_TEST_MARKER();
6498 /* If the running task is not the task that holds the mutex
6499 * then the task that holds the mutex could be in either the
6500 * Ready, Blocked or Suspended states. Only remove the task
6501 * from its current state list if it is in the Ready state as
6502 * the task's priority is going to change and there is one
6503 * Ready list per priority. */
6504 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6506 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6508 /* It is known that the task is in its ready list so
6509 * there is no need to check again and the port level
6510 * reset macro can be called directly. */
6511 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6515 mtCOVERAGE_TEST_MARKER();
6518 prvAddTaskToReadyList( pxTCB );
6519 #if ( configNUMBER_OF_CORES > 1 )
6521 /* The priority of the task is dropped. Yield the core on
6522 * which the task is running. */
6523 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6525 prvYieldCore( pxTCB->xTaskRunState );
6528 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6532 mtCOVERAGE_TEST_MARKER();
6537 mtCOVERAGE_TEST_MARKER();
6542 mtCOVERAGE_TEST_MARKER();
6547 mtCOVERAGE_TEST_MARKER();
6550 traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6553 #endif /* configUSE_MUTEXES */
6554 /*-----------------------------------------------------------*/
6556 #if ( configNUMBER_OF_CORES > 1 )
6558 /* If not in a critical section then yield immediately.
6559 * Otherwise set xYieldPendings to true to wait to
6560 * yield until exiting the critical section.
6562 void vTaskYieldWithinAPI( void )
6564 traceENTER_vTaskYieldWithinAPI();
6566 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6572 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6575 traceRETURN_vTaskYieldWithinAPI();
6577 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6579 /*-----------------------------------------------------------*/
6581 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6583 void vTaskEnterCritical( void )
6585 traceENTER_vTaskEnterCritical();
6587 portDISABLE_INTERRUPTS();
6589 if( xSchedulerRunning != pdFALSE )
6591 ( pxCurrentTCB->uxCriticalNesting )++;
6593 /* This is not the interrupt safe version of the enter critical
6594 * function so assert() if it is being called from an interrupt
6595 * context. Only API functions that end in "FromISR" can be used in an
6596 * interrupt. Only assert if the critical nesting count is 1 to
6597 * protect against recursive calls if the assert function also uses a
6598 * critical section. */
6599 if( pxCurrentTCB->uxCriticalNesting == 1 )
6601 portASSERT_IF_IN_ISR();
6606 mtCOVERAGE_TEST_MARKER();
6609 traceRETURN_vTaskEnterCritical();
6612 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6613 /*-----------------------------------------------------------*/
6615 #if ( configNUMBER_OF_CORES > 1 )
6617 void vTaskEnterCritical( void )
6619 traceENTER_vTaskEnterCritical();
6621 portDISABLE_INTERRUPTS();
6623 if( xSchedulerRunning != pdFALSE )
6625 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6627 portGET_TASK_LOCK();
6631 portINCREMENT_CRITICAL_NESTING_COUNT();
6633 /* This is not the interrupt safe version of the enter critical
6634 * function so assert() if it is being called from an interrupt
6635 * context. Only API functions that end in "FromISR" can be used in an
6636 * interrupt. Only assert if the critical nesting count is 1 to
6637 * protect against recursive calls if the assert function also uses a
6638 * critical section. */
6639 if( portGET_CRITICAL_NESTING_COUNT() == 1U )
6641 portASSERT_IF_IN_ISR();
6643 if( uxSchedulerSuspended == 0U )
6645 /* The only time there would be a problem is if this is called
6646 * before a context switch and vTaskExitCritical() is called
6647 * after pxCurrentTCB changes. Therefore this should not be
6648 * used within vTaskSwitchContext(). */
6649 prvCheckForRunStateChange();
6655 mtCOVERAGE_TEST_MARKER();
6658 traceRETURN_vTaskEnterCritical();
6661 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6663 /*-----------------------------------------------------------*/
6665 #if ( configNUMBER_OF_CORES > 1 )
6667 UBaseType_t vTaskEnterCriticalFromISR( void )
6669 UBaseType_t uxSavedInterruptStatus = 0;
6671 traceENTER_vTaskEnterCriticalFromISR();
6673 if( xSchedulerRunning != pdFALSE )
6675 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6677 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6682 portINCREMENT_CRITICAL_NESTING_COUNT();
6686 mtCOVERAGE_TEST_MARKER();
6689 traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
6691 return uxSavedInterruptStatus;
6694 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6695 /*-----------------------------------------------------------*/
6697 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6699 void vTaskExitCritical( void )
6701 traceENTER_vTaskExitCritical();
6703 if( xSchedulerRunning != pdFALSE )
6705 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
6706 * does not match a previous call to vTaskEnterCritical(). */
6707 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
6709 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
6710 * to exit critical section from ISR. */
6711 portASSERT_IF_IN_ISR();
6713 if( pxCurrentTCB->uxCriticalNesting > 0U )
6715 ( pxCurrentTCB->uxCriticalNesting )--;
6717 if( pxCurrentTCB->uxCriticalNesting == 0U )
6719 portENABLE_INTERRUPTS();
6723 mtCOVERAGE_TEST_MARKER();
6728 mtCOVERAGE_TEST_MARKER();
6733 mtCOVERAGE_TEST_MARKER();
6736 traceRETURN_vTaskExitCritical();
6739 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6740 /*-----------------------------------------------------------*/
6742 #if ( configNUMBER_OF_CORES > 1 )
6744 void vTaskExitCritical( void )
6746 traceENTER_vTaskExitCritical();
6748 if( xSchedulerRunning != pdFALSE )
6750 /* If critical nesting count is zero then this function
6751 * does not match a previous call to vTaskEnterCritical(). */
6752 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
6754 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
6755 * to exit critical section from ISR. */
6756 portASSERT_IF_IN_ISR();
6758 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
6760 portDECREMENT_CRITICAL_NESTING_COUNT();
6762 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6764 BaseType_t xYieldCurrentTask;
6766 /* Get the xYieldPending stats inside the critical section. */
6767 xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
6769 portRELEASE_ISR_LOCK();
6770 portRELEASE_TASK_LOCK();
6771 portENABLE_INTERRUPTS();
6773 /* When a task yields in a critical section it just sets
6774 * xYieldPending to true. So now that we have exited the
6775 * critical section check if xYieldPending is true, and
6777 if( xYieldCurrentTask != pdFALSE )
6784 mtCOVERAGE_TEST_MARKER();
6789 mtCOVERAGE_TEST_MARKER();
6794 mtCOVERAGE_TEST_MARKER();
6797 traceRETURN_vTaskExitCritical();
6800 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6801 /*-----------------------------------------------------------*/
6803 #if ( configNUMBER_OF_CORES > 1 )
6805 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
6807 traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
6809 if( xSchedulerRunning != pdFALSE )
6811 /* If critical nesting count is zero then this function
6812 * does not match a previous call to vTaskEnterCritical(). */
6813 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
6815 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
6817 portDECREMENT_CRITICAL_NESTING_COUNT();
6819 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6821 portRELEASE_ISR_LOCK();
6822 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6826 mtCOVERAGE_TEST_MARKER();
6831 mtCOVERAGE_TEST_MARKER();
6836 mtCOVERAGE_TEST_MARKER();
6839 traceRETURN_vTaskExitCriticalFromISR();
6842 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6843 /*-----------------------------------------------------------*/
6845 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
6847 static char * prvWriteNameToBuffer( char * pcBuffer,
6848 const char * pcTaskName )
6852 /* Start by copying the entire string. */
6853 ( void ) strcpy( pcBuffer, pcTaskName );
6855 /* Pad the end of the string with spaces to ensure columns line up when
6857 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
6859 pcBuffer[ x ] = ' ';
6863 pcBuffer[ x ] = ( char ) 0x00;
6865 /* Return the new end of string. */
6866 return &( pcBuffer[ x ] );
6869 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
6870 /*-----------------------------------------------------------*/
6872 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
6874 void vTaskListTasks( char * pcWriteBuffer,
6875 size_t uxBufferLength )
6877 TaskStatus_t * pxTaskStatusArray;
6878 size_t uxConsumedBufferLength = 0;
6879 size_t uxCharsWrittenBySnprintf;
6880 int iSnprintfReturnValue;
6881 BaseType_t xOutputBufferFull = pdFALSE;
6882 UBaseType_t uxArraySize, x;
6885 traceENTER_vTaskListTasks( pcWriteBuffer, uxBufferLength );
6890 * This function is provided for convenience only, and is used by many
6891 * of the demo applications. Do not consider it to be part of the
6894 * vTaskListTasks() calls uxTaskGetSystemState(), then formats part of the
6895 * uxTaskGetSystemState() output into a human readable table that
6896 * displays task: names, states, priority, stack usage and task number.
6897 * Stack usage specified as the number of unused StackType_t words stack can hold
6898 * on top of stack - not the number of bytes.
6900 * vTaskListTasks() has a dependency on the snprintf() C library function that
6901 * might bloat the code size, use a lot of stack, and provide different
6902 * results on different platforms. An alternative, tiny, third party,
6903 * and limited functionality implementation of snprintf() is provided in
6904 * many of the FreeRTOS/Demo sub-directories in a file called
6905 * printf-stdarg.c (note printf-stdarg.c does not provide a full
6906 * snprintf() implementation!).
6908 * It is recommended that production systems call uxTaskGetSystemState()
6909 * directly to get access to raw stats data, rather than indirectly
6910 * through a call to vTaskListTasks().
6914 /* Make sure the write buffer does not contain a string. */
6915 *pcWriteBuffer = ( char ) 0x00;
6917 /* Take a snapshot of the number of tasks in case it changes while this
6918 * function is executing. */
6919 uxArraySize = uxCurrentNumberOfTasks;
6921 /* Allocate an array index for each task. NOTE! if
6922 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
6923 * equate to NULL. */
6924 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. */
6926 if( pxTaskStatusArray != NULL )
6928 /* Generate the (binary) data. */
6929 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
6931 /* Create a human readable table from the binary data. */
6932 for( x = 0; ( x < uxArraySize ) && ( xOutputBufferFull == pdFALSE ); x++ )
6934 switch( pxTaskStatusArray[ x ].eCurrentState )
6937 cStatus = tskRUNNING_CHAR;
6941 cStatus = tskREADY_CHAR;
6945 cStatus = tskBLOCKED_CHAR;
6949 cStatus = tskSUSPENDED_CHAR;
6953 cStatus = tskDELETED_CHAR;
6956 case eInvalid: /* Fall through. */
6957 default: /* Should not get here, but it is included
6958 * to prevent static checking errors. */
6959 cStatus = ( char ) 0x00;
6963 /* Is there enough space in the buffer to hold task name? */
6964 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
6966 /* Write the task name to the string, padding with spaces so it
6967 * can be printed in tabular form more easily. */
6968 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
6969 /* Do not count the terminating null character. */
6970 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1 );
6972 /* Is there space left in the buffer? -1 is done because snprintf
6973 * writes a terminating null character. So we are essentially
6974 * checking if the buffer has space to write at least one non-null
6976 if( uxConsumedBufferLength < ( uxBufferLength - 1 ) )
6978 /* Write the rest of the string. */
6979 iSnprintfReturnValue = snprintf( pcWriteBuffer,
6980 uxBufferLength - uxConsumedBufferLength,
6981 "\t%c\t%u\t%u\t%u\r\n",
6983 ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
6984 ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
6985 ( 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. */
6986 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
6988 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
6989 pcWriteBuffer += uxCharsWrittenBySnprintf; /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
6993 xOutputBufferFull = pdTRUE;
6998 xOutputBufferFull = pdTRUE;
7002 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7003 * is 0 then vPortFree() will be #defined to nothing. */
7004 vPortFree( pxTaskStatusArray );
7008 mtCOVERAGE_TEST_MARKER();
7011 traceRETURN_vTaskListTasks();
7014 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7015 /*----------------------------------------------------------*/
7017 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
7019 void vTaskGetRunTimeStatistics( char * pcWriteBuffer,
7020 size_t uxBufferLength )
7022 TaskStatus_t * pxTaskStatusArray;
7023 size_t uxConsumedBufferLength = 0;
7024 size_t uxCharsWrittenBySnprintf;
7025 int iSnprintfReturnValue;
7026 BaseType_t xOutputBufferFull = pdFALSE;
7027 UBaseType_t uxArraySize, x;
7028 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
7030 traceENTER_vTaskGetRunTimeStatistics( pcWriteBuffer, uxBufferLength );
7035 * This function is provided for convenience only, and is used by many
7036 * of the demo applications. Do not consider it to be part of the
7039 * vTaskGetRunTimeStatistics() calls uxTaskGetSystemState(), then formats part
7040 * of the uxTaskGetSystemState() output into a human readable table that
7041 * displays the amount of time each task has spent in the Running state
7042 * in both absolute and percentage terms.
7044 * vTaskGetRunTimeStatistics() has a dependency on the snprintf() C library
7045 * function that might bloat the code size, use a lot of stack, and
7046 * provide different results on different platforms. An alternative,
7047 * tiny, third party, and limited functionality implementation of
7048 * snprintf() is provided in many of the FreeRTOS/Demo sub-directories in
7049 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
7050 * a full snprintf() implementation!).
7052 * It is recommended that production systems call uxTaskGetSystemState()
7053 * directly to get access to raw stats data, rather than indirectly
7054 * through a call to vTaskGetRunTimeStatistics().
7057 /* Make sure the write buffer does not contain a string. */
7058 *pcWriteBuffer = ( char ) 0x00;
7060 /* Take a snapshot of the number of tasks in case it changes while this
7061 * function is executing. */
7062 uxArraySize = uxCurrentNumberOfTasks;
7064 /* Allocate an array index for each task. NOTE! If
7065 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7066 * equate to NULL. */
7067 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. */
7069 if( pxTaskStatusArray != NULL )
7071 /* Generate the (binary) data. */
7072 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
7074 /* For percentage calculations. */
7075 ulTotalTime /= 100UL;
7077 /* Avoid divide by zero errors. */
7078 if( ulTotalTime > 0UL )
7080 /* Create a human readable table from the binary data. */
7081 for( x = 0; ( x < uxArraySize ) && ( xOutputBufferFull == pdFALSE ); x++ )
7083 /* What percentage of the total run time has the task used?
7084 * This will always be rounded down to the nearest integer.
7085 * ulTotalRunTime has already been divided by 100. */
7086 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
7088 /* Is there enough space in the buffer to hold task name? */
7089 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7091 /* Write the task name to the string, padding with
7092 * spaces so it can be printed in tabular form more
7094 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7095 /* Do not count the terminating null character. */
7096 uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1 );
7098 /* Is there space left in the buffer? -1 is done because snprintf
7099 * writes a terminating null character. So we are essentially
7100 * checking if the buffer has space to write at least one non-null
7102 if( uxConsumedBufferLength < ( uxBufferLength - 1 ) )
7104 if( ulStatsAsPercentage > 0UL )
7106 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7108 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7109 uxBufferLength - uxConsumedBufferLength,
7110 "\t%lu\t\t%lu%%\r\n",
7111 pxTaskStatusArray[ x ].ulRunTimeCounter,
7112 ulStatsAsPercentage );
7116 /* sizeof( int ) == sizeof( long ) so a smaller
7117 * printf() library can be used. */
7118 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7119 uxBufferLength - uxConsumedBufferLength,
7121 ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter,
7122 ( 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. */
7124 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7128 /* If the percentage is zero here then the task has
7129 * consumed less than 1% of the total run time. */
7130 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7132 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7133 uxBufferLength - uxConsumedBufferLength,
7134 "\t%lu\t\t<1%%\r\n",
7135 pxTaskStatusArray[ x ].ulRunTimeCounter );
7139 /* sizeof( int ) == sizeof( long ) so a smaller
7140 * printf() library can be used. */
7141 iSnprintfReturnValue = snprintf( pcWriteBuffer,
7142 uxBufferLength - uxConsumedBufferLength,
7144 ( 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. */
7146 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7149 uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7150 uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7151 pcWriteBuffer += uxCharsWrittenBySnprintf; /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
7155 xOutputBufferFull = pdTRUE;
7160 xOutputBufferFull = pdTRUE;
7166 mtCOVERAGE_TEST_MARKER();
7169 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7170 * is 0 then vPortFree() will be #defined to nothing. */
7171 vPortFree( pxTaskStatusArray );
7175 mtCOVERAGE_TEST_MARKER();
7178 traceRETURN_vTaskGetRunTimeStatistics();
7181 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7182 /*-----------------------------------------------------------*/
7184 TickType_t uxTaskResetEventItemValue( void )
7186 TickType_t uxReturn;
7188 traceENTER_uxTaskResetEventItemValue();
7190 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7192 /* Reset the event list item to its normal value - so it can be used with
7193 * queues and semaphores. */
7194 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. */
7196 traceRETURN_uxTaskResetEventItemValue( uxReturn );
7200 /*-----------------------------------------------------------*/
7202 #if ( configUSE_MUTEXES == 1 )
7204 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7208 traceENTER_pvTaskIncrementMutexHeldCount();
7210 pxTCB = pxCurrentTCB;
7212 /* If xSemaphoreCreateMutex() is called before any tasks have been created
7213 * then pxCurrentTCB will be NULL. */
7216 ( pxTCB->uxMutexesHeld )++;
7219 traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7224 #endif /* configUSE_MUTEXES */
7225 /*-----------------------------------------------------------*/
7227 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7229 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7230 BaseType_t xClearCountOnExit,
7231 TickType_t xTicksToWait )
7235 traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7237 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7239 taskENTER_CRITICAL();
7241 /* Only block if the notification count is not already non-zero. */
7242 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0UL )
7244 /* Mark this task as waiting for a notification. */
7245 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7247 if( xTicksToWait > ( TickType_t ) 0 )
7249 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7250 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7252 /* All ports are written to allow a yield in a critical
7253 * section (some will yield immediately, others wait until the
7254 * critical section exits) - but it is not something that
7255 * application code should ever do. */
7256 #if ( configNUMBER_OF_CORES == 1 )
7258 portYIELD_WITHIN_API();
7262 vTaskYieldWithinAPI();
7268 mtCOVERAGE_TEST_MARKER();
7273 mtCOVERAGE_TEST_MARKER();
7276 taskEXIT_CRITICAL();
7278 taskENTER_CRITICAL();
7280 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7281 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7283 if( ulReturn != 0UL )
7285 if( xClearCountOnExit != pdFALSE )
7287 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = 0UL;
7291 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7296 mtCOVERAGE_TEST_MARKER();
7299 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7301 taskEXIT_CRITICAL();
7303 traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7308 #endif /* configUSE_TASK_NOTIFICATIONS */
7309 /*-----------------------------------------------------------*/
7311 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7313 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7314 uint32_t ulBitsToClearOnEntry,
7315 uint32_t ulBitsToClearOnExit,
7316 uint32_t * pulNotificationValue,
7317 TickType_t xTicksToWait )
7321 traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7323 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7325 taskENTER_CRITICAL();
7327 /* Only block if a notification is not already pending. */
7328 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7330 /* Clear bits in the task's notification value as bits may get
7331 * set by the notifying task or interrupt. This can be used to
7332 * clear the value to zero. */
7333 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7335 /* Mark this task as waiting for a notification. */
7336 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7338 if( xTicksToWait > ( TickType_t ) 0 )
7340 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7341 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7343 /* All ports are written to allow a yield in a critical
7344 * section (some will yield immediately, others wait until the
7345 * critical section exits) - but it is not something that
7346 * application code should ever do. */
7347 #if ( configNUMBER_OF_CORES == 1 )
7349 portYIELD_WITHIN_API();
7353 vTaskYieldWithinAPI();
7359 mtCOVERAGE_TEST_MARKER();
7364 mtCOVERAGE_TEST_MARKER();
7367 taskEXIT_CRITICAL();
7369 taskENTER_CRITICAL();
7371 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7373 if( pulNotificationValue != NULL )
7375 /* Output the current notification value, which may or may not
7377 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7380 /* If ucNotifyValue is set then either the task never entered the
7381 * blocked state (because a notification was already pending) or the
7382 * task unblocked because of a notification. Otherwise the task
7383 * unblocked because of a timeout. */
7384 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7386 /* A notification was not received. */
7391 /* A notification was already pending or a notification was
7392 * received while the task was waiting. */
7393 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7397 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7399 taskEXIT_CRITICAL();
7401 traceRETURN_xTaskGenericNotifyWait( xReturn );
7406 #endif /* configUSE_TASK_NOTIFICATIONS */
7407 /*-----------------------------------------------------------*/
7409 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7411 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7412 UBaseType_t uxIndexToNotify,
7414 eNotifyAction eAction,
7415 uint32_t * pulPreviousNotificationValue )
7418 BaseType_t xReturn = pdPASS;
7419 uint8_t ucOriginalNotifyState;
7421 traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7423 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7424 configASSERT( xTaskToNotify );
7425 pxTCB = xTaskToNotify;
7427 taskENTER_CRITICAL();
7429 if( pulPreviousNotificationValue != NULL )
7431 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7434 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7436 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7441 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7445 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7448 case eSetValueWithOverwrite:
7449 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7452 case eSetValueWithoutOverwrite:
7454 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7456 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7460 /* The value could not be written to the task. */
7468 /* The task is being notified without its notify value being
7474 /* Should not get here if all enums are handled.
7475 * Artificially force an assert by testing a value the
7476 * compiler can't assume is const. */
7477 configASSERT( xTickCount == ( TickType_t ) 0 );
7482 traceTASK_NOTIFY( uxIndexToNotify );
7484 /* If the task is in the blocked state specifically to wait for a
7485 * notification then unblock it now. */
7486 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7488 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7489 prvAddTaskToReadyList( pxTCB );
7491 /* The task should not have been on an event list. */
7492 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7494 #if ( configUSE_TICKLESS_IDLE != 0 )
7496 /* If a task is blocked waiting for a notification then
7497 * xNextTaskUnblockTime might be set to the blocked task's time
7498 * out time. If the task is unblocked for a reason other than
7499 * a timeout xNextTaskUnblockTime is normally left unchanged,
7500 * because it will automatically get reset to a new value when
7501 * the tick count equals xNextTaskUnblockTime. However if
7502 * tickless idling is used it might be more important to enter
7503 * sleep mode at the earliest possible time - so reset
7504 * xNextTaskUnblockTime here to ensure it is updated at the
7505 * earliest possible time. */
7506 prvResetNextTaskUnblockTime();
7510 /* Check if the notified task has a priority above the currently
7511 * executing task. */
7512 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7516 mtCOVERAGE_TEST_MARKER();
7519 taskEXIT_CRITICAL();
7521 traceRETURN_xTaskGenericNotify( xReturn );
7526 #endif /* configUSE_TASK_NOTIFICATIONS */
7527 /*-----------------------------------------------------------*/
7529 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7531 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
7532 UBaseType_t uxIndexToNotify,
7534 eNotifyAction eAction,
7535 uint32_t * pulPreviousNotificationValue,
7536 BaseType_t * pxHigherPriorityTaskWoken )
7539 uint8_t ucOriginalNotifyState;
7540 BaseType_t xReturn = pdPASS;
7541 UBaseType_t uxSavedInterruptStatus;
7543 traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
7545 configASSERT( xTaskToNotify );
7546 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7548 /* RTOS ports that support interrupt nesting have the concept of a
7549 * maximum system call (or maximum API call) interrupt priority.
7550 * Interrupts that are above the maximum system call priority are keep
7551 * permanently enabled, even when the RTOS kernel is in a critical section,
7552 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7553 * is defined in FreeRTOSConfig.h then
7554 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7555 * failure if a FreeRTOS API function is called from an interrupt that has
7556 * been assigned a priority above the configured maximum system call
7557 * priority. Only FreeRTOS functions that end in FromISR can be called
7558 * from interrupts that have been assigned a priority at or (logically)
7559 * below the maximum system call interrupt priority. FreeRTOS maintains a
7560 * separate interrupt safe API to ensure interrupt entry is as fast and as
7561 * simple as possible. More information (albeit Cortex-M specific) is
7562 * provided on the following link:
7563 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7564 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7566 pxTCB = xTaskToNotify;
7568 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7570 if( pulPreviousNotificationValue != NULL )
7572 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7575 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7576 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7581 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7585 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7588 case eSetValueWithOverwrite:
7589 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7592 case eSetValueWithoutOverwrite:
7594 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7596 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7600 /* The value could not be written to the task. */
7608 /* The task is being notified without its notify value being
7614 /* Should not get here if all enums are handled.
7615 * Artificially force an assert by testing a value the
7616 * compiler can't assume is const. */
7617 configASSERT( xTickCount == ( TickType_t ) 0 );
7621 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
7623 /* If the task is in the blocked state specifically to wait for a
7624 * notification then unblock it now. */
7625 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7627 /* The task should not have been on an event list. */
7628 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7630 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
7632 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7633 prvAddTaskToReadyList( pxTCB );
7637 /* The delayed and ready lists cannot be accessed, so hold
7638 * this task pending until the scheduler is resumed. */
7639 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
7642 #if ( configNUMBER_OF_CORES == 1 )
7644 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7646 /* The notified task has a priority above the currently
7647 * executing task so a yield is required. */
7648 if( pxHigherPriorityTaskWoken != NULL )
7650 *pxHigherPriorityTaskWoken = pdTRUE;
7653 /* Mark that a yield is pending in case the user is not
7654 * using the "xHigherPriorityTaskWoken" parameter to an ISR
7655 * safe FreeRTOS function. */
7656 xYieldPendings[ 0 ] = pdTRUE;
7660 mtCOVERAGE_TEST_MARKER();
7663 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
7665 #if ( configUSE_PREEMPTION == 1 )
7667 prvYieldForTask( pxTCB );
7669 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
7671 if( pxHigherPriorityTaskWoken != NULL )
7673 *pxHigherPriorityTaskWoken = pdTRUE;
7677 #endif /* if ( configUSE_PREEMPTION == 1 ) */
7679 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7682 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
7684 traceRETURN_xTaskGenericNotifyFromISR( xReturn );
7689 #endif /* configUSE_TASK_NOTIFICATIONS */
7690 /*-----------------------------------------------------------*/
7692 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7694 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
7695 UBaseType_t uxIndexToNotify,
7696 BaseType_t * pxHigherPriorityTaskWoken )
7699 uint8_t ucOriginalNotifyState;
7700 UBaseType_t uxSavedInterruptStatus;
7702 traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
7704 configASSERT( xTaskToNotify );
7705 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7707 /* RTOS ports that support interrupt nesting have the concept of a
7708 * maximum system call (or maximum API call) interrupt priority.
7709 * Interrupts that are above the maximum system call priority are keep
7710 * permanently enabled, even when the RTOS kernel is in a critical section,
7711 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7712 * is defined in FreeRTOSConfig.h then
7713 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7714 * failure if a FreeRTOS API function is called from an interrupt that has
7715 * been assigned a priority above the configured maximum system call
7716 * priority. Only FreeRTOS functions that end in FromISR can be called
7717 * from interrupts that have been assigned a priority at or (logically)
7718 * below the maximum system call interrupt priority. FreeRTOS maintains a
7719 * separate interrupt safe API to ensure interrupt entry is as fast and as
7720 * simple as possible. More information (albeit Cortex-M specific) is
7721 * provided on the following link:
7722 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7723 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7725 pxTCB = xTaskToNotify;
7727 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7729 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7730 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7732 /* 'Giving' is equivalent to incrementing a count in a counting
7734 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7736 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
7738 /* If the task is in the blocked state specifically to wait for a
7739 * notification then unblock it now. */
7740 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7742 /* The task should not have been on an event list. */
7743 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7745 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
7747 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7748 prvAddTaskToReadyList( pxTCB );
7752 /* The delayed and ready lists cannot be accessed, so hold
7753 * this task pending until the scheduler is resumed. */
7754 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
7757 #if ( configNUMBER_OF_CORES == 1 )
7759 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7761 /* The notified task has a priority above the currently
7762 * executing task so a yield is required. */
7763 if( pxHigherPriorityTaskWoken != NULL )
7765 *pxHigherPriorityTaskWoken = pdTRUE;
7768 /* Mark that a yield is pending in case the user is not
7769 * using the "xHigherPriorityTaskWoken" parameter in an ISR
7770 * safe FreeRTOS function. */
7771 xYieldPendings[ 0 ] = pdTRUE;
7775 mtCOVERAGE_TEST_MARKER();
7778 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
7780 #if ( configUSE_PREEMPTION == 1 )
7782 prvYieldForTask( pxTCB );
7784 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
7786 if( pxHigherPriorityTaskWoken != NULL )
7788 *pxHigherPriorityTaskWoken = pdTRUE;
7792 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
7794 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7797 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
7799 traceRETURN_vTaskGenericNotifyGiveFromISR();
7802 #endif /* configUSE_TASK_NOTIFICATIONS */
7803 /*-----------------------------------------------------------*/
7805 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7807 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
7808 UBaseType_t uxIndexToClear )
7813 traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
7815 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7817 /* If null is passed in here then it is the calling task that is having
7818 * its notification state cleared. */
7819 pxTCB = prvGetTCBFromHandle( xTask );
7821 taskENTER_CRITICAL();
7823 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
7825 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
7833 taskEXIT_CRITICAL();
7835 traceRETURN_xTaskGenericNotifyStateClear( xReturn );
7840 #endif /* configUSE_TASK_NOTIFICATIONS */
7841 /*-----------------------------------------------------------*/
7843 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7845 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
7846 UBaseType_t uxIndexToClear,
7847 uint32_t ulBitsToClear )
7852 traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
7854 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7856 /* If null is passed in here then it is the calling task that is having
7857 * its notification state cleared. */
7858 pxTCB = prvGetTCBFromHandle( xTask );
7860 taskENTER_CRITICAL();
7862 /* Return the notification as it was before the bits were cleared,
7863 * then clear the bit mask. */
7864 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
7865 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
7867 taskEXIT_CRITICAL();
7869 traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
7874 #endif /* configUSE_TASK_NOTIFICATIONS */
7875 /*-----------------------------------------------------------*/
7877 #if ( configGENERATE_RUN_TIME_STATS == 1 )
7879 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
7883 traceENTER_ulTaskGetRunTimeCounter( xTask );
7885 pxTCB = prvGetTCBFromHandle( xTask );
7887 traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
7889 return pxTCB->ulRunTimeCounter;
7892 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
7893 /*-----------------------------------------------------------*/
7895 #if ( configGENERATE_RUN_TIME_STATS == 1 )
7897 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
7900 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
7902 traceENTER_ulTaskGetRunTimePercent( xTask );
7904 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
7906 /* For percentage calculations. */
7907 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
7909 /* Avoid divide by zero errors. */
7910 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
7912 pxTCB = prvGetTCBFromHandle( xTask );
7913 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
7920 traceRETURN_ulTaskGetRunTimePercent( ulReturn );
7925 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
7926 /*-----------------------------------------------------------*/
7928 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
7930 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
7932 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
7935 traceENTER_ulTaskGetIdleRunTimeCounter();
7937 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
7939 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
7942 traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
7947 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
7948 /*-----------------------------------------------------------*/
7950 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
7952 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
7954 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
7955 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
7958 traceENTER_ulTaskGetIdleRunTimePercent();
7960 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
7962 /* For percentage calculations. */
7963 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
7965 /* Avoid divide by zero errors. */
7966 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
7968 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
7970 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
7973 ulReturn = ulRunTimeCounter / ulTotalTime;
7980 traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
7985 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
7986 /*-----------------------------------------------------------*/
7988 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
7989 const BaseType_t xCanBlockIndefinitely )
7991 TickType_t xTimeToWake;
7992 const TickType_t xConstTickCount = xTickCount;
7994 #if ( INCLUDE_xTaskAbortDelay == 1 )
7996 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
7997 * reset to pdFALSE so it can be detected as having been set to pdTRUE
7998 * when the task leaves the Blocked state. */
7999 pxCurrentTCB->ucDelayAborted = pdFALSE;
8003 /* Remove the task from the ready list before adding it to the blocked list
8004 * as the same list item is used for both lists. */
8005 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
8007 /* The current task must be in a ready list, so there is no need to
8008 * check, and the port reset macro can be called directly. */
8009 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. */
8013 mtCOVERAGE_TEST_MARKER();
8016 #if ( INCLUDE_vTaskSuspend == 1 )
8018 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
8020 /* Add the task to the suspended task list instead of a delayed task
8021 * list to ensure it is not woken by a timing event. It will block
8023 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
8027 /* Calculate the time at which the task should be woken if the event
8028 * does not occur. This may overflow but this doesn't matter, the
8029 * kernel will manage it correctly. */
8030 xTimeToWake = xConstTickCount + xTicksToWait;
8032 /* The list item will be inserted in wake time order. */
8033 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8035 if( xTimeToWake < xConstTickCount )
8037 /* Wake time has overflowed. Place this item in the overflow
8039 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8040 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8044 /* The wake time has not overflowed, so the current block list
8046 traceMOVED_TASK_TO_DELAYED_LIST();
8047 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8049 /* If the task entering the blocked state was placed at the
8050 * head of the list of blocked tasks then xNextTaskUnblockTime
8051 * needs to be updated too. */
8052 if( xTimeToWake < xNextTaskUnblockTime )
8054 xNextTaskUnblockTime = xTimeToWake;
8058 mtCOVERAGE_TEST_MARKER();
8063 #else /* INCLUDE_vTaskSuspend */
8065 /* Calculate the time at which the task should be woken if the event
8066 * does not occur. This may overflow but this doesn't matter, the kernel
8067 * will manage it correctly. */
8068 xTimeToWake = xConstTickCount + xTicksToWait;
8070 /* The list item will be inserted in wake time order. */
8071 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8073 if( xTimeToWake < xConstTickCount )
8075 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8076 /* Wake time has overflowed. Place this item in the overflow list. */
8077 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8081 traceMOVED_TASK_TO_DELAYED_LIST();
8082 /* The wake time has not overflowed, so the current block list is used. */
8083 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8085 /* If the task entering the blocked state was placed at the head of the
8086 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
8088 if( xTimeToWake < xNextTaskUnblockTime )
8090 xNextTaskUnblockTime = xTimeToWake;
8094 mtCOVERAGE_TEST_MARKER();
8098 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
8099 ( void ) xCanBlockIndefinitely;
8101 #endif /* INCLUDE_vTaskSuspend */
8103 /*-----------------------------------------------------------*/
8105 #if ( portUSING_MPU_WRAPPERS == 1 )
8107 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
8111 traceENTER_xTaskGetMPUSettings( xTask );
8113 pxTCB = prvGetTCBFromHandle( xTask );
8115 traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
8117 return &( pxTCB->xMPUSettings );
8120 #endif /* portUSING_MPU_WRAPPERS */
8121 /*-----------------------------------------------------------*/
8123 /* Code below here allows additional code to be inserted into this source file,
8124 * especially where access to file scope functions and data is needed (for example
8125 * when performing module tests). */
8127 #ifdef FREERTOS_MODULE_TEST
8128 #include "tasks_test_access_functions.h"
8132 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
8134 #include "freertos_tasks_c_additions.h"
8136 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8137 static void freertos_tasks_c_additions_init( void )
8139 FREERTOS_TASKS_C_ADDITIONS_INIT();
8143 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8144 /*-----------------------------------------------------------*/
8146 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8149 * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8150 * to provide the memory that is used by the Idle task. It is used when
8151 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8152 * it's own implementation of vApplicationGetIdleTaskMemory by setting
8153 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8155 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8156 StackType_t ** ppxIdleTaskStackBuffer,
8157 uint32_t * pulIdleTaskStackSize )
8159 static StaticTask_t xIdleTaskTCB;
8160 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8162 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8163 *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8164 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8167 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8168 /*-----------------------------------------------------------*/
8170 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8173 * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8174 * to provide the memory that is used by the Timer service task. It is used when
8175 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8176 * it's own implementation of vApplicationGetTimerTaskMemory by setting
8177 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8179 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8180 StackType_t ** ppxTimerTaskStackBuffer,
8181 uint32_t * pulTimerTaskStackSize )
8183 static StaticTask_t xTimerTaskTCB;
8184 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8186 *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8187 *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8188 *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8191 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8192 /*-----------------------------------------------------------*/