2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /* Standard includes. */
33 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
34 * all the API functions to use the MPU wrappers. That should only be done when
35 * task.h is included from an application file. */
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
38 /* FreeRTOS includes. */
42 #include "stack_macros.h"
44 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
45 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
46 * for the header files above, but not in this file, in order to generate the
47 * correct privileged Vs unprivileged linkage and placement. */
48 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
50 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
51 * functions but without including stdio.h here. */
52 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
54 /* At the bottom of this file are two optional functions that can be used
55 * to generate human readable text from the raw data generated by the
56 * uxTaskGetSystemState() function. Note the formatting functions are provided
57 * for convenience only, and are NOT considered part of the kernel. */
59 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
61 #if ( configUSE_PREEMPTION == 0 )
63 /* If the cooperative scheduler is being used then a yield should not be
64 * performed just because a higher priority task has been woken. */
65 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB )
66 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB )
69 #if ( configNUMBER_OF_CORES == 1 )
71 /* This macro requests the running task pxTCB to yield. In single core
72 * scheduler, a running task always runs on core 0 and portYIELD_WITHIN_API()
73 * can be used to request the task running on core 0 to yield. Therefore, pxTCB
74 * is not used in this macro. */
75 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) \
78 portYIELD_WITHIN_API(); \
81 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \
83 if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \
85 portYIELD_WITHIN_API(); \
89 mtCOVERAGE_TEST_MARKER(); \
93 #else /* if ( configNUMBER_OF_CORES == 1 ) */
95 /* Yield the core on which this task is running. */
96 #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldCore( ( pxTCB )->xTaskRunState )
98 /* Yield for the task if a running task has priority lower than this task. */
99 #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldForTask( pxTCB )
101 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
103 #endif /* if ( configUSE_PREEMPTION == 0 ) */
105 /* Values that can be assigned to the ucNotifyState member of the TCB. */
106 #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
107 #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
108 #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
111 * The value used to fill the stack of a task when the task is created. This
112 * is used purely for checking the high water mark for tasks.
114 #define tskSTACK_FILL_BYTE ( 0xa5U )
116 /* Bits used to record how a task's stack and TCB were allocated. */
117 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
118 #define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
119 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
121 /* If any of the following are set then task stacks are filled with a known
122 * value so the high water mark can be determined. If none of the following are
123 * set then don't fill the stack so there is no unnecessary dependency on memset. */
124 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
125 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
127 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
131 * Macros used by vListTask to indicate which state a task is in.
133 #define tskRUNNING_CHAR ( 'X' )
134 #define tskBLOCKED_CHAR ( 'B' )
135 #define tskREADY_CHAR ( 'R' )
136 #define tskDELETED_CHAR ( 'D' )
137 #define tskSUSPENDED_CHAR ( 'S' )
140 * Some kernel aware debuggers require the data the debugger needs access to to
141 * be global, rather than file scope.
143 #ifdef portREMOVE_STATIC_QUALIFIER
147 /* The name allocated to the Idle task. This can be overridden by defining
148 * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
149 #ifndef configIDLE_TASK_NAME
150 #define configIDLE_TASK_NAME "IDLE"
153 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
155 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
156 * performed in a generic way that is not optimised to any particular
157 * microcontroller architecture. */
159 /* uxTopReadyPriority holds the priority of the highest priority ready
161 #define taskRECORD_READY_PRIORITY( uxPriority ) \
163 if( ( uxPriority ) > uxTopReadyPriority ) \
165 uxTopReadyPriority = ( uxPriority ); \
167 } while( 0 ) /* taskRECORD_READY_PRIORITY */
169 /*-----------------------------------------------------------*/
171 #if ( configNUMBER_OF_CORES == 1 )
172 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
174 UBaseType_t uxTopPriority = uxTopReadyPriority; \
176 /* Find the highest priority queue that contains ready tasks. */ \
177 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
179 configASSERT( uxTopPriority ); \
183 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
184 * the same priority get an equal share of the processor time. */ \
185 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
186 uxTopReadyPriority = uxTopPriority; \
187 } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */
188 #else /* if ( configNUMBER_OF_CORES == 1 ) */
190 #define taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID ) prvSelectHighestPriorityTask( xCoreID )
192 #endif /* if ( configNUMBER_OF_CORES == 1 ) */
194 /*-----------------------------------------------------------*/
196 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
197 * they are only required when a port optimised method of task selection is
199 #define taskRESET_READY_PRIORITY( uxPriority )
200 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
202 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
204 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
205 * performed in a way that is tailored to the particular microcontroller
206 * architecture being used. */
208 /* A port optimised version is provided. Call the port defined macros. */
209 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( ( uxPriority ), uxTopReadyPriority )
211 /*-----------------------------------------------------------*/
213 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
215 UBaseType_t uxTopPriority; \
217 /* Find the highest priority list that contains ready tasks. */ \
218 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
219 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
220 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
223 /*-----------------------------------------------------------*/
225 /* A port optimised version is provided, call it only if the TCB being reset
226 * is being referenced from a ready list. If it is referenced from a delayed
227 * or suspended list then it won't be in a ready list. */
228 #define taskRESET_READY_PRIORITY( uxPriority ) \
230 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
232 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
236 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
238 /*-----------------------------------------------------------*/
240 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
241 * count overflows. */
242 #define taskSWITCH_DELAYED_LISTS() \
246 /* The delayed tasks list should be empty when the lists are switched. */ \
247 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
249 pxTemp = pxDelayedTaskList; \
250 pxDelayedTaskList = pxOverflowDelayedTaskList; \
251 pxOverflowDelayedTaskList = pxTemp; \
253 prvResetNextTaskUnblockTime(); \
256 /*-----------------------------------------------------------*/
259 * Place the task represented by pxTCB into the appropriate ready list for
260 * the task. It is inserted at the end of the list.
262 #define prvAddTaskToReadyList( pxTCB ) \
264 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
265 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
266 listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
267 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB ); \
269 /*-----------------------------------------------------------*/
272 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
273 * where NULL is used to indicate that the handle of the currently executing
274 * task should be used in place of the parameter. This macro simply checks to
275 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
277 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
279 /* The item value of the event list item is normally used to hold the priority
280 * of the task to which it belongs (coded to allow it to be held in reverse
281 * priority order). However, it is occasionally borrowed for other purposes. It
282 * is important its value is not updated due to a task priority change while it is
283 * being used for another purpose. The following bit definition is used to inform
284 * the scheduler that the value should not be changed - in which case it is the
285 * responsibility of whichever module is using the value to ensure it gets set back
286 * to its original value when it is released. */
287 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
288 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U
289 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
290 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
291 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
292 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000000000000000ULL
295 /* Indicates that the task is not actively running on any core. */
296 #define taskTASK_NOT_RUNNING ( ( BaseType_t ) ( -1 ) )
298 /* Indicates that the task is actively running but scheduled to yield. */
299 #define taskTASK_SCHEDULED_TO_YIELD ( ( BaseType_t ) ( -2 ) )
301 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
302 #if ( configNUMBER_OF_CORES == 1 )
303 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
304 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
306 #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) )
307 #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB )->xTaskRunState != taskTASK_NOT_RUNNING ) ? ( pdTRUE ) : ( pdFALSE ) )
310 /* Indicates that the task is an Idle task. */
311 #define taskATTRIBUTE_IS_IDLE ( UBaseType_t ) ( 1UL << 0UL )
313 #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) )
314 #define portGET_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting )
315 #define portSET_CRITICAL_NESTING_COUNT( x ) ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting = ( x ) )
316 #define portINCREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting++ )
317 #define portDECREMENT_CRITICAL_NESTING_COUNT() ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting-- )
318 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) */
320 /* Code below here allows infinite loop controlling, especially for the infinite loop
321 * in idle task function (for example when performing unit tests). */
322 #ifndef INFINITE_LOOP
323 #define INFINITE_LOOP() 1
326 #define taskBITS_PER_BYTE ( ( size_t ) 8 )
329 * Task control block. A task control block (TCB) is allocated for each task,
330 * and stores task state information, including a pointer to the task's context
331 * (the task's run time environment, including register values)
333 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
335 volatile StackType_t * pxTopOfStack; /**< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
337 #if ( portUSING_MPU_WRAPPERS == 1 )
338 xMPU_SETTINGS xMPUSettings; /**< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
341 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
342 UBaseType_t uxCoreAffinityMask; /**< Used to link the task to certain cores. UBaseType_t must have greater than or equal to the number of bits as configNUMBER_OF_CORES. */
345 ListItem_t xStateListItem; /**< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
346 ListItem_t xEventListItem; /**< Used to reference a task from an event list. */
347 UBaseType_t uxPriority; /**< The priority of the task. 0 is the lowest priority. */
348 StackType_t * pxStack; /**< Points to the start of the stack. */
349 #if ( configNUMBER_OF_CORES > 1 )
350 volatile BaseType_t xTaskRunState; /**< Used to identify the core the task is running on, if the task is running. Otherwise, identifies the task's state - not running or yielding. */
351 UBaseType_t uxTaskAttributes; /**< Task's attributes - currently used to identify the idle tasks. */
353 char pcTaskName[ configMAX_TASK_NAME_LEN ]; /**< Descriptive name given to the task when created. Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
355 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
356 BaseType_t xPreemptionDisable; /**< Used to prevent the task from being preempted. */
359 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
360 StackType_t * pxEndOfStack; /**< Points to the highest valid address for the stack. */
363 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
364 UBaseType_t uxCriticalNesting; /**< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
367 #if ( configUSE_TRACE_FACILITY == 1 )
368 UBaseType_t uxTCBNumber; /**< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
369 UBaseType_t uxTaskNumber; /**< Stores a number specifically for use by third party trace code. */
372 #if ( configUSE_MUTEXES == 1 )
373 UBaseType_t uxBasePriority; /**< The priority last assigned to the task - used by the priority inheritance mechanism. */
374 UBaseType_t uxMutexesHeld;
377 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
378 TaskHookFunction_t pxTaskTag;
381 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
382 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
385 #if ( configGENERATE_RUN_TIME_STATS == 1 )
386 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /**< Stores the amount of time the task has spent in the Running state. */
389 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
390 configTLS_BLOCK_TYPE xTLSBlock; /**< Memory block used as Thread Local Storage (TLS) Block for the task. */
393 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
394 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
395 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
398 /* See the comments in FreeRTOS.h with the definition of
399 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
400 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
401 uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
404 #if ( INCLUDE_xTaskAbortDelay == 1 )
405 uint8_t ucDelayAborted;
408 #if ( configUSE_POSIX_ERRNO == 1 )
413 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
414 * below to enable the use of older kernel aware debuggers. */
415 typedef tskTCB TCB_t;
417 /*lint -save -e956 A manual analysis and inspection has been used to determine
418 * which static variables must be declared volatile. */
419 #if ( configNUMBER_OF_CORES == 1 )
420 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
422 /* MISRA Ref 8.4.1 [Declaration shall be visible] */
423 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
424 /* coverity[misra_c_2012_rule_8_4_violation] */
425 portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ];
426 #define pxCurrentTCB xTaskGetCurrentTaskHandle()
429 /* Lists for ready and blocked tasks. --------------------
430 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
431 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
432 * the static qualifier. */
433 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
434 PRIVILEGED_DATA static List_t xDelayedTaskList1; /**< Delayed tasks. */
435 PRIVILEGED_DATA static List_t xDelayedTaskList2; /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
436 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /**< Points to the delayed task list currently being used. */
437 PRIVILEGED_DATA static List_t * volatile pxOverflowDelayedTaskList; /**< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
438 PRIVILEGED_DATA static List_t xPendingReadyList; /**< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready list when the scheduler is resumed. */
440 #if ( INCLUDE_vTaskDelete == 1 )
442 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
443 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
447 #if ( INCLUDE_vTaskSuspend == 1 )
449 PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
453 /* Global POSIX errno. Its value is changed upon context switching to match
454 * the errno of the currently running task. */
455 #if ( configUSE_POSIX_ERRNO == 1 )
456 int FreeRTOS_errno = 0;
459 /* Other file private variables. --------------------------------*/
460 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
461 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
462 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
463 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
464 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
465 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
466 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
467 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
468 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
469 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandles[ configNUMBER_OF_CORES ]; /**< Holds the handles of the idle tasks. The idle tasks are created automatically when the scheduler is started. */
471 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
472 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
473 * to determine the number of priority lists to read back from the remote target. */
474 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
476 /* Context switches are held pending while the scheduler is suspended. Also,
477 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
478 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
479 * If an interrupt needs to unblock a task while the scheduler is suspended then it
480 * moves the task's event list item into the xPendingReadyList, ready for the
481 * kernel to move the task from the pending ready list into the real ready list
482 * when the scheduler is unsuspended. The pending ready list itself can only be
483 * accessed from a critical section.
485 * Updates to uxSchedulerSuspended must be protected by both the task lock and the ISR lock
486 * and must not be done from an ISR. Reads must be protected by either lock and may be done
487 * from either an ISR or a task. */
488 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
490 #if ( configGENERATE_RUN_TIME_STATS == 1 )
492 /* Do not move these variables to function scope as doing so prevents the
493 * code working with debuggers that need to remove the static qualifier. */
494 PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the value of a timer/counter the last time a task was switched in. */
495 PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the total amount of execution time as defined by the run time counter clock. */
499 #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configNUMBER_OF_CORES > 1 )
501 /* Do not move these variables to function scope as doing so prevents the
502 * code working with debuggers that need to remove the static qualifier. */
503 static StaticTask_t xIdleTCBBuffers[ configNUMBER_OF_CORES - 1 ];
504 static StackType_t xIdleTaskStackBuffers[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
506 #endif /* #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
510 /*-----------------------------------------------------------*/
512 /* File private functions. --------------------------------*/
515 * Creates the idle tasks during scheduler start.
517 static BaseType_t prvCreateIdleTasks( void );
519 #if ( configNUMBER_OF_CORES > 1 )
522 * Checks to see if another task moved the current task out of the ready
523 * list while it was waiting to enter a critical section and yields, if so.
525 static void prvCheckForRunStateChange( void );
526 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
528 #if ( configNUMBER_OF_CORES > 1 )
531 * Yields the given core.
533 static void prvYieldCore( BaseType_t xCoreID );
534 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
536 #if ( configNUMBER_OF_CORES > 1 )
539 * Yields a core, or cores if multiple priorities are not allowed to run
540 * simultaneously, to allow the task pxTCB to run.
542 static void prvYieldForTask( const TCB_t * pxTCB );
543 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
545 #if ( configNUMBER_OF_CORES > 1 )
548 * Selects the highest priority available task for the given core.
550 static void prvSelectHighestPriorityTask( BaseType_t xCoreID );
551 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
554 * Utility task that simply returns pdTRUE if the task referenced by xTask is
555 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
556 * is in any other state.
558 #if ( INCLUDE_vTaskSuspend == 1 )
560 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
562 #endif /* INCLUDE_vTaskSuspend */
565 * Utility to ready all the lists used by the scheduler. This is called
566 * automatically upon the creation of the first task.
568 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
571 * The idle task, which as all tasks is implemented as a never ending loop.
572 * The idle task is automatically created and added to the ready lists upon
573 * creation of the first user task.
575 * In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 minimal idle tasks are also
576 * created to ensure that each core has an idle task to run when no other
577 * task is available to run.
579 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
580 * language extensions. The equivalent prototype for these functions are:
582 * void prvIdleTask( void *pvParameters );
583 * void prvMinimalIdleTask( void *pvParameters );
586 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
587 #if ( configNUMBER_OF_CORES > 1 )
588 static portTASK_FUNCTION_PROTO( prvMinimalIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
592 * Utility to free all memory allocated by the scheduler to hold a TCB,
593 * including the stack pointed to by the TCB.
595 * This does not free memory allocated by the task itself (i.e. memory
596 * allocated by calls to pvPortMalloc from within the tasks application code).
598 #if ( INCLUDE_vTaskDelete == 1 )
600 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
605 * Used only by the idle task. This checks to see if anything has been placed
606 * in the list of tasks waiting to be deleted. If so the task is cleaned up
607 * and its TCB deleted.
609 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
612 * The currently executing task is entering the Blocked state. Add the task to
613 * either the current or the overflow delayed task list.
615 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
616 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
619 * Fills an TaskStatus_t structure with information on each task that is
620 * referenced from the pxList list (which may be a ready list, a delayed list,
621 * a suspended list, etc.).
623 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
624 * NORMAL APPLICATION CODE.
626 #if ( configUSE_TRACE_FACILITY == 1 )
628 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
630 eTaskState eState ) PRIVILEGED_FUNCTION;
635 * Searches pxList for a task with name pcNameToQuery - returning a handle to
636 * the task if it is found, or NULL if the task is not found.
638 #if ( INCLUDE_xTaskGetHandle == 1 )
640 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
641 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
646 * When a task is created, the stack of the task is filled with a known value.
647 * This function determines the 'high water mark' of the task stack by
648 * determining how much of the stack remains at the original preset value.
650 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
652 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
657 * Return the amount of time, in ticks, that will pass before the kernel will
658 * next move a task from the Blocked state to the Running state.
660 * This conditional compilation should use inequality to 0, not equality to 1.
661 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
662 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
663 * set to a value other than 1.
665 #if ( configUSE_TICKLESS_IDLE != 0 )
667 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
672 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
673 * will exit the Blocked state.
675 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
677 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
680 * Helper function used to pad task names with spaces when printing out
681 * human readable tables of task information.
683 static char * prvWriteNameToBuffer( char * pcBuffer,
684 const char * pcTaskName ) PRIVILEGED_FUNCTION;
689 * Called after a Task_t structure has been allocated either statically or
690 * dynamically to fill in the structure's members.
692 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
693 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
694 const uint32_t ulStackDepth,
695 void * const pvParameters,
696 UBaseType_t uxPriority,
697 TaskHandle_t * const pxCreatedTask,
699 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
702 * Called after a new task has been created and initialised to place the task
703 * under the control of the scheduler.
705 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
708 * freertos_tasks_c_additions_init() should only be called if the user definable
709 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
710 * called by the function.
712 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
714 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
718 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
719 extern void vApplicationMinimalIdleHook( void );
720 #endif /* #if ( configUSE_MINIMAL_IDLE_HOOK == 1 ) */
722 /*-----------------------------------------------------------*/
724 #if ( configNUMBER_OF_CORES > 1 )
725 static void prvCheckForRunStateChange( void )
727 UBaseType_t uxPrevCriticalNesting;
728 const TCB_t * pxThisTCB;
730 /* This must only be called from within a task. */
731 portASSERT_IF_IN_ISR();
733 /* This function is always called with interrupts disabled
734 * so this is safe. */
735 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
737 while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD )
739 /* We are only here if we just entered a critical section
740 * or if we just suspended the scheduler, and another task
741 * has requested that we yield.
743 * This is slightly complicated since we need to save and restore
744 * the suspension and critical nesting counts, as well as release
745 * and reacquire the correct locks. And then, do it all over again
746 * if our state changed again during the reacquisition. */
747 uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
749 if( uxPrevCriticalNesting > 0U )
751 portSET_CRITICAL_NESTING_COUNT( 0U );
752 portRELEASE_ISR_LOCK();
756 /* The scheduler is suspended. uxSchedulerSuspended is updated
757 * only when the task is not requested to yield. */
758 mtCOVERAGE_TEST_MARKER();
761 portRELEASE_TASK_LOCK();
762 portMEMORY_BARRIER();
763 configASSERT( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD );
765 portENABLE_INTERRUPTS();
767 /* Enabling interrupts should cause this core to immediately
768 * service the pending interrupt and yield. If the run state is still
769 * yielding here then that is a problem. */
770 configASSERT( pxThisTCB->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD );
772 portDISABLE_INTERRUPTS();
776 portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
778 if( uxPrevCriticalNesting == 0U )
780 portRELEASE_ISR_LOCK();
784 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
786 /*-----------------------------------------------------------*/
788 #if ( configNUMBER_OF_CORES > 1 )
789 static void prvYieldCore( BaseType_t xCoreID )
791 /* This must be called from a critical section and xCoreID must be valid. */
792 if( ( portCHECK_IF_IN_ISR() == pdTRUE ) && ( xCoreID == ( BaseType_t ) portGET_CORE_ID() ) )
794 xYieldPendings[ xCoreID ] = pdTRUE;
798 if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD )
800 if( xCoreID == ( BaseType_t ) portGET_CORE_ID() )
802 xYieldPendings[ xCoreID ] = pdTRUE;
806 portYIELD_CORE( xCoreID );
807 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_SCHEDULED_TO_YIELD;
812 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
813 /*-----------------------------------------------------------*/
815 #if ( configNUMBER_OF_CORES > 1 )
816 static void prvYieldForTask( const TCB_t * pxTCB )
818 BaseType_t xLowestPriorityToPreempt;
819 BaseType_t xCurrentCoreTaskPriority;
820 BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
823 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
824 BaseType_t xYieldCount = 0;
825 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
827 /* This must be called from a critical section. */
828 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
830 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
832 /* No task should yield for this one if it is a lower priority
833 * than priority level of currently ready tasks. */
834 if( pxTCB->uxPriority >= uxTopReadyPriority )
836 /* Yield is not required for a task which is already running. */
837 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
840 xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
842 /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
843 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
844 --xLowestPriorityToPreempt;
846 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
848 xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
850 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
851 if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
853 xCurrentCoreTaskPriority = xCurrentCoreTaskPriority - 1;
856 if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
858 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
859 if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
862 if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
864 #if ( configUSE_CORE_AFFINITY == 1 )
865 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
868 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
869 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
872 xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
873 xLowestPriorityCore = xCoreID;
879 mtCOVERAGE_TEST_MARKER();
883 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
885 /* Yield all currently running non-idle tasks with a priority lower than
886 * the task that needs to run. */
887 if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
888 ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
890 prvYieldCore( xCoreID );
895 mtCOVERAGE_TEST_MARKER();
898 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
902 mtCOVERAGE_TEST_MARKER();
906 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
907 if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
908 #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
909 if( xLowestPriorityCore >= 0 )
910 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
912 prvYieldCore( xLowestPriorityCore );
915 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
916 /* Verify that the calling core always yields to higher priority tasks. */
917 if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0 ) &&
918 ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
920 configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) ||
921 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) );
926 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
927 /*-----------------------------------------------------------*/
929 #if ( configNUMBER_OF_CORES > 1 )
930 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
932 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
933 BaseType_t xTaskScheduled = pdFALSE;
934 BaseType_t xDecrementTopPriority = pdTRUE;
936 #if ( configUSE_CORE_AFFINITY == 1 )
937 const TCB_t * pxPreviousTCB = NULL;
939 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
940 BaseType_t xPriorityDropped = pdFALSE;
943 /* This function should be called when scheduler is running. */
944 configASSERT( xSchedulerRunning == pdTRUE );
946 /* A new task is created and a running task with the same priority yields
947 * itself to run the new task. When a running task yields itself, it is still
948 * in the ready list. This running task will be selected before the new task
949 * since the new task is always added to the end of the ready list.
950 * The other problem is that the running task still in the same position of
951 * the ready list when it yields itself. It is possible that it will be selected
952 * earlier then other tasks which waits longer than this task.
954 * To fix these problems, the running task should be put to the end of the
955 * ready list before searching for the ready task in the ready list. */
956 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
957 &pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
959 ( void ) uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
960 vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
961 &pxCurrentTCBs[ xCoreID ]->xStateListItem );
964 while( xTaskScheduled == pdFALSE )
966 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
968 if( uxCurrentPriority < uxTopReadyPriority )
970 /* We can't schedule any tasks, other than idle, that have a
971 * priority lower than the priority of a task currently running
972 * on another core. */
973 uxCurrentPriority = tskIDLE_PRIORITY;
978 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
980 const List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
981 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxReadyList );
982 ListItem_t * pxIterator;
984 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
985 * must not be decremented any further. */
986 xDecrementTopPriority = pdFALSE;
988 for( pxIterator = listGET_HEAD_ENTRY( pxReadyList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
990 TCB_t * pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
992 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
994 /* When falling back to the idle priority because only one priority
995 * level is allowed to run at a time, we should ONLY schedule the true
996 * idle tasks, not user tasks at the idle priority. */
997 if( uxCurrentPriority < uxTopReadyPriority )
999 if( ( pxTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0 )
1005 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1007 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
1009 #if ( configUSE_CORE_AFFINITY == 1 )
1010 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1013 /* If the task is not being executed by any core swap it in. */
1014 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
1015 #if ( configUSE_CORE_AFFINITY == 1 )
1016 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
1018 pxTCB->xTaskRunState = xCoreID;
1019 pxCurrentTCBs[ xCoreID ] = pxTCB;
1020 xTaskScheduled = pdTRUE;
1023 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
1025 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) );
1027 #if ( configUSE_CORE_AFFINITY == 1 )
1028 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1031 /* The task is already running on this core, mark it as scheduled. */
1032 pxTCB->xTaskRunState = xCoreID;
1033 xTaskScheduled = pdTRUE;
1038 /* This task is running on the core other than xCoreID. */
1039 mtCOVERAGE_TEST_MARKER();
1042 if( xTaskScheduled != pdFALSE )
1044 /* A task has been selected to run on this core. */
1051 if( xDecrementTopPriority != pdFALSE )
1053 uxTopReadyPriority--;
1054 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1056 xPriorityDropped = pdTRUE;
1062 /* There are configNUMBER_OF_CORES Idle tasks created when scheduler started.
1063 * The scheduler should be able to select a task to run when uxCurrentPriority
1064 * is tskIDLE_PRIORITY. uxCurrentPriority is never decreased to value blow
1065 * tskIDLE_PRIORITY. */
1066 if( uxCurrentPriority > tskIDLE_PRIORITY )
1068 uxCurrentPriority--;
1072 /* This function is called when idle task is not created. Break the
1073 * loop to prevent uxCurrentPriority overrun. */
1078 #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1080 if( xTaskScheduled == pdTRUE )
1082 if( xPriorityDropped != pdFALSE )
1084 /* There may be several ready tasks that were being prevented from running because there was
1085 * a higher priority task running. Now that the last of the higher priority tasks is no longer
1086 * running, make sure all the other idle tasks yield. */
1089 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUMBER_OF_CORES; x++ )
1091 if( ( pxCurrentTCBs[ x ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0 )
1099 #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1101 #if ( configUSE_CORE_AFFINITY == 1 )
1103 if( xTaskScheduled == pdTRUE )
1105 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
1107 /* A ready task was just evicted from this core. See if it can be
1108 * scheduled on any other core. */
1109 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
1110 BaseType_t xLowestPriority = ( BaseType_t ) pxPreviousTCB->uxPriority;
1111 BaseType_t xLowestPriorityCore = -1;
1114 if( ( pxPreviousTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1116 xLowestPriority = xLowestPriority - 1;
1119 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1121 /* The ready task that was removed from this core is not excluded from it.
1122 * Only look at the intersection of the cores the removed task is allowed to run
1123 * on with the cores that the new task is excluded from. It is possible that the
1124 * new task was only placed onto this core because it is excluded from another.
1125 * Check to see if the previous task could run on one of those cores. */
1126 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1130 /* The ready task that was removed from this core is excluded from it. */
1133 uxCoreMap &= ( ( 1U << configNUMBER_OF_CORES ) - 1U );
1135 for( x = ( ( BaseType_t ) configNUMBER_OF_CORES - 1 ); x >= ( BaseType_t ) 0; x-- )
1137 UBaseType_t uxCore = ( UBaseType_t ) x;
1138 BaseType_t xTaskPriority;
1140 if( ( uxCoreMap & ( ( UBaseType_t ) 1U << uxCore ) ) != 0U )
1142 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority;
1144 if( ( pxCurrentTCBs[ uxCore ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1146 xTaskPriority = xTaskPriority - ( BaseType_t ) 1;
1149 uxCoreMap &= ~( ( UBaseType_t ) 1U << uxCore );
1151 if( ( xTaskPriority < xLowestPriority ) &&
1152 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ] ) != pdFALSE ) &&
1153 ( xYieldPendings[ uxCore ] == pdFALSE ) )
1155 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1156 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1159 xLowestPriority = xTaskPriority;
1160 xLowestPriorityCore = ( BaseType_t ) uxCore;
1166 if( xLowestPriorityCore >= 0 )
1168 prvYieldCore( xLowestPriorityCore );
1173 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) */
1176 #endif /* ( configNUMBER_OF_CORES > 1 ) */
1178 /*-----------------------------------------------------------*/
1180 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1182 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1183 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1184 const uint32_t ulStackDepth,
1185 void * const pvParameters,
1186 UBaseType_t uxPriority,
1187 StackType_t * const puxStackBuffer,
1188 StaticTask_t * const pxTaskBuffer )
1189 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1191 return xTaskCreateStaticAffinitySet( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, tskNO_AFFINITY );
1194 TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1195 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1196 const uint32_t ulStackDepth,
1197 void * const pvParameters,
1198 UBaseType_t uxPriority,
1199 StackType_t * const puxStackBuffer,
1200 StaticTask_t * const pxTaskBuffer,
1201 UBaseType_t uxCoreAffinityMask )
1202 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1205 TaskHandle_t xReturn;
1207 traceENTER_xTaskCreateStatic( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer );
1209 configASSERT( puxStackBuffer != NULL );
1210 configASSERT( pxTaskBuffer != NULL );
1212 #if ( configASSERT_DEFINED == 1 )
1214 /* Sanity check that the size of the structure used to declare a
1215 * variable of type StaticTask_t equals the size of the real task
1217 volatile size_t xSize = sizeof( StaticTask_t );
1218 configASSERT( xSize == sizeof( TCB_t ) );
1219 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1221 #endif /* configASSERT_DEFINED */
1223 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1225 /* The memory used for the task's TCB and stack are passed into this
1226 * function - use them. */
1227 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. */
1228 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1229 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1231 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1233 /* Tasks can be created statically or dynamically, so note this
1234 * task was created statically in case the task is later deleted. */
1235 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1237 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1239 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1241 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1243 /* Set the task's affinity before scheduling it. */
1244 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1248 prvAddNewTaskToReadyList( pxNewTCB );
1255 traceRETURN_xTaskCreateStatic( xReturn );
1260 #endif /* SUPPORT_STATIC_ALLOCATION */
1261 /*-----------------------------------------------------------*/
1263 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1265 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1266 TaskHandle_t * pxCreatedTask )
1267 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1269 return xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1272 BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1273 UBaseType_t uxCoreAffinityMask,
1274 TaskHandle_t * pxCreatedTask )
1275 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1278 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1280 traceENTER_xTaskCreateRestrictedStatic( pxTaskDefinition, pxCreatedTask );
1282 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1283 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1285 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1287 /* Allocate space for the TCB. Where the memory comes from depends
1288 * on the implementation of the port malloc function and whether or
1289 * not static allocation is being used. */
1290 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1291 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1293 /* Store the stack location in the TCB. */
1294 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1296 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1298 /* Tasks can be created statically or dynamically, so note this
1299 * task was created statically in case the task is later deleted. */
1300 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1302 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1304 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1305 pxTaskDefinition->pcName,
1306 ( uint32_t ) pxTaskDefinition->usStackDepth,
1307 pxTaskDefinition->pvParameters,
1308 pxTaskDefinition->uxPriority,
1309 pxCreatedTask, pxNewTCB,
1310 pxTaskDefinition->xRegions );
1312 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1314 /* Set the task's affinity before scheduling it. */
1315 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1319 prvAddNewTaskToReadyList( pxNewTCB );
1323 traceRETURN_xTaskCreateRestrictedStatic( xReturn );
1328 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1329 /*-----------------------------------------------------------*/
1331 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1333 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1334 TaskHandle_t * pxCreatedTask )
1335 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1337 return xTaskCreateRestrictedAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1340 BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1341 UBaseType_t uxCoreAffinityMask,
1342 TaskHandle_t * pxCreatedTask )
1343 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1346 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1348 traceENTER_xTaskCreateRestricted( pxTaskDefinition, pxCreatedTask );
1350 configASSERT( pxTaskDefinition->puxStackBuffer );
1352 if( pxTaskDefinition->puxStackBuffer != NULL )
1354 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1356 if( pxNewTCB != NULL )
1358 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1360 /* Store the stack location in the TCB. */
1361 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1363 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1365 /* Tasks can be created statically or dynamically, so note
1366 * this task had a statically allocated stack in case it is
1367 * later deleted. The TCB was allocated dynamically. */
1368 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1370 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1372 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1373 pxTaskDefinition->pcName,
1374 ( uint32_t ) pxTaskDefinition->usStackDepth,
1375 pxTaskDefinition->pvParameters,
1376 pxTaskDefinition->uxPriority,
1377 pxCreatedTask, pxNewTCB,
1378 pxTaskDefinition->xRegions );
1380 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1382 /* Set the task's affinity before scheduling it. */
1383 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1387 prvAddNewTaskToReadyList( pxNewTCB );
1392 traceRETURN_xTaskCreateRestricted( xReturn );
1397 #endif /* portUSING_MPU_WRAPPERS */
1398 /*-----------------------------------------------------------*/
1400 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1402 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1403 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1404 const configSTACK_DEPTH_TYPE usStackDepth,
1405 void * const pvParameters,
1406 UBaseType_t uxPriority,
1407 TaskHandle_t * const pxCreatedTask )
1408 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1410 return xTaskCreateAffinitySet( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, tskNO_AFFINITY, pxCreatedTask );
1413 BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1414 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1415 const configSTACK_DEPTH_TYPE usStackDepth,
1416 void * const pvParameters,
1417 UBaseType_t uxPriority,
1418 UBaseType_t uxCoreAffinityMask,
1419 TaskHandle_t * const pxCreatedTask )
1420 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1425 traceENTER_xTaskCreate( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask );
1427 /* If the stack grows down then allocate the stack then the TCB so the stack
1428 * does not grow into the TCB. Likewise if the stack grows up then allocate
1429 * the TCB then the stack. */
1430 #if ( portSTACK_GROWTH > 0 )
1432 /* Allocate space for the TCB. Where the memory comes from depends on
1433 * the implementation of the port malloc function and whether or not static
1434 * allocation is being used. */
1435 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1437 if( pxNewTCB != NULL )
1439 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1441 /* Allocate space for the stack used by the task being created.
1442 * The base of the stack memory stored in the TCB so the task can
1443 * be deleted later if required. */
1444 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1446 if( pxNewTCB->pxStack == NULL )
1448 /* Could not allocate the stack. Delete the allocated TCB. */
1449 vPortFree( pxNewTCB );
1454 #else /* portSTACK_GROWTH */
1456 StackType_t * pxStack;
1458 /* Allocate space for the stack used by the task being created. */
1459 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. */
1461 if( pxStack != NULL )
1463 /* Allocate space for the TCB. */
1464 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. */
1466 if( pxNewTCB != NULL )
1468 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1470 /* Store the stack location in the TCB. */
1471 pxNewTCB->pxStack = pxStack;
1475 /* The stack cannot be used as the TCB was not created. Free
1477 vPortFreeStack( pxStack );
1485 #endif /* portSTACK_GROWTH */
1487 if( pxNewTCB != NULL )
1489 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1491 /* Tasks can be created statically or dynamically, so note this
1492 * task was created dynamically in case it is later deleted. */
1493 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1495 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1497 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1499 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1501 /* Set the task's affinity before scheduling it. */
1502 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1506 prvAddNewTaskToReadyList( pxNewTCB );
1511 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1514 traceRETURN_xTaskCreate( xReturn );
1519 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1520 /*-----------------------------------------------------------*/
1522 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1523 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1524 const uint32_t ulStackDepth,
1525 void * const pvParameters,
1526 UBaseType_t uxPriority,
1527 TaskHandle_t * const pxCreatedTask,
1529 const MemoryRegion_t * const xRegions )
1531 StackType_t * pxTopOfStack;
1534 #if ( portUSING_MPU_WRAPPERS == 1 )
1535 /* Should the task be created in privileged mode? */
1536 BaseType_t xRunPrivileged;
1538 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1540 xRunPrivileged = pdTRUE;
1544 xRunPrivileged = pdFALSE;
1546 uxPriority &= ~portPRIVILEGE_BIT;
1547 #endif /* portUSING_MPU_WRAPPERS == 1 */
1549 /* Avoid dependency on memset() if it is not required. */
1550 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1552 /* Fill the stack with a known value to assist debugging. */
1553 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1555 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1557 /* Calculate the top of stack address. This depends on whether the stack
1558 * grows from high memory to low (as per the 80x86) or vice versa.
1559 * portSTACK_GROWTH is used to make the result positive or negative as required
1561 #if ( portSTACK_GROWTH < 0 )
1563 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1564 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(). */
1566 /* Check the alignment of the calculated top of stack is correct. */
1567 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1569 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1571 /* Also record the stack's high address, which may assist
1573 pxNewTCB->pxEndOfStack = pxTopOfStack;
1575 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1577 #else /* portSTACK_GROWTH */
1579 pxTopOfStack = pxNewTCB->pxStack;
1580 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(). */
1582 /* Check the alignment of the calculated top of stack is correct. */
1583 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1585 /* The other extreme of the stack space is required if stack checking is
1587 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1589 #endif /* portSTACK_GROWTH */
1591 /* Store the task name in the TCB. */
1592 if( pcName != NULL )
1594 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1596 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1598 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1599 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1600 * string is not accessible (extremely unlikely). */
1601 if( pcName[ x ] == ( char ) 0x00 )
1607 mtCOVERAGE_TEST_MARKER();
1611 /* Ensure the name string is terminated in the case that the string length
1612 * was greater or equal to configMAX_TASK_NAME_LEN. */
1613 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1617 mtCOVERAGE_TEST_MARKER();
1620 /* This is used as an array index so must ensure it's not too large. */
1621 configASSERT( uxPriority < configMAX_PRIORITIES );
1623 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1625 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1629 mtCOVERAGE_TEST_MARKER();
1632 pxNewTCB->uxPriority = uxPriority;
1633 #if ( configUSE_MUTEXES == 1 )
1635 pxNewTCB->uxBasePriority = uxPriority;
1637 #endif /* configUSE_MUTEXES */
1639 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1640 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1642 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1643 * back to the containing TCB from a generic item in a list. */
1644 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1646 /* Event lists are always in priority order. */
1647 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. */
1648 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1650 #if ( portUSING_MPU_WRAPPERS == 1 )
1652 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1656 /* Avoid compiler warning about unreferenced parameter. */
1661 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
1663 /* Allocate and initialize memory for the task's TLS Block. */
1664 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
1668 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1670 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1674 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1676 pxNewTCB->xPreemptionDisable = 0;
1680 /* Initialize the TCB stack to look as if the task was already running,
1681 * but had been interrupted by the scheduler. The return address is set
1682 * to the start of the task function. Once the stack has been initialised
1683 * the top of stack variable is updated. */
1684 #if ( portUSING_MPU_WRAPPERS == 1 )
1686 /* If the port has capability to detect stack overflow,
1687 * pass the stack end address to the stack initialization
1688 * function as well. */
1689 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1691 #if ( portSTACK_GROWTH < 0 )
1693 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1695 #else /* portSTACK_GROWTH */
1697 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1699 #endif /* portSTACK_GROWTH */
1701 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1703 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1705 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1707 #else /* portUSING_MPU_WRAPPERS */
1709 /* If the port has capability to detect stack overflow,
1710 * pass the stack end address to the stack initialization
1711 * function as well. */
1712 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1714 #if ( portSTACK_GROWTH < 0 )
1716 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1718 #else /* portSTACK_GROWTH */
1720 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1722 #endif /* portSTACK_GROWTH */
1724 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1726 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1728 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1730 #endif /* portUSING_MPU_WRAPPERS */
1732 /* Initialize task state and task attributes. */
1733 #if ( configNUMBER_OF_CORES > 1 )
1735 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1737 /* Is this an idle task? */
1738 if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvMinimalIdleTask ) )
1740 pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
1743 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
1745 if( pxCreatedTask != NULL )
1747 /* Pass the handle out in an anonymous way. The handle can be used to
1748 * change the created task's priority, delete the created task, etc.*/
1749 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1753 mtCOVERAGE_TEST_MARKER();
1756 /*-----------------------------------------------------------*/
1758 #if ( configNUMBER_OF_CORES == 1 )
1760 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1762 /* Ensure interrupts don't access the task lists while the lists are being
1764 taskENTER_CRITICAL();
1766 uxCurrentNumberOfTasks++;
1768 if( pxCurrentTCB == NULL )
1770 /* There are no other tasks, or all the other tasks are in
1771 * the suspended state - make this the current task. */
1772 pxCurrentTCB = pxNewTCB;
1774 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1776 /* This is the first task to be created so do the preliminary
1777 * initialisation required. We will not recover if this call
1778 * fails, but we will report the failure. */
1779 prvInitialiseTaskLists();
1783 mtCOVERAGE_TEST_MARKER();
1788 /* If the scheduler is not already running, make this task the
1789 * current task if it is the highest priority task to be created
1791 if( xSchedulerRunning == pdFALSE )
1793 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
1795 pxCurrentTCB = pxNewTCB;
1799 mtCOVERAGE_TEST_MARKER();
1804 mtCOVERAGE_TEST_MARKER();
1810 #if ( configUSE_TRACE_FACILITY == 1 )
1812 /* Add a counter into the TCB for tracing only. */
1813 pxNewTCB->uxTCBNumber = uxTaskNumber;
1815 #endif /* configUSE_TRACE_FACILITY */
1816 traceTASK_CREATE( pxNewTCB );
1818 prvAddTaskToReadyList( pxNewTCB );
1820 portSETUP_TCB( pxNewTCB );
1822 taskEXIT_CRITICAL();
1824 if( xSchedulerRunning != pdFALSE )
1826 /* If the created task is of a higher priority than the current task
1827 * then it should run now. */
1828 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
1832 mtCOVERAGE_TEST_MARKER();
1836 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
1838 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1840 /* Ensure interrupts don't access the task lists while the lists are being
1842 taskENTER_CRITICAL();
1844 uxCurrentNumberOfTasks++;
1846 if( xSchedulerRunning == pdFALSE )
1848 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1850 /* This is the first task to be created so do the preliminary
1851 * initialisation required. We will not recover if this call
1852 * fails, but we will report the failure. */
1853 prvInitialiseTaskLists();
1857 mtCOVERAGE_TEST_MARKER();
1860 if( ( pxNewTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1864 /* Check if a core is free. */
1865 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
1867 if( pxCurrentTCBs[ xCoreID ] == NULL )
1869 pxNewTCB->xTaskRunState = xCoreID;
1870 pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1875 mtCOVERAGE_TEST_MARKER();
1881 mtCOVERAGE_TEST_MARKER();
1887 #if ( configUSE_TRACE_FACILITY == 1 )
1889 /* Add a counter into the TCB for tracing only. */
1890 pxNewTCB->uxTCBNumber = uxTaskNumber;
1892 #endif /* configUSE_TRACE_FACILITY */
1893 traceTASK_CREATE( pxNewTCB );
1895 prvAddTaskToReadyList( pxNewTCB );
1897 portSETUP_TCB( pxNewTCB );
1899 if( xSchedulerRunning != pdFALSE )
1901 /* If the created task is of a higher priority than another
1902 * currently running task and preemption is on then it should
1904 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
1908 mtCOVERAGE_TEST_MARKER();
1911 taskEXIT_CRITICAL();
1914 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
1915 /*-----------------------------------------------------------*/
1917 #if ( INCLUDE_vTaskDelete == 1 )
1919 void vTaskDelete( TaskHandle_t xTaskToDelete )
1923 traceENTER_vTaskDelete( xTaskToDelete );
1925 taskENTER_CRITICAL();
1927 /* If null is passed in here then it is the calling task that is
1929 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1931 /* Remove task from the ready/delayed list. */
1932 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1934 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1938 mtCOVERAGE_TEST_MARKER();
1941 /* Is the task waiting on an event also? */
1942 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1944 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1948 mtCOVERAGE_TEST_MARKER();
1951 /* Increment the uxTaskNumber also so kernel aware debuggers can
1952 * detect that the task lists need re-generating. This is done before
1953 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1957 /* If the task is running (or yielding), we must add it to the
1958 * termination list so that an idle task can delete it when it is
1959 * no longer running. */
1960 if( taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) != pdFALSE )
1962 /* A running task or a task which is scheduled to yield is being
1963 * deleted. This cannot complete when the task is still running
1964 * on a core, as a context switch to another task is required.
1965 * Place the task in the termination list. The idle task will check
1966 * the termination list and free up any memory allocated by the
1967 * scheduler for the TCB and stack of the deleted task. */
1968 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1970 /* Increment the ucTasksDeleted variable so the idle task knows
1971 * there is a task that has been deleted and that it should therefore
1972 * check the xTasksWaitingTermination list. */
1973 ++uxDeletedTasksWaitingCleanUp;
1975 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1976 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1977 traceTASK_DELETE( pxTCB );
1979 /* The pre-delete hook is primarily for the Windows simulator,
1980 * in which Windows specific clean up operations are performed,
1981 * after which it is not possible to yield away from this task -
1982 * hence xYieldPending is used to latch that a context switch is
1984 #if ( configNUMBER_OF_CORES == 1 )
1985 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
1987 portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
1992 --uxCurrentNumberOfTasks;
1993 traceTASK_DELETE( pxTCB );
1995 /* Reset the next expected unblock time in case it referred to
1996 * the task that has just been deleted. */
1997 prvResetNextTaskUnblockTime();
2001 #if ( configNUMBER_OF_CORES == 1 )
2003 taskEXIT_CRITICAL();
2005 /* If the task is not deleting itself, call prvDeleteTCB from outside of
2006 * critical section. If a task deletes itself, prvDeleteTCB is called
2007 * from prvCheckTasksWaitingTermination which is called from Idle task. */
2008 if( pxTCB != pxCurrentTCB )
2010 prvDeleteTCB( pxTCB );
2013 /* Force a reschedule if it is the currently running task that has just
2015 if( xSchedulerRunning != pdFALSE )
2017 if( pxTCB == pxCurrentTCB )
2019 configASSERT( uxSchedulerSuspended == 0 );
2020 portYIELD_WITHIN_API();
2024 mtCOVERAGE_TEST_MARKER();
2028 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2030 /* If a running task is not deleting itself, call prvDeleteTCB. If a running
2031 * task deletes itself, prvDeleteTCB is called from prvCheckTasksWaitingTermination
2032 * which is called from Idle task. */
2033 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
2035 prvDeleteTCB( pxTCB );
2038 /* Force a reschedule if the task that has just been deleted was running. */
2039 if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) )
2041 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2043 configASSERT( uxSchedulerSuspended == 0 );
2044 vTaskYieldWithinAPI();
2048 prvYieldCore( pxTCB->xTaskRunState );
2052 taskEXIT_CRITICAL();
2054 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2056 traceRETURN_vTaskDelete();
2059 #endif /* INCLUDE_vTaskDelete */
2060 /*-----------------------------------------------------------*/
2062 #if ( INCLUDE_xTaskDelayUntil == 1 )
2064 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2065 const TickType_t xTimeIncrement )
2067 TickType_t xTimeToWake;
2068 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2070 traceENTER_xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
2072 configASSERT( pxPreviousWakeTime );
2073 configASSERT( ( xTimeIncrement > 0U ) );
2077 /* Minor optimisation. The tick count cannot change in this
2079 const TickType_t xConstTickCount = xTickCount;
2081 configASSERT( uxSchedulerSuspended == 1U );
2083 /* Generate the tick time at which the task wants to wake. */
2084 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2086 if( xConstTickCount < *pxPreviousWakeTime )
2088 /* The tick count has overflowed since this function was
2089 * lasted called. In this case the only time we should ever
2090 * actually delay is if the wake time has also overflowed,
2091 * and the wake time is greater than the tick time. When this
2092 * is the case it is as if neither time had overflowed. */
2093 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2095 xShouldDelay = pdTRUE;
2099 mtCOVERAGE_TEST_MARKER();
2104 /* The tick time has not overflowed. In this case we will
2105 * delay if either the wake time has overflowed, and/or the
2106 * tick time is less than the wake time. */
2107 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2109 xShouldDelay = pdTRUE;
2113 mtCOVERAGE_TEST_MARKER();
2117 /* Update the wake time ready for the next call. */
2118 *pxPreviousWakeTime = xTimeToWake;
2120 if( xShouldDelay != pdFALSE )
2122 traceTASK_DELAY_UNTIL( xTimeToWake );
2124 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2125 * the time to wake, so subtract the current tick count. */
2126 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2130 mtCOVERAGE_TEST_MARKER();
2133 xAlreadyYielded = xTaskResumeAll();
2135 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2136 * have put ourselves to sleep. */
2137 if( xAlreadyYielded == pdFALSE )
2139 #if ( configNUMBER_OF_CORES == 1 )
2140 portYIELD_WITHIN_API();
2142 vTaskYieldWithinAPI();
2147 mtCOVERAGE_TEST_MARKER();
2150 traceRETURN_xTaskDelayUntil( xShouldDelay );
2152 return xShouldDelay;
2155 #endif /* INCLUDE_xTaskDelayUntil */
2156 /*-----------------------------------------------------------*/
2158 #if ( INCLUDE_vTaskDelay == 1 )
2160 void vTaskDelay( const TickType_t xTicksToDelay )
2162 BaseType_t xAlreadyYielded = pdFALSE;
2164 traceENTER_vTaskDelay( xTicksToDelay );
2166 /* A delay time of zero just forces a reschedule. */
2167 if( xTicksToDelay > ( TickType_t ) 0U )
2171 configASSERT( uxSchedulerSuspended == 1U );
2175 /* A task that is removed from the event list while the
2176 * scheduler is suspended will not get placed in the ready
2177 * list or removed from the blocked list until the scheduler
2180 * This task cannot be in an event list as it is the currently
2181 * executing task. */
2182 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2184 xAlreadyYielded = xTaskResumeAll();
2188 mtCOVERAGE_TEST_MARKER();
2191 /* Force a reschedule if xTaskResumeAll has not already done so, we may
2192 * have put ourselves to sleep. */
2193 if( xAlreadyYielded == pdFALSE )
2195 #if ( configNUMBER_OF_CORES == 1 )
2196 portYIELD_WITHIN_API();
2198 vTaskYieldWithinAPI();
2203 mtCOVERAGE_TEST_MARKER();
2206 traceRETURN_vTaskDelay();
2209 #endif /* INCLUDE_vTaskDelay */
2210 /*-----------------------------------------------------------*/
2212 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2214 eTaskState eTaskGetState( TaskHandle_t xTask )
2217 List_t const * pxStateList;
2218 List_t const * pxEventList;
2219 List_t const * pxDelayedList;
2220 List_t const * pxOverflowedDelayedList;
2221 const TCB_t * const pxTCB = xTask;
2223 traceENTER_eTaskGetState( xTask );
2225 configASSERT( pxTCB );
2227 #if ( configNUMBER_OF_CORES == 1 )
2228 if( pxTCB == pxCurrentTCB )
2230 /* The task calling this function is querying its own state. */
2236 taskENTER_CRITICAL();
2238 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2239 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2240 pxDelayedList = pxDelayedTaskList;
2241 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2243 taskEXIT_CRITICAL();
2245 if( pxEventList == &xPendingReadyList )
2247 /* The task has been placed on the pending ready list, so its
2248 * state is eReady regardless of what list the task's state list
2249 * item is currently placed on. */
2252 else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2254 /* The task being queried is referenced from one of the Blocked
2259 #if ( INCLUDE_vTaskSuspend == 1 )
2260 else if( pxStateList == &xSuspendedTaskList )
2262 /* The task being queried is referenced from the suspended
2263 * list. Is it genuinely suspended or is it blocked
2265 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2267 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2271 /* The task does not appear on the event list item of
2272 * and of the RTOS objects, but could still be in the
2273 * blocked state if it is waiting on its notification
2274 * rather than waiting on an object. If not, is
2276 eReturn = eSuspended;
2278 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2280 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2287 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2289 eReturn = eSuspended;
2291 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2298 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2300 #if ( INCLUDE_vTaskDelete == 1 )
2301 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2303 /* The task being queried is referenced from the deleted
2304 * tasks list, or it is not referenced from any lists at
2310 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
2312 #if ( configNUMBER_OF_CORES == 1 )
2314 /* If the task is not in any other state, it must be in the
2315 * Ready (including pending ready) state. */
2318 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2320 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2322 /* Is it actively running on a core? */
2327 /* If the task is not in any other state, it must be in the
2328 * Ready (including pending ready) state. */
2332 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2336 traceRETURN_eTaskGetState( eReturn );
2339 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2341 #endif /* INCLUDE_eTaskGetState */
2342 /*-----------------------------------------------------------*/
2344 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2346 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2348 TCB_t const * pxTCB;
2349 UBaseType_t uxReturn;
2351 traceENTER_uxTaskPriorityGet( xTask );
2353 taskENTER_CRITICAL();
2355 /* If null is passed in here then it is the priority of the task
2356 * that called uxTaskPriorityGet() that is being queried. */
2357 pxTCB = prvGetTCBFromHandle( xTask );
2358 uxReturn = pxTCB->uxPriority;
2360 taskEXIT_CRITICAL();
2362 traceRETURN_uxTaskPriorityGet( uxReturn );
2367 #endif /* INCLUDE_uxTaskPriorityGet */
2368 /*-----------------------------------------------------------*/
2370 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2372 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2374 TCB_t const * pxTCB;
2375 UBaseType_t uxReturn;
2376 UBaseType_t uxSavedInterruptStatus;
2378 traceENTER_uxTaskPriorityGetFromISR( xTask );
2380 /* RTOS ports that support interrupt nesting have the concept of a
2381 * maximum system call (or maximum API call) interrupt priority.
2382 * Interrupts that are above the maximum system call priority are keep
2383 * permanently enabled, even when the RTOS kernel is in a critical section,
2384 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2385 * is defined in FreeRTOSConfig.h then
2386 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2387 * failure if a FreeRTOS API function is called from an interrupt that has
2388 * been assigned a priority above the configured maximum system call
2389 * priority. Only FreeRTOS functions that end in FromISR can be called
2390 * from interrupts that have been assigned a priority at or (logically)
2391 * below the maximum system call interrupt priority. FreeRTOS maintains a
2392 * separate interrupt safe API to ensure interrupt entry is as fast and as
2393 * simple as possible. More information (albeit Cortex-M specific) is
2394 * provided on the following link:
2395 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2396 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2398 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
2400 /* If null is passed in here then it is the priority of the calling
2401 * task that is being queried. */
2402 pxTCB = prvGetTCBFromHandle( xTask );
2403 uxReturn = pxTCB->uxPriority;
2405 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2407 traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
2412 #endif /* INCLUDE_uxTaskPriorityGet */
2413 /*-----------------------------------------------------------*/
2415 #if ( INCLUDE_vTaskPrioritySet == 1 )
2417 void vTaskPrioritySet( TaskHandle_t xTask,
2418 UBaseType_t uxNewPriority )
2421 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2422 BaseType_t xYieldRequired = pdFALSE;
2424 traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
2426 #if ( configNUMBER_OF_CORES > 1 )
2427 BaseType_t xYieldForTask = pdFALSE;
2430 configASSERT( uxNewPriority < configMAX_PRIORITIES );
2432 /* Ensure the new priority is valid. */
2433 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2435 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2439 mtCOVERAGE_TEST_MARKER();
2442 taskENTER_CRITICAL();
2444 /* If null is passed in here then it is the priority of the calling
2445 * task that is being changed. */
2446 pxTCB = prvGetTCBFromHandle( xTask );
2448 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2450 #if ( configUSE_MUTEXES == 1 )
2452 uxCurrentBasePriority = pxTCB->uxBasePriority;
2456 uxCurrentBasePriority = pxTCB->uxPriority;
2460 if( uxCurrentBasePriority != uxNewPriority )
2462 /* The priority change may have readied a task of higher
2463 * priority than a running task. */
2464 if( uxNewPriority > uxCurrentBasePriority )
2466 #if ( configNUMBER_OF_CORES == 1 )
2468 if( pxTCB != pxCurrentTCB )
2470 /* The priority of a task other than the currently
2471 * running task is being raised. Is the priority being
2472 * raised above that of the running task? */
2473 if( uxNewPriority > pxCurrentTCB->uxPriority )
2475 xYieldRequired = pdTRUE;
2479 mtCOVERAGE_TEST_MARKER();
2484 /* The priority of the running task is being raised,
2485 * but the running task must already be the highest
2486 * priority task able to run so no yield is required. */
2489 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2491 /* The priority of a task is being raised so
2492 * perform a yield for this task later. */
2493 xYieldForTask = pdTRUE;
2495 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2497 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2499 /* Setting the priority of a running task down means
2500 * there may now be another task of higher priority that
2501 * is ready to execute. */
2502 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2503 if( pxTCB->xPreemptionDisable == pdFALSE )
2506 xYieldRequired = pdTRUE;
2511 /* Setting the priority of any other task down does not
2512 * require a yield as the running task must be above the
2513 * new priority of the task being modified. */
2516 /* Remember the ready list the task might be referenced from
2517 * before its uxPriority member is changed so the
2518 * taskRESET_READY_PRIORITY() macro can function correctly. */
2519 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2521 #if ( configUSE_MUTEXES == 1 )
2523 /* Only change the priority being used if the task is not
2524 * currently using an inherited priority or the new priority
2525 * is bigger than the inherited priority. */
2526 if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2528 pxTCB->uxPriority = uxNewPriority;
2532 mtCOVERAGE_TEST_MARKER();
2535 /* The base priority gets set whatever. */
2536 pxTCB->uxBasePriority = uxNewPriority;
2538 #else /* if ( configUSE_MUTEXES == 1 ) */
2540 pxTCB->uxPriority = uxNewPriority;
2542 #endif /* if ( configUSE_MUTEXES == 1 ) */
2544 /* Only reset the event list item value if the value is not
2545 * being used for anything else. */
2546 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2548 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. */
2552 mtCOVERAGE_TEST_MARKER();
2555 /* If the task is in the blocked or suspended list we need do
2556 * nothing more than change its priority variable. However, if
2557 * the task is in a ready list it needs to be removed and placed
2558 * in the list appropriate to its new priority. */
2559 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2561 /* The task is currently in its ready list - remove before
2562 * adding it to its new ready list. As we are in a critical
2563 * section we can do this even if the scheduler is suspended. */
2564 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2566 /* It is known that the task is in its ready list so
2567 * there is no need to check again and the port level
2568 * reset macro can be called directly. */
2569 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2573 mtCOVERAGE_TEST_MARKER();
2576 prvAddTaskToReadyList( pxTCB );
2580 #if ( configNUMBER_OF_CORES == 1 )
2582 mtCOVERAGE_TEST_MARKER();
2586 /* It's possible that xYieldForTask was already set to pdTRUE because
2587 * its priority is being raised. However, since it is not in a ready list
2588 * we don't actually need to yield for it. */
2589 xYieldForTask = pdFALSE;
2594 if( xYieldRequired != pdFALSE )
2596 /* The running task priority is set down. Request the task to yield. */
2597 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2601 #if ( configNUMBER_OF_CORES > 1 )
2602 if( xYieldForTask != pdFALSE )
2604 /* The priority of the task is being raised. If a running
2605 * task has priority lower than this task, it should yield
2607 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2610 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2612 mtCOVERAGE_TEST_MARKER();
2616 /* Remove compiler warning about unused variables when the port
2617 * optimised task selection is not being used. */
2618 ( void ) uxPriorityUsedOnEntry;
2621 taskEXIT_CRITICAL();
2623 traceRETURN_vTaskPrioritySet();
2626 #endif /* INCLUDE_vTaskPrioritySet */
2627 /*-----------------------------------------------------------*/
2629 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2630 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2631 UBaseType_t uxCoreAffinityMask )
2635 UBaseType_t uxPrevCoreAffinityMask;
2637 #if ( configUSE_PREEMPTION == 1 )
2638 UBaseType_t uxPrevNotAllowedCores;
2641 traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
2643 taskENTER_CRITICAL();
2645 pxTCB = prvGetTCBFromHandle( xTask );
2647 uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2648 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2650 if( xSchedulerRunning != pdFALSE )
2652 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2654 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2656 /* If the task can no longer run on the core it was running,
2657 * request the core to yield. */
2658 if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
2660 prvYieldCore( xCoreID );
2665 #if ( configUSE_PREEMPTION == 1 )
2667 /* Calculate the cores on which this task was not allowed to
2668 * run previously. */
2669 uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
2671 /* Does the new core mask enables this task to run on any of the
2672 * previously not allowed cores? If yes, check if this task can be
2673 * scheduled on any of those cores. */
2674 if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
2676 prvYieldForTask( pxTCB );
2679 #else /* #if( configUSE_PREEMPTION == 1 ) */
2681 mtCOVERAGE_TEST_MARKER();
2683 #endif /* #if( configUSE_PREEMPTION == 1 ) */
2687 taskEXIT_CRITICAL();
2689 traceRETURN_vTaskCoreAffinitySet();
2691 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
2692 /*-----------------------------------------------------------*/
2694 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2695 UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
2697 const TCB_t * pxTCB;
2698 UBaseType_t uxCoreAffinityMask;
2700 traceENTER_vTaskCoreAffinityGet( xTask );
2702 taskENTER_CRITICAL();
2704 pxTCB = prvGetTCBFromHandle( xTask );
2705 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2707 taskEXIT_CRITICAL();
2709 traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
2711 return uxCoreAffinityMask;
2713 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
2715 /*-----------------------------------------------------------*/
2717 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2719 void vTaskPreemptionDisable( const TaskHandle_t xTask )
2723 traceENTER_vTaskPreemptionDisable( xTask );
2725 taskENTER_CRITICAL();
2727 pxTCB = prvGetTCBFromHandle( xTask );
2729 pxTCB->xPreemptionDisable = pdTRUE;
2731 taskEXIT_CRITICAL();
2733 traceRETURN_vTaskPreemptionDisable();
2736 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
2737 /*-----------------------------------------------------------*/
2739 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2741 void vTaskPreemptionEnable( const TaskHandle_t xTask )
2746 traceENTER_vTaskPreemptionEnable( xTask );
2748 taskENTER_CRITICAL();
2750 pxTCB = prvGetTCBFromHandle( xTask );
2752 pxTCB->xPreemptionDisable = pdFALSE;
2754 if( xSchedulerRunning != pdFALSE )
2756 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2758 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2759 prvYieldCore( xCoreID );
2763 taskEXIT_CRITICAL();
2765 traceRETURN_vTaskPreemptionEnable();
2768 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
2769 /*-----------------------------------------------------------*/
2771 #if ( INCLUDE_vTaskSuspend == 1 )
2773 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2777 #if ( configNUMBER_OF_CORES > 1 )
2778 BaseType_t xTaskRunningOnCore;
2781 traceENTER_vTaskSuspend( xTaskToSuspend );
2783 taskENTER_CRITICAL();
2785 /* If null is passed in here then it is the running task that is
2786 * being suspended. */
2787 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2789 traceTASK_SUSPEND( pxTCB );
2791 #if ( configNUMBER_OF_CORES > 1 )
2792 xTaskRunningOnCore = pxTCB->xTaskRunState;
2795 /* Remove task from the ready/delayed list and place in the
2796 * suspended list. */
2797 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2799 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2803 mtCOVERAGE_TEST_MARKER();
2806 /* Is the task waiting on an event also? */
2807 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2809 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2813 mtCOVERAGE_TEST_MARKER();
2816 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2818 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2822 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2824 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2826 /* The task was blocked to wait for a notification, but is
2827 * now suspended, so no notification was received. */
2828 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2832 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2835 #if ( configNUMBER_OF_CORES == 1 )
2837 taskEXIT_CRITICAL();
2839 if( xSchedulerRunning != pdFALSE )
2841 /* Reset the next expected unblock time in case it referred to the
2842 * task that is now in the Suspended state. */
2843 taskENTER_CRITICAL();
2845 prvResetNextTaskUnblockTime();
2847 taskEXIT_CRITICAL();
2851 mtCOVERAGE_TEST_MARKER();
2854 if( pxTCB == pxCurrentTCB )
2856 if( xSchedulerRunning != pdFALSE )
2858 /* The current task has just been suspended. */
2859 configASSERT( uxSchedulerSuspended == 0 );
2860 portYIELD_WITHIN_API();
2864 /* The scheduler is not running, but the task that was pointed
2865 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
2866 * must be adjusted to point to a different task. */
2867 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
2869 /* No other tasks are ready, so set pxCurrentTCB back to
2870 * NULL so when the next task is created pxCurrentTCB will
2871 * be set to point to it no matter what its relative priority
2873 pxCurrentTCB = NULL;
2877 vTaskSwitchContext();
2883 mtCOVERAGE_TEST_MARKER();
2886 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2888 if( xSchedulerRunning != pdFALSE )
2890 /* Reset the next expected unblock time in case it referred to the
2891 * task that is now in the Suspended state. */
2892 prvResetNextTaskUnblockTime();
2896 mtCOVERAGE_TEST_MARKER();
2899 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2901 if( xSchedulerRunning != pdFALSE )
2903 if( xTaskRunningOnCore == ( BaseType_t ) portGET_CORE_ID() )
2905 /* The current task has just been suspended. */
2906 configASSERT( uxSchedulerSuspended == 0 );
2907 vTaskYieldWithinAPI();
2911 prvYieldCore( xTaskRunningOnCore );
2916 /* This code path is not possible because only Idle tasks are
2917 * assigned a core before the scheduler is started ( i.e.
2918 * taskTASK_IS_RUNNING is only true for idle tasks before
2919 * the scheduler is started ) and idle tasks cannot be
2921 mtCOVERAGE_TEST_MARKER();
2926 mtCOVERAGE_TEST_MARKER();
2929 taskEXIT_CRITICAL();
2931 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2933 traceRETURN_vTaskSuspend();
2936 #endif /* INCLUDE_vTaskSuspend */
2937 /*-----------------------------------------------------------*/
2939 #if ( INCLUDE_vTaskSuspend == 1 )
2941 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
2943 BaseType_t xReturn = pdFALSE;
2944 const TCB_t * const pxTCB = xTask;
2946 /* Accesses xPendingReadyList so must be called from a critical
2949 /* It does not make sense to check if the calling task is suspended. */
2950 configASSERT( xTask );
2952 /* Is the task being resumed actually in the suspended list? */
2953 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
2955 /* Has the task already been resumed from within an ISR? */
2956 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
2958 /* Is it in the suspended list because it is in the Suspended
2959 * state, or because is is blocked with no timeout? */
2960 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
2966 mtCOVERAGE_TEST_MARKER();
2971 mtCOVERAGE_TEST_MARKER();
2976 mtCOVERAGE_TEST_MARKER();
2980 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2982 #endif /* INCLUDE_vTaskSuspend */
2983 /*-----------------------------------------------------------*/
2985 #if ( INCLUDE_vTaskSuspend == 1 )
2987 void vTaskResume( TaskHandle_t xTaskToResume )
2989 TCB_t * const pxTCB = xTaskToResume;
2991 traceENTER_vTaskResume( xTaskToResume );
2993 /* It does not make sense to resume the calling task. */
2994 configASSERT( xTaskToResume );
2996 #if ( configNUMBER_OF_CORES == 1 )
2998 /* The parameter cannot be NULL as it is impossible to resume the
2999 * currently executing task. */
3000 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
3003 /* The parameter cannot be NULL as it is impossible to resume the
3004 * currently executing task. It is also impossible to resume a task
3005 * that is actively running on another core but it is not safe
3006 * to check their run state here. Therefore, we get into a critical
3007 * section and check if the task is actually suspended or not. */
3011 taskENTER_CRITICAL();
3013 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3015 traceTASK_RESUME( pxTCB );
3017 /* The ready list can be accessed even if the scheduler is
3018 * suspended because this is inside a critical section. */
3019 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3020 prvAddTaskToReadyList( pxTCB );
3022 /* This yield may not cause the task just resumed to run,
3023 * but will leave the lists in the correct state for the
3025 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
3029 mtCOVERAGE_TEST_MARKER();
3032 taskEXIT_CRITICAL();
3036 mtCOVERAGE_TEST_MARKER();
3039 traceRETURN_vTaskResume();
3042 #endif /* INCLUDE_vTaskSuspend */
3044 /*-----------------------------------------------------------*/
3046 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
3048 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
3050 BaseType_t xYieldRequired = pdFALSE;
3051 TCB_t * const pxTCB = xTaskToResume;
3052 UBaseType_t uxSavedInterruptStatus;
3054 traceENTER_xTaskResumeFromISR( xTaskToResume );
3056 configASSERT( xTaskToResume );
3058 /* RTOS ports that support interrupt nesting have the concept of a
3059 * maximum system call (or maximum API call) interrupt priority.
3060 * Interrupts that are above the maximum system call priority are keep
3061 * permanently enabled, even when the RTOS kernel is in a critical section,
3062 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
3063 * is defined in FreeRTOSConfig.h then
3064 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3065 * failure if a FreeRTOS API function is called from an interrupt that has
3066 * been assigned a priority above the configured maximum system call
3067 * priority. Only FreeRTOS functions that end in FromISR can be called
3068 * from interrupts that have been assigned a priority at or (logically)
3069 * below the maximum system call interrupt priority. FreeRTOS maintains a
3070 * separate interrupt safe API to ensure interrupt entry is as fast and as
3071 * simple as possible. More information (albeit Cortex-M specific) is
3072 * provided on the following link:
3073 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3074 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3076 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3078 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3080 traceTASK_RESUME_FROM_ISR( pxTCB );
3082 /* Check the ready lists can be accessed. */
3083 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3085 #if ( configNUMBER_OF_CORES == 1 )
3087 /* Ready lists can be accessed so move the task from the
3088 * suspended list to the ready list directly. */
3089 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3091 xYieldRequired = pdTRUE;
3093 /* Mark that a yield is pending in case the user is not
3094 * using the return value to initiate a context switch
3095 * from the ISR using portYIELD_FROM_ISR. */
3096 xYieldPendings[ 0 ] = pdTRUE;
3100 mtCOVERAGE_TEST_MARKER();
3103 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3105 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3106 prvAddTaskToReadyList( pxTCB );
3110 /* The delayed or ready lists cannot be accessed so the task
3111 * is held in the pending ready list until the scheduler is
3113 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3116 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3118 prvYieldForTask( pxTCB );
3120 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3122 xYieldRequired = pdTRUE;
3125 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3129 mtCOVERAGE_TEST_MARKER();
3132 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3134 traceRETURN_xTaskResumeFromISR( xYieldRequired );
3136 return xYieldRequired;
3139 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3140 /*-----------------------------------------------------------*/
3142 static BaseType_t prvCreateIdleTasks( void )
3144 BaseType_t xReturn = pdPASS;
3146 #if ( configNUMBER_OF_CORES == 1 )
3148 /* Add the idle task at the lowest priority. */
3149 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3151 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3152 StackType_t * pxIdleTaskStackBuffer = NULL;
3153 uint32_t ulIdleTaskStackSize;
3155 /* The Idle task is created using user provided RAM - obtain the
3156 * address of the RAM then create the idle task. */
3157 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3158 xIdleTaskHandles[ 0 ] = xTaskCreateStatic( prvIdleTask,
3159 configIDLE_TASK_NAME,
3160 ulIdleTaskStackSize,
3161 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3162 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3163 pxIdleTaskStackBuffer,
3164 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3166 if( xIdleTaskHandles[ 0 ] != NULL )
3175 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3177 /* The Idle task is being created using dynamically allocated RAM. */
3178 xReturn = xTaskCreate( prvIdleTask,
3179 configIDLE_TASK_NAME,
3180 configMINIMAL_STACK_SIZE,
3182 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3183 &xIdleTaskHandles[ 0 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3185 #endif /* configSUPPORT_STATIC_ALLOCATION */
3187 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3190 char cIdleName[ configMAX_TASK_NAME_LEN ];
3192 /* Add each idle task at the lowest priority. */
3193 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3197 if( xReturn == pdFAIL )
3203 mtCOVERAGE_TEST_MARKER();
3206 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3208 cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
3210 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3211 * configMAX_TASK_NAME_LEN characters just in case the memory after the
3212 * string is not accessible (extremely unlikely). */
3213 if( cIdleName[ x ] == ( char ) 0x00 )
3219 mtCOVERAGE_TEST_MARKER();
3223 /* Append the idle task number to the end of the name if there is space. */
3224 if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3226 cIdleName[ x ] = ( char ) ( xCoreID + '0' );
3229 /* And append a null character if there is space. */
3230 if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3232 cIdleName[ x ] = '\0';
3236 mtCOVERAGE_TEST_MARKER();
3241 mtCOVERAGE_TEST_MARKER();
3244 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3248 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3249 StackType_t * pxIdleTaskStackBuffer = NULL;
3250 uint32_t ulIdleTaskStackSize;
3252 /* The Idle task is created using user provided RAM - obtain the
3253 * address of the RAM then create the idle task. */
3254 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3255 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
3257 ulIdleTaskStackSize,
3258 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3259 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3260 pxIdleTaskStackBuffer,
3261 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3265 xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( prvMinimalIdleTask,
3267 configMINIMAL_STACK_SIZE,
3268 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
3269 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3270 xIdleTaskStackBuffers[ xCoreID - 1 ],
3271 &xIdleTCBBuffers[ xCoreID - 1 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3274 if( xIdleTaskHandles[ xCoreID ] != NULL )
3283 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3287 /* The Idle task is being created using dynamically allocated RAM. */
3288 xReturn = xTaskCreate( prvIdleTask,
3290 configMINIMAL_STACK_SIZE,
3292 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3293 &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3297 xReturn = xTaskCreate( prvMinimalIdleTask,
3299 configMINIMAL_STACK_SIZE,
3301 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3302 &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3305 #endif /* configSUPPORT_STATIC_ALLOCATION */
3308 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3313 /*-----------------------------------------------------------*/
3315 void vTaskStartScheduler( void )
3319 traceENTER_vTaskStartScheduler();
3321 #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3323 /* Sanity check that the UBaseType_t must have greater than or equal to
3324 * the number of bits as confNUMBER_OF_CORES. */
3325 configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3327 #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3329 xReturn = prvCreateIdleTasks();
3331 #if ( configUSE_TIMERS == 1 )
3333 if( xReturn == pdPASS )
3335 xReturn = xTimerCreateTimerTask();
3339 mtCOVERAGE_TEST_MARKER();
3342 #endif /* configUSE_TIMERS */
3344 if( xReturn == pdPASS )
3346 /* freertos_tasks_c_additions_init() should only be called if the user
3347 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3348 * the only macro called by the function. */
3349 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3351 freertos_tasks_c_additions_init();
3355 /* Interrupts are turned off here, to ensure a tick does not occur
3356 * before or during the call to xPortStartScheduler(). The stacks of
3357 * the created tasks contain a status word with interrupts switched on
3358 * so interrupts will automatically get re-enabled when the first task
3360 portDISABLE_INTERRUPTS();
3362 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3364 /* Switch C-Runtime's TLS Block to point to the TLS
3365 * block specific to the task that will run first. */
3366 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3370 xNextTaskUnblockTime = portMAX_DELAY;
3371 xSchedulerRunning = pdTRUE;
3372 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3374 /* If configGENERATE_RUN_TIME_STATS is defined then the following
3375 * macro must be defined to configure the timer/counter used to generate
3376 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
3377 * is set to 0 and the following line fails to build then ensure you do not
3378 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3379 * FreeRTOSConfig.h file. */
3380 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3382 traceTASK_SWITCHED_IN();
3384 /* Setting up the timer tick is hardware specific and thus in the
3385 * portable interface. */
3386 xPortStartScheduler();
3388 /* In most cases, xPortStartScheduler() will not return. If it
3389 * returns pdTRUE then there was not enough heap memory available
3390 * to create either the Idle or the Timer task. If it returned
3391 * pdFALSE, then the application called xTaskEndScheduler().
3392 * Most ports don't implement xTaskEndScheduler() as there is
3393 * nothing to return to. */
3397 /* This line will only be reached if the kernel could not be started,
3398 * because there was not enough FreeRTOS heap to create the idle task
3399 * or the timer task. */
3400 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3403 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3404 * meaning xIdleTaskHandles are not used anywhere else. */
3405 ( void ) xIdleTaskHandles;
3407 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3408 * from getting optimized out as it is no longer used by the kernel. */
3409 ( void ) uxTopUsedPriority;
3411 traceRETURN_vTaskStartScheduler();
3413 /*-----------------------------------------------------------*/
3415 void vTaskEndScheduler( void )
3417 traceENTER_vTaskEndScheduler();
3419 /* Stop the scheduler interrupts and call the portable scheduler end
3420 * routine so the original ISRs can be restored if necessary. The port
3421 * layer must ensure interrupts enable bit is left in the correct state. */
3422 portDISABLE_INTERRUPTS();
3423 xSchedulerRunning = pdFALSE;
3424 vPortEndScheduler();
3426 traceRETURN_vTaskEndScheduler();
3428 /*----------------------------------------------------------*/
3430 void vTaskSuspendAll( void )
3432 traceENTER_vTaskSuspendAll();
3434 #if ( configNUMBER_OF_CORES == 1 )
3436 /* A critical section is not required as the variable is of type
3437 * BaseType_t. Please read Richard Barry's reply in the following link to a
3438 * post in the FreeRTOS support forum before reporting this as a bug! -
3439 * https://goo.gl/wu4acr */
3441 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3442 * do not otherwise exhibit real time behaviour. */
3443 portSOFTWARE_BARRIER();
3445 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3446 * is used to allow calls to vTaskSuspendAll() to nest. */
3447 ++uxSchedulerSuspended;
3449 /* Enforces ordering for ports and optimised compilers that may otherwise place
3450 * the above increment elsewhere. */
3451 portMEMORY_BARRIER();
3453 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3455 UBaseType_t ulState;
3457 /* This must only be called from within a task. */
3458 portASSERT_IF_IN_ISR();
3460 if( xSchedulerRunning != pdFALSE )
3462 /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3463 * We must disable interrupts before we grab the locks in the event that this task is
3464 * interrupted and switches context before incrementing uxSchedulerSuspended.
3465 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3466 * uxSchedulerSuspended since that will prevent context switches. */
3467 ulState = portSET_INTERRUPT_MASK();
3469 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3470 * do not otherwise exhibit real time behaviour. */
3471 portSOFTWARE_BARRIER();
3473 portGET_TASK_LOCK();
3475 /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3476 * purpose is to prevent altering the variable when fromISR APIs are readying
3478 if( uxSchedulerSuspended == 0U )
3480 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
3482 prvCheckForRunStateChange();
3486 mtCOVERAGE_TEST_MARKER();
3491 mtCOVERAGE_TEST_MARKER();
3496 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3497 * is used to allow calls to vTaskSuspendAll() to nest. */
3498 ++uxSchedulerSuspended;
3499 portRELEASE_ISR_LOCK();
3501 portCLEAR_INTERRUPT_MASK( ulState );
3505 mtCOVERAGE_TEST_MARKER();
3508 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3510 traceRETURN_vTaskSuspendAll();
3513 /*----------------------------------------------------------*/
3515 #if ( configUSE_TICKLESS_IDLE != 0 )
3517 static TickType_t prvGetExpectedIdleTime( void )
3520 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
3522 /* uxHigherPriorityReadyTasks takes care of the case where
3523 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3524 * task that are in the Ready state, even though the idle task is
3526 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3528 if( uxTopReadyPriority > tskIDLE_PRIORITY )
3530 uxHigherPriorityReadyTasks = pdTRUE;
3535 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3537 /* When port optimised task selection is used the uxTopReadyPriority
3538 * variable is used as a bit map. If bits other than the least
3539 * significant bit are set then there are tasks that have a priority
3540 * above the idle priority that are in the Ready state. This takes
3541 * care of the case where the co-operative scheduler is in use. */
3542 if( uxTopReadyPriority > uxLeastSignificantBit )
3544 uxHigherPriorityReadyTasks = pdTRUE;
3547 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3549 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3553 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
3555 /* There are other idle priority tasks in the ready state. If
3556 * time slicing is used then the very next tick interrupt must be
3560 else if( uxHigherPriorityReadyTasks != pdFALSE )
3562 /* There are tasks in the Ready state that have a priority above the
3563 * idle priority. This path can only be reached if
3564 * configUSE_PREEMPTION is 0. */
3569 xReturn = xNextTaskUnblockTime - xTickCount;
3575 #endif /* configUSE_TICKLESS_IDLE */
3576 /*----------------------------------------------------------*/
3578 BaseType_t xTaskResumeAll( void )
3580 TCB_t * pxTCB = NULL;
3581 BaseType_t xAlreadyYielded = pdFALSE;
3583 traceENTER_xTaskResumeAll();
3585 #if ( configNUMBER_OF_CORES > 1 )
3586 if( xSchedulerRunning != pdFALSE )
3589 /* It is possible that an ISR caused a task to be removed from an event
3590 * list while the scheduler was suspended. If this was the case then the
3591 * removed task will have been added to the xPendingReadyList. Once the
3592 * scheduler has been resumed it is safe to move all the pending ready
3593 * tasks from this list into their appropriate ready list. */
3594 taskENTER_CRITICAL();
3597 xCoreID = ( BaseType_t ) portGET_CORE_ID();
3599 /* If uxSchedulerSuspended is zero then this function does not match a
3600 * previous call to vTaskSuspendAll(). */
3601 configASSERT( uxSchedulerSuspended != 0U );
3603 --uxSchedulerSuspended;
3604 portRELEASE_TASK_LOCK();
3606 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3608 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3610 /* Move any readied tasks from the pending list into the
3611 * appropriate ready list. */
3612 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3614 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. */
3615 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
3616 portMEMORY_BARRIER();
3617 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
3618 prvAddTaskToReadyList( pxTCB );
3620 #if ( configNUMBER_OF_CORES == 1 )
3622 /* If the moved task has a priority higher than the current
3623 * task then a yield must be performed. */
3624 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3626 xYieldPendings[ xCoreID ] = pdTRUE;
3630 mtCOVERAGE_TEST_MARKER();
3633 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3635 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
3636 * If the current core yielded then vTaskSwitchContext() has already been called
3637 * which sets xYieldPendings for the current core to pdTRUE. */
3639 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3644 /* A task was unblocked while the scheduler was suspended,
3645 * which may have prevented the next unblock time from being
3646 * re-calculated, in which case re-calculate it now. Mainly
3647 * important for low power tickless implementations, where
3648 * this can prevent an unnecessary exit from low power
3650 prvResetNextTaskUnblockTime();
3653 /* If any ticks occurred while the scheduler was suspended then
3654 * they should be processed now. This ensures the tick count does
3655 * not slip, and that any delayed tasks are resumed at the correct
3658 * It should be safe to call xTaskIncrementTick here from any core
3659 * since we are in a critical section and xTaskIncrementTick itself
3660 * protects itself within a critical section. Suspending the scheduler
3661 * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
3663 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3665 if( xPendedCounts > ( TickType_t ) 0U )
3669 if( xTaskIncrementTick() != pdFALSE )
3671 /* Other cores are interrupted from
3672 * within xTaskIncrementTick(). */
3673 xYieldPendings[ xCoreID ] = pdTRUE;
3677 mtCOVERAGE_TEST_MARKER();
3681 } while( xPendedCounts > ( TickType_t ) 0U );
3687 mtCOVERAGE_TEST_MARKER();
3691 if( xYieldPendings[ xCoreID ] != pdFALSE )
3693 #if ( configUSE_PREEMPTION != 0 )
3695 xAlreadyYielded = pdTRUE;
3697 #endif /* #if ( configUSE_PREEMPTION != 0 ) */
3699 #if ( configNUMBER_OF_CORES == 1 )
3701 taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
3703 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3707 mtCOVERAGE_TEST_MARKER();
3713 mtCOVERAGE_TEST_MARKER();
3716 taskEXIT_CRITICAL();
3719 traceRETURN_xTaskResumeAll( xAlreadyYielded );
3721 return xAlreadyYielded;
3723 /*-----------------------------------------------------------*/
3725 TickType_t xTaskGetTickCount( void )
3729 traceENTER_xTaskGetTickCount();
3731 /* Critical section required if running on a 16 bit processor. */
3732 portTICK_TYPE_ENTER_CRITICAL();
3734 xTicks = xTickCount;
3736 portTICK_TYPE_EXIT_CRITICAL();
3738 traceRETURN_xTaskGetTickCount( xTicks );
3742 /*-----------------------------------------------------------*/
3744 TickType_t xTaskGetTickCountFromISR( void )
3747 UBaseType_t uxSavedInterruptStatus;
3749 traceENTER_xTaskGetTickCountFromISR();
3751 /* RTOS ports that support interrupt nesting have the concept of a maximum
3752 * system call (or maximum API call) interrupt priority. Interrupts that are
3753 * above the maximum system call priority are kept permanently enabled, even
3754 * when the RTOS kernel is in a critical section, but cannot make any calls to
3755 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
3756 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3757 * failure if a FreeRTOS API function is called from an interrupt that has been
3758 * assigned a priority above the configured maximum system call priority.
3759 * Only FreeRTOS functions that end in FromISR can be called from interrupts
3760 * that have been assigned a priority at or (logically) below the maximum
3761 * system call interrupt priority. FreeRTOS maintains a separate interrupt
3762 * safe API to ensure interrupt entry is as fast and as simple as possible.
3763 * More information (albeit Cortex-M specific) is provided on the following
3764 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3765 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3767 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
3769 xReturn = xTickCount;
3771 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3773 traceRETURN_xTaskGetTickCountFromISR( xReturn );
3777 /*-----------------------------------------------------------*/
3779 UBaseType_t uxTaskGetNumberOfTasks( void )
3781 traceENTER_uxTaskGetNumberOfTasks();
3783 /* A critical section is not required because the variables are of type
3785 traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
3787 return uxCurrentNumberOfTasks;
3789 /*-----------------------------------------------------------*/
3791 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3795 traceENTER_pcTaskGetName( xTaskToQuery );
3797 /* If null is passed in here then the name of the calling task is being
3799 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3800 configASSERT( pxTCB );
3802 traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
3804 return &( pxTCB->pcTaskName[ 0 ] );
3806 /*-----------------------------------------------------------*/
3808 #if ( INCLUDE_xTaskGetHandle == 1 )
3810 #if ( configNUMBER_OF_CORES == 1 )
3811 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3812 const char pcNameToQuery[] )
3816 TCB_t * pxReturn = NULL;
3819 BaseType_t xBreakLoop;
3821 /* This function is called with the scheduler suspended. */
3823 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3825 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. */
3829 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. */
3831 /* Check each character in the name looking for a match or
3833 xBreakLoop = pdFALSE;
3835 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3837 cNextChar = pxNextTCB->pcTaskName[ x ];
3839 if( cNextChar != pcNameToQuery[ x ] )
3841 /* Characters didn't match. */
3842 xBreakLoop = pdTRUE;
3844 else if( cNextChar == ( char ) 0x00 )
3846 /* Both strings terminated, a match must have been
3848 pxReturn = pxNextTCB;
3849 xBreakLoop = pdTRUE;
3853 mtCOVERAGE_TEST_MARKER();
3856 if( xBreakLoop != pdFALSE )
3862 if( pxReturn != NULL )
3864 /* The handle has been found. */
3867 } while( pxNextTCB != pxFirstTCB );
3871 mtCOVERAGE_TEST_MARKER();
3876 #else /* if ( configNUMBER_OF_CORES == 1 ) */
3877 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3878 const char pcNameToQuery[] )
3880 TCB_t * pxReturn = NULL;
3883 BaseType_t xBreakLoop;
3884 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
3885 ListItem_t * pxIterator;
3887 /* This function is called with the scheduler suspended. */
3889 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3891 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
3893 TCB_t * pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
3895 /* Check each character in the name looking for a match or
3897 xBreakLoop = pdFALSE;
3899 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3901 cNextChar = pxTCB->pcTaskName[ x ];
3903 if( cNextChar != pcNameToQuery[ x ] )
3905 /* Characters didn't match. */
3906 xBreakLoop = pdTRUE;
3908 else if( cNextChar == ( char ) 0x00 )
3910 /* Both strings terminated, a match must have been
3913 xBreakLoop = pdTRUE;
3917 mtCOVERAGE_TEST_MARKER();
3920 if( xBreakLoop != pdFALSE )
3926 if( pxReturn != NULL )
3928 /* The handle has been found. */
3935 mtCOVERAGE_TEST_MARKER();
3940 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3942 #endif /* INCLUDE_xTaskGetHandle */
3943 /*-----------------------------------------------------------*/
3945 #if ( INCLUDE_xTaskGetHandle == 1 )
3947 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3949 UBaseType_t uxQueue = configMAX_PRIORITIES;
3952 traceENTER_xTaskGetHandle( pcNameToQuery );
3954 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
3955 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
3959 /* Search the ready lists. */
3963 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
3967 /* Found the handle. */
3970 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3972 /* Search the delayed lists. */
3975 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
3980 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
3983 #if ( INCLUDE_vTaskSuspend == 1 )
3987 /* Search the suspended list. */
3988 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
3993 #if ( INCLUDE_vTaskDelete == 1 )
3997 /* Search the deleted list. */
3998 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
4003 ( void ) xTaskResumeAll();
4005 traceRETURN_xTaskGetHandle( pxTCB );
4010 #endif /* INCLUDE_xTaskGetHandle */
4011 /*-----------------------------------------------------------*/
4013 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4015 BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4016 StackType_t ** ppuxStackBuffer,
4017 StaticTask_t ** ppxTaskBuffer )
4022 traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4024 configASSERT( ppuxStackBuffer != NULL );
4025 configASSERT( ppxTaskBuffer != NULL );
4027 pxTCB = prvGetTCBFromHandle( xTask );
4029 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4031 if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4033 *ppuxStackBuffer = pxTCB->pxStack;
4034 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4037 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4039 *ppuxStackBuffer = pxTCB->pxStack;
4040 *ppxTaskBuffer = NULL;
4048 #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4050 *ppuxStackBuffer = pxTCB->pxStack;
4051 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4054 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4056 traceRETURN_xTaskGetStaticBuffers( xReturn );
4061 #endif /* configSUPPORT_STATIC_ALLOCATION */
4062 /*-----------------------------------------------------------*/
4064 #if ( configUSE_TRACE_FACILITY == 1 )
4066 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4067 const UBaseType_t uxArraySize,
4068 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4070 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4072 traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4076 /* Is there a space in the array for each task in the system? */
4077 if( uxArraySize >= uxCurrentNumberOfTasks )
4079 /* Fill in an TaskStatus_t structure with information on each
4080 * task in the Ready state. */
4084 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4085 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4087 /* Fill in an TaskStatus_t structure with information on each
4088 * task in the Blocked state. */
4089 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4090 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4092 #if ( INCLUDE_vTaskDelete == 1 )
4094 /* Fill in an TaskStatus_t structure with information on
4095 * each task that has been deleted but not yet cleaned up. */
4096 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4100 #if ( INCLUDE_vTaskSuspend == 1 )
4102 /* Fill in an TaskStatus_t structure with information on
4103 * each task in the Suspended state. */
4104 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4108 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4110 if( pulTotalRunTime != NULL )
4112 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4113 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4115 *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4119 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4121 if( pulTotalRunTime != NULL )
4123 *pulTotalRunTime = 0;
4126 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4130 mtCOVERAGE_TEST_MARKER();
4133 ( void ) xTaskResumeAll();
4135 traceRETURN_uxTaskGetSystemState( uxTask );
4140 #endif /* configUSE_TRACE_FACILITY */
4141 /*----------------------------------------------------------*/
4143 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4145 /* SMP_TODO : This function returns only idle task handle for core 0.
4146 * Consider to add another function to return the idle task handles. */
4147 TaskHandle_t xTaskGetIdleTaskHandle( void )
4149 traceENTER_xTaskGetIdleTaskHandle();
4151 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4152 * started, then xIdleTaskHandles will be NULL. */
4153 configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4155 traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4157 return xIdleTaskHandles[ 0 ];
4160 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4161 /*----------------------------------------------------------*/
4163 /* This conditional compilation should use inequality to 0, not equality to 1.
4164 * This is to ensure vTaskStepTick() is available when user defined low power mode
4165 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4167 #if ( configUSE_TICKLESS_IDLE != 0 )
4169 void vTaskStepTick( TickType_t xTicksToJump )
4171 traceENTER_vTaskStepTick( xTicksToJump );
4173 /* Correct the tick count value after a period during which the tick
4174 * was suppressed. Note this does *not* call the tick hook function for
4175 * each stepped tick. */
4176 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
4178 if( ( xTickCount + xTicksToJump ) == xNextTaskUnblockTime )
4180 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4181 * xTaskIncrementTick() when the scheduler resumes. This ensures
4182 * that any delayed tasks are resumed at the correct time. */
4183 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4184 configASSERT( xTicksToJump != ( TickType_t ) 0 );
4186 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4187 taskENTER_CRITICAL();
4191 taskEXIT_CRITICAL();
4196 mtCOVERAGE_TEST_MARKER();
4199 xTickCount += xTicksToJump;
4201 traceINCREASE_TICK_COUNT( xTicksToJump );
4202 traceRETURN_vTaskStepTick();
4205 #endif /* configUSE_TICKLESS_IDLE */
4206 /*----------------------------------------------------------*/
4208 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4210 BaseType_t xYieldOccurred;
4212 traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4214 /* Must not be called with the scheduler suspended as the implementation
4215 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4216 configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4218 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4219 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4222 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4223 taskENTER_CRITICAL();
4225 xPendedTicks += xTicksToCatchUp;
4227 taskEXIT_CRITICAL();
4228 xYieldOccurred = xTaskResumeAll();
4230 traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4232 return xYieldOccurred;
4234 /*----------------------------------------------------------*/
4236 #if ( INCLUDE_xTaskAbortDelay == 1 )
4238 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4240 TCB_t * pxTCB = xTask;
4243 traceENTER_xTaskAbortDelay( xTask );
4245 configASSERT( pxTCB );
4249 /* A task can only be prematurely removed from the Blocked state if
4250 * it is actually in the Blocked state. */
4251 if( eTaskGetState( xTask ) == eBlocked )
4255 /* Remove the reference to the task from the blocked list. An
4256 * interrupt won't touch the xStateListItem because the
4257 * scheduler is suspended. */
4258 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4260 /* Is the task waiting on an event also? If so remove it from
4261 * the event list too. Interrupts can touch the event list item,
4262 * even though the scheduler is suspended, so a critical section
4264 taskENTER_CRITICAL();
4266 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4268 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4270 /* This lets the task know it was forcibly removed from the
4271 * blocked state so it should not re-evaluate its block time and
4272 * then block again. */
4273 pxTCB->ucDelayAborted = pdTRUE;
4277 mtCOVERAGE_TEST_MARKER();
4280 taskEXIT_CRITICAL();
4282 /* Place the unblocked task into the appropriate ready list. */
4283 prvAddTaskToReadyList( pxTCB );
4285 /* A task being unblocked cannot cause an immediate context
4286 * switch if preemption is turned off. */
4287 #if ( configUSE_PREEMPTION == 1 )
4289 #if ( configNUMBER_OF_CORES == 1 )
4291 /* Preemption is on, but a context switch should only be
4292 * performed if the unblocked task has a priority that is
4293 * higher than the currently executing task. */
4294 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4296 /* Pend the yield to be performed when the scheduler
4297 * is unsuspended. */
4298 xYieldPendings[ 0 ] = pdTRUE;
4302 mtCOVERAGE_TEST_MARKER();
4305 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4307 taskENTER_CRITICAL();
4309 prvYieldForTask( pxTCB );
4311 taskEXIT_CRITICAL();
4313 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4315 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4322 ( void ) xTaskResumeAll();
4324 traceRETURN_xTaskAbortDelay( xReturn );
4329 #endif /* INCLUDE_xTaskAbortDelay */
4330 /*----------------------------------------------------------*/
4332 BaseType_t xTaskIncrementTick( void )
4335 TickType_t xItemValue;
4336 BaseType_t xSwitchRequired = pdFALSE;
4338 traceENTER_xTaskIncrementTick();
4340 #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4341 BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4342 #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4344 /* Called by the portable layer each time a tick interrupt occurs.
4345 * Increments the tick then checks to see if the new tick value will cause any
4346 * tasks to be unblocked. */
4347 traceTASK_INCREMENT_TICK( xTickCount );
4349 /* Tick increment should occur on every kernel timer event. Core 0 has the
4350 * responsibility to increment the tick, or increment the pended ticks if the
4351 * scheduler is suspended. If pended ticks is greater than zero, the core that
4352 * calls xTaskResumeAll has the responsibility to increment the tick. */
4353 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4355 /* Minor optimisation. The tick count cannot change in this
4357 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4359 /* Increment the RTOS tick, switching the delayed and overflowed
4360 * delayed lists if it wraps to 0. */
4361 xTickCount = xConstTickCount;
4363 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
4365 taskSWITCH_DELAYED_LISTS();
4369 mtCOVERAGE_TEST_MARKER();
4372 /* See if this tick has made a timeout expire. Tasks are stored in
4373 * the queue in the order of their wake time - meaning once one task
4374 * has been found whose block time has not expired there is no need to
4375 * look any further down the list. */
4376 if( xConstTickCount >= xNextTaskUnblockTime )
4380 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4382 /* The delayed list is empty. Set xNextTaskUnblockTime
4383 * to the maximum possible value so it is extremely
4385 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4386 * next time through. */
4387 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4392 /* The delayed list is not empty, get the value of the
4393 * item at the head of the delayed list. This is the time
4394 * at which the task at the head of the delayed list must
4395 * be removed from the Blocked state. */
4396 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. */
4397 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4399 if( xConstTickCount < xItemValue )
4401 /* It is not time to unblock this item yet, but the
4402 * item value is the time at which the task at the head
4403 * of the blocked list must be removed from the Blocked
4404 * state - so record the item value in
4405 * xNextTaskUnblockTime. */
4406 xNextTaskUnblockTime = xItemValue;
4407 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
4411 mtCOVERAGE_TEST_MARKER();
4414 /* It is time to remove the item from the Blocked state. */
4415 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4417 /* Is the task waiting on an event also? If so remove
4418 * it from the event list. */
4419 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4421 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4425 mtCOVERAGE_TEST_MARKER();
4428 /* Place the unblocked task into the appropriate ready
4430 prvAddTaskToReadyList( pxTCB );
4432 /* A task being unblocked cannot cause an immediate
4433 * context switch if preemption is turned off. */
4434 #if ( configUSE_PREEMPTION == 1 )
4436 #if ( configNUMBER_OF_CORES == 1 )
4438 /* Preemption is on, but a context switch should
4439 * only be performed if the unblocked task's
4440 * priority is higher than the currently executing
4442 * The case of equal priority tasks sharing
4443 * processing time (which happens when both
4444 * preemption and time slicing are on) is
4446 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4448 xSwitchRequired = pdTRUE;
4452 mtCOVERAGE_TEST_MARKER();
4455 #else /* #if( configNUMBER_OF_CORES == 1 ) */
4457 prvYieldForTask( pxTCB );
4459 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4461 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4466 /* Tasks of equal priority to the currently running task will share
4467 * processing time (time slice) if preemption is on, and the application
4468 * writer has not explicitly turned time slicing off. */
4469 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4471 #if ( configNUMBER_OF_CORES == 1 )
4473 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 )
4475 xSwitchRequired = pdTRUE;
4479 mtCOVERAGE_TEST_MARKER();
4482 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4486 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4488 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1 )
4490 xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4494 mtCOVERAGE_TEST_MARKER();
4498 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4500 #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4502 #if ( configUSE_TICK_HOOK == 1 )
4504 /* Guard against the tick hook being called when the pended tick
4505 * count is being unwound (when the scheduler is being unlocked). */
4506 if( xPendedTicks == ( TickType_t ) 0 )
4508 vApplicationTickHook();
4512 mtCOVERAGE_TEST_MARKER();
4515 #endif /* configUSE_TICK_HOOK */
4517 #if ( configUSE_PREEMPTION == 1 )
4519 #if ( configNUMBER_OF_CORES == 1 )
4521 /* For single core the core ID is always 0. */
4522 if( xYieldPendings[ 0 ] != pdFALSE )
4524 xSwitchRequired = pdTRUE;
4528 mtCOVERAGE_TEST_MARKER();
4531 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4533 BaseType_t xCoreID, xCurrentCoreID;
4534 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4536 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4538 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4539 if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4542 if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4544 if( xCoreID == xCurrentCoreID )
4546 xSwitchRequired = pdTRUE;
4550 prvYieldCore( xCoreID );
4555 mtCOVERAGE_TEST_MARKER();
4560 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4562 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4568 /* The tick hook gets called at regular intervals, even if the
4569 * scheduler is locked. */
4570 #if ( configUSE_TICK_HOOK == 1 )
4572 vApplicationTickHook();
4577 traceRETURN_xTaskIncrementTick( xSwitchRequired );
4579 return xSwitchRequired;
4581 /*-----------------------------------------------------------*/
4583 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4585 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4586 TaskHookFunction_t pxHookFunction )
4590 traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4592 /* If xTask is NULL then it is the task hook of the calling task that is
4596 xTCB = ( TCB_t * ) pxCurrentTCB;
4603 /* Save the hook function in the TCB. A critical section is required as
4604 * the value can be accessed from an interrupt. */
4605 taskENTER_CRITICAL();
4607 xTCB->pxTaskTag = pxHookFunction;
4609 taskEXIT_CRITICAL();
4611 traceRETURN_vTaskSetApplicationTaskTag();
4614 #endif /* configUSE_APPLICATION_TASK_TAG */
4615 /*-----------------------------------------------------------*/
4617 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4619 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4622 TaskHookFunction_t xReturn;
4624 traceENTER_xTaskGetApplicationTaskTag( xTask );
4626 /* If xTask is NULL then set the calling task's hook. */
4627 pxTCB = prvGetTCBFromHandle( xTask );
4629 /* Save the hook function in the TCB. A critical section is required as
4630 * the value can be accessed from an interrupt. */
4631 taskENTER_CRITICAL();
4633 xReturn = pxTCB->pxTaskTag;
4635 taskEXIT_CRITICAL();
4637 traceRETURN_xTaskGetApplicationTaskTag( xReturn );
4642 #endif /* configUSE_APPLICATION_TASK_TAG */
4643 /*-----------------------------------------------------------*/
4645 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4647 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
4650 TaskHookFunction_t xReturn;
4651 UBaseType_t uxSavedInterruptStatus;
4653 traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
4655 /* If xTask is NULL then set the calling task's hook. */
4656 pxTCB = prvGetTCBFromHandle( xTask );
4658 /* Save the hook function in the TCB. A critical section is required as
4659 * the value can be accessed from an interrupt. */
4660 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
4662 xReturn = pxTCB->pxTaskTag;
4664 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
4666 traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
4671 #endif /* configUSE_APPLICATION_TASK_TAG */
4672 /*-----------------------------------------------------------*/
4674 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4676 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
4677 void * pvParameter )
4682 traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
4684 /* If xTask is NULL then we are calling our own task hook. */
4687 xTCB = pxCurrentTCB;
4694 if( xTCB->pxTaskTag != NULL )
4696 xReturn = xTCB->pxTaskTag( pvParameter );
4703 traceRETURN_xTaskCallApplicationTaskHook( xReturn );
4708 #endif /* configUSE_APPLICATION_TASK_TAG */
4709 /*-----------------------------------------------------------*/
4711 #if ( configNUMBER_OF_CORES == 1 )
4712 void vTaskSwitchContext( void )
4714 traceENTER_vTaskSwitchContext();
4716 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
4718 /* The scheduler is currently suspended - do not allow a context
4720 xYieldPendings[ 0 ] = pdTRUE;
4724 xYieldPendings[ 0 ] = pdFALSE;
4725 traceTASK_SWITCHED_OUT();
4727 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4729 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4730 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
4732 ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
4735 /* Add the amount of time the task has been running to the
4736 * accumulated time so far. The time the task started running was
4737 * stored in ulTaskSwitchedInTime. Note that there is no overflow
4738 * protection here so count values are only valid until the timer
4739 * overflows. The guard against negative values is to protect
4740 * against suspect run time stat counter implementations - which
4741 * are provided by the application, not the kernel. */
4742 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
4744 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
4748 mtCOVERAGE_TEST_MARKER();
4751 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
4753 #endif /* configGENERATE_RUN_TIME_STATS */
4755 /* Check for stack overflow, if configured. */
4756 taskCHECK_FOR_STACK_OVERFLOW();
4758 /* Before the currently running task is switched out, save its errno. */
4759 #if ( configUSE_POSIX_ERRNO == 1 )
4761 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
4765 /* Select a new task to run using either the generic C or port
4766 * optimised asm code. */
4767 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. */
4768 traceTASK_SWITCHED_IN();
4770 /* After the new task is switched in, update the global errno. */
4771 #if ( configUSE_POSIX_ERRNO == 1 )
4773 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
4777 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
4779 /* Switch C-Runtime's TLS Block to point to the TLS
4780 * Block specific to this task. */
4781 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
4786 traceRETURN_vTaskSwitchContext();
4788 #else /* if ( configNUMBER_OF_CORES == 1 ) */
4789 void vTaskSwitchContext( BaseType_t xCoreID )
4791 traceENTER_vTaskSwitchContext();
4793 /* Acquire both locks:
4794 * - The ISR lock protects the ready list from simultaneous access by
4795 * both other ISRs and tasks.
4796 * - We also take the task lock to pause here in case another core has
4797 * suspended the scheduler. We don't want to simply set xYieldPending
4798 * and move on if another core suspended the scheduler. We should only
4799 * do that if the current core has suspended the scheduler. */
4801 portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
4804 /* vTaskSwitchContext() must never be called from within a critical section.
4805 * This is not necessarily true for single core FreeRTOS, but it is for this
4807 configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
4809 if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
4811 /* The scheduler is currently suspended - do not allow a context
4813 xYieldPendings[ xCoreID ] = pdTRUE;
4817 xYieldPendings[ xCoreID ] = pdFALSE;
4818 traceTASK_SWITCHED_OUT();
4820 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4822 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4823 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
4825 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
4828 /* Add the amount of time the task has been running to the
4829 * accumulated time so far. The time the task started running was
4830 * stored in ulTaskSwitchedInTime. Note that there is no overflow
4831 * protection here so count values are only valid until the timer
4832 * overflows. The guard against negative values is to protect
4833 * against suspect run time stat counter implementations - which
4834 * are provided by the application, not the kernel. */
4835 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
4837 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
4841 mtCOVERAGE_TEST_MARKER();
4844 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
4846 #endif /* configGENERATE_RUN_TIME_STATS */
4848 /* Check for stack overflow, if configured. */
4849 taskCHECK_FOR_STACK_OVERFLOW();
4851 /* Before the currently running task is switched out, save its errno. */
4852 #if ( configUSE_POSIX_ERRNO == 1 )
4854 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
4858 /* Select a new task to run. */
4859 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
4860 traceTASK_SWITCHED_IN();
4862 /* After the new task is switched in, update the global errno. */
4863 #if ( configUSE_POSIX_ERRNO == 1 )
4865 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
4869 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
4871 /* Switch C-Runtime's TLS Block to point to the TLS
4872 * Block specific to this task. */
4873 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
4878 portRELEASE_ISR_LOCK();
4879 portRELEASE_TASK_LOCK();
4881 traceRETURN_vTaskSwitchContext();
4883 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
4884 /*-----------------------------------------------------------*/
4886 void vTaskPlaceOnEventList( List_t * const pxEventList,
4887 const TickType_t xTicksToWait )
4889 traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
4891 configASSERT( pxEventList );
4893 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
4894 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
4896 /* Place the event list item of the TCB in the appropriate event list.
4897 * This is placed in the list in priority order so the highest priority task
4898 * is the first to be woken by the event.
4900 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
4901 * Normally, the xItemValue of a TCB's ListItem_t members is:
4902 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
4903 * Therefore, the event list is sorted in descending priority order.
4905 * The queue that contains the event list is locked, preventing
4906 * simultaneous access from interrupts. */
4907 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4909 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4911 traceRETURN_vTaskPlaceOnEventList();
4913 /*-----------------------------------------------------------*/
4915 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
4916 const TickType_t xItemValue,
4917 const TickType_t xTicksToWait )
4919 traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
4921 configASSERT( pxEventList );
4923 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
4924 * the event groups implementation. */
4925 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4927 /* Store the item value in the event list item. It is safe to access the
4928 * event list item here as interrupts won't access the event list item of a
4929 * task that is not in the Blocked state. */
4930 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4932 /* Place the event list item of the TCB at the end of the appropriate event
4933 * list. It is safe to access the event list here because it is part of an
4934 * event group implementation - and interrupts don't access event groups
4935 * directly (instead they access them indirectly by pending function calls to
4936 * the task level). */
4937 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4939 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4941 traceRETURN_vTaskPlaceOnUnorderedEventList();
4943 /*-----------------------------------------------------------*/
4945 #if ( configUSE_TIMERS == 1 )
4947 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
4948 TickType_t xTicksToWait,
4949 const BaseType_t xWaitIndefinitely )
4951 traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
4953 configASSERT( pxEventList );
4955 /* This function should not be called by application code hence the
4956 * 'Restricted' in its name. It is not part of the public API. It is
4957 * designed for use by kernel code, and has special calling requirements -
4958 * it should be called with the scheduler suspended. */
4961 /* Place the event list item of the TCB in the appropriate event list.
4962 * In this case it is assume that this is the only task that is going to
4963 * be waiting on this event list, so the faster vListInsertEnd() function
4964 * can be used in place of vListInsert. */
4965 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4967 /* If the task should block indefinitely then set the block time to a
4968 * value that will be recognised as an indefinite delay inside the
4969 * prvAddCurrentTaskToDelayedList() function. */
4970 if( xWaitIndefinitely != pdFALSE )
4972 xTicksToWait = portMAX_DELAY;
4975 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
4976 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
4978 traceRETURN_vTaskPlaceOnEventListRestricted();
4981 #endif /* configUSE_TIMERS */
4982 /*-----------------------------------------------------------*/
4984 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
4986 TCB_t * pxUnblockedTCB;
4989 traceENTER_xTaskRemoveFromEventList( pxEventList );
4991 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
4992 * called from a critical section within an ISR. */
4994 /* The event list is sorted in priority order, so the first in the list can
4995 * be removed as it is known to be the highest priority. Remove the TCB from
4996 * the delayed list, and add it to the ready list.
4998 * If an event is for a queue that is locked then this function will never
4999 * get called - the lock count on the queue will get modified instead. This
5000 * means exclusive access to the event list is guaranteed here.
5002 * This function assumes that a check has already been made to ensure that
5003 * pxEventList is not empty. */
5004 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. */
5005 configASSERT( pxUnblockedTCB );
5006 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5008 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5010 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5011 prvAddTaskToReadyList( pxUnblockedTCB );
5013 #if ( configUSE_TICKLESS_IDLE != 0 )
5015 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5016 * might be set to the blocked task's time out time. If the task is
5017 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5018 * normally left unchanged, because it is automatically reset to a new
5019 * value when the tick count equals xNextTaskUnblockTime. However if
5020 * tickless idling is used it might be more important to enter sleep mode
5021 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5022 * ensure it is updated at the earliest possible time. */
5023 prvResetNextTaskUnblockTime();
5029 /* The delayed and ready lists cannot be accessed, so hold this task
5030 * pending until the scheduler is resumed. */
5031 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5034 #if ( configNUMBER_OF_CORES == 1 )
5036 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5038 /* Return true if the task removed from the event list has a higher
5039 * priority than the calling task. This allows the calling task to know if
5040 * it should force a context switch now. */
5043 /* Mark that a yield is pending in case the user is not using the
5044 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5045 xYieldPendings[ 0 ] = pdTRUE;
5052 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5056 #if ( configUSE_PREEMPTION == 1 )
5058 prvYieldForTask( pxUnblockedTCB );
5060 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5065 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5067 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5069 traceRETURN_xTaskRemoveFromEventList( xReturn );
5072 /*-----------------------------------------------------------*/
5074 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5075 const TickType_t xItemValue )
5077 TCB_t * pxUnblockedTCB;
5079 traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5081 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
5082 * the event flags implementation. */
5083 configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5085 /* Store the new item value in the event list. */
5086 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5088 /* Remove the event list form the event flag. Interrupts do not access
5090 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. */
5091 configASSERT( pxUnblockedTCB );
5092 listREMOVE_ITEM( pxEventListItem );
5094 #if ( configUSE_TICKLESS_IDLE != 0 )
5096 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5097 * might be set to the blocked task's time out time. If the task is
5098 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5099 * normally left unchanged, because it is automatically reset to a new
5100 * value when the tick count equals xNextTaskUnblockTime. However if
5101 * tickless idling is used it might be more important to enter sleep mode
5102 * at the earliest possible time - so reset xNextTaskUnblockTime here to
5103 * ensure it is updated at the earliest possible time. */
5104 prvResetNextTaskUnblockTime();
5108 /* Remove the task from the delayed list and add it to the ready list. The
5109 * scheduler is suspended so interrupts will not be accessing the ready
5111 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5112 prvAddTaskToReadyList( pxUnblockedTCB );
5114 #if ( configNUMBER_OF_CORES == 1 )
5116 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5118 /* The unblocked task has a priority above that of the calling task, so
5119 * a context switch is required. This function is called with the
5120 * scheduler suspended so xYieldPending is set so the context switch
5121 * occurs immediately that the scheduler is resumed (unsuspended). */
5122 xYieldPendings[ 0 ] = pdTRUE;
5125 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5127 #if ( configUSE_PREEMPTION == 1 )
5129 taskENTER_CRITICAL();
5131 prvYieldForTask( pxUnblockedTCB );
5133 taskEXIT_CRITICAL();
5137 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5139 traceRETURN_vTaskRemoveFromUnorderedEventList();
5141 /*-----------------------------------------------------------*/
5143 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5145 traceENTER_vTaskSetTimeOutState( pxTimeOut );
5147 configASSERT( pxTimeOut );
5148 taskENTER_CRITICAL();
5150 pxTimeOut->xOverflowCount = xNumOfOverflows;
5151 pxTimeOut->xTimeOnEntering = xTickCount;
5153 taskEXIT_CRITICAL();
5155 traceRETURN_vTaskSetTimeOutState();
5157 /*-----------------------------------------------------------*/
5159 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5161 traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5163 /* For internal use only as it does not use a critical section. */
5164 pxTimeOut->xOverflowCount = xNumOfOverflows;
5165 pxTimeOut->xTimeOnEntering = xTickCount;
5167 traceRETURN_vTaskInternalSetTimeOutState();
5169 /*-----------------------------------------------------------*/
5171 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5172 TickType_t * const pxTicksToWait )
5176 traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5178 configASSERT( pxTimeOut );
5179 configASSERT( pxTicksToWait );
5181 taskENTER_CRITICAL();
5183 /* Minor optimisation. The tick count cannot change in this block. */
5184 const TickType_t xConstTickCount = xTickCount;
5185 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5187 #if ( INCLUDE_xTaskAbortDelay == 1 )
5188 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5190 /* The delay was aborted, which is not the same as a time out,
5191 * but has the same result. */
5192 pxCurrentTCB->ucDelayAborted = pdFALSE;
5198 #if ( INCLUDE_vTaskSuspend == 1 )
5199 if( *pxTicksToWait == portMAX_DELAY )
5201 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5202 * specified is the maximum block time then the task should block
5203 * indefinitely, and therefore never time out. */
5209 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
5211 /* The tick count is greater than the time at which
5212 * vTaskSetTimeout() was called, but has also overflowed since
5213 * vTaskSetTimeOut() was called. It must have wrapped all the way
5214 * around and gone past again. This passed since vTaskSetTimeout()
5217 *pxTicksToWait = ( TickType_t ) 0;
5219 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
5221 /* Not a genuine timeout. Adjust parameters for time remaining. */
5222 *pxTicksToWait -= xElapsedTime;
5223 vTaskInternalSetTimeOutState( pxTimeOut );
5228 *pxTicksToWait = ( TickType_t ) 0;
5232 taskEXIT_CRITICAL();
5234 traceRETURN_xTaskCheckForTimeOut( xReturn );
5238 /*-----------------------------------------------------------*/
5240 void vTaskMissedYield( void )
5242 traceENTER_vTaskMissedYield();
5244 /* Must be called from within a critical section. */
5245 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5247 traceRETURN_vTaskMissedYield();
5249 /*-----------------------------------------------------------*/
5251 #if ( configUSE_TRACE_FACILITY == 1 )
5253 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5255 UBaseType_t uxReturn;
5256 TCB_t const * pxTCB;
5258 traceENTER_uxTaskGetTaskNumber( xTask );
5263 uxReturn = pxTCB->uxTaskNumber;
5270 traceRETURN_uxTaskGetTaskNumber( uxReturn );
5275 #endif /* configUSE_TRACE_FACILITY */
5276 /*-----------------------------------------------------------*/
5278 #if ( configUSE_TRACE_FACILITY == 1 )
5280 void vTaskSetTaskNumber( TaskHandle_t xTask,
5281 const UBaseType_t uxHandle )
5285 traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5290 pxTCB->uxTaskNumber = uxHandle;
5293 traceRETURN_vTaskSetTaskNumber();
5296 #endif /* configUSE_TRACE_FACILITY */
5297 /*-----------------------------------------------------------*/
5300 * -----------------------------------------------------------
5301 * The MinimalIdle task.
5302 * ----------------------------------------------------------
5304 * The minimal idle task is used for all the additional cores in a SMP
5305 * system. There must be only 1 idle task and the rest are minimal idle
5308 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5309 * language extensions. The equivalent prototype for this function is:
5311 * void prvMinimalIdleTask( void *pvParameters );
5314 #if ( configNUMBER_OF_CORES > 1 )
5315 static portTASK_FUNCTION( prvMinimalIdleTask, pvParameters )
5317 ( void ) pvParameters;
5321 for( ; INFINITE_LOOP(); )
5323 #if ( configUSE_PREEMPTION == 0 )
5325 /* If we are not using preemption we keep forcing a task switch to
5326 * see if any other task has become available. If we are using
5327 * preemption we don't need to do this as any task becoming available
5328 * will automatically get the processor anyway. */
5331 #endif /* configUSE_PREEMPTION */
5333 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5335 /* When using preemption tasks of equal priority will be
5336 * timesliced. If a task that is sharing the idle priority is ready
5337 * to run then the idle task should yield before the end of the
5340 * A critical region is not required here as we are just reading from
5341 * the list, and an occasional incorrect value will not matter. If
5342 * the ready list at the idle priority contains one more task than the
5343 * number of idle tasks, which is equal to the configured numbers of cores
5344 * then a task other than the idle task is ready to execute. */
5345 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5351 mtCOVERAGE_TEST_MARKER();
5354 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5356 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
5358 /* Call the user defined function from within the idle task. This
5359 * allows the application designer to add background functionality
5360 * without the overhead of a separate task.
5362 * This hook is intended to manage core activity such as disabling cores that go idle.
5364 * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5365 * CALL A FUNCTION THAT MIGHT BLOCK. */
5366 vApplicationMinimalIdleHook();
5368 #endif /* configUSE_MINIMAL_IDLE_HOOK */
5371 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5374 * -----------------------------------------------------------
5376 * ----------------------------------------------------------
5378 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5379 * language extensions. The equivalent prototype for this function is:
5381 * void prvIdleTask( void *pvParameters );
5385 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5387 /* Stop warnings. */
5388 ( void ) pvParameters;
5390 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5391 * SCHEDULER IS STARTED. **/
5393 /* In case a task that has a secure context deletes itself, in which case
5394 * the idle task is responsible for deleting the task's secure context, if
5396 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5398 #if ( configNUMBER_OF_CORES > 1 )
5400 /* SMP all cores start up in the idle task. This initial yield gets the application
5404 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5406 for( ; INFINITE_LOOP(); )
5408 /* See if any tasks have deleted themselves - if so then the idle task
5409 * is responsible for freeing the deleted task's TCB and stack. */
5410 prvCheckTasksWaitingTermination();
5412 #if ( configUSE_PREEMPTION == 0 )
5414 /* If we are not using preemption we keep forcing a task switch to
5415 * see if any other task has become available. If we are using
5416 * preemption we don't need to do this as any task becoming available
5417 * will automatically get the processor anyway. */
5420 #endif /* configUSE_PREEMPTION */
5422 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5424 /* When using preemption tasks of equal priority will be
5425 * timesliced. If a task that is sharing the idle priority is ready
5426 * to run then the idle task should yield before the end of the
5429 * A critical region is not required here as we are just reading from
5430 * the list, and an occasional incorrect value will not matter. If
5431 * the ready list at the idle priority contains one more task than the
5432 * number of idle tasks, which is equal to the configured numbers of cores
5433 * then a task other than the idle task is ready to execute. */
5434 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5440 mtCOVERAGE_TEST_MARKER();
5443 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5445 #if ( configUSE_IDLE_HOOK == 1 )
5447 /* Call the user defined function from within the idle task. */
5448 vApplicationIdleHook();
5450 #endif /* configUSE_IDLE_HOOK */
5452 /* This conditional compilation should use inequality to 0, not equality
5453 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5454 * user defined low power mode implementations require
5455 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5456 #if ( configUSE_TICKLESS_IDLE != 0 )
5458 TickType_t xExpectedIdleTime;
5460 /* It is not desirable to suspend then resume the scheduler on
5461 * each iteration of the idle task. Therefore, a preliminary
5462 * test of the expected idle time is performed without the
5463 * scheduler suspended. The result here is not necessarily
5465 xExpectedIdleTime = prvGetExpectedIdleTime();
5467 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5471 /* Now the scheduler is suspended, the expected idle
5472 * time can be sampled again, and this time its value can
5474 configASSERT( xNextTaskUnblockTime >= xTickCount );
5475 xExpectedIdleTime = prvGetExpectedIdleTime();
5477 /* Define the following macro to set xExpectedIdleTime to 0
5478 * if the application does not want
5479 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5480 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5482 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5484 traceLOW_POWER_IDLE_BEGIN();
5485 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5486 traceLOW_POWER_IDLE_END();
5490 mtCOVERAGE_TEST_MARKER();
5493 ( void ) xTaskResumeAll();
5497 mtCOVERAGE_TEST_MARKER();
5500 #endif /* configUSE_TICKLESS_IDLE */
5502 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_MINIMAL_IDLE_HOOK == 1 ) )
5504 /* Call the user defined function from within the idle task. This
5505 * allows the application designer to add background functionality
5506 * without the overhead of a separate task.
5508 * This hook is intended to manage core activity such as disabling cores that go idle.
5510 * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5511 * CALL A FUNCTION THAT MIGHT BLOCK. */
5512 vApplicationMinimalIdleHook();
5514 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_MINIMAL_IDLE_HOOK == 1 ) ) */
5517 /*-----------------------------------------------------------*/
5519 #if ( configUSE_TICKLESS_IDLE != 0 )
5521 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5523 #if ( INCLUDE_vTaskSuspend == 1 )
5524 /* The idle task exists in addition to the application tasks. */
5525 const UBaseType_t uxNonApplicationTasks = 1;
5526 #endif /* INCLUDE_vTaskSuspend */
5528 traceENTER_eTaskConfirmSleepModeStatus();
5530 eSleepModeStatus eReturn = eStandardSleep;
5532 /* This function must be called from a critical section. */
5534 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
5536 /* A task was made ready while the scheduler was suspended. */
5537 eReturn = eAbortSleep;
5539 else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5541 /* A yield was pended while the scheduler was suspended. */
5542 eReturn = eAbortSleep;
5544 else if( xPendedTicks != 0 )
5546 /* A tick interrupt has already occurred but was held pending
5547 * because the scheduler is suspended. */
5548 eReturn = eAbortSleep;
5551 #if ( INCLUDE_vTaskSuspend == 1 )
5552 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5554 /* If all the tasks are in the suspended list (which might mean they
5555 * have an infinite block time rather than actually being suspended)
5556 * then it is safe to turn all clocks off and just wait for external
5558 eReturn = eNoTasksWaitingTimeout;
5560 #endif /* INCLUDE_vTaskSuspend */
5563 mtCOVERAGE_TEST_MARKER();
5566 traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5571 #endif /* configUSE_TICKLESS_IDLE */
5572 /*-----------------------------------------------------------*/
5574 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5576 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5582 traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
5584 if( ( xIndex >= 0 ) &&
5585 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5587 pxTCB = prvGetTCBFromHandle( xTaskToSet );
5588 configASSERT( pxTCB != NULL );
5589 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5592 traceRETURN_vTaskSetThreadLocalStoragePointer();
5595 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5596 /*-----------------------------------------------------------*/
5598 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5600 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5603 void * pvReturn = NULL;
5606 traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
5608 if( ( xIndex >= 0 ) &&
5609 ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5611 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
5612 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
5619 traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
5624 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5625 /*-----------------------------------------------------------*/
5627 #if ( portUSING_MPU_WRAPPERS == 1 )
5629 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
5630 const MemoryRegion_t * const pxRegions )
5634 traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
5636 /* If null is passed in here then we are modifying the MPU settings of
5637 * the calling task. */
5638 pxTCB = prvGetTCBFromHandle( xTaskToModify );
5640 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
5642 traceRETURN_vTaskAllocateMPURegions();
5645 #endif /* portUSING_MPU_WRAPPERS */
5646 /*-----------------------------------------------------------*/
5648 static void prvInitialiseTaskLists( void )
5650 UBaseType_t uxPriority;
5652 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
5654 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
5657 vListInitialise( &xDelayedTaskList1 );
5658 vListInitialise( &xDelayedTaskList2 );
5659 vListInitialise( &xPendingReadyList );
5661 #if ( INCLUDE_vTaskDelete == 1 )
5663 vListInitialise( &xTasksWaitingTermination );
5665 #endif /* INCLUDE_vTaskDelete */
5667 #if ( INCLUDE_vTaskSuspend == 1 )
5669 vListInitialise( &xSuspendedTaskList );
5671 #endif /* INCLUDE_vTaskSuspend */
5673 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
5675 pxDelayedTaskList = &xDelayedTaskList1;
5676 pxOverflowDelayedTaskList = &xDelayedTaskList2;
5678 /*-----------------------------------------------------------*/
5680 static void prvCheckTasksWaitingTermination( void )
5682 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
5684 #if ( INCLUDE_vTaskDelete == 1 )
5688 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
5689 * being called too often in the idle task. */
5690 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
5692 #if ( configNUMBER_OF_CORES == 1 )
5694 taskENTER_CRITICAL();
5697 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. */
5698 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5699 --uxCurrentNumberOfTasks;
5700 --uxDeletedTasksWaitingCleanUp;
5703 taskEXIT_CRITICAL();
5705 prvDeleteTCB( pxTCB );
5707 #else /* #if( configNUMBER_OF_CORES == 1 ) */
5711 taskENTER_CRITICAL();
5713 /* For SMP, multiple idles can be running simultaneously
5714 * and we need to check that other idles did not cleanup while we were
5715 * waiting to enter the critical section. */
5716 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
5718 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. */
5720 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
5722 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5723 --uxCurrentNumberOfTasks;
5724 --uxDeletedTasksWaitingCleanUp;
5728 /* The TCB to be deleted still has not yet been switched out
5729 * by the scheduler, so we will just exit this loop early and
5730 * try again next time. */
5731 taskEXIT_CRITICAL();
5736 taskEXIT_CRITICAL();
5740 prvDeleteTCB( pxTCB );
5743 #endif /* #if( configNUMBER_OF_CORES == 1 ) */
5746 #endif /* INCLUDE_vTaskDelete */
5748 /*-----------------------------------------------------------*/
5750 #if ( configUSE_TRACE_FACILITY == 1 )
5752 void vTaskGetInfo( TaskHandle_t xTask,
5753 TaskStatus_t * pxTaskStatus,
5754 BaseType_t xGetFreeStackSpace,
5759 traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
5761 /* xTask is NULL then get the state of the calling task. */
5762 pxTCB = prvGetTCBFromHandle( xTask );
5764 pxTaskStatus->xHandle = pxTCB;
5765 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
5766 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
5767 pxTaskStatus->pxStackBase = pxTCB->pxStack;
5768 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
5769 pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
5770 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
5772 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
5774 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
5776 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
5780 #if ( configUSE_MUTEXES == 1 )
5782 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
5786 pxTaskStatus->uxBasePriority = 0;
5790 #if ( configGENERATE_RUN_TIME_STATS == 1 )
5792 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
5796 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
5800 /* Obtaining the task state is a little fiddly, so is only done if the
5801 * value of eState passed into this function is eInvalid - otherwise the
5802 * state is just set to whatever is passed in. */
5803 if( eState != eInvalid )
5805 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
5807 pxTaskStatus->eCurrentState = eRunning;
5811 pxTaskStatus->eCurrentState = eState;
5813 #if ( INCLUDE_vTaskSuspend == 1 )
5815 /* If the task is in the suspended list then there is a
5816 * chance it is actually just blocked indefinitely - so really
5817 * it should be reported as being in the Blocked state. */
5818 if( eState == eSuspended )
5822 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
5824 pxTaskStatus->eCurrentState = eBlocked;
5827 ( void ) xTaskResumeAll();
5830 #endif /* INCLUDE_vTaskSuspend */
5832 /* Tasks can be in pending ready list and other state list at the
5833 * same time. These tasks are in ready state no matter what state
5834 * list the task is in. */
5835 taskENTER_CRITICAL();
5837 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
5839 pxTaskStatus->eCurrentState = eReady;
5842 taskEXIT_CRITICAL();
5847 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
5850 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
5851 * parameter is provided to allow it to be skipped. */
5852 if( xGetFreeStackSpace != pdFALSE )
5854 #if ( portSTACK_GROWTH > 0 )
5856 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
5860 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
5866 pxTaskStatus->usStackHighWaterMark = 0;
5869 traceRETURN_vTaskGetInfo();
5872 #endif /* configUSE_TRACE_FACILITY */
5873 /*-----------------------------------------------------------*/
5875 #if ( configUSE_TRACE_FACILITY == 1 )
5877 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
5881 configLIST_VOLATILE TCB_t * pxNextTCB;
5882 configLIST_VOLATILE TCB_t * pxFirstTCB;
5883 UBaseType_t uxTask = 0;
5885 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
5887 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. */
5889 /* Populate an TaskStatus_t structure within the
5890 * pxTaskStatusArray array for each task that is referenced from
5891 * pxList. See the definition of TaskStatus_t in task.h for the
5892 * meaning of each TaskStatus_t structure member. */
5895 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. */
5896 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
5898 } while( pxNextTCB != pxFirstTCB );
5902 mtCOVERAGE_TEST_MARKER();
5908 #endif /* configUSE_TRACE_FACILITY */
5909 /*-----------------------------------------------------------*/
5911 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
5913 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
5915 uint32_t ulCount = 0U;
5917 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
5919 pucStackByte -= portSTACK_GROWTH;
5923 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
5925 return ( configSTACK_DEPTH_TYPE ) ulCount;
5928 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
5929 /*-----------------------------------------------------------*/
5931 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
5933 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
5934 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
5935 * user to determine the return type. It gets around the problem of the value
5936 * overflowing on 8-bit types without breaking backward compatibility for
5937 * applications that expect an 8-bit return type. */
5938 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
5941 uint8_t * pucEndOfStack;
5942 configSTACK_DEPTH_TYPE uxReturn;
5944 traceENTER_uxTaskGetStackHighWaterMark2( xTask );
5946 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
5947 * the same except for their return type. Using configSTACK_DEPTH_TYPE
5948 * allows the user to determine the return type. It gets around the
5949 * problem of the value overflowing on 8-bit types without breaking
5950 * backward compatibility for applications that expect an 8-bit return
5953 pxTCB = prvGetTCBFromHandle( xTask );
5955 #if portSTACK_GROWTH < 0
5957 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
5961 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
5965 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
5967 traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
5972 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
5973 /*-----------------------------------------------------------*/
5975 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
5977 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
5980 uint8_t * pucEndOfStack;
5981 UBaseType_t uxReturn;
5983 traceENTER_uxTaskGetStackHighWaterMark( xTask );
5985 pxTCB = prvGetTCBFromHandle( xTask );
5987 #if portSTACK_GROWTH < 0
5989 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
5993 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
5997 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
5999 traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6004 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6005 /*-----------------------------------------------------------*/
6007 #if ( INCLUDE_vTaskDelete == 1 )
6009 static void prvDeleteTCB( TCB_t * pxTCB )
6011 /* This call is required specifically for the TriCore port. It must be
6012 * above the vPortFree() calls. The call is also used by ports/demos that
6013 * want to allocate and clean RAM statically. */
6014 portCLEAN_UP_TCB( pxTCB );
6016 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6018 /* Free up the memory allocated for the task's TLS Block. */
6019 configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6023 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6025 /* The task can only have been allocated dynamically - free both
6026 * the stack and TCB. */
6027 vPortFreeStack( pxTCB->pxStack );
6030 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
6032 /* The task could have been allocated statically or dynamically, so
6033 * check what was statically allocated before trying to free the
6035 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6037 /* Both the stack and TCB were allocated dynamically, so both
6039 vPortFreeStack( pxTCB->pxStack );
6042 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6044 /* Only the stack was statically allocated, so the TCB is the
6045 * only memory that must be freed. */
6050 /* Neither the stack nor the TCB were allocated dynamically, so
6051 * nothing needs to be freed. */
6052 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6053 mtCOVERAGE_TEST_MARKER();
6056 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6059 #endif /* INCLUDE_vTaskDelete */
6060 /*-----------------------------------------------------------*/
6062 static void prvResetNextTaskUnblockTime( void )
6064 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6066 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
6067 * the maximum possible value so it is extremely unlikely that the
6068 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6069 * there is an item in the delayed list. */
6070 xNextTaskUnblockTime = portMAX_DELAY;
6074 /* The new current delayed list is not empty, get the value of
6075 * the item at the head of the delayed list. This is the time at
6076 * which the task at the head of the delayed list should be removed
6077 * from the Blocked state. */
6078 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6081 /*-----------------------------------------------------------*/
6083 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6085 #if ( configNUMBER_OF_CORES == 1 )
6086 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6088 TaskHandle_t xReturn;
6090 traceENTER_xTaskGetCurrentTaskHandle();
6092 /* A critical section is not required as this is not called from
6093 * an interrupt and the current TCB will always be the same for any
6094 * individual execution thread. */
6095 xReturn = pxCurrentTCB;
6097 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6101 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6102 TaskHandle_t xTaskGetCurrentTaskHandle( void )
6104 TaskHandle_t xReturn;
6105 UBaseType_t uxSavedInterruptStatus;
6107 traceENTER_xTaskGetCurrentTaskHandle();
6109 uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6111 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6113 portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6115 traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6120 TaskHandle_t xTaskGetCurrentTaskHandleCPU( BaseType_t xCoreID )
6122 TaskHandle_t xReturn = NULL;
6124 traceENTER_xTaskGetCurrentTaskHandleCPU( xCoreID );
6126 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6128 xReturn = pxCurrentTCBs[ xCoreID ];
6131 traceRETURN_xTaskGetCurrentTaskHandleCPU( xReturn );
6135 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6137 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
6138 /*-----------------------------------------------------------*/
6140 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6142 BaseType_t xTaskGetSchedulerState( void )
6146 traceENTER_xTaskGetSchedulerState();
6148 if( xSchedulerRunning == pdFALSE )
6150 xReturn = taskSCHEDULER_NOT_STARTED;
6154 #if ( configNUMBER_OF_CORES > 1 )
6155 taskENTER_CRITICAL();
6158 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6160 xReturn = taskSCHEDULER_RUNNING;
6164 xReturn = taskSCHEDULER_SUSPENDED;
6167 #if ( configNUMBER_OF_CORES > 1 )
6168 taskEXIT_CRITICAL();
6172 traceRETURN_xTaskGetSchedulerState( xReturn );
6177 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6178 /*-----------------------------------------------------------*/
6180 #if ( configUSE_MUTEXES == 1 )
6182 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6184 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6185 BaseType_t xReturn = pdFALSE;
6187 traceENTER_xTaskPriorityInherit( pxMutexHolder );
6189 /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6190 * inheritance is not applied in this scenario. */
6191 if( pxMutexHolder != NULL )
6193 /* If the holder of the mutex has a priority below the priority of
6194 * the task attempting to obtain the mutex then it will temporarily
6195 * inherit the priority of the task attempting to obtain the mutex. */
6196 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6198 /* Adjust the mutex holder state to account for its new
6199 * priority. Only reset the event list item value if the value is
6200 * not being used for anything else. */
6201 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
6203 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. */
6207 mtCOVERAGE_TEST_MARKER();
6210 /* If the task being modified is in the ready state it will need
6211 * to be moved into a new list. */
6212 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6214 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6216 /* It is known that the task is in its ready list so
6217 * there is no need to check again and the port level
6218 * reset macro can be called directly. */
6219 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6223 mtCOVERAGE_TEST_MARKER();
6226 /* Inherit the priority before being moved into the new list. */
6227 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6228 prvAddTaskToReadyList( pxMutexHolderTCB );
6229 #if ( configNUMBER_OF_CORES > 1 )
6231 /* The priority of the task is raised. Yield for this task
6232 * if it is not running. */
6233 if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6235 prvYieldForTask( pxMutexHolderTCB );
6238 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6242 /* Just inherit the priority. */
6243 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6246 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6248 /* Inheritance occurred. */
6253 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6255 /* The base priority of the mutex holder is lower than the
6256 * priority of the task attempting to take the mutex, but the
6257 * current priority of the mutex holder is not lower than the
6258 * priority of the task attempting to take the mutex.
6259 * Therefore the mutex holder must have already inherited a
6260 * priority, but inheritance would have occurred if that had
6261 * not been the case. */
6266 mtCOVERAGE_TEST_MARKER();
6272 mtCOVERAGE_TEST_MARKER();
6275 traceRETURN_xTaskPriorityInherit( xReturn );
6280 #endif /* configUSE_MUTEXES */
6281 /*-----------------------------------------------------------*/
6283 #if ( configUSE_MUTEXES == 1 )
6285 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6287 TCB_t * const pxTCB = pxMutexHolder;
6288 BaseType_t xReturn = pdFALSE;
6290 traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6292 if( pxMutexHolder != NULL )
6294 /* A task can only have an inherited priority if it holds the mutex.
6295 * If the mutex is held by a task then it cannot be given from an
6296 * interrupt, and if a mutex is given by the holding task then it must
6297 * be the running state task. */
6298 configASSERT( pxTCB == pxCurrentTCB );
6299 configASSERT( pxTCB->uxMutexesHeld );
6300 ( pxTCB->uxMutexesHeld )--;
6302 /* Has the holder of the mutex inherited the priority of another
6304 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6306 /* Only disinherit if no other mutexes are held. */
6307 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6309 /* A task can only have an inherited priority if it holds
6310 * the mutex. If the mutex is held by a task then it cannot be
6311 * given from an interrupt, and if a mutex is given by the
6312 * holding task then it must be the running state task. Remove
6313 * the holding task from the ready list. */
6314 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6316 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6320 mtCOVERAGE_TEST_MARKER();
6323 /* Disinherit the priority before adding the task into the
6324 * new ready list. */
6325 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6326 pxTCB->uxPriority = pxTCB->uxBasePriority;
6328 /* Reset the event list item value. It cannot be in use for
6329 * any other purpose if this task is running, and it must be
6330 * running to give back the mutex. */
6331 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. */
6332 prvAddTaskToReadyList( pxTCB );
6333 #if ( configNUMBER_OF_CORES > 1 )
6335 /* The priority of the task is dropped. Yield the core on
6336 * which the task is running. */
6337 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6339 prvYieldCore( pxTCB->xTaskRunState );
6342 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6344 /* Return true to indicate that a context switch is required.
6345 * This is only actually required in the corner case whereby
6346 * multiple mutexes were held and the mutexes were given back
6347 * in an order different to that in which they were taken.
6348 * If a context switch did not occur when the first mutex was
6349 * returned, even if a task was waiting on it, then a context
6350 * switch should occur when the last mutex is returned whether
6351 * a task is waiting on it or not. */
6356 mtCOVERAGE_TEST_MARKER();
6361 mtCOVERAGE_TEST_MARKER();
6366 mtCOVERAGE_TEST_MARKER();
6369 traceRETURN_xTaskPriorityDisinherit( xReturn );
6374 #endif /* configUSE_MUTEXES */
6375 /*-----------------------------------------------------------*/
6377 #if ( configUSE_MUTEXES == 1 )
6379 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6380 UBaseType_t uxHighestPriorityWaitingTask )
6382 TCB_t * const pxTCB = pxMutexHolder;
6383 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6384 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6386 traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6388 if( pxMutexHolder != NULL )
6390 /* If pxMutexHolder is not NULL then the holder must hold at least
6392 configASSERT( pxTCB->uxMutexesHeld );
6394 /* Determine the priority to which the priority of the task that
6395 * holds the mutex should be set. This will be the greater of the
6396 * holding task's base priority and the priority of the highest
6397 * priority task that is waiting to obtain the mutex. */
6398 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6400 uxPriorityToUse = uxHighestPriorityWaitingTask;
6404 uxPriorityToUse = pxTCB->uxBasePriority;
6407 /* Does the priority need to change? */
6408 if( pxTCB->uxPriority != uxPriorityToUse )
6410 /* Only disinherit if no other mutexes are held. This is a
6411 * simplification in the priority inheritance implementation. If
6412 * the task that holds the mutex is also holding other mutexes then
6413 * the other mutexes may have caused the priority inheritance. */
6414 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6416 /* If a task has timed out because it already holds the
6417 * mutex it was trying to obtain then it cannot of inherited
6418 * its own priority. */
6419 configASSERT( pxTCB != pxCurrentTCB );
6421 /* Disinherit the priority, remembering the previous
6422 * priority to facilitate determining the subject task's
6424 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6425 uxPriorityUsedOnEntry = pxTCB->uxPriority;
6426 pxTCB->uxPriority = uxPriorityToUse;
6428 /* Only reset the event list item value if the value is not
6429 * being used for anything else. */
6430 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
6432 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. */
6436 mtCOVERAGE_TEST_MARKER();
6439 /* If the running task is not the task that holds the mutex
6440 * then the task that holds the mutex could be in either the
6441 * Ready, Blocked or Suspended states. Only remove the task
6442 * from its current state list if it is in the Ready state as
6443 * the task's priority is going to change and there is one
6444 * Ready list per priority. */
6445 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6447 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6449 /* It is known that the task is in its ready list so
6450 * there is no need to check again and the port level
6451 * reset macro can be called directly. */
6452 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6456 mtCOVERAGE_TEST_MARKER();
6459 prvAddTaskToReadyList( pxTCB );
6460 #if ( configNUMBER_OF_CORES > 1 )
6462 /* The priority of the task is dropped. Yield the core on
6463 * which the task is running. */
6464 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6466 prvYieldCore( pxTCB->xTaskRunState );
6469 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6473 mtCOVERAGE_TEST_MARKER();
6478 mtCOVERAGE_TEST_MARKER();
6483 mtCOVERAGE_TEST_MARKER();
6488 mtCOVERAGE_TEST_MARKER();
6491 traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6494 #endif /* configUSE_MUTEXES */
6495 /*-----------------------------------------------------------*/
6497 #if ( configNUMBER_OF_CORES > 1 )
6499 /* If not in a critical section then yield immediately.
6500 * Otherwise set xYieldPendings to true to wait to
6501 * yield until exiting the critical section.
6503 void vTaskYieldWithinAPI( void )
6505 traceENTER_vTaskYieldWithinAPI();
6507 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6513 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6516 traceRETURN_vTaskYieldWithinAPI();
6518 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6520 /*-----------------------------------------------------------*/
6522 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6524 void vTaskEnterCritical( void )
6526 traceENTER_vTaskEnterCritical();
6528 portDISABLE_INTERRUPTS();
6530 if( xSchedulerRunning != pdFALSE )
6532 ( pxCurrentTCB->uxCriticalNesting )++;
6534 /* This is not the interrupt safe version of the enter critical
6535 * function so assert() if it is being called from an interrupt
6536 * context. Only API functions that end in "FromISR" can be used in an
6537 * interrupt. Only assert if the critical nesting count is 1 to
6538 * protect against recursive calls if the assert function also uses a
6539 * critical section. */
6540 if( pxCurrentTCB->uxCriticalNesting == 1 )
6542 portASSERT_IF_IN_ISR();
6547 mtCOVERAGE_TEST_MARKER();
6550 traceRETURN_vTaskEnterCritical();
6553 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6554 /*-----------------------------------------------------------*/
6556 #if ( configNUMBER_OF_CORES > 1 )
6558 void vTaskEnterCritical( void )
6560 traceENTER_vTaskEnterCritical();
6562 portDISABLE_INTERRUPTS();
6564 if( xSchedulerRunning != pdFALSE )
6566 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6568 portGET_TASK_LOCK();
6572 portINCREMENT_CRITICAL_NESTING_COUNT();
6574 /* This is not the interrupt safe version of the enter critical
6575 * function so assert() if it is being called from an interrupt
6576 * context. Only API functions that end in "FromISR" can be used in an
6577 * interrupt. Only assert if the critical nesting count is 1 to
6578 * protect against recursive calls if the assert function also uses a
6579 * critical section. */
6580 if( portGET_CRITICAL_NESTING_COUNT() == 1U )
6582 portASSERT_IF_IN_ISR();
6584 if( uxSchedulerSuspended == 0U )
6586 /* The only time there would be a problem is if this is called
6587 * before a context switch and vTaskExitCritical() is called
6588 * after pxCurrentTCB changes. Therefore this should not be
6589 * used within vTaskSwitchContext(). */
6590 prvCheckForRunStateChange();
6596 mtCOVERAGE_TEST_MARKER();
6599 traceRETURN_vTaskEnterCritical();
6602 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6604 /*-----------------------------------------------------------*/
6606 #if ( configNUMBER_OF_CORES > 1 )
6608 UBaseType_t vTaskEnterCriticalFromISR( void )
6610 UBaseType_t uxSavedInterruptStatus = 0;
6612 traceENTER_vTaskEnterCriticalFromISR();
6614 if( xSchedulerRunning != pdFALSE )
6616 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6618 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6623 portINCREMENT_CRITICAL_NESTING_COUNT();
6627 mtCOVERAGE_TEST_MARKER();
6630 traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
6632 return uxSavedInterruptStatus;
6635 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6636 /*-----------------------------------------------------------*/
6638 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6640 void vTaskExitCritical( void )
6642 traceENTER_vTaskExitCritical();
6644 if( xSchedulerRunning != pdFALSE )
6646 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
6647 * does not match a previous call to vTaskEnterCritical(). */
6648 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
6650 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
6651 * to exit critical section from ISR. */
6652 portASSERT_IF_IN_ISR();
6654 if( pxCurrentTCB->uxCriticalNesting > 0U )
6656 ( pxCurrentTCB->uxCriticalNesting )--;
6658 if( pxCurrentTCB->uxCriticalNesting == 0U )
6660 portENABLE_INTERRUPTS();
6664 mtCOVERAGE_TEST_MARKER();
6669 mtCOVERAGE_TEST_MARKER();
6674 mtCOVERAGE_TEST_MARKER();
6677 traceRETURN_vTaskExitCritical();
6680 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6681 /*-----------------------------------------------------------*/
6683 #if ( configNUMBER_OF_CORES > 1 )
6685 void vTaskExitCritical( void )
6687 traceENTER_vTaskExitCritical();
6689 if( xSchedulerRunning != pdFALSE )
6691 /* If critical nesting count is zero then this function
6692 * does not match a previous call to vTaskEnterCritical(). */
6693 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
6695 /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
6696 * to exit critical section from ISR. */
6697 portASSERT_IF_IN_ISR();
6699 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
6701 portDECREMENT_CRITICAL_NESTING_COUNT();
6703 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6705 BaseType_t xYieldCurrentTask;
6707 /* Get the xYieldPending stats inside the critical section. */
6708 xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
6710 portRELEASE_ISR_LOCK();
6711 portRELEASE_TASK_LOCK();
6712 portENABLE_INTERRUPTS();
6714 /* When a task yields in a critical section it just sets
6715 * xYieldPending to true. So now that we have exited the
6716 * critical section check if xYieldPending is true, and
6718 if( xYieldCurrentTask != pdFALSE )
6725 mtCOVERAGE_TEST_MARKER();
6730 mtCOVERAGE_TEST_MARKER();
6735 mtCOVERAGE_TEST_MARKER();
6738 traceRETURN_vTaskExitCritical();
6741 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6742 /*-----------------------------------------------------------*/
6744 #if ( configNUMBER_OF_CORES > 1 )
6746 void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
6748 traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
6750 if( xSchedulerRunning != pdFALSE )
6752 /* If critical nesting count is zero then this function
6753 * does not match a previous call to vTaskEnterCritical(). */
6754 configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
6756 if( portGET_CRITICAL_NESTING_COUNT() > 0U )
6758 portDECREMENT_CRITICAL_NESTING_COUNT();
6760 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6762 portRELEASE_ISR_LOCK();
6763 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6767 mtCOVERAGE_TEST_MARKER();
6772 mtCOVERAGE_TEST_MARKER();
6777 mtCOVERAGE_TEST_MARKER();
6780 traceRETURN_vTaskExitCriticalFromISR();
6783 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6784 /*-----------------------------------------------------------*/
6786 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
6788 static char * prvWriteNameToBuffer( char * pcBuffer,
6789 const char * pcTaskName )
6793 /* Start by copying the entire string. */
6794 ( void ) strcpy( pcBuffer, pcTaskName );
6796 /* Pad the end of the string with spaces to ensure columns line up when
6798 for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
6800 pcBuffer[ x ] = ' ';
6804 pcBuffer[ x ] = ( char ) 0x00;
6806 /* Return the new end of string. */
6807 return &( pcBuffer[ x ] );
6810 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
6811 /*-----------------------------------------------------------*/
6813 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
6815 void vTaskList( char * pcWriteBuffer )
6817 TaskStatus_t * pxTaskStatusArray;
6818 UBaseType_t uxArraySize, x;
6821 traceENTER_vTaskList( pcWriteBuffer );
6826 * This function is provided for convenience only, and is used by many
6827 * of the demo applications. Do not consider it to be part of the
6830 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
6831 * uxTaskGetSystemState() output into a human readable table that
6832 * displays task: names, states, priority, stack usage and task number.
6833 * Stack usage specified as the number of unused StackType_t words stack can hold
6834 * on top of stack - not the number of bytes.
6836 * vTaskList() has a dependency on the sprintf() C library function that
6837 * might bloat the code size, use a lot of stack, and provide different
6838 * results on different platforms. An alternative, tiny, third party,
6839 * and limited functionality implementation of sprintf() is provided in
6840 * many of the FreeRTOS/Demo sub-directories in a file called
6841 * printf-stdarg.c (note printf-stdarg.c does not provide a full
6842 * snprintf() implementation!).
6844 * It is recommended that production systems call uxTaskGetSystemState()
6845 * directly to get access to raw stats data, rather than indirectly
6846 * through a call to vTaskList().
6850 /* Make sure the write buffer does not contain a string. */
6851 *pcWriteBuffer = ( char ) 0x00;
6853 /* Take a snapshot of the number of tasks in case it changes while this
6854 * function is executing. */
6855 uxArraySize = uxCurrentNumberOfTasks;
6857 /* Allocate an array index for each task. NOTE! if
6858 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
6859 * equate to NULL. */
6860 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. */
6862 if( pxTaskStatusArray != NULL )
6864 /* Generate the (binary) data. */
6865 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
6867 /* Create a human readable table from the binary data. */
6868 for( x = 0; x < uxArraySize; x++ )
6870 switch( pxTaskStatusArray[ x ].eCurrentState )
6873 cStatus = tskRUNNING_CHAR;
6877 cStatus = tskREADY_CHAR;
6881 cStatus = tskBLOCKED_CHAR;
6885 cStatus = tskSUSPENDED_CHAR;
6889 cStatus = tskDELETED_CHAR;
6892 case eInvalid: /* Fall through. */
6893 default: /* Should not get here, but it is included
6894 * to prevent static checking errors. */
6895 cStatus = ( char ) 0x00;
6899 /* Write the task name to the string, padding with spaces so it
6900 * can be printed in tabular form more easily. */
6901 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
6903 /* Write the rest of the string. */
6904 sprintf( pcWriteBuffer, "\t%c\t%u\t%u\t%u\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
6905 pcWriteBuffer += strlen( pcWriteBuffer ); /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
6908 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
6909 * is 0 then vPortFree() will be #defined to nothing. */
6910 vPortFree( pxTaskStatusArray );
6914 mtCOVERAGE_TEST_MARKER();
6917 traceRETURN_vTaskList();
6920 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
6921 /*----------------------------------------------------------*/
6923 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
6925 void vTaskGetRunTimeStats( char * pcWriteBuffer )
6927 TaskStatus_t * pxTaskStatusArray;
6928 UBaseType_t uxArraySize, x;
6929 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
6931 traceENTER_vTaskGetRunTimeStats( pcWriteBuffer );
6936 * This function is provided for convenience only, and is used by many
6937 * of the demo applications. Do not consider it to be part of the
6940 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
6941 * of the uxTaskGetSystemState() output into a human readable table that
6942 * displays the amount of time each task has spent in the Running state
6943 * in both absolute and percentage terms.
6945 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
6946 * function that might bloat the code size, use a lot of stack, and
6947 * provide different results on different platforms. An alternative,
6948 * tiny, third party, and limited functionality implementation of
6949 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
6950 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
6951 * a full snprintf() implementation!).
6953 * It is recommended that production systems call uxTaskGetSystemState()
6954 * directly to get access to raw stats data, rather than indirectly
6955 * through a call to vTaskGetRunTimeStats().
6958 /* Make sure the write buffer does not contain a string. */
6959 *pcWriteBuffer = ( char ) 0x00;
6961 /* Take a snapshot of the number of tasks in case it changes while this
6962 * function is executing. */
6963 uxArraySize = uxCurrentNumberOfTasks;
6965 /* Allocate an array index for each task. NOTE! If
6966 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
6967 * equate to NULL. */
6968 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. */
6970 if( pxTaskStatusArray != NULL )
6972 /* Generate the (binary) data. */
6973 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
6975 /* For percentage calculations. */
6976 ulTotalTime /= 100UL;
6978 /* Avoid divide by zero errors. */
6979 if( ulTotalTime > 0UL )
6981 /* Create a human readable table from the binary data. */
6982 for( x = 0; x < uxArraySize; x++ )
6984 /* What percentage of the total run time has the task used?
6985 * This will always be rounded down to the nearest integer.
6986 * ulTotalRunTime has already been divided by 100. */
6987 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
6989 /* Write the task name to the string, padding with
6990 * spaces so it can be printed in tabular form more
6992 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
6994 if( ulStatsAsPercentage > 0UL )
6996 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
6998 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
7002 /* sizeof( int ) == sizeof( long ) so a smaller
7003 * printf() library can be used. */
7004 sprintf( pcWriteBuffer, "\t%u\t\t%u%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
7010 /* If the percentage is zero here then the task has
7011 * consumed less than 1% of the total run time. */
7012 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7014 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
7018 /* sizeof( int ) == sizeof( long ) so a smaller
7019 * printf() library can be used. */
7020 sprintf( pcWriteBuffer, "\t%u\t\t<1%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
7025 pcWriteBuffer += strlen( pcWriteBuffer ); /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
7030 mtCOVERAGE_TEST_MARKER();
7033 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
7034 * is 0 then vPortFree() will be #defined to nothing. */
7035 vPortFree( pxTaskStatusArray );
7039 mtCOVERAGE_TEST_MARKER();
7042 traceRETURN_vTaskGetRunTimeStats();
7045 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7046 /*-----------------------------------------------------------*/
7048 TickType_t uxTaskResetEventItemValue( void )
7050 TickType_t uxReturn;
7052 traceENTER_uxTaskResetEventItemValue();
7054 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7056 /* Reset the event list item to its normal value - so it can be used with
7057 * queues and semaphores. */
7058 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. */
7060 traceRETURN_uxTaskResetEventItemValue( uxReturn );
7064 /*-----------------------------------------------------------*/
7066 #if ( configUSE_MUTEXES == 1 )
7068 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7072 traceENTER_pvTaskIncrementMutexHeldCount();
7074 pxTCB = pxCurrentTCB;
7076 /* If xSemaphoreCreateMutex() is called before any tasks have been created
7077 * then pxCurrentTCB will be NULL. */
7080 ( pxTCB->uxMutexesHeld )++;
7083 traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7088 #endif /* configUSE_MUTEXES */
7089 /*-----------------------------------------------------------*/
7091 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7093 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7094 BaseType_t xClearCountOnExit,
7095 TickType_t xTicksToWait )
7099 traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7101 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7103 taskENTER_CRITICAL();
7105 /* Only block if the notification count is not already non-zero. */
7106 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0UL )
7108 /* Mark this task as waiting for a notification. */
7109 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7111 if( xTicksToWait > ( TickType_t ) 0 )
7113 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7114 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7116 /* All ports are written to allow a yield in a critical
7117 * section (some will yield immediately, others wait until the
7118 * critical section exits) - but it is not something that
7119 * application code should ever do. */
7120 #if ( configNUMBER_OF_CORES == 1 )
7122 portYIELD_WITHIN_API();
7126 vTaskYieldWithinAPI();
7132 mtCOVERAGE_TEST_MARKER();
7137 mtCOVERAGE_TEST_MARKER();
7140 taskEXIT_CRITICAL();
7142 taskENTER_CRITICAL();
7144 traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7145 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7147 if( ulReturn != 0UL )
7149 if( xClearCountOnExit != pdFALSE )
7151 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = 0UL;
7155 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7160 mtCOVERAGE_TEST_MARKER();
7163 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7165 taskEXIT_CRITICAL();
7167 traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7172 #endif /* configUSE_TASK_NOTIFICATIONS */
7173 /*-----------------------------------------------------------*/
7175 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7177 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7178 uint32_t ulBitsToClearOnEntry,
7179 uint32_t ulBitsToClearOnExit,
7180 uint32_t * pulNotificationValue,
7181 TickType_t xTicksToWait )
7185 traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7187 configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7189 taskENTER_CRITICAL();
7191 /* Only block if a notification is not already pending. */
7192 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7194 /* Clear bits in the task's notification value as bits may get
7195 * set by the notifying task or interrupt. This can be used to
7196 * clear the value to zero. */
7197 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7199 /* Mark this task as waiting for a notification. */
7200 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7202 if( xTicksToWait > ( TickType_t ) 0 )
7204 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7205 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7207 /* All ports are written to allow a yield in a critical
7208 * section (some will yield immediately, others wait until the
7209 * critical section exits) - but it is not something that
7210 * application code should ever do. */
7211 #if ( configNUMBER_OF_CORES == 1 )
7213 portYIELD_WITHIN_API();
7217 vTaskYieldWithinAPI();
7223 mtCOVERAGE_TEST_MARKER();
7228 mtCOVERAGE_TEST_MARKER();
7231 taskEXIT_CRITICAL();
7233 taskENTER_CRITICAL();
7235 traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7237 if( pulNotificationValue != NULL )
7239 /* Output the current notification value, which may or may not
7241 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7244 /* If ucNotifyValue is set then either the task never entered the
7245 * blocked state (because a notification was already pending) or the
7246 * task unblocked because of a notification. Otherwise the task
7247 * unblocked because of a timeout. */
7248 if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7250 /* A notification was not received. */
7255 /* A notification was already pending or a notification was
7256 * received while the task was waiting. */
7257 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7261 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7263 taskEXIT_CRITICAL();
7265 traceRETURN_xTaskGenericNotifyWait( xReturn );
7270 #endif /* configUSE_TASK_NOTIFICATIONS */
7271 /*-----------------------------------------------------------*/
7273 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7275 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7276 UBaseType_t uxIndexToNotify,
7278 eNotifyAction eAction,
7279 uint32_t * pulPreviousNotificationValue )
7282 BaseType_t xReturn = pdPASS;
7283 uint8_t ucOriginalNotifyState;
7285 traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7287 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7288 configASSERT( xTaskToNotify );
7289 pxTCB = xTaskToNotify;
7291 taskENTER_CRITICAL();
7293 if( pulPreviousNotificationValue != NULL )
7295 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7298 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7300 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7305 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7309 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7312 case eSetValueWithOverwrite:
7313 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7316 case eSetValueWithoutOverwrite:
7318 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7320 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7324 /* The value could not be written to the task. */
7332 /* The task is being notified without its notify value being
7338 /* Should not get here if all enums are handled.
7339 * Artificially force an assert by testing a value the
7340 * compiler can't assume is const. */
7341 configASSERT( xTickCount == ( TickType_t ) 0 );
7346 traceTASK_NOTIFY( uxIndexToNotify );
7348 /* If the task is in the blocked state specifically to wait for a
7349 * notification then unblock it now. */
7350 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7352 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7353 prvAddTaskToReadyList( pxTCB );
7355 /* The task should not have been on an event list. */
7356 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7358 #if ( configUSE_TICKLESS_IDLE != 0 )
7360 /* If a task is blocked waiting for a notification then
7361 * xNextTaskUnblockTime might be set to the blocked task's time
7362 * out time. If the task is unblocked for a reason other than
7363 * a timeout xNextTaskUnblockTime is normally left unchanged,
7364 * because it will automatically get reset to a new value when
7365 * the tick count equals xNextTaskUnblockTime. However if
7366 * tickless idling is used it might be more important to enter
7367 * sleep mode at the earliest possible time - so reset
7368 * xNextTaskUnblockTime here to ensure it is updated at the
7369 * earliest possible time. */
7370 prvResetNextTaskUnblockTime();
7374 /* Check if the notified task has a priority above the currently
7375 * executing task. */
7376 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7380 mtCOVERAGE_TEST_MARKER();
7383 taskEXIT_CRITICAL();
7385 traceRETURN_xTaskGenericNotify( xReturn );
7390 #endif /* configUSE_TASK_NOTIFICATIONS */
7391 /*-----------------------------------------------------------*/
7393 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7395 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
7396 UBaseType_t uxIndexToNotify,
7398 eNotifyAction eAction,
7399 uint32_t * pulPreviousNotificationValue,
7400 BaseType_t * pxHigherPriorityTaskWoken )
7403 uint8_t ucOriginalNotifyState;
7404 BaseType_t xReturn = pdPASS;
7405 UBaseType_t uxSavedInterruptStatus;
7407 traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
7409 configASSERT( xTaskToNotify );
7410 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7412 /* RTOS ports that support interrupt nesting have the concept of a
7413 * maximum system call (or maximum API call) interrupt priority.
7414 * Interrupts that are above the maximum system call priority are keep
7415 * permanently enabled, even when the RTOS kernel is in a critical section,
7416 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7417 * is defined in FreeRTOSConfig.h then
7418 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7419 * failure if a FreeRTOS API function is called from an interrupt that has
7420 * been assigned a priority above the configured maximum system call
7421 * priority. Only FreeRTOS functions that end in FromISR can be called
7422 * from interrupts that have been assigned a priority at or (logically)
7423 * below the maximum system call interrupt priority. FreeRTOS maintains a
7424 * separate interrupt safe API to ensure interrupt entry is as fast and as
7425 * simple as possible. More information (albeit Cortex-M specific) is
7426 * provided on the following link:
7427 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7428 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7430 pxTCB = xTaskToNotify;
7432 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7434 if( pulPreviousNotificationValue != NULL )
7436 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7439 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7440 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7445 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7449 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7452 case eSetValueWithOverwrite:
7453 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7456 case eSetValueWithoutOverwrite:
7458 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7460 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7464 /* The value could not be written to the task. */
7472 /* The task is being notified without its notify value being
7478 /* Should not get here if all enums are handled.
7479 * Artificially force an assert by testing a value the
7480 * compiler can't assume is const. */
7481 configASSERT( xTickCount == ( TickType_t ) 0 );
7485 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
7487 /* If the task is in the blocked state specifically to wait for a
7488 * notification then unblock it now. */
7489 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7491 /* The task should not have been on an event list. */
7492 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7494 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
7496 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7497 prvAddTaskToReadyList( pxTCB );
7501 /* The delayed and ready lists cannot be accessed, so hold
7502 * this task pending until the scheduler is resumed. */
7503 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
7506 #if ( configNUMBER_OF_CORES == 1 )
7508 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7510 /* The notified task has a priority above the currently
7511 * executing task so a yield is required. */
7512 if( pxHigherPriorityTaskWoken != NULL )
7514 *pxHigherPriorityTaskWoken = pdTRUE;
7517 /* Mark that a yield is pending in case the user is not
7518 * using the "xHigherPriorityTaskWoken" parameter to an ISR
7519 * safe FreeRTOS function. */
7520 xYieldPendings[ 0 ] = pdTRUE;
7524 mtCOVERAGE_TEST_MARKER();
7527 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
7529 #if ( configUSE_PREEMPTION == 1 )
7531 prvYieldForTask( pxTCB );
7533 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
7535 if( pxHigherPriorityTaskWoken != NULL )
7537 *pxHigherPriorityTaskWoken = pdTRUE;
7541 #endif /* if ( configUSE_PREEMPTION == 1 ) */
7543 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7546 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
7548 traceRETURN_xTaskGenericNotifyFromISR( xReturn );
7553 #endif /* configUSE_TASK_NOTIFICATIONS */
7554 /*-----------------------------------------------------------*/
7556 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7558 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
7559 UBaseType_t uxIndexToNotify,
7560 BaseType_t * pxHigherPriorityTaskWoken )
7563 uint8_t ucOriginalNotifyState;
7564 UBaseType_t uxSavedInterruptStatus;
7566 traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
7568 configASSERT( xTaskToNotify );
7569 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7571 /* RTOS ports that support interrupt nesting have the concept of a
7572 * maximum system call (or maximum API call) interrupt priority.
7573 * Interrupts that are above the maximum system call priority are keep
7574 * permanently enabled, even when the RTOS kernel is in a critical section,
7575 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
7576 * is defined in FreeRTOSConfig.h then
7577 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7578 * failure if a FreeRTOS API function is called from an interrupt that has
7579 * been assigned a priority above the configured maximum system call
7580 * priority. Only FreeRTOS functions that end in FromISR can be called
7581 * from interrupts that have been assigned a priority at or (logically)
7582 * below the maximum system call interrupt priority. FreeRTOS maintains a
7583 * separate interrupt safe API to ensure interrupt entry is as fast and as
7584 * simple as possible. More information (albeit Cortex-M specific) is
7585 * provided on the following link:
7586 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7587 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7589 pxTCB = xTaskToNotify;
7591 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7593 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7594 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7596 /* 'Giving' is equivalent to incrementing a count in a counting
7598 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7600 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
7602 /* If the task is in the blocked state specifically to wait for a
7603 * notification then unblock it now. */
7604 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7606 /* The task should not have been on an event list. */
7607 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7609 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
7611 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7612 prvAddTaskToReadyList( pxTCB );
7616 /* The delayed and ready lists cannot be accessed, so hold
7617 * this task pending until the scheduler is resumed. */
7618 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
7621 #if ( configNUMBER_OF_CORES == 1 )
7623 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7625 /* The notified task has a priority above the currently
7626 * executing task so a yield is required. */
7627 if( pxHigherPriorityTaskWoken != NULL )
7629 *pxHigherPriorityTaskWoken = pdTRUE;
7632 /* Mark that a yield is pending in case the user is not
7633 * using the "xHigherPriorityTaskWoken" parameter in an ISR
7634 * safe FreeRTOS function. */
7635 xYieldPendings[ 0 ] = pdTRUE;
7639 mtCOVERAGE_TEST_MARKER();
7642 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
7644 #if ( configUSE_PREEMPTION == 1 )
7646 prvYieldForTask( pxTCB );
7648 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
7650 if( pxHigherPriorityTaskWoken != NULL )
7652 *pxHigherPriorityTaskWoken = pdTRUE;
7656 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
7658 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7661 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
7663 traceRETURN_vTaskGenericNotifyGiveFromISR();
7666 #endif /* configUSE_TASK_NOTIFICATIONS */
7667 /*-----------------------------------------------------------*/
7669 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7671 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
7672 UBaseType_t uxIndexToClear )
7677 traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
7679 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7681 /* If null is passed in here then it is the calling task that is having
7682 * its notification state cleared. */
7683 pxTCB = prvGetTCBFromHandle( xTask );
7685 taskENTER_CRITICAL();
7687 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
7689 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
7697 taskEXIT_CRITICAL();
7699 traceRETURN_xTaskGenericNotifyStateClear( xReturn );
7704 #endif /* configUSE_TASK_NOTIFICATIONS */
7705 /*-----------------------------------------------------------*/
7707 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7709 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
7710 UBaseType_t uxIndexToClear,
7711 uint32_t ulBitsToClear )
7716 traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
7718 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7720 /* If null is passed in here then it is the calling task that is having
7721 * its notification state cleared. */
7722 pxTCB = prvGetTCBFromHandle( xTask );
7724 taskENTER_CRITICAL();
7726 /* Return the notification as it was before the bits were cleared,
7727 * then clear the bit mask. */
7728 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
7729 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
7731 taskEXIT_CRITICAL();
7733 traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
7738 #endif /* configUSE_TASK_NOTIFICATIONS */
7739 /*-----------------------------------------------------------*/
7741 #if ( configGENERATE_RUN_TIME_STATS == 1 )
7743 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
7747 traceENTER_ulTaskGetRunTimeCounter( xTask );
7749 pxTCB = prvGetTCBFromHandle( xTask );
7751 traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
7753 return pxTCB->ulRunTimeCounter;
7756 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
7757 /*-----------------------------------------------------------*/
7759 #if ( configGENERATE_RUN_TIME_STATS == 1 )
7761 configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
7764 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
7766 traceENTER_ulTaskGetRunTimePercent( xTask );
7768 ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
7770 /* For percentage calculations. */
7771 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
7773 /* Avoid divide by zero errors. */
7774 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
7776 pxTCB = prvGetTCBFromHandle( xTask );
7777 ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
7784 traceRETURN_ulTaskGetRunTimePercent( ulReturn );
7789 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
7790 /*-----------------------------------------------------------*/
7792 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
7794 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
7796 configRUN_TIME_COUNTER_TYPE ulReturn = 0;
7799 traceENTER_ulTaskGetIdleRunTimeCounter();
7801 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
7803 ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
7806 traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
7811 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
7812 /*-----------------------------------------------------------*/
7814 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
7816 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
7818 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
7819 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
7822 traceENTER_ulTaskGetIdleRunTimePercent();
7824 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
7826 /* For percentage calculations. */
7827 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
7829 /* Avoid divide by zero errors. */
7830 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
7832 for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
7834 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
7837 ulReturn = ulRunTimeCounter / ulTotalTime;
7844 traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
7849 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
7850 /*-----------------------------------------------------------*/
7852 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
7853 const BaseType_t xCanBlockIndefinitely )
7855 TickType_t xTimeToWake;
7856 const TickType_t xConstTickCount = xTickCount;
7858 #if ( INCLUDE_xTaskAbortDelay == 1 )
7860 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
7861 * reset to pdFALSE so it can be detected as having been set to pdTRUE
7862 * when the task leaves the Blocked state. */
7863 pxCurrentTCB->ucDelayAborted = pdFALSE;
7867 /* Remove the task from the ready list before adding it to the blocked list
7868 * as the same list item is used for both lists. */
7869 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
7871 /* The current task must be in a ready list, so there is no need to
7872 * check, and the port reset macro can be called directly. */
7873 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. */
7877 mtCOVERAGE_TEST_MARKER();
7880 #if ( INCLUDE_vTaskSuspend == 1 )
7882 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
7884 /* Add the task to the suspended task list instead of a delayed task
7885 * list to ensure it is not woken by a timing event. It will block
7887 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
7891 /* Calculate the time at which the task should be woken if the event
7892 * does not occur. This may overflow but this doesn't matter, the
7893 * kernel will manage it correctly. */
7894 xTimeToWake = xConstTickCount + xTicksToWait;
7896 /* The list item will be inserted in wake time order. */
7897 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
7899 if( xTimeToWake < xConstTickCount )
7901 /* Wake time has overflowed. Place this item in the overflow
7903 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
7904 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7908 /* The wake time has not overflowed, so the current block list
7910 traceMOVED_TASK_TO_DELAYED_LIST();
7911 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7913 /* If the task entering the blocked state was placed at the
7914 * head of the list of blocked tasks then xNextTaskUnblockTime
7915 * needs to be updated too. */
7916 if( xTimeToWake < xNextTaskUnblockTime )
7918 xNextTaskUnblockTime = xTimeToWake;
7922 mtCOVERAGE_TEST_MARKER();
7927 #else /* INCLUDE_vTaskSuspend */
7929 /* Calculate the time at which the task should be woken if the event
7930 * does not occur. This may overflow but this doesn't matter, the kernel
7931 * will manage it correctly. */
7932 xTimeToWake = xConstTickCount + xTicksToWait;
7934 /* The list item will be inserted in wake time order. */
7935 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
7937 if( xTimeToWake < xConstTickCount )
7939 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
7940 /* Wake time has overflowed. Place this item in the overflow list. */
7941 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7945 traceMOVED_TASK_TO_DELAYED_LIST();
7946 /* The wake time has not overflowed, so the current block list is used. */
7947 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
7949 /* If the task entering the blocked state was placed at the head of the
7950 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
7952 if( xTimeToWake < xNextTaskUnblockTime )
7954 xNextTaskUnblockTime = xTimeToWake;
7958 mtCOVERAGE_TEST_MARKER();
7962 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
7963 ( void ) xCanBlockIndefinitely;
7965 #endif /* INCLUDE_vTaskSuspend */
7967 /*-----------------------------------------------------------*/
7969 #if ( portUSING_MPU_WRAPPERS == 1 )
7971 xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
7975 traceENTER_xTaskGetMPUSettings( xTask );
7977 pxTCB = prvGetTCBFromHandle( xTask );
7979 traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
7981 return &( pxTCB->xMPUSettings );
7984 #endif /* portUSING_MPU_WRAPPERS */
7985 /*-----------------------------------------------------------*/
7987 /* Code below here allows additional code to be inserted into this source file,
7988 * especially where access to file scope functions and data is needed (for example
7989 * when performing module tests). */
7991 #ifdef FREERTOS_MODULE_TEST
7992 #include "tasks_test_access_functions.h"
7996 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
7998 #include "freertos_tasks_c_additions.h"
8000 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8001 static void freertos_tasks_c_additions_init( void )
8003 FREERTOS_TASKS_C_ADDITIONS_INIT();
8007 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8008 /*-----------------------------------------------------------*/
8010 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8013 * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8014 * to provide the memory that is used by the Idle task. It is used when
8015 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8016 * it's own implementation of vApplicationGetIdleTaskMemory by setting
8017 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8019 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8020 StackType_t ** ppxIdleTaskStackBuffer,
8021 uint32_t * pulIdleTaskStackSize )
8023 static StaticTask_t xIdleTaskTCB;
8024 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8026 *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8027 *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8028 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8031 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8032 /*-----------------------------------------------------------*/
8034 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8037 * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8038 * to provide the memory that is used by the Timer service task. It is used when
8039 * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8040 * it's own implementation of vApplicationGetTimerTaskMemory by setting
8041 * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8043 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8044 StackType_t ** ppxTimerTaskStackBuffer,
8045 uint32_t * pulTimerTaskStackSize )
8047 static StaticTask_t xTimerTaskTCB;
8048 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8050 *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8051 *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8052 *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8055 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8056 /*-----------------------------------------------------------*/