2 * FreeRTOS SMP Kernel V202110.00
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
27 /* Standard includes. */
31 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
32 * all the API functions to use the MPU wrappers. That should only be done when
33 * task.h is included from an application file. */
34 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
36 /* FreeRTOS includes. */
40 #include "stack_macros.h"
42 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
43 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
44 * for the header files above, but not in this file, in order to generate the
45 * correct privileged Vs unprivileged linkage and placement. */
46 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
48 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
49 * functions but without including stdio.h here. */
50 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
52 /* At the bottom of this file are two optional functions that can be used
53 * to generate human readable text from the raw data generated by the
54 * uxTaskGetSystemState() function. Note the formatting functions are provided
55 * for convenience only, and are NOT considered part of the kernel. */
57 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
59 #if ( configUSE_PREEMPTION == 0 )
61 /* If the cooperative scheduler is being used then a yield should not be
62 * performed just because a higher priority task has been woken. */
63 #define taskYIELD_IF_USING_PREEMPTION()
65 #define taskYIELD_IF_USING_PREEMPTION() vTaskYieldWithinAPI()
68 /* Values that can be assigned to the ucNotifyState member of the TCB. */
69 #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
70 #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
71 #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
74 * The value used to fill the stack of a task when the task is created. This
75 * is used purely for checking the high water mark for tasks.
77 #define tskSTACK_FILL_BYTE ( 0xa5U )
79 /* Bits used to record how a task's stack and TCB were allocated. */
80 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
81 #define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
82 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
84 /* If any of the following are set then task stacks are filled with a known
85 * value so the high water mark can be determined. If none of the following are
86 * set then don't fill the stack so there is no unnecessary dependency on memset. */
87 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
88 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
90 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
94 * Macros used by vListTask to indicate which state a task is in.
96 #define tskRUNNING_CHAR ( 'X' )
97 #define tskBLOCKED_CHAR ( 'B' )
98 #define tskREADY_CHAR ( 'R' )
99 #define tskDELETED_CHAR ( 'D' )
100 #define tskSUSPENDED_CHAR ( 'S' )
103 * Some kernel aware debuggers require the data the debugger needs access to to
104 * be global, rather than file scope.
106 #ifdef portREMOVE_STATIC_QUALIFIER
110 /* The name allocated to the Idle task. This can be overridden by defining
111 * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
112 #ifndef configIDLE_TASK_NAME
113 #define configIDLE_TASK_NAME "IDLE"
116 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
118 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
119 * performed in a generic way that is not optimised to any particular
120 * microcontroller architecture. */
122 /* uxTopReadyPriority holds the priority of the highest priority ready
124 #define taskRECORD_READY_PRIORITY( uxPriority ) \
126 if( ( uxPriority ) > uxTopReadyPriority ) \
128 uxTopReadyPriority = ( uxPriority ); \
130 } /* taskRECORD_READY_PRIORITY */
132 /*-----------------------------------------------------------*/
134 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
135 * they are only required when a port optimised method of task selection is
137 #define taskRESET_READY_PRIORITY( uxPriority )
138 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
140 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
142 #error configUSE_PORT_OPTIMISED_TASK_SELECTION not yet supported in SMP
144 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
145 * performed in a way that is tailored to the particular microcontroller
146 * architecture being used. */
148 /* A port optimised version is provided. Call the port defined macros. */
149 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( uxPriority, uxTopReadyPriority )
151 /*-----------------------------------------------------------*/
153 /* A port optimised version is provided, call it only if the TCB being reset
154 * is being referenced from a ready list. If it is referenced from a delayed
155 * or suspended list then it won't be in a ready list. */
156 #define taskRESET_READY_PRIORITY( uxPriority ) \
158 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
160 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
164 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
166 /*-----------------------------------------------------------*/
168 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
169 * count overflows. */
170 #define taskSWITCH_DELAYED_LISTS() \
174 /* The delayed tasks list should be empty when the lists are switched. */ \
175 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
177 pxTemp = pxDelayedTaskList; \
178 pxDelayedTaskList = pxOverflowDelayedTaskList; \
179 pxOverflowDelayedTaskList = pxTemp; \
181 prvResetNextTaskUnblockTime(); \
184 /*-----------------------------------------------------------*/
187 * Place the task represented by pxTCB into the appropriate ready list for
188 * the task. It is inserted at the end of the list.
190 #define prvAddTaskToReadyList( pxTCB ) \
191 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
192 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
193 vListInsertEnd( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
194 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB )
195 /*-----------------------------------------------------------*/
198 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
199 * where NULL is used to indicate that the handle of the currently executing
200 * task should be used in place of the parameter. This macro simply checks to
201 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
203 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
205 /* The item value of the event list item is normally used to hold the priority
206 * of the task to which it belongs (coded to allow it to be held in reverse
207 * priority order). However, it is occasionally borrowed for other purposes. It
208 * is important its value is not updated due to a task priority change while it is
209 * being used for another purpose. The following bit definition is used to inform
210 * the scheduler that the value should not be changed - in which case it is the
211 * responsibility of whichever module is using the value to ensure it gets set back
212 * to its original value when it is released. */
213 #if ( configUSE_16_BIT_TICKS == 1 )
214 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U
216 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
219 /* Indicates that the task is not actively running on any core. */
220 #define taskTASK_NOT_RUNNING ( TaskRunning_t ) ( -1 )
222 /* Indicates that the task is actively running but scheduled to yield. */
223 #define taskTASK_YIELDING ( TaskRunning_t ) ( -2 )
225 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
226 #define taskTASK_IS_RUNNING( xTaskRunState ) ( ( 0 <= xTaskRunState ) && ( xTaskRunState < configNUM_CORES ) )
228 typedef BaseType_t TaskRunning_t;
231 * Task control block. A task control block (TCB) is allocated for each task,
232 * and stores task state information, including a pointer to the task's context
233 * (the task's run time environment, including register values)
235 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
237 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. */
239 #if ( portUSING_MPU_WRAPPERS == 1 )
240 xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
243 #if ( configUSE_CORE_AFFINITY == 1 && configNUM_CORES > 1 )
244 UBaseType_t uxCoreAffinityMask; /*< Used to link the task to certain cores. UBaseType_t must have >= the same number of bits as SMP confNUM_CORES */
247 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 ). */
248 ListItem_t xEventListItem; /*< Used to reference a task from an event list. */
249 UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */
250 StackType_t * pxStack; /*< Points to the start of the stack. */
251 volatile TaskRunning_t xTaskRunState; /*< Used to identify the core the task is running on, if any. */
252 BaseType_t xIsIdle; /*< Used to identify the idle tasks. */
253 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. */
255 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
256 BaseType_t xPreemptionDisable; /*< Used to prevent the task from being preempted */
259 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
260 StackType_t * pxEndOfStack; /*< Points to the highest valid address for the stack. */
263 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
264 UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
267 #if ( configUSE_TRACE_FACILITY == 1 )
268 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. */
269 UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
272 #if ( configUSE_MUTEXES == 1 )
273 UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
274 UBaseType_t uxMutexesHeld;
277 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
278 TaskHookFunction_t pxTaskTag;
281 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
282 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
285 #if ( configGENERATE_RUN_TIME_STATS == 1 )
286 uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
289 #if ( configUSE_NEWLIB_REENTRANT == 1 )
290 /* Allocate a Newlib reent structure that is specific to this task.
291 * Note Newlib support has been included by popular demand, but is not
292 * used by the FreeRTOS maintainers themselves. FreeRTOS is not
293 * responsible for resulting newlib operation. User must be familiar with
294 * newlib and must provide system-wide implementations of the necessary
295 * stubs. Be warned that (at the time of writing) the current newlib design
296 * implements a system-wide malloc() that must be provided with locks.
298 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
299 * for additional information. */
300 struct _reent xNewLib_reent;
303 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
304 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
305 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
308 /* See the comments in FreeRTOS.h with the definition of
309 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
310 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
311 uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
314 #if ( INCLUDE_xTaskAbortDelay == 1 )
315 uint8_t ucDelayAborted;
318 #if ( configUSE_POSIX_ERRNO == 1 )
323 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
324 * below to enable the use of older kernel aware debuggers. */
325 typedef tskTCB TCB_t;
327 /*lint -save -e956 A manual analysis and inspection has been used to determine
328 * which static variables must be declared volatile. */
329 PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUM_CORES ] = { NULL };
330 #define pxCurrentTCB xTaskGetCurrentTaskHandle()
332 /* Lists for ready and blocked tasks. --------------------
333 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
334 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
335 * the static qualifier. */
336 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */
337 PRIVILEGED_DATA static List_t xDelayedTaskList1; /*< Delayed tasks. */
338 PRIVILEGED_DATA static List_t xDelayedTaskList2; /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
339 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /*< Points to the delayed task list currently being used. */
340 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. */
341 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. */
343 #if ( INCLUDE_vTaskDelete == 1 )
345 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /*< Tasks that have been deleted - but their memory not yet freed. */
346 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
350 #if ( INCLUDE_vTaskSuspend == 1 )
352 PRIVILEGED_DATA static List_t xSuspendedTaskList; /*< Tasks that are currently suspended. */
356 /* Global POSIX errno. Its value is changed upon context switching to match
357 * the errno of the currently running task. */
358 #if ( configUSE_POSIX_ERRNO == 1 )
359 int FreeRTOS_errno = 0;
362 /* Other file private variables. --------------------------------*/
363 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
364 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
365 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
366 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
367 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
368 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUM_CORES ] = { pdFALSE };
369 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
370 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
371 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
372 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandle[ configNUM_CORES ] = { NULL }; /*< Holds the handle of the idle task. The idle task is created automatically when the scheduler is started. */
374 #define xYieldPending prvGetCurrentYieldPending()
376 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
377 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
378 * to determine the number of priority lists to read back from the remote target. */
379 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
381 /* Context switches are held pending while the scheduler is suspended. Also,
382 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
383 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
384 * If an interrupt needs to unblock a task while the scheduler is suspended then it
385 * moves the task's event list item into the xPendingReadyList, ready for the
386 * kernel to move the task from the pending ready list into the real ready list
387 * when the scheduler is unsuspended. The pending ready list itself can only be
388 * accessed from a critical section.
390 * Updates to uxSchedulerSuspended must be protected by both the task and ISR locks and
391 * must not be done by an ISR. Reads must be protected by either lock and may be done by
392 * either an ISR or a task. */
393 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) pdFALSE;
395 #if ( configGENERATE_RUN_TIME_STATS == 1 )
397 /* Do not move these variables to function scope as doing so prevents the
398 * code working with debuggers that need to remove the static qualifier. */
399 PRIVILEGED_DATA static uint32_t ulTaskSwitchedInTime[ configNUM_CORES ] = { 0UL }; /*< Holds the value of a timer/counter the last time a task was switched in. */
400 PRIVILEGED_DATA static volatile uint32_t ulTotalRunTime[ configNUM_CORES ] = { 0UL }; /*< Holds the total amount of execution time as defined by the run time counter clock. */
406 /*-----------------------------------------------------------*/
408 /* File private functions. --------------------------------*/
411 * Creates the idle tasks during scheduler start
413 static BaseType_t prvCreateIdleTasks( void );
416 * Returns the yield pending count for the calling core.
418 static BaseType_t prvGetCurrentYieldPending( void );
421 * Checks to see if another task moved the current task out of the ready
422 * list while it was waiting to enter a critical section and yields if so.
424 static void prvCheckForRunStateChange( void );
427 * Yields the given core.
429 static void prvYieldCore( BaseType_t xCoreID );
432 * Yields a core, or cores if multiple priorities are not allowed to run
433 * simultaneously, to allow the task pxTCB to run.
435 static void prvYieldForTask( TCB_t * pxTCB,
436 const BaseType_t xPreemptEqualPriority );
439 * Selects the highest priority available task
441 static void prvSelectHighestPriorityTask( const BaseType_t xCoreID );
444 * Utility task that simply returns pdTRUE if the task referenced by xTask is
445 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
446 * is in any other state.
448 #if ( INCLUDE_vTaskSuspend == 1 )
450 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
452 #endif /* INCLUDE_vTaskSuspend */
455 * Utility to ready all the lists used by the scheduler. This is called
456 * automatically upon the creation of the first task.
458 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
461 * The idle task, which as all tasks is implemented as a never ending loop.
462 * The idle task is automatically created and added to the ready lists upon
463 * creation of the first user task.
466 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
467 #if ( configNUM_CORES > 1 )
468 static portTASK_FUNCTION_PROTO( prvMinimalIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
472 * Utility to free all memory allocated by the scheduler to hold a TCB,
473 * including the stack pointed to by the TCB.
475 * This does not free memory allocated by the task itself (i.e. memory
476 * allocated by calls to pvPortMalloc from within the tasks application code).
478 #if ( INCLUDE_vTaskDelete == 1 )
480 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
485 * Used only by the idle task. This checks to see if anything has been placed
486 * in the list of tasks waiting to be deleted. If so the task is cleaned up
487 * and its TCB deleted.
489 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
492 * The currently executing task is entering the Blocked state. Add the task to
493 * either the current or the overflow delayed task list.
495 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
496 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
499 * Fills an TaskStatus_t structure with information on each task that is
500 * referenced from the pxList list (which may be a ready list, a delayed list,
501 * a suspended list, etc.).
503 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
504 * NORMAL APPLICATION CODE.
506 #if ( configUSE_TRACE_FACILITY == 1 )
508 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
510 eTaskState eState ) PRIVILEGED_FUNCTION;
515 * Searches pxList for a task with name pcNameToQuery - returning a handle to
516 * the task if it is found, or NULL if the task is not found.
518 #if ( INCLUDE_xTaskGetHandle == 1 )
520 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
521 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
526 * When a task is created, the stack of the task is filled with a known value.
527 * This function determines the 'high water mark' of the task stack by
528 * determining how much of the stack remains at the original preset value.
530 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
532 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
537 * Return the amount of time, in ticks, that will pass before the kernel will
538 * next move a task from the Blocked state to the Running state.
540 * This conditional compilation should use inequality to 0, not equality to 1.
541 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
542 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
543 * set to a value other than 1.
545 #if ( configUSE_TICKLESS_IDLE != 0 )
547 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
552 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
553 * will exit the Blocked state.
555 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
557 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
560 * Helper function used to pad task names with spaces when printing out
561 * human readable tables of task information.
563 static char * prvWriteNameToBuffer( char * pcBuffer,
564 const char * pcTaskName ) PRIVILEGED_FUNCTION;
569 * Called after a Task_t structure has been allocated either statically or
570 * dynamically to fill in the structure's members.
572 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
573 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
574 const uint32_t ulStackDepth,
575 void * const pvParameters,
576 UBaseType_t uxPriority,
577 TaskHandle_t * const pxCreatedTask,
579 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
582 * Called after a new task has been created and initialised to place the task
583 * under the control of the scheduler.
585 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
588 * freertos_tasks_c_additions_init() should only be called if the user definable
589 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
590 * called by the function.
592 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
594 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
598 /*-----------------------------------------------------------*/
600 static BaseType_t prvGetCurrentYieldPending( void )
605 ulState = portDISABLE_INTERRUPTS();
606 xReturn = xYieldPendings[ portGET_CORE_ID() ];
607 portRESTORE_INTERRUPTS( ulState );
612 /*-----------------------------------------------------------*/
614 static void prvCheckForRunStateChange( void )
616 UBaseType_t uxPrevCriticalNesting;
617 UBaseType_t uxPrevSchedulerSuspended;
620 /* This should be skipped when entering a critical section within
621 * an ISR. If the task on the current core is no longer running, then
622 * vTaskSwitchContext() probably should be run before returning, but
623 * we don't have a way to force that to happen from here. */
624 if( portCHECK_IF_IN_ISR() == pdFALSE )
626 /* This function is always called with interrupts disabled
627 * so this is safe. */
628 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
630 while( pxThisTCB->xTaskRunState == taskTASK_YIELDING )
632 /* We are only here if we just entered a critical section
633 * or if we just suspended the scheduler, and another task
634 * has requested that we yield.
636 * This is slightly complicated since we need to save and restore
637 * the suspension and critical nesting counts, as well as release
638 * and reacquire the correct locks. And then do it all over again
639 * if our state changed again during the reacquisition. */
641 uxPrevCriticalNesting = pxThisTCB->uxCriticalNesting;
642 uxPrevSchedulerSuspended = uxSchedulerSuspended;
644 /* this must only be called the first time we enter into a critical
645 * section, otherwise it could context switch in the middle of a
646 * critical section. */
647 configASSERT( uxPrevCriticalNesting + uxPrevSchedulerSuspended == 1U );
649 uxSchedulerSuspended = 0U;
651 if( uxPrevCriticalNesting > 0U )
653 pxThisTCB->uxCriticalNesting = 0U;
654 portRELEASE_ISR_LOCK();
655 portRELEASE_TASK_LOCK();
659 /* uxPrevSchedulerSuspended must be 1 */
660 portRELEASE_TASK_LOCK();
663 portMEMORY_BARRIER();
664 configASSERT( pxThisTCB->xTaskRunState == taskTASK_YIELDING );
666 portENABLE_INTERRUPTS();
668 /* Enabling interrupts should cause this core to immediately
669 * service the pending interrupt and yield. If the run state is still
670 * yielding here then that is a problem. */
671 configASSERT( pxThisTCB->xTaskRunState != taskTASK_YIELDING );
673 portDISABLE_INTERRUPTS();
676 pxCurrentTCB->uxCriticalNesting = uxPrevCriticalNesting;
677 uxSchedulerSuspended = uxPrevSchedulerSuspended;
679 if( uxPrevCriticalNesting == 0U )
681 /* uxPrevSchedulerSuspended must be 1 */
682 configASSERT( uxPrevSchedulerSuspended != ( UBaseType_t ) pdFALSE );
683 portRELEASE_ISR_LOCK();
689 /*-----------------------------------------------------------*/
691 static void prvYieldCore( BaseType_t xCoreID )
693 /* This must be called from a critical section and
694 * xCoreID must be valid. */
696 if( portCHECK_IF_IN_ISR() && ( xCoreID == portGET_CORE_ID() ) )
698 xYieldPendings[ xCoreID ] = pdTRUE;
700 else if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_YIELDING )
702 if( xCoreID == portGET_CORE_ID() )
704 xYieldPendings[ xCoreID ] = pdTRUE;
706 #if ( configNUM_CORES > 1 )
709 portYIELD_CORE( xCoreID );
710 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_YIELDING;
716 /*-----------------------------------------------------------*/
718 static void prvYieldForTask( TCB_t * pxTCB,
719 const BaseType_t xPreemptEqualPriority )
721 BaseType_t xLowestPriority;
722 BaseType_t xTaskPriority;
723 BaseType_t xLowestPriorityCore = -1;
724 BaseType_t xYieldCount = 0;
726 TaskRunning_t xTaskRunState;
728 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION */
730 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
732 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
734 /* No task should yield for this one if it is a lower priority
735 * than priority level of currently ready tasks. */
736 if( pxTCB->uxPriority < uxTopReadyPriority )
743 xLowestPriority = ( BaseType_t ) pxTCB->uxPriority;
745 if( xPreemptEqualPriority == pdFALSE )
747 /* xLowestPriority will be decremented to -1 if the priority of pxTCB
748 * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
752 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUM_CORES; x++ )
754 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here */
755 xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ x ]->uxPriority - pxCurrentTCBs[ x ]->xIsIdle;
756 xTaskRunState = pxCurrentTCBs[ x ]->xTaskRunState;
758 if( ( taskTASK_IS_RUNNING( xTaskRunState ) != pdFALSE ) && ( xYieldPendings[ x ] == pdFALSE ) )
760 if( xTaskPriority <= xLowestPriority )
762 #if ( configNUM_CORES > 1 )
763 #if ( configUSE_CORE_AFFINITY == 1 )
764 if( ( pxTCB->uxCoreAffinityMask & ( 1 << x ) ) != 0 )
768 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
769 if( pxCurrentTCBs[ x ]->xPreemptionDisable == pdFALSE )
772 xLowestPriority = xTaskPriority;
773 xLowestPriorityCore = x;
779 mtCOVERAGE_TEST_MARKER();
782 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) && 1
784 /* Yield all currently running non-idle tasks with a priority lower than
785 * the task that needs to run. */
786 if( ( ( BaseType_t ) tskIDLE_PRIORITY - 1 < xTaskPriority ) && ( xTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
793 mtCOVERAGE_TEST_MARKER();
796 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) && 1 */
800 mtCOVERAGE_TEST_MARKER();
804 if( ( xYieldCount == 0 ) && taskVALID_CORE_ID( xLowestPriorityCore ) )
806 prvYieldCore( xLowestPriorityCore );
810 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
811 /* Verify that the calling core always yields to higher priority tasks */
812 if( !pxCurrentTCBs[ portGET_CORE_ID() ]->xIsIdle && ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
814 configASSERT( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE || taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ]->xTaskRunState ) == pdFALSE );
818 /*-----------------------------------------------------------*/
820 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
822 static void prvSelectHighestPriorityTask( const BaseType_t xCoreID )
824 UBaseType_t uxCurrentPriority = uxTopReadyPriority;
825 BaseType_t xTaskScheduled = pdFALSE;
826 BaseType_t xDecrementTopPriority = pdTRUE;
828 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
829 TCB_t * pxPreviousTCB = NULL;
831 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
832 BaseType_t xPriorityDropped = pdFALSE;
835 /* This function should be called when scheduler is running. */
836 configASSERT( xSchedulerRunning == pdTRUE );
838 while( xTaskScheduled == pdFALSE )
840 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
842 if( uxCurrentPriority < uxTopReadyPriority )
844 /* We can't schedule any tasks, other than idle, that have a
845 * priority lower than the priority of a task currently running
846 * on another core. */
847 uxCurrentPriority = tskIDLE_PRIORITY;
852 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
854 List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
855 ListItem_t * pxLastTaskItem = pxReadyList->pxIndex->pxPrevious;
856 ListItem_t * pxTaskItem = pxLastTaskItem;
858 if( ( void * ) pxLastTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
860 pxLastTaskItem = pxLastTaskItem->pxPrevious;
863 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
864 * must not be decremented any further */
865 xDecrementTopPriority = pdFALSE;
871 pxTaskItem = pxTaskItem->pxNext;
873 if( ( void * ) pxTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
875 pxTaskItem = pxTaskItem->pxNext;
878 pxTCB = pxTaskItem->pvOwner;
880 /*debug_printf("Attempting to schedule %s on core %d\n", pxTCB->pcTaskName, portGET_CORE_ID() ); */
882 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
884 /* When falling back to the idle priority because only one priority
885 * level is allowed to run at a time, we should ONLY schedule the true
886 * idle tasks, not user tasks at the idle priority. */
887 if( uxCurrentPriority < uxTopReadyPriority )
889 if( pxTCB->xIsIdle == pdFALSE )
895 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
897 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
899 #if ( configNUM_CORES > 1 )
900 #if ( configUSE_CORE_AFFINITY == 1 )
901 if( ( pxTCB->uxCoreAffinityMask & ( 1 << xCoreID ) ) != 0 )
905 /* If the task is not being executed by any core swap it in */
906 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
907 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
908 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
910 pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
911 pxCurrentTCBs[ xCoreID ] = pxTCB;
912 xTaskScheduled = pdTRUE;
915 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
917 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_YIELDING ) );
918 #if ( configNUM_CORES > 1 )
919 #if ( configUSE_CORE_AFFINITY == 1 )
920 if( ( pxTCB->uxCoreAffinityMask & ( 1 << xCoreID ) ) != 0 )
924 /* The task is already running on this core, mark it as scheduled */
925 pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
926 xTaskScheduled = pdTRUE;
930 if( xTaskScheduled != pdFALSE )
932 /* Once a task has been selected to run on this core,
933 * move it to the end of the ready task list. */
934 uxListRemove( pxTaskItem );
935 vListInsertEnd( pxReadyList, pxTaskItem );
938 } while( pxTaskItem != pxLastTaskItem );
942 if( xDecrementTopPriority != pdFALSE )
944 uxTopReadyPriority--;
945 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
947 xPriorityDropped = pdTRUE;
953 /* Theare are configNUMBER_OF_CORES Idle tasks created when scheduler started.
954 * The scheduler should be able to select a task to run when uxCurrentPriority
955 * is tskIDLE_PRIORITY. */
956 configASSERT( ( uxCurrentPriority > tskIDLE_PRIORITY ) || ( xTaskScheduled == pdTRUE ) );
960 configASSERT( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ]->xTaskRunState ) );
962 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
963 if( xPriorityDropped != pdFALSE )
965 /* There may be several ready tasks that were being prevented from running because there was
966 * a higher priority task running. Now that the last of the higher priority tasks is no longer
967 * running, make sure all the other idle tasks yield. */
970 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUM_CORES; x++ )
972 if( pxCurrentTCBs[ x ]->xIsIdle != pdFALSE )
978 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
980 #if ( configNUM_CORES > 1 )
981 #if ( configUSE_CORE_AFFINITY == 1 )
982 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
984 /* A ready task was just bumped off this core. Look at the cores it can run from
985 * from to see if it is able to run on any of them */
986 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
987 BaseType_t xLowestPriority = pxPreviousTCB->uxPriority - pxPreviousTCB->xIsIdle;
988 BaseType_t xLowestPriorityCore = -1;
990 if( ( uxCoreMap & ( 1 << xCoreID ) ) != 0 )
992 /* The ready task that was removed from this core is not excluded from it.
993 * Only look at the intersection of the cores the removed task is allowed to run
994 * on with the cores that the new task is excluded from. It is possible that the
995 * new task was only placed onto this core because it is excluded from another.
996 * Check to see if the previous task could run on one of those cores. */
997 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1001 /* The ready task that was removed from this core is excluded from it. */
1004 uxCoreMap &= ( ( 1 << configNUM_CORES ) - 1 );
1006 while( uxCoreMap != 0 )
1008 int uxCore = 31UL - ( uint32_t ) __builtin_clz( uxCoreMap );
1010 configASSERT( taskVALID_CORE_ID( uxCore ) );
1012 uxCoreMap &= ~( 1 << uxCore );
1014 BaseType_t xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority - pxCurrentTCBs[ uxCore ]->xIsIdle;
1016 if( ( xTaskPriority < xLowestPriority ) && ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ]->xTaskRunState ) != pdFALSE ) && ( xYieldPendings[ uxCore ] == pdFALSE ) )
1018 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1019 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1022 xLowestPriority = xTaskPriority;
1023 xLowestPriorityCore = uxCore;
1028 if( taskVALID_CORE_ID( xLowestPriorityCore ) )
1030 prvYieldCore( xLowestPriorityCore );
1033 #endif /* if ( configUSE_CORE_AFFINITY == 1 ) */
1034 #endif /* if ( configNUM_CORES > 1 ) */
1037 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1039 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
1041 UBaseType_t uxTopPriority;
1043 /* Find the highest priority list that contains ready tasks. */
1044 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority );
1045 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 );
1046 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) );
1049 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1050 /*-----------------------------------------------------------*/
1052 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1054 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1055 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1056 const uint32_t ulStackDepth,
1057 void * const pvParameters,
1058 UBaseType_t uxPriority,
1059 StackType_t * const puxStackBuffer,
1060 StaticTask_t * const pxTaskBuffer )
1061 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1063 return xTaskCreateStaticAffinitySet(pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, tskNO_AFFINITY);
1066 TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1067 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1068 const uint32_t ulStackDepth,
1069 void * const pvParameters,
1070 UBaseType_t uxPriority,
1071 StackType_t * const puxStackBuffer,
1072 StaticTask_t * const pxTaskBuffer,
1073 UBaseType_t uxCoreAffinityMask )
1074 #endif /* ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) */
1077 TaskHandle_t xReturn;
1079 configASSERT( puxStackBuffer != NULL );
1080 configASSERT( pxTaskBuffer != NULL );
1082 #if ( configASSERT_DEFINED == 1 )
1084 /* Sanity check that the size of the structure used to declare a
1085 * variable of type StaticTask_t equals the size of the real task
1087 volatile size_t xSize = sizeof( StaticTask_t );
1088 configASSERT( xSize == sizeof( TCB_t ) );
1089 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1091 #endif /* configASSERT_DEFINED */
1093 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1095 /* The memory used for the task's TCB and stack are passed into this
1096 * function - use them. */
1097 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. */
1098 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1100 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1102 /* Tasks can be created statically or dynamically, so note this
1103 * task was created statically in case the task is later deleted. */
1104 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1106 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1108 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1110 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1112 /* Set the task's affinity before scheduling it */
1113 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1117 prvAddNewTaskToReadyList( pxNewTCB );
1127 #endif /* SUPPORT_STATIC_ALLOCATION */
1128 /*-----------------------------------------------------------*/
1130 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1132 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1133 TaskHandle_t * pxCreatedTask )
1134 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1136 return xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1139 BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1140 UBaseType_t uxCoreAffinityMask,
1141 TaskHandle_t * pxCreatedTask )
1142 #endif /* ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) */
1145 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1147 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1148 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1150 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1152 /* Allocate space for the TCB. Where the memory comes from depends
1153 * on the implementation of the port malloc function and whether or
1154 * not static allocation is being used. */
1155 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1157 /* Store the stack location in the TCB. */
1158 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1160 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1162 /* Tasks can be created statically or dynamically, so note this
1163 * task was created statically in case the task is later deleted. */
1164 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1166 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1168 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1169 pxTaskDefinition->pcName,
1170 ( uint32_t ) pxTaskDefinition->usStackDepth,
1171 pxTaskDefinition->pvParameters,
1172 pxTaskDefinition->uxPriority,
1173 pxCreatedTask, pxNewTCB,
1174 pxTaskDefinition->xRegions );
1176 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1178 /* Set the task's affinity before scheduling it */
1179 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1183 prvAddNewTaskToReadyList( pxNewTCB );
1190 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1191 /*-----------------------------------------------------------*/
1193 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1195 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1196 TaskHandle_t * pxCreatedTask )
1197 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1199 return xTaskCreateRestrictedAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1202 BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1203 UBaseType_t uxCoreAffinityMask,
1204 TaskHandle_t * pxCreatedTask )
1205 #endif /* ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) */
1208 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1210 configASSERT( pxTaskDefinition->puxStackBuffer );
1212 if( pxTaskDefinition->puxStackBuffer != NULL )
1214 /* Allocate space for the TCB. Where the memory comes from depends
1215 * on the implementation of the port malloc function and whether or
1216 * not static allocation is being used. */
1217 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1219 if( pxNewTCB != NULL )
1221 /* Store the stack location in the TCB. */
1222 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1224 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1226 /* Tasks can be created statically or dynamically, so note
1227 * this task had a statically allocated stack in case it is
1228 * later deleted. The TCB was allocated dynamically. */
1229 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1231 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1233 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1234 pxTaskDefinition->pcName,
1235 ( uint32_t ) pxTaskDefinition->usStackDepth,
1236 pxTaskDefinition->pvParameters,
1237 pxTaskDefinition->uxPriority,
1238 pxCreatedTask, pxNewTCB,
1239 pxTaskDefinition->xRegions );
1241 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1243 /* Set the task's affinity before scheduling it */
1244 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1248 prvAddNewTaskToReadyList( pxNewTCB );
1256 #endif /* portUSING_MPU_WRAPPERS */
1257 /*-----------------------------------------------------------*/
1259 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1261 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1262 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1263 const configSTACK_DEPTH_TYPE usStackDepth,
1264 void * const pvParameters,
1265 UBaseType_t uxPriority,
1266 TaskHandle_t * const pxCreatedTask )
1267 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1269 return xTaskCreateAffinitySet(pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, tskNO_AFFINITY, pxCreatedTask);
1272 BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1273 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1274 const configSTACK_DEPTH_TYPE usStackDepth,
1275 void * const pvParameters,
1276 UBaseType_t uxPriority,
1277 UBaseType_t uxCoreAffinityMask,
1278 TaskHandle_t * const pxCreatedTask )
1279 #endif /* ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) */
1284 /* If the stack grows down then allocate the stack then the TCB so the stack
1285 * does not grow into the TCB. Likewise if the stack grows up then allocate
1286 * the TCB then the stack. */
1287 #if ( portSTACK_GROWTH > 0 )
1289 /* Allocate space for the TCB. Where the memory comes from depends on
1290 * the implementation of the port malloc function and whether or not static
1291 * allocation is being used. */
1292 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1294 if( pxNewTCB != NULL )
1296 /* Allocate space for the stack used by the task being created.
1297 * The base of the stack memory stored in the TCB so the task can
1298 * be deleted later if required. */
1299 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1301 if( pxNewTCB->pxStack == NULL )
1303 /* Could not allocate the stack. Delete the allocated TCB. */
1304 vPortFree( pxNewTCB );
1309 #else /* portSTACK_GROWTH */
1311 StackType_t * pxStack;
1313 /* Allocate space for the stack used by the task being created. */
1314 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. */
1316 if( pxStack != NULL )
1318 /* Allocate space for the TCB. */
1319 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. */
1321 if( pxNewTCB != NULL )
1323 /* Store the stack location in the TCB. */
1324 pxNewTCB->pxStack = pxStack;
1328 /* The stack cannot be used as the TCB was not created. Free
1330 vPortFreeStack( pxStack );
1338 #endif /* portSTACK_GROWTH */
1340 if( pxNewTCB != NULL )
1342 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1344 /* Tasks can be created statically or dynamically, so note this
1345 * task was created dynamically in case it is later deleted. */
1346 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1348 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1350 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1352 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1354 /* Set the task's affinity before scheduling it */
1355 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1359 prvAddNewTaskToReadyList( pxNewTCB );
1364 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1370 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1371 /*-----------------------------------------------------------*/
1373 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1374 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1375 const uint32_t ulStackDepth,
1376 void * const pvParameters,
1377 UBaseType_t uxPriority,
1378 TaskHandle_t * const pxCreatedTask,
1380 const MemoryRegion_t * const xRegions )
1382 StackType_t * pxTopOfStack;
1385 #if ( portUSING_MPU_WRAPPERS == 1 )
1386 /* Should the task be created in privileged mode? */
1387 BaseType_t xRunPrivileged;
1389 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1391 xRunPrivileged = pdTRUE;
1395 xRunPrivileged = pdFALSE;
1397 uxPriority &= ~portPRIVILEGE_BIT;
1398 #endif /* portUSING_MPU_WRAPPERS == 1 */
1400 /* Avoid dependency on memset() if it is not required. */
1401 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1403 /* Fill the stack with a known value to assist debugging. */
1404 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1406 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1408 /* Calculate the top of stack address. This depends on whether the stack
1409 * grows from high memory to low (as per the 80x86) or vice versa.
1410 * portSTACK_GROWTH is used to make the result positive or negative as required
1412 #if ( portSTACK_GROWTH < 0 )
1414 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1415 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(). */
1417 /* Check the alignment of the calculated top of stack is correct. */
1418 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1420 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1422 /* Also record the stack's high address, which may assist
1424 pxNewTCB->pxEndOfStack = pxTopOfStack;
1426 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1428 #else /* portSTACK_GROWTH */
1430 pxTopOfStack = pxNewTCB->pxStack;
1432 /* Check the alignment of the stack buffer is correct. */
1433 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1435 /* The other extreme of the stack space is required if stack checking is
1437 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1439 #endif /* portSTACK_GROWTH */
1441 /* Store the task name in the TCB. */
1442 if( pcName != NULL )
1444 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1446 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1448 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1449 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1450 * string is not accessible (extremely unlikely). */
1451 if( pcName[ x ] == ( char ) 0x00 )
1457 mtCOVERAGE_TEST_MARKER();
1461 /* Ensure the name string is terminated in the case that the string length
1462 * was greater or equal to configMAX_TASK_NAME_LEN. */
1463 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1467 /* The task has not been given a name, so just ensure there is a NULL
1468 * terminator when it is read out. */
1469 pxNewTCB->pcTaskName[ 0 ] = 0x00;
1472 /* This is used as an array index so must ensure it's not too large. First
1473 * remove the privilege bit if one is present. */
1474 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1476 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1480 mtCOVERAGE_TEST_MARKER();
1483 pxNewTCB->uxPriority = uxPriority;
1484 #if ( configUSE_MUTEXES == 1 )
1486 pxNewTCB->uxBasePriority = uxPriority;
1487 pxNewTCB->uxMutexesHeld = 0;
1489 #endif /* configUSE_MUTEXES */
1491 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1492 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1494 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1495 * back to the containing TCB from a generic item in a list. */
1496 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1498 /* Event lists are always in priority order. */
1499 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. */
1500 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1502 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
1504 pxNewTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
1506 #endif /* portCRITICAL_NESTING_IN_TCB */
1508 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
1510 pxNewTCB->pxTaskTag = NULL;
1512 #endif /* configUSE_APPLICATION_TASK_TAG */
1514 #if ( configGENERATE_RUN_TIME_STATS == 1 )
1516 pxNewTCB->ulRunTimeCounter = 0UL;
1518 #endif /* configGENERATE_RUN_TIME_STATS */
1520 #if ( portUSING_MPU_WRAPPERS == 1 )
1522 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1526 /* Avoid compiler warning about unreferenced parameter. */
1531 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
1533 memset( ( void * ) &( pxNewTCB->pvThreadLocalStoragePointers[ 0 ] ), 0x00, sizeof( pxNewTCB->pvThreadLocalStoragePointers ) );
1537 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1539 memset( ( void * ) &( pxNewTCB->ulNotifiedValue[ 0 ] ), 0x00, sizeof( pxNewTCB->ulNotifiedValue ) );
1540 memset( ( void * ) &( pxNewTCB->ucNotifyState[ 0 ] ), 0x00, sizeof( pxNewTCB->ucNotifyState ) );
1544 #if ( configUSE_NEWLIB_REENTRANT == 1 )
1546 /* Initialise this task's Newlib reent structure.
1547 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
1548 * for additional information. */
1549 _REENT_INIT_PTR( ( &( pxNewTCB->xNewLib_reent ) ) );
1553 #if ( INCLUDE_xTaskAbortDelay == 1 )
1555 pxNewTCB->ucDelayAborted = pdFALSE;
1559 #if ( configNUM_CORES > 1 )
1560 #if ( configUSE_CORE_AFFINITY == 1 )
1562 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1566 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1568 pxNewTCB->xPreemptionDisable = 0;
1572 /* Initialize the TCB stack to look as if the task was already running,
1573 * but had been interrupted by the scheduler. The return address is set
1574 * to the start of the task function. Once the stack has been initialised
1575 * the top of stack variable is updated. */
1576 #if ( portUSING_MPU_WRAPPERS == 1 )
1578 /* If the port has capability to detect stack overflow,
1579 * pass the stack end address to the stack initialization
1580 * function as well. */
1581 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1583 #if ( portSTACK_GROWTH < 0 )
1585 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
1587 #else /* portSTACK_GROWTH */
1589 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1591 #endif /* portSTACK_GROWTH */
1593 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1595 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1597 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1599 #else /* portUSING_MPU_WRAPPERS */
1601 /* If the port has capability to detect stack overflow,
1602 * pass the stack end address to the stack initialization
1603 * function as well. */
1604 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1606 #if ( portSTACK_GROWTH < 0 )
1608 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1610 #else /* portSTACK_GROWTH */
1612 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1614 #endif /* portSTACK_GROWTH */
1616 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1618 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1620 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1622 #endif /* portUSING_MPU_WRAPPERS */
1624 /* Initialize to not running */
1625 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1627 /* Is this an idle task? */
1628 if( pxTaskCode == prvIdleTask )
1630 pxNewTCB->xIsIdle = pdTRUE;
1633 #if ( configNUM_CORES > 1 )
1634 else if( pxTaskCode == prvMinimalIdleTask )
1636 pxNewTCB->xIsIdle = pdTRUE;
1641 pxNewTCB->xIsIdle = pdFALSE;
1644 if( pxCreatedTask != NULL )
1646 /* Pass the handle out in an anonymous way. The handle can be used to
1647 * change the created task's priority, delete the created task, etc.*/
1648 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1652 mtCOVERAGE_TEST_MARKER();
1655 /*-----------------------------------------------------------*/
1657 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1659 /* Ensure interrupts don't access the task lists while the lists are being
1661 taskENTER_CRITICAL();
1663 uxCurrentNumberOfTasks++;
1665 if( xSchedulerRunning == pdFALSE )
1667 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1669 /* This is the first task to be created so do the preliminary
1670 * initialisation required. We will not recover if this call
1671 * fails, but we will report the failure. */
1672 prvInitialiseTaskLists();
1676 mtCOVERAGE_TEST_MARKER();
1679 if( pxNewTCB->xIsIdle != pdFALSE )
1683 /* Check if a core is free. */
1684 for( xCoreID = ( UBaseType_t ) 0; xCoreID < ( UBaseType_t ) configNUM_CORES; xCoreID++ )
1686 if( pxCurrentTCBs[ xCoreID ] == NULL )
1688 pxNewTCB->xTaskRunState = xCoreID;
1689 pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1697 mtCOVERAGE_TEST_MARKER();
1702 #if ( configUSE_TRACE_FACILITY == 1 )
1704 /* Add a counter into the TCB for tracing only. */
1705 pxNewTCB->uxTCBNumber = uxTaskNumber;
1707 #endif /* configUSE_TRACE_FACILITY */
1708 traceTASK_CREATE( pxNewTCB );
1710 prvAddTaskToReadyList( pxNewTCB );
1712 portSETUP_TCB( pxNewTCB );
1714 if( xSchedulerRunning != pdFALSE )
1716 /* If the created task is of a higher priority than another
1717 * currently running task and preemption is on then it should
1719 #if ( configUSE_PREEMPTION == 1 )
1720 prvYieldForTask( pxNewTCB, pdFALSE );
1725 mtCOVERAGE_TEST_MARKER();
1728 taskEXIT_CRITICAL();
1730 /*-----------------------------------------------------------*/
1732 #if ( INCLUDE_vTaskDelete == 1 )
1734 void vTaskDelete( TaskHandle_t xTaskToDelete )
1737 TaskRunning_t xTaskRunningOnCore;
1739 taskENTER_CRITICAL();
1741 /* If null is passed in here then it is the calling task that is
1743 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1745 xTaskRunningOnCore = pxTCB->xTaskRunState;
1747 /* Remove task from the ready/delayed list. */
1748 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1750 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1754 mtCOVERAGE_TEST_MARKER();
1757 /* Is the task waiting on an event also? */
1758 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1760 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1764 mtCOVERAGE_TEST_MARKER();
1767 /* Increment the uxTaskNumber also so kernel aware debuggers can
1768 * detect that the task lists need re-generating. This is done before
1769 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1773 /* If the task is running (or yielding), we must add it to the
1774 * termination list so that an idle task can delete it when it is
1775 * no longer running. */
1776 if( xTaskRunningOnCore != taskTASK_NOT_RUNNING )
1778 /* A running task is being deleted. This cannot complete within the
1779 * task itself, as a context switch to another task is required.
1780 * Place the task in the termination list. The idle task will
1781 * check the termination list and free up any memory allocated by
1782 * the scheduler for the TCB and stack of the deleted task. */
1783 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1785 /* Increment the ucTasksDeleted variable so the idle task knows
1786 * there is a task that has been deleted and that it should therefore
1787 * check the xTasksWaitingTermination list. */
1788 ++uxDeletedTasksWaitingCleanUp;
1790 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1791 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1792 traceTASK_DELETE( pxTCB );
1794 /* The pre-delete hook is primarily for the Windows simulator,
1795 * in which Windows specific clean up operations are performed,
1796 * after which it is not possible to yield away from this task -
1797 * hence xYieldPending is used to latch that a context switch is
1799 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPendings[ pxTCB->xTaskRunState ] );
1803 --uxCurrentNumberOfTasks;
1804 traceTASK_DELETE( pxTCB );
1805 prvDeleteTCB( pxTCB );
1807 /* Reset the next expected unblock time in case it referred to
1808 * the task that has just been deleted. */
1809 prvResetNextTaskUnblockTime();
1812 /* Force a reschedule if the task that has just been deleted was running. */
1813 if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( xTaskRunningOnCore ) ) )
1817 xCoreID = portGET_CORE_ID();
1819 if( xTaskRunningOnCore == xCoreID )
1821 configASSERT( uxSchedulerSuspended == 0 );
1822 vTaskYieldWithinAPI();
1826 prvYieldCore( xTaskRunningOnCore );
1830 taskEXIT_CRITICAL();
1833 #endif /* INCLUDE_vTaskDelete */
1834 /*-----------------------------------------------------------*/
1836 #if ( INCLUDE_xTaskDelayUntil == 1 )
1838 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
1839 const TickType_t xTimeIncrement )
1841 TickType_t xTimeToWake;
1842 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
1844 configASSERT( pxPreviousWakeTime );
1845 configASSERT( ( xTimeIncrement > 0U ) );
1849 configASSERT( uxSchedulerSuspended == 1 );
1851 /* Minor optimisation. The tick count cannot change in this
1853 const TickType_t xConstTickCount = xTickCount;
1855 /* Generate the tick time at which the task wants to wake. */
1856 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
1858 if( xConstTickCount < *pxPreviousWakeTime )
1860 /* The tick count has overflowed since this function was
1861 * lasted called. In this case the only time we should ever
1862 * actually delay is if the wake time has also overflowed,
1863 * and the wake time is greater than the tick time. When this
1864 * is the case it is as if neither time had overflowed. */
1865 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1867 xShouldDelay = pdTRUE;
1871 mtCOVERAGE_TEST_MARKER();
1876 /* The tick time has not overflowed. In this case we will
1877 * delay if either the wake time has overflowed, and/or the
1878 * tick time is less than the wake time. */
1879 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1881 xShouldDelay = pdTRUE;
1885 mtCOVERAGE_TEST_MARKER();
1889 /* Update the wake time ready for the next call. */
1890 *pxPreviousWakeTime = xTimeToWake;
1892 if( xShouldDelay != pdFALSE )
1894 traceTASK_DELAY_UNTIL( xTimeToWake );
1896 /* prvAddCurrentTaskToDelayedList() needs the block time, not
1897 * the time to wake, so subtract the current tick count. */
1898 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
1902 mtCOVERAGE_TEST_MARKER();
1905 xAlreadyYielded = xTaskResumeAll();
1907 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1908 * have put ourselves to sleep. */
1909 if( xAlreadyYielded == pdFALSE )
1911 vTaskYieldWithinAPI();
1915 mtCOVERAGE_TEST_MARKER();
1918 return xShouldDelay;
1921 #endif /* INCLUDE_xTaskDelayUntil */
1922 /*-----------------------------------------------------------*/
1924 #if ( INCLUDE_vTaskDelay == 1 )
1926 void vTaskDelay( const TickType_t xTicksToDelay )
1928 BaseType_t xAlreadyYielded = pdFALSE;
1930 /* A delay time of zero just forces a reschedule. */
1931 if( xTicksToDelay > ( TickType_t ) 0U )
1935 configASSERT( uxSchedulerSuspended == 1 );
1938 /* A task that is removed from the event list while the
1939 * scheduler is suspended will not get placed in the ready
1940 * list or removed from the blocked list until the scheduler
1943 * This task cannot be in an event list as it is the currently
1944 * executing task. */
1945 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
1947 xAlreadyYielded = xTaskResumeAll();
1951 mtCOVERAGE_TEST_MARKER();
1954 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1955 * have put ourselves to sleep. */
1956 if( xAlreadyYielded == pdFALSE )
1958 vTaskYieldWithinAPI();
1962 mtCOVERAGE_TEST_MARKER();
1966 #endif /* INCLUDE_vTaskDelay */
1967 /*-----------------------------------------------------------*/
1969 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
1971 eTaskState eTaskGetState( TaskHandle_t xTask )
1974 List_t const * pxStateList, * pxDelayedList, * pxOverflowedDelayedList;
1975 const TCB_t * const pxTCB = xTask;
1977 configASSERT( pxTCB );
1979 taskENTER_CRITICAL();
1981 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
1982 pxDelayedList = pxDelayedTaskList;
1983 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
1985 taskEXIT_CRITICAL();
1987 if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
1989 /* The task being queried is referenced from one of the Blocked
1994 #if ( INCLUDE_vTaskSuspend == 1 )
1995 else if( pxStateList == &xSuspendedTaskList )
1997 /* The task being queried is referenced from the suspended
1998 * list. Is it genuinely suspended or is it blocked
2000 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2002 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2006 /* The task does not appear on the event list item of
2007 * and of the RTOS objects, but could still be in the
2008 * blocked state if it is waiting on its notification
2009 * rather than waiting on an object. If not, is
2011 eReturn = eSuspended;
2013 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2015 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2022 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2024 eReturn = eSuspended;
2026 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2033 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2035 #if ( INCLUDE_vTaskDelete == 1 )
2036 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2038 /* The task being queried is referenced from the deleted
2039 * tasks list, or it is not referenced from any lists at
2045 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
2047 /* If the task is not in any other state, it must be in the
2048 * Ready (including pending ready) state. */
2049 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2051 /* Is it actively running on a core? */
2061 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2063 #endif /* INCLUDE_eTaskGetState */
2064 /*-----------------------------------------------------------*/
2066 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2068 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2070 TCB_t const * pxTCB;
2071 UBaseType_t uxReturn;
2073 taskENTER_CRITICAL();
2075 /* If null is passed in here then it is the priority of the task
2076 * that called uxTaskPriorityGet() that is being queried. */
2077 pxTCB = prvGetTCBFromHandle( xTask );
2078 uxReturn = pxTCB->uxPriority;
2080 taskEXIT_CRITICAL();
2085 #endif /* INCLUDE_uxTaskPriorityGet */
2086 /*-----------------------------------------------------------*/
2088 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2090 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2092 TCB_t const * pxTCB;
2093 UBaseType_t uxReturn, uxSavedInterruptState;
2095 /* RTOS ports that support interrupt nesting have the concept of a
2096 * maximum system call (or maximum API call) interrupt priority.
2097 * Interrupts that are above the maximum system call priority are keep
2098 * permanently enabled, even when the RTOS kernel is in a critical section,
2099 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2100 * is defined in FreeRTOSConfig.h then
2101 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2102 * failure if a FreeRTOS API function is called from an interrupt that has
2103 * been assigned a priority above the configured maximum system call
2104 * priority. Only FreeRTOS functions that end in FromISR can be called
2105 * from interrupts that have been assigned a priority at or (logically)
2106 * below the maximum system call interrupt priority. FreeRTOS maintains a
2107 * separate interrupt safe API to ensure interrupt entry is as fast and as
2108 * simple as possible. More information (albeit Cortex-M specific) is
2109 * provided on the following link:
2110 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2111 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2113 uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
2115 /* If null is passed in here then it is the priority of the calling
2116 * task that is being queried. */
2117 pxTCB = prvGetTCBFromHandle( xTask );
2118 uxReturn = pxTCB->uxPriority;
2120 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
2125 #endif /* INCLUDE_uxTaskPriorityGet */
2126 /*-----------------------------------------------------------*/
2128 #if ( INCLUDE_vTaskPrioritySet == 1 )
2130 void vTaskPrioritySet( TaskHandle_t xTask,
2131 UBaseType_t uxNewPriority )
2134 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2135 BaseType_t xYieldRequired = pdFALSE;
2136 BaseType_t xYieldForTask = pdFALSE;
2139 configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
2141 /* Ensure the new priority is valid. */
2142 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2144 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2148 mtCOVERAGE_TEST_MARKER();
2151 taskENTER_CRITICAL();
2153 /* If null is passed in here then it is the priority of the calling
2154 * task that is being changed. */
2155 pxTCB = prvGetTCBFromHandle( xTask );
2157 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2159 #if ( configUSE_MUTEXES == 1 )
2161 uxCurrentBasePriority = pxTCB->uxBasePriority;
2165 uxCurrentBasePriority = pxTCB->uxPriority;
2169 if( uxCurrentBasePriority != uxNewPriority )
2171 /* The priority change may have readied a task of higher
2172 * priority than a running task. */
2173 if( uxNewPriority > uxCurrentBasePriority )
2175 /* The priority of a task is being raised so
2176 * perform a yield for this task later. */
2177 xYieldForTask = pdTRUE;
2179 else if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2181 /* Setting the priority of a running task down means
2182 * there may now be another task of higher priority that
2183 * is ready to execute. */
2184 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2185 if( pxTCB->xPreemptionDisable == pdFALSE )
2188 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2189 xYieldRequired = pdTRUE;
2194 /* Setting the priority of any other task down does not
2195 * require a yield as the running task must be above the
2196 * new priority of the task being modified. */
2199 /* Remember the ready list the task might be referenced from
2200 * before its uxPriority member is changed so the
2201 * taskRESET_READY_PRIORITY() macro can function correctly. */
2202 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2204 #if ( configUSE_MUTEXES == 1 )
2206 /* Only change the priority being used if the task is not
2207 * currently using an inherited priority. */
2208 if( pxTCB->uxBasePriority == pxTCB->uxPriority )
2210 pxTCB->uxPriority = uxNewPriority;
2214 mtCOVERAGE_TEST_MARKER();
2217 /* The base priority gets set whatever. */
2218 pxTCB->uxBasePriority = uxNewPriority;
2220 #else /* if ( configUSE_MUTEXES == 1 ) */
2222 pxTCB->uxPriority = uxNewPriority;
2224 #endif /* if ( configUSE_MUTEXES == 1 ) */
2226 /* Only reset the event list item value if the value is not
2227 * being used for anything else. */
2228 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2230 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. */
2234 mtCOVERAGE_TEST_MARKER();
2237 /* If the task is in the blocked or suspended list we need do
2238 * nothing more than change its priority variable. However, if
2239 * the task is in a ready list it needs to be removed and placed
2240 * in the list appropriate to its new priority. */
2241 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2243 /* The task is currently in its ready list - remove before
2244 * adding it to its new ready list. As we are in a critical
2245 * section we can do this even if the scheduler is suspended. */
2246 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2248 /* It is known that the task is in its ready list so
2249 * there is no need to check again and the port level
2250 * reset macro can be called directly. */
2251 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2255 mtCOVERAGE_TEST_MARKER();
2258 prvAddTaskToReadyList( pxTCB );
2262 /* It's possible that xYieldForTask was already set to pdTRUE because
2263 * its priority is being raised. However, since it is not in a ready list
2264 * we don't actually need to yield for it. */
2265 xYieldForTask = pdFALSE;
2268 #if ( configUSE_PREEMPTION == 1 )
2269 if( xYieldRequired != pdFALSE )
2271 prvYieldCore( xCoreID );
2273 else if( xYieldForTask != pdFALSE )
2275 prvYieldForTask( pxTCB, pdTRUE );
2279 mtCOVERAGE_TEST_MARKER();
2281 #endif /* if ( configUSE_PREEMPTION == 1 ) */
2283 /* Remove compiler warning about unused variables when the port
2284 * optimised task selection is not being used. */
2285 ( void ) uxPriorityUsedOnEntry;
2288 taskEXIT_CRITICAL();
2291 #endif /* INCLUDE_vTaskPrioritySet */
2292 /*-----------------------------------------------------------*/
2294 #if ( configNUM_CORES > 1 )
2295 #if ( configUSE_CORE_AFFINITY == 1 )
2297 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2298 UBaseType_t uxCoreAffinityMask )
2303 taskENTER_CRITICAL();
2305 pxTCB = prvGetTCBFromHandle( xTask );
2307 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2309 if( xSchedulerRunning != pdFALSE )
2311 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2313 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2315 if( ( uxCoreAffinityMask & ( 1 << xCoreID ) ) == 0 )
2317 prvYieldCore( xCoreID );
2322 taskEXIT_CRITICAL();
2325 #endif /* configUSE_CORE_AFFINITY */
2326 #endif /* if ( configNUM_CORES > 1 ) */
2327 /*-----------------------------------------------------------*/
2329 #if ( configNUM_CORES > 1 )
2330 #if ( configUSE_CORE_AFFINITY == 1 )
2332 UBaseType_t vTaskCoreAffinityGet( const TaskHandle_t xTask )
2335 UBaseType_t uxCoreAffinityMask;
2337 taskENTER_CRITICAL();
2339 pxTCB = prvGetTCBFromHandle( xTask );
2340 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2342 taskEXIT_CRITICAL();
2344 return uxCoreAffinityMask;
2347 #endif /* configUSE_CORE_AFFINITY */
2348 #endif /* if ( configNUM_CORES > 1 ) */
2350 /*-----------------------------------------------------------*/
2352 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2354 void vTaskPreemptionDisable( const TaskHandle_t xTask )
2358 taskENTER_CRITICAL();
2360 pxTCB = prvGetTCBFromHandle( xTask );
2362 pxTCB->xPreemptionDisable = pdTRUE;
2364 taskEXIT_CRITICAL();
2367 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2368 /*-----------------------------------------------------------*/
2370 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2372 void vTaskPreemptionEnable( const TaskHandle_t xTask )
2377 taskENTER_CRITICAL();
2379 pxTCB = prvGetTCBFromHandle( xTask );
2381 pxTCB->xPreemptionDisable = pdFALSE;
2383 if( xSchedulerRunning != pdFALSE )
2385 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2387 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2388 prvYieldCore( xCoreID );
2392 taskEXIT_CRITICAL();
2395 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2396 /*-----------------------------------------------------------*/
2398 #if ( INCLUDE_vTaskSuspend == 1 )
2400 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2403 TaskRunning_t xTaskRunningOnCore;
2405 taskENTER_CRITICAL();
2407 /* If null is passed in here then it is the running task that is
2408 * being suspended. */
2409 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2411 traceTASK_SUSPEND( pxTCB );
2413 xTaskRunningOnCore = pxTCB->xTaskRunState;
2415 /* Remove task from the ready/delayed list and place in the
2416 * suspended list. */
2417 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2419 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2423 mtCOVERAGE_TEST_MARKER();
2426 /* Is the task waiting on an event also? */
2427 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2429 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2433 mtCOVERAGE_TEST_MARKER();
2436 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2438 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2442 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2444 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2446 /* The task was blocked to wait for a notification, but is
2447 * now suspended, so no notification was received. */
2448 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2452 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2454 if( xSchedulerRunning != pdFALSE )
2456 /* Reset the next expected unblock time in case it referred to the
2457 * task that is now in the Suspended state. */
2458 prvResetNextTaskUnblockTime();
2462 mtCOVERAGE_TEST_MARKER();
2465 if( taskTASK_IS_RUNNING( xTaskRunningOnCore ) )
2467 if( xSchedulerRunning != pdFALSE )
2469 if( xTaskRunningOnCore == portGET_CORE_ID() )
2471 /* The current task has just been suspended. */
2472 configASSERT( uxSchedulerSuspended == 0 );
2473 vTaskYieldWithinAPI();
2477 prvYieldCore( xTaskRunningOnCore );
2482 /* This code path is not possible because only Idle tasks are
2483 * assigned a core before the scheduler is started ( i.e.
2484 * taskTASK_IS_RUNNING is only true for idle tasks before
2485 * the scheduler is started ) and idle tasks cannot be
2487 mtCOVERAGE_TEST_MARKER();
2492 mtCOVERAGE_TEST_MARKER();
2495 taskEXIT_CRITICAL();
2498 #endif /* INCLUDE_vTaskSuspend */
2499 /*-----------------------------------------------------------*/
2501 #if ( INCLUDE_vTaskSuspend == 1 )
2503 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
2505 BaseType_t xReturn = pdFALSE;
2506 const TCB_t * const pxTCB = xTask;
2508 /* Accesses xPendingReadyList so must be called from a critical section. */
2510 /* It does not make sense to check if the calling task is suspended. */
2511 configASSERT( xTask );
2513 /* Is the task being resumed actually in the suspended list? */
2514 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
2516 /* Has the task already been resumed from within an ISR? */
2517 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
2519 /* Is it in the suspended list because it is in the Suspended
2520 * state, or because is is blocked with no timeout? */
2521 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
2527 mtCOVERAGE_TEST_MARKER();
2532 mtCOVERAGE_TEST_MARKER();
2537 mtCOVERAGE_TEST_MARKER();
2541 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2543 #endif /* INCLUDE_vTaskSuspend */
2544 /*-----------------------------------------------------------*/
2546 #if ( INCLUDE_vTaskSuspend == 1 )
2548 void vTaskResume( TaskHandle_t xTaskToResume )
2550 TCB_t * const pxTCB = xTaskToResume;
2552 /* It does not make sense to resume the calling task. */
2553 configASSERT( xTaskToResume );
2555 /* The parameter cannot be NULL as it is impossible to resume the
2556 * currently executing task. It is also impossible to resume a task
2557 * that is actively running on another core but it is too dangerous
2558 * to check their run state here. Safer to get into a critical section
2559 * and check if it is actually suspended or not below. */
2562 taskENTER_CRITICAL();
2564 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2566 traceTASK_RESUME( pxTCB );
2568 /* The ready list can be accessed even if the scheduler is
2569 * suspended because this is inside a critical section. */
2570 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2571 prvAddTaskToReadyList( pxTCB );
2573 /* A higher priority task may have just been resumed. */
2574 #if ( configUSE_PREEMPTION == 1 )
2576 prvYieldForTask( pxTCB, pdTRUE );
2582 mtCOVERAGE_TEST_MARKER();
2585 taskEXIT_CRITICAL();
2589 mtCOVERAGE_TEST_MARKER();
2593 #endif /* INCLUDE_vTaskSuspend */
2595 /*-----------------------------------------------------------*/
2597 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
2599 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
2601 BaseType_t xYieldRequired = pdFALSE;
2602 TCB_t * const pxTCB = xTaskToResume;
2603 UBaseType_t uxSavedInterruptStatus;
2605 configASSERT( xTaskToResume );
2607 /* RTOS ports that support interrupt nesting have the concept of a
2608 * maximum system call (or maximum API call) interrupt priority.
2609 * Interrupts that are above the maximum system call priority are keep
2610 * permanently enabled, even when the RTOS kernel is in a critical section,
2611 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2612 * is defined in FreeRTOSConfig.h then
2613 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2614 * failure if a FreeRTOS API function is called from an interrupt that has
2615 * been assigned a priority above the configured maximum system call
2616 * priority. Only FreeRTOS functions that end in FromISR can be called
2617 * from interrupts that have been assigned a priority at or (logically)
2618 * below the maximum system call interrupt priority. FreeRTOS maintains a
2619 * separate interrupt safe API to ensure interrupt entry is as fast and as
2620 * simple as possible. More information (albeit Cortex-M specific) is
2621 * provided on the following link:
2622 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2623 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2625 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2627 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2629 traceTASK_RESUME_FROM_ISR( pxTCB );
2631 /* Check the ready lists can be accessed. */
2632 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2634 /* Ready lists can be accessed so move the task from the
2635 * suspended list to the ready list directly. */
2637 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2638 prvAddTaskToReadyList( pxTCB );
2642 /* The delayed or ready lists cannot be accessed so the task
2643 * is held in the pending ready list until the scheduler is
2645 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
2648 #if ( configUSE_PREEMPTION == 1 )
2649 prvYieldForTask( pxTCB, pdTRUE );
2651 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
2653 xYieldRequired = pdTRUE;
2659 mtCOVERAGE_TEST_MARKER();
2662 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2664 return xYieldRequired;
2667 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
2668 /*-----------------------------------------------------------*/
2670 static BaseType_t prvCreateIdleTasks( void )
2672 BaseType_t xReturn = pdPASS;
2674 char cIdleName[ configMAX_TASK_NAME_LEN ];
2676 /* Add each idle task at the lowest priority. */
2677 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUM_CORES; xCoreID++ )
2681 if( xReturn == pdFAIL )
2687 mtCOVERAGE_TEST_MARKER();
2690 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
2692 cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
2694 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
2695 * configMAX_TASK_NAME_LEN characters just in case the memory after the
2696 * string is not accessible (extremely unlikely). */
2697 if( cIdleName[ x ] == ( char ) 0x00 )
2703 mtCOVERAGE_TEST_MARKER();
2707 /* Append the idle task number to the end of the name if there is space */
2708 if( x < configMAX_TASK_NAME_LEN )
2710 cIdleName[ x++ ] = xCoreID + '0';
2712 /* And append a null character if there is space */
2713 if( x < configMAX_TASK_NAME_LEN )
2715 cIdleName[ x ] = '\0';
2719 mtCOVERAGE_TEST_MARKER();
2724 mtCOVERAGE_TEST_MARKER();
2727 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
2731 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
2732 StackType_t * pxIdleTaskStackBuffer = NULL;
2733 uint32_t ulIdleTaskStackSize;
2735 /* The Idle task is created using user provided RAM - obtain the
2736 * address of the RAM then create the idle task. */
2737 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
2738 xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
2740 ulIdleTaskStackSize,
2741 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
2742 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2743 pxIdleTaskStackBuffer,
2744 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2747 #if ( configNUM_CORES > 1 )
2750 static StaticTask_t xIdleTCBBuffers[ configNUM_CORES - 1 ];
2751 static StackType_t xIdleTaskStackBuffers[ configNUM_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
2753 xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvMinimalIdleTask,
2755 configMINIMAL_STACK_SIZE,
2756 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
2757 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2758 xIdleTaskStackBuffers[ xCoreID - 1 ],
2759 &xIdleTCBBuffers[ xCoreID - 1 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2761 #endif /* if ( configNUM_CORES > 1 ) */
2763 if( xIdleTaskHandle[ xCoreID ] != NULL )
2772 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
2776 /* The Idle task is being created using dynamically allocated RAM. */
2777 xReturn = xTaskCreate( prvIdleTask,
2779 configMINIMAL_STACK_SIZE,
2781 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2782 &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2785 #if ( configNUM_CORES > 1 )
2788 xReturn = xTaskCreate( prvMinimalIdleTask,
2790 configMINIMAL_STACK_SIZE,
2792 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2793 &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2797 #endif /* configSUPPORT_STATIC_ALLOCATION */
2803 void vTaskStartScheduler( void )
2807 #if ( configUSE_TIMERS == 1 )
2809 xReturn = xTimerCreateTimerTask();
2811 #endif /* configUSE_TIMERS */
2813 xReturn = prvCreateIdleTasks();
2815 if( xReturn == pdPASS )
2817 /* freertos_tasks_c_additions_init() should only be called if the user
2818 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
2819 * the only macro called by the function. */
2820 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
2822 freertos_tasks_c_additions_init();
2826 /* Interrupts are turned off here, to ensure a tick does not occur
2827 * before or during the call to xPortStartScheduler(). The stacks of
2828 * the created tasks contain a status word with interrupts switched on
2829 * so interrupts will automatically get re-enabled when the first task
2831 portDISABLE_INTERRUPTS();
2833 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) && ( configNEWLIB_REENTRANT_IS_DYNAMIC == 0 ) )
2835 /* Switch Newlib's _impure_ptr variable to point to the _reent
2836 * structure specific to the task that will run first.
2837 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
2838 * for additional information.
2840 * Note: Updating the _impure_ptr is not required when Newlib is compiled with
2841 * __DYNAMIC_REENT__ enabled. The port should provide __getreent() instead. */
2842 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
2844 #endif /* ( configUSE_NEWLIB_REENTRANT == 1 ) && ( configNEWLIB_REENTRANT_IS_DYNAMIC == 0 ) */
2846 xNextTaskUnblockTime = portMAX_DELAY;
2847 xSchedulerRunning = pdTRUE;
2848 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
2850 /* If configGENERATE_RUN_TIME_STATS is defined then the following
2851 * macro must be defined to configure the timer/counter used to generate
2852 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
2853 * is set to 0 and the following line fails to build then ensure you do not
2854 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
2855 * FreeRTOSConfig.h file. */
2856 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
2858 traceTASK_SWITCHED_IN();
2860 /* Setting up the timer tick is hardware specific and thus in the
2861 * portable interface. */
2862 if( xPortStartScheduler() != pdFALSE )
2864 /* Should not reach here as if the scheduler is running the
2865 * function will not return. */
2869 /* Should only reach here if a task calls xTaskEndScheduler(). */
2874 /* This line will only be reached if the kernel could not be started,
2875 * because there was not enough FreeRTOS heap to create the idle task
2876 * or the timer task. */
2877 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
2880 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
2881 * meaning xIdleTaskHandle is not used anywhere else. */
2882 ( void ) xIdleTaskHandle;
2884 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
2885 * from getting optimized out as it is no longer used by the kernel. */
2886 ( void ) uxTopUsedPriority;
2888 /*-----------------------------------------------------------*/
2890 void vTaskEndScheduler( void )
2892 /* Stop the scheduler interrupts and call the portable scheduler end
2893 * routine so the original ISRs can be restored if necessary. The port
2894 * layer must ensure interrupts enable bit is left in the correct state. */
2895 portDISABLE_INTERRUPTS();
2896 xSchedulerRunning = pdFALSE;
2897 vPortEndScheduler();
2899 /*----------------------------------------------------------*/
2901 void vTaskSuspendAll( void )
2903 UBaseType_t ulState;
2905 /* This must only be called from within a task */
2906 portASSERT_IF_IN_ISR();
2908 if( xSchedulerRunning != pdFALSE )
2910 /* writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
2911 * We must disable interrupts before we grab the locks in the event that this task is
2912 * interrupted and switches context before incrementing uxSchedulerSuspended.
2913 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
2914 * uxSchedulerSuspended since that will prevent context switches. */
2915 ulState = portDISABLE_INTERRUPTS();
2917 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
2918 * do not otherwise exhibit real time behaviour. */
2919 portSOFTWARE_BARRIER();
2921 portGET_TASK_LOCK();
2924 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
2925 * is used to allow calls to vTaskSuspendAll() to nest. */
2926 ++uxSchedulerSuspended;
2927 portRELEASE_ISR_LOCK();
2929 if( ( uxSchedulerSuspended == 1U ) && ( pxCurrentTCB->uxCriticalNesting == 0U ) )
2931 prvCheckForRunStateChange();
2934 portRESTORE_INTERRUPTS( ulState );
2938 mtCOVERAGE_TEST_MARKER();
2941 /*----------------------------------------------------------*/
2943 #if ( configUSE_TICKLESS_IDLE != 0 )
2945 static TickType_t prvGetExpectedIdleTime( void )
2948 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
2950 /* uxHigherPriorityReadyTasks takes care of the case where
2951 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
2952 * task that are in the Ready state, even though the idle task is
2954 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
2956 if( uxTopReadyPriority > tskIDLE_PRIORITY )
2958 uxHigherPriorityReadyTasks = pdTRUE;
2963 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
2965 /* When port optimised task selection is used the uxTopReadyPriority
2966 * variable is used as a bit map. If bits other than the least
2967 * significant bit are set then there are tasks that have a priority
2968 * above the idle priority that are in the Ready state. This takes
2969 * care of the case where the co-operative scheduler is in use. */
2970 if( uxTopReadyPriority > uxLeastSignificantBit )
2972 uxHigherPriorityReadyTasks = pdTRUE;
2975 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
2977 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
2981 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
2983 /* There are other idle priority tasks in the ready state. If
2984 * time slicing is used then the very next tick interrupt must be
2988 else if( uxHigherPriorityReadyTasks != pdFALSE )
2990 /* There are tasks in the Ready state that have a priority above the
2991 * idle priority. This path can only be reached if
2992 * configUSE_PREEMPTION is 0. */
2997 xReturn = xNextTaskUnblockTime - xTickCount;
3003 #endif /* configUSE_TICKLESS_IDLE */
3004 /*----------------------------------------------------------*/
3006 BaseType_t xTaskResumeAll( void )
3008 TCB_t * pxTCB = NULL;
3009 BaseType_t xAlreadyYielded = pdFALSE;
3011 if( xSchedulerRunning != pdFALSE )
3013 /* It is possible that an ISR caused a task to be removed from an event
3014 * list while the scheduler was suspended. If this was the case then the
3015 * removed task will have been added to the xPendingReadyList. Once the
3016 * scheduler has been resumed it is safe to move all the pending ready
3017 * tasks from this list into their appropriate ready list. */
3018 taskENTER_CRITICAL();
3022 xCoreID = portGET_CORE_ID();
3024 /* If uxSchedulerSuspended is zero then this function does not match a
3025 * previous call to vTaskSuspendAll(). */
3026 configASSERT( uxSchedulerSuspended );
3028 --uxSchedulerSuspended;
3029 portRELEASE_TASK_LOCK();
3031 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3033 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3035 /* Move any readied tasks from the pending list into the
3036 * appropriate ready list. */
3037 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3039 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) ); /*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. */
3040 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3041 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3042 prvAddTaskToReadyList( pxTCB );
3044 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
3045 * If the current core yielded then vTaskSwitchContext() has already been called
3046 * which sets xYieldPendings for the current core to pdTRUE. */
3051 /* A task was unblocked while the scheduler was suspended,
3052 * which may have prevented the next unblock time from being
3053 * re-calculated, in which case re-calculate it now. Mainly
3054 * important for low power tickless implementations, where
3055 * this can prevent an unnecessary exit from low power
3057 prvResetNextTaskUnblockTime();
3060 /* If any ticks occurred while the scheduler was suspended then
3061 * they should be processed now. This ensures the tick count does
3062 * not slip, and that any delayed tasks are resumed at the correct
3065 * It should be safe to call xTaskIncrementTick here from any core
3066 * since we are in a critical section and xTaskIncrementTick itself
3067 * protects itself within a critical section. Suspending the scheduler
3068 * from any core causes xTaskIncrementTick to increment uxPendedCounts.*/
3070 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3072 if( xPendedCounts > ( TickType_t ) 0U )
3076 if( xTaskIncrementTick() != pdFALSE )
3078 /* other cores are interrupted from
3079 * within xTaskIncrementTick(). */
3080 xYieldPendings[ xCoreID ] = pdTRUE;
3084 mtCOVERAGE_TEST_MARKER();
3088 } while( xPendedCounts > ( TickType_t ) 0U );
3094 mtCOVERAGE_TEST_MARKER();
3098 if( xYieldPendings[ xCoreID ] != pdFALSE )
3100 /* If xYieldPendings is true then taskEXIT_CRITICAL()
3101 * will yield, so make sure we return true to let the
3102 * caller know a yield has already happened. */
3103 xAlreadyYielded = pdTRUE;
3109 mtCOVERAGE_TEST_MARKER();
3112 taskEXIT_CRITICAL();
3116 mtCOVERAGE_TEST_MARKER();
3119 return xAlreadyYielded;
3121 /*-----------------------------------------------------------*/
3123 TickType_t xTaskGetTickCount( void )
3127 /* Critical section required if running on a 16 bit processor. */
3128 portTICK_TYPE_ENTER_CRITICAL();
3130 xTicks = xTickCount;
3132 portTICK_TYPE_EXIT_CRITICAL();
3136 /*-----------------------------------------------------------*/
3138 TickType_t xTaskGetTickCountFromISR( void )
3141 UBaseType_t uxSavedInterruptStatus;
3143 /* RTOS ports that support interrupt nesting have the concept of a maximum
3144 * system call (or maximum API call) interrupt priority. Interrupts that are
3145 * above the maximum system call priority are kept permanently enabled, even
3146 * when the RTOS kernel is in a critical section, but cannot make any calls to
3147 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
3148 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3149 * failure if a FreeRTOS API function is called from an interrupt that has been
3150 * assigned a priority above the configured maximum system call priority.
3151 * Only FreeRTOS functions that end in FromISR can be called from interrupts
3152 * that have been assigned a priority at or (logically) below the maximum
3153 * system call interrupt priority. FreeRTOS maintains a separate interrupt
3154 * safe API to ensure interrupt entry is as fast and as simple as possible.
3155 * More information (albeit Cortex-M specific) is provided on the following
3156 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3157 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3159 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
3161 xReturn = xTickCount;
3163 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3167 /*-----------------------------------------------------------*/
3169 UBaseType_t uxTaskGetNumberOfTasks( void )
3171 /* A critical section is not required because the variables are of type
3173 return uxCurrentNumberOfTasks;
3175 /*-----------------------------------------------------------*/
3177 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3181 /* If null is passed in here then the name of the calling task is being
3183 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3184 configASSERT( pxTCB );
3185 return &( pxTCB->pcTaskName[ 0 ] );
3187 /*-----------------------------------------------------------*/
3189 #if ( INCLUDE_xTaskGetHandle == 1 )
3191 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3192 const char pcNameToQuery[] )
3194 TCB_t * pxNextTCB, * pxFirstTCB, * pxReturn = NULL;
3197 BaseType_t xBreakLoop;
3199 /* This function is called with the scheduler suspended. */
3201 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3203 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. */
3207 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. */
3209 /* Check each character in the name looking for a match or
3211 xBreakLoop = pdFALSE;
3213 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3215 cNextChar = pxNextTCB->pcTaskName[ x ];
3217 if( cNextChar != pcNameToQuery[ x ] )
3219 /* Characters didn't match. */
3220 xBreakLoop = pdTRUE;
3222 else if( cNextChar == ( char ) 0x00 )
3224 /* Both strings terminated, a match must have been
3226 pxReturn = pxNextTCB;
3227 xBreakLoop = pdTRUE;
3231 mtCOVERAGE_TEST_MARKER();
3234 if( xBreakLoop != pdFALSE )
3240 if( pxReturn != NULL )
3242 /* The handle has been found. */
3245 } while( pxNextTCB != pxFirstTCB );
3249 mtCOVERAGE_TEST_MARKER();
3255 #endif /* INCLUDE_xTaskGetHandle */
3256 /*-----------------------------------------------------------*/
3258 #if ( INCLUDE_xTaskGetHandle == 1 )
3260 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3262 UBaseType_t uxQueue = configMAX_PRIORITIES;
3265 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
3266 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
3270 /* Search the ready lists. */
3274 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
3278 /* Found the handle. */
3281 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3283 /* Search the delayed lists. */
3286 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
3291 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
3294 #if ( INCLUDE_vTaskSuspend == 1 )
3298 /* Search the suspended list. */
3299 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
3304 #if ( INCLUDE_vTaskDelete == 1 )
3308 /* Search the deleted list. */
3309 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
3314 ( void ) xTaskResumeAll();
3319 #endif /* INCLUDE_xTaskGetHandle */
3320 /*-----------------------------------------------------------*/
3322 #if ( configUSE_TRACE_FACILITY == 1 )
3324 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
3325 const UBaseType_t uxArraySize,
3326 uint32_t * const pulTotalRunTime )
3328 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
3332 /* Is there a space in the array for each task in the system? */
3333 if( uxArraySize >= uxCurrentNumberOfTasks )
3335 /* Fill in an TaskStatus_t structure with information on each
3336 * task in the Ready state. */
3340 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
3341 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3343 /* Fill in an TaskStatus_t structure with information on each
3344 * task in the Blocked state. */
3345 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
3346 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
3348 #if ( INCLUDE_vTaskDelete == 1 )
3350 /* Fill in an TaskStatus_t structure with information on
3351 * each task that has been deleted but not yet cleaned up. */
3352 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
3356 #if ( INCLUDE_vTaskSuspend == 1 )
3358 /* Fill in an TaskStatus_t structure with information on
3359 * each task in the Suspended state. */
3360 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
3364 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3366 if( pulTotalRunTime != NULL )
3368 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3369 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
3371 *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3375 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3377 if( pulTotalRunTime != NULL )
3379 *pulTotalRunTime = 0;
3382 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3386 mtCOVERAGE_TEST_MARKER();
3389 ( void ) xTaskResumeAll();
3394 #endif /* configUSE_TRACE_FACILITY */
3395 /*----------------------------------------------------------*/
3397 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
3399 TaskHandle_t * xTaskGetIdleTaskHandle( void )
3401 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
3402 * started, then xIdleTaskHandle will be NULL. */
3403 configASSERT( ( xIdleTaskHandle != NULL ) );
3404 return &( xIdleTaskHandle[ 0 ] );
3407 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
3408 /*----------------------------------------------------------*/
3410 /* This conditional compilation should use inequality to 0, not equality to 1.
3411 * This is to ensure vTaskStepTick() is available when user defined low power mode
3412 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
3414 #if ( configUSE_TICKLESS_IDLE != 0 )
3416 void vTaskStepTick( const TickType_t xTicksToJump )
3418 /* Correct the tick count value after a period during which the tick
3419 * was suppressed. Note this does *not* call the tick hook function for
3420 * each stepped tick. */
3421 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
3422 xTickCount += xTicksToJump;
3423 traceINCREASE_TICK_COUNT( xTicksToJump );
3426 #endif /* configUSE_TICKLESS_IDLE */
3427 /*----------------------------------------------------------*/
3429 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
3431 BaseType_t xYieldOccurred;
3433 /* Must not be called with the scheduler suspended as the implementation
3434 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
3435 configASSERT( uxSchedulerSuspended == 0 );
3437 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
3438 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
3440 xPendedTicks += xTicksToCatchUp;
3441 xYieldOccurred = xTaskResumeAll();
3443 return xYieldOccurred;
3445 /*----------------------------------------------------------*/
3447 #if ( INCLUDE_xTaskAbortDelay == 1 )
3449 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
3451 TCB_t * pxTCB = xTask;
3454 configASSERT( pxTCB );
3458 /* A task can only be prematurely removed from the Blocked state if
3459 * it is actually in the Blocked state. */
3460 if( eTaskGetState( xTask ) == eBlocked )
3464 /* Remove the reference to the task from the blocked list. An
3465 * interrupt won't touch the xStateListItem because the
3466 * scheduler is suspended. */
3467 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3469 /* Is the task waiting on an event also? If so remove it from
3470 * the event list too. Interrupts can touch the event list item,
3471 * even though the scheduler is suspended, so a critical section
3473 taskENTER_CRITICAL();
3475 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3477 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3479 /* This lets the task know it was forcibly removed from the
3480 * blocked state so it should not re-evaluate its block time and
3481 * then block again. */
3482 pxTCB->ucDelayAborted = pdTRUE;
3486 mtCOVERAGE_TEST_MARKER();
3489 taskEXIT_CRITICAL();
3491 /* Place the unblocked task into the appropriate ready list. */
3492 prvAddTaskToReadyList( pxTCB );
3494 /* A task being unblocked cannot cause an immediate context
3495 * switch if preemption is turned off. */
3496 #if ( configUSE_PREEMPTION == 1 )
3498 taskENTER_CRITICAL();
3500 prvYieldForTask( pxTCB, pdFALSE );
3502 taskEXIT_CRITICAL();
3504 #endif /* configUSE_PREEMPTION */
3511 ( void ) xTaskResumeAll();
3516 #endif /* INCLUDE_xTaskAbortDelay */
3517 /*----------------------------------------------------------*/
3519 BaseType_t xTaskIncrementTick( void )
3522 TickType_t xItemValue;
3523 BaseType_t xSwitchRequired = pdFALSE;
3525 #if ( configUSE_PREEMPTION == 1 )
3527 BaseType_t xCoreYieldList[ configNUM_CORES ] = { pdFALSE };
3528 #endif /* configUSE_PREEMPTION */
3530 taskENTER_CRITICAL();
3532 /* Called by the portable layer each time a tick interrupt occurs.
3533 * Increments the tick then checks to see if the new tick value will cause any
3534 * tasks to be unblocked. */
3535 traceTASK_INCREMENT_TICK( xTickCount );
3537 /* Tick increment should occur on every kernel timer event. Core 0 has the
3538 * responsibility to increment the tick, or increment the pended ticks if the
3539 * scheduler is suspended. If pended ticks is greater than zero, the core that
3540 * calls xTaskResumeAll has the responsibility to increment the tick. */
3541 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3543 /* Minor optimisation. The tick count cannot change in this
3545 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
3547 /* Increment the RTOS tick, switching the delayed and overflowed
3548 * delayed lists if it wraps to 0. */
3549 xTickCount = xConstTickCount;
3551 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
3553 taskSWITCH_DELAYED_LISTS();
3557 mtCOVERAGE_TEST_MARKER();
3560 /* See if this tick has made a timeout expire. Tasks are stored in
3561 * the queue in the order of their wake time - meaning once one task
3562 * has been found whose block time has not expired there is no need to
3563 * look any further down the list. */
3564 if( xConstTickCount >= xNextTaskUnblockTime )
3568 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
3570 /* The delayed list is empty. Set xNextTaskUnblockTime
3571 * to the maximum possible value so it is extremely
3573 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
3574 * next time through. */
3575 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3580 /* The delayed list is not empty, get the value of the
3581 * item at the head of the delayed list. This is the time
3582 * at which the task at the head of the delayed list must
3583 * be removed from the Blocked state. */
3584 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. */
3585 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
3587 if( xConstTickCount < xItemValue )
3589 /* It is not time to unblock this item yet, but the
3590 * item value is the time at which the task at the head
3591 * of the blocked list must be removed from the Blocked
3592 * state - so record the item value in
3593 * xNextTaskUnblockTime. */
3594 xNextTaskUnblockTime = xItemValue;
3595 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
3599 mtCOVERAGE_TEST_MARKER();
3602 /* It is time to remove the item from the Blocked state. */
3603 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3605 /* Is the task waiting on an event also? If so remove
3606 * it from the event list. */
3607 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3609 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3613 mtCOVERAGE_TEST_MARKER();
3616 /* Place the unblocked task into the appropriate ready
3618 prvAddTaskToReadyList( pxTCB );
3620 /* A task being unblocked cannot cause an immediate
3621 * context switch if preemption is turned off. */
3622 #if ( configUSE_PREEMPTION == 1 )
3624 prvYieldForTask( pxTCB, pdTRUE );
3626 #endif /* configUSE_PREEMPTION */
3631 /* Tasks of equal priority to the currently running task will share
3632 * processing time (time slice) if preemption is on, and the application
3633 * writer has not explicitly turned time slicing off. */
3634 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
3636 /* TODO: If there are fewer "non-IDLE" READY tasks than cores, do not
3637 * force a context switch that would just shuffle tasks around cores */
3638 /* TODO: There are certainly better ways of doing this that would reduce
3639 * the number of interrupts and also potentially help prevent tasks from
3640 * moving between cores as often. This, however, works for now. */
3641 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3643 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ x ]->uxPriority ] ) ) > ( UBaseType_t ) 1 )
3645 xCoreYieldList[ x ] = pdTRUE;
3649 mtCOVERAGE_TEST_MARKER();
3653 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
3655 #if ( configUSE_TICK_HOOK == 1 )
3657 /* Guard against the tick hook being called when the pended tick
3658 * count is being unwound (when the scheduler is being unlocked). */
3659 if( xPendedTicks == ( TickType_t ) 0 )
3661 vApplicationTickHook();
3665 mtCOVERAGE_TEST_MARKER();
3668 #endif /* configUSE_TICK_HOOK */
3670 #if ( configUSE_PREEMPTION == 1 )
3672 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3674 if( xYieldPendings[ x ] != pdFALSE )
3676 xCoreYieldList[ x ] = pdTRUE;
3680 mtCOVERAGE_TEST_MARKER();
3684 #endif /* configUSE_PREEMPTION */
3686 #if ( configUSE_PREEMPTION == 1 )
3690 xCoreID = portGET_CORE_ID();
3692 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3694 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3695 if( pxCurrentTCBs[ x ]->xPreemptionDisable == pdFALSE )
3698 if( xCoreYieldList[ x ] != pdFALSE )
3702 xSwitchRequired = pdTRUE;
3711 mtCOVERAGE_TEST_MARKER();
3716 #endif /* configUSE_PREEMPTION */
3722 /* The tick hook gets called at regular intervals, even if the
3723 * scheduler is locked. */
3724 #if ( configUSE_TICK_HOOK == 1 )
3726 vApplicationTickHook();
3731 taskEXIT_CRITICAL();
3733 return xSwitchRequired;
3735 /*-----------------------------------------------------------*/
3737 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3739 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
3740 TaskHookFunction_t pxHookFunction )
3744 /* If xTask is NULL then it is the task hook of the calling task that is
3748 xTCB = ( TCB_t * ) pxCurrentTCB;
3755 /* Save the hook function in the TCB. A critical section is required as
3756 * the value can be accessed from an interrupt. */
3757 taskENTER_CRITICAL();
3759 xTCB->pxTaskTag = pxHookFunction;
3761 taskEXIT_CRITICAL();
3764 #endif /* configUSE_APPLICATION_TASK_TAG */
3765 /*-----------------------------------------------------------*/
3767 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3769 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
3772 TaskHookFunction_t xReturn;
3774 /* If xTask is NULL then set the calling task's hook. */
3775 pxTCB = prvGetTCBFromHandle( xTask );
3777 /* Save the hook function in the TCB. A critical section is required as
3778 * the value can be accessed from an interrupt. */
3779 taskENTER_CRITICAL();
3781 xReturn = pxTCB->pxTaskTag;
3783 taskEXIT_CRITICAL();
3788 #endif /* configUSE_APPLICATION_TASK_TAG */
3789 /*-----------------------------------------------------------*/
3791 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3793 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
3796 TaskHookFunction_t xReturn;
3797 UBaseType_t uxSavedInterruptStatus;
3799 /* If xTask is NULL then set the calling task's hook. */
3800 pxTCB = prvGetTCBFromHandle( xTask );
3802 /* Save the hook function in the TCB. A critical section is required as
3803 * the value can be accessed from an interrupt. */
3804 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
3806 xReturn = pxTCB->pxTaskTag;
3808 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3813 #endif /* configUSE_APPLICATION_TASK_TAG */
3814 /*-----------------------------------------------------------*/
3816 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3818 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
3819 void * pvParameter )
3824 /* If xTask is NULL then we are calling our own task hook. */
3827 xTCB = pxCurrentTCB;
3834 if( xTCB->pxTaskTag != NULL )
3836 xReturn = xTCB->pxTaskTag( pvParameter );
3846 #endif /* configUSE_APPLICATION_TASK_TAG */
3847 /*-----------------------------------------------------------*/
3849 void vTaskSwitchContext( BaseType_t xCoreID )
3851 /* Acquire both locks:
3852 * - The ISR lock protects the ready list from simultaneous access by
3853 * both other ISRs and tasks.
3854 * - We also take the task lock to pause here in case another core has
3855 * suspended the scheduler. We don't want to simply set xYieldPending
3856 * and move on if another core suspended the scheduler. We should only
3857 * do that if the current core has suspended the scheduler. */
3859 portGET_TASK_LOCK(); /* Must always acquire the task lock first */
3862 /* vTaskSwitchContext() must never be called from within a critical section.
3863 * This is not necessarily true for vanilla FreeRTOS, but it is for this SMP port. */
3864 configASSERT( pxCurrentTCB->uxCriticalNesting == 0 );
3866 if( uxSchedulerSuspended != ( UBaseType_t ) pdFALSE )
3868 /* The scheduler is currently suspended - do not allow a context
3870 xYieldPendings[ xCoreID ] = pdTRUE;
3874 xYieldPendings[ xCoreID ] = pdFALSE;
3875 traceTASK_SWITCHED_OUT();
3877 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3879 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3880 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
3882 ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
3885 /* Add the amount of time the task has been running to the
3886 * accumulated time so far. The time the task started running was
3887 * stored in ulTaskSwitchedInTime. Note that there is no overflow
3888 * protection here so count values are only valid until the timer
3889 * overflows. The guard against negative values is to protect
3890 * against suspect run time stat counter implementations - which
3891 * are provided by the application, not the kernel. */
3892 if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
3894 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
3898 mtCOVERAGE_TEST_MARKER();
3901 ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
3903 #endif /* configGENERATE_RUN_TIME_STATS */
3905 /* Check for stack overflow, if configured. */
3906 taskCHECK_FOR_STACK_OVERFLOW();
3908 /* Before the currently running task is switched out, save its errno. */
3909 #if ( configUSE_POSIX_ERRNO == 1 )
3911 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
3915 /* Select a new task to run using either the generic C or port
3916 * optimised asm code. */
3917 prvSelectHighestPriorityTask( xCoreID );
3918 traceTASK_SWITCHED_IN();
3920 /* After the new task is switched in, update the global errno. */
3921 #if ( configUSE_POSIX_ERRNO == 1 )
3923 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
3927 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) && ( configNEWLIB_REENTRANT_IS_DYNAMIC == 0 ) )
3929 /* Switch Newlib's _impure_ptr variable to point to the _reent
3930 * structure specific to this task.
3931 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
3932 * for additional information.
3934 * Note: Updating the _impure_ptr is not required when Newlib is compiled with
3935 * __DYNAMIC_REENT__ enabled. The the port should provide __getreent() instead. */
3936 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
3938 #endif /* ( configUSE_NEWLIB_REENTRANT == 1 ) && ( configNEWLIB_REENTRANT_IS_DYNAMIC == 0 ) */
3941 portRELEASE_ISR_LOCK();
3942 portRELEASE_TASK_LOCK();
3944 /*-----------------------------------------------------------*/
3946 void vTaskPlaceOnEventList( List_t * const pxEventList,
3947 const TickType_t xTicksToWait )
3949 configASSERT( pxEventList );
3951 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
3952 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
3954 /* Place the event list item of the TCB in the appropriate event list.
3955 * This is placed in the list in priority order so the highest priority task
3956 * is the first to be woken by the event. The queue that contains the event
3957 * list is locked, preventing simultaneous access from interrupts. */
3958 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3960 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3962 /*-----------------------------------------------------------*/
3964 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
3965 const TickType_t xItemValue,
3966 const TickType_t xTicksToWait )
3968 configASSERT( pxEventList );
3970 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3971 * the event groups implementation. */
3972 configASSERT( uxSchedulerSuspended != 0 );
3974 /* Store the item value in the event list item. It is safe to access the
3975 * event list item here as interrupts won't access the event list item of a
3976 * task that is not in the Blocked state. */
3977 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
3979 /* Place the event list item of the TCB at the end of the appropriate event
3980 * list. It is safe to access the event list here because it is part of an
3981 * event group implementation - and interrupts don't access event groups
3982 * directly (instead they access them indirectly by pending function calls to
3983 * the task level). */
3984 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3986 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3988 /*-----------------------------------------------------------*/
3990 #if ( configUSE_TIMERS == 1 )
3992 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
3993 TickType_t xTicksToWait,
3994 const BaseType_t xWaitIndefinitely )
3996 configASSERT( pxEventList );
3998 /* This function should not be called by application code hence the
3999 * 'Restricted' in its name. It is not part of the public API. It is
4000 * designed for use by kernel code, and has special calling requirements -
4001 * it should be called with the scheduler suspended. */
4004 /* Place the event list item of the TCB in the appropriate event list.
4005 * In this case it is assume that this is the only task that is going to
4006 * be waiting on this event list, so the faster vListInsertEnd() function
4007 * can be used in place of vListInsert. */
4008 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4010 /* If the task should block indefinitely then set the block time to a
4011 * value that will be recognised as an indefinite delay inside the
4012 * prvAddCurrentTaskToDelayedList() function. */
4013 if( xWaitIndefinitely != pdFALSE )
4015 xTicksToWait = portMAX_DELAY;
4018 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
4019 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
4022 #endif /* configUSE_TIMERS */
4023 /*-----------------------------------------------------------*/
4025 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
4027 TCB_t * pxUnblockedTCB;
4030 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
4031 * called from a critical section within an ISR. */
4033 /* The event list is sorted in priority order, so the first in the list can
4034 * be removed as it is known to be the highest priority. Remove the TCB from
4035 * the delayed list, and add it to the ready list.
4037 * If an event is for a queue that is locked then this function will never
4038 * get called - the lock count on the queue will get modified instead. This
4039 * means exclusive access to the event list is guaranteed here.
4041 * This function assumes that a check has already been made to ensure that
4042 * pxEventList is not empty. */
4043 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. */
4044 configASSERT( pxUnblockedTCB );
4045 ( void ) uxListRemove( &( pxUnblockedTCB->xEventListItem ) );
4047 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4049 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
4050 prvAddTaskToReadyList( pxUnblockedTCB );
4052 #if ( configUSE_TICKLESS_IDLE != 0 )
4054 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4055 * might be set to the blocked task's time out time. If the task is
4056 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4057 * normally left unchanged, because it is automatically reset to a new
4058 * value when the tick count equals xNextTaskUnblockTime. However if
4059 * tickless idling is used it might be more important to enter sleep mode
4060 * at the earliest possible time - so reset xNextTaskUnblockTime here to
4061 * ensure it is updated at the earliest possible time. */
4062 prvResetNextTaskUnblockTime();
4068 /* The delayed and ready lists cannot be accessed, so hold this task
4069 * pending until the scheduler is resumed. */
4070 vListInsertEnd( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
4074 #if ( configUSE_PREEMPTION == 1 )
4075 prvYieldForTask( pxUnblockedTCB, pdFALSE );
4077 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
4085 /*-----------------------------------------------------------*/
4087 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
4088 const TickType_t xItemValue )
4090 TCB_t * pxUnblockedTCB;
4092 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
4093 * the event flags implementation. */
4094 configASSERT( uxSchedulerSuspended != pdFALSE );
4096 /* Store the new item value in the event list. */
4097 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4099 /* Remove the event list form the event flag. Interrupts do not access
4101 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. */
4102 configASSERT( pxUnblockedTCB );
4103 ( void ) uxListRemove( pxEventListItem );
4105 #if ( configUSE_TICKLESS_IDLE != 0 )
4107 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4108 * might be set to the blocked task's time out time. If the task is
4109 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4110 * normally left unchanged, because it is automatically reset to a new
4111 * value when the tick count equals xNextTaskUnblockTime. However if
4112 * tickless idling is used it might be more important to enter sleep mode
4113 * at the earliest possible time - so reset xNextTaskUnblockTime here to
4114 * ensure it is updated at the earliest possible time. */
4115 prvResetNextTaskUnblockTime();
4119 /* Remove the task from the delayed list and add it to the ready list. The
4120 * scheduler is suspended so interrupts will not be accessing the ready
4122 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
4123 prvAddTaskToReadyList( pxUnblockedTCB );
4125 #if ( configUSE_PREEMPTION == 1 )
4126 taskENTER_CRITICAL();
4128 prvYieldForTask( pxUnblockedTCB, pdFALSE );
4130 taskEXIT_CRITICAL();
4133 /*-----------------------------------------------------------*/
4135 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
4137 configASSERT( pxTimeOut );
4138 taskENTER_CRITICAL();
4140 pxTimeOut->xOverflowCount = xNumOfOverflows;
4141 pxTimeOut->xTimeOnEntering = xTickCount;
4143 taskEXIT_CRITICAL();
4145 /*-----------------------------------------------------------*/
4147 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
4149 /* For internal use only as it does not use a critical section. */
4150 pxTimeOut->xOverflowCount = xNumOfOverflows;
4151 pxTimeOut->xTimeOnEntering = xTickCount;
4153 /*-----------------------------------------------------------*/
4155 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
4156 TickType_t * const pxTicksToWait )
4160 configASSERT( pxTimeOut );
4161 configASSERT( pxTicksToWait );
4163 taskENTER_CRITICAL();
4165 /* Minor optimisation. The tick count cannot change in this block. */
4166 const TickType_t xConstTickCount = xTickCount;
4167 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
4169 #if ( INCLUDE_xTaskAbortDelay == 1 )
4170 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
4172 /* The delay was aborted, which is not the same as a time out,
4173 * but has the same result. */
4174 pxCurrentTCB->ucDelayAborted = pdFALSE;
4180 #if ( INCLUDE_vTaskSuspend == 1 )
4181 if( *pxTicksToWait == portMAX_DELAY )
4183 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
4184 * specified is the maximum block time then the task should block
4185 * indefinitely, and therefore never time out. */
4191 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
4193 /* The tick count is greater than the time at which
4194 * vTaskSetTimeout() was called, but has also overflowed since
4195 * vTaskSetTimeOut() was called. It must have wrapped all the way
4196 * around and gone past again. This passed since vTaskSetTimeout()
4199 *pxTicksToWait = ( TickType_t ) 0;
4201 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
4203 /* Not a genuine timeout. Adjust parameters for time remaining. */
4204 *pxTicksToWait -= xElapsedTime;
4205 vTaskInternalSetTimeOutState( pxTimeOut );
4210 *pxTicksToWait = ( TickType_t ) 0;
4214 taskEXIT_CRITICAL();
4218 /*-----------------------------------------------------------*/
4220 void vTaskMissedYield( void )
4222 /* Must be called from within a critical section */
4223 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
4225 /*-----------------------------------------------------------*/
4227 #if ( configUSE_TRACE_FACILITY == 1 )
4229 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
4231 UBaseType_t uxReturn;
4232 TCB_t const * pxTCB;
4237 uxReturn = pxTCB->uxTaskNumber;
4247 #endif /* configUSE_TRACE_FACILITY */
4248 /*-----------------------------------------------------------*/
4250 #if ( configUSE_TRACE_FACILITY == 1 )
4252 void vTaskSetTaskNumber( TaskHandle_t xTask,
4253 const UBaseType_t uxHandle )
4260 pxTCB->uxTaskNumber = uxHandle;
4264 #endif /* configUSE_TRACE_FACILITY */
4267 * -----------------------------------------------------------
4268 * The MinimalIdle task.
4269 * ----------------------------------------------------------
4271 * The minimal idle task is used for all the additional Cores in a SMP system.
4272 * There must be only 1 idle task and the rest are minimal idle tasks.
4274 * @todo additional conditional compiles to remove this function.
4277 #if ( configNUM_CORES > 1 )
4278 static portTASK_FUNCTION( prvMinimalIdleTask, pvParameters )
4284 #if ( configUSE_PREEMPTION == 0 )
4286 /* If we are not using preemption we keep forcing a task switch to
4287 * see if any other task has become available. If we are using
4288 * preemption we don't need to do this as any task becoming available
4289 * will automatically get the processor anyway. */
4292 #endif /* configUSE_PREEMPTION */
4294 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4296 /* When using preemption tasks of equal priority will be
4297 * timesliced. If a task that is sharing the idle priority is ready
4298 * to run then the idle task should yield before the end of the
4301 * A critical region is not required here as we are just reading from
4302 * the list, and an occasional incorrect value will not matter. If
4303 * the ready list at the idle priority contains one more task than the
4304 * number of idle tasks, which is equal to the configured numbers of cores
4305 * then a task other than the idle task is ready to execute. */
4306 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4312 mtCOVERAGE_TEST_MARKER();
4315 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4317 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
4319 extern void vApplicationMinimalIdleHook( void );
4321 /* Call the user defined function from within the idle task. This
4322 * allows the application designer to add background functionality
4323 * without the overhead of a separate task.
4325 * This hook is intended to manage core activity such as disabling cores that go idle.
4327 * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
4328 * CALL A FUNCTION THAT MIGHT BLOCK. */
4329 vApplicationMinimalIdleHook();
4331 #endif /* configUSE_MINIMAL_IDLE_HOOK */
4334 #endif /* if ( configNUM_CORES > 1 ) */
4337 * -----------------------------------------------------------
4339 * ----------------------------------------------------------
4343 static portTASK_FUNCTION( prvIdleTask, pvParameters )
4345 /* Stop warnings. */
4346 ( void ) pvParameters;
4348 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
4349 * SCHEDULER IS STARTED. **/
4351 /* In case a task that has a secure context deletes itself, in which case
4352 * the idle task is responsible for deleting the task's secure context, if
4354 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
4356 /* All cores start up in the idle task. This initial yield gets the application
4362 /* See if any tasks have deleted themselves - if so then the idle task
4363 * is responsible for freeing the deleted task's TCB and stack. */
4364 prvCheckTasksWaitingTermination();
4366 #if ( configUSE_PREEMPTION == 0 )
4368 /* If we are not using preemption we keep forcing a task switch to
4369 * see if any other task has become available. If we are using
4370 * preemption we don't need to do this as any task becoming available
4371 * will automatically get the processor anyway. */
4374 #endif /* configUSE_PREEMPTION */
4376 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4378 /* When using preemption tasks of equal priority will be
4379 * timesliced. If a task that is sharing the idle priority is ready
4380 * to run then the idle task should yield before the end of the
4383 * A critical region is not required here as we are just reading from
4384 * the list, and an occasional incorrect value will not matter. If
4385 * the ready list at the idle priority contains one more task than the
4386 * number of idle tasks, which is equal to the configured numbers of cores
4387 * then a task other than the idle task is ready to execute. */
4388 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4394 mtCOVERAGE_TEST_MARKER();
4397 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4399 #if ( configUSE_IDLE_HOOK == 1 )
4401 extern void vApplicationIdleHook( void );
4403 /* Call the user defined function from within the idle task. This
4404 * allows the application designer to add background functionality
4405 * without the overhead of a separate task.
4407 * NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
4408 * CALL A FUNCTION THAT MIGHT BLOCK. */
4409 vApplicationIdleHook();
4411 #endif /* configUSE_IDLE_HOOK */
4413 /* This conditional compilation should use inequality to 0, not equality
4414 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
4415 * user defined low power mode implementations require
4416 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
4417 #if ( configUSE_TICKLESS_IDLE != 0 )
4419 TickType_t xExpectedIdleTime;
4421 /* It is not desirable to suspend then resume the scheduler on
4422 * each iteration of the idle task. Therefore, a preliminary
4423 * test of the expected idle time is performed without the
4424 * scheduler suspended. The result here is not necessarily
4426 xExpectedIdleTime = prvGetExpectedIdleTime();
4428 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4432 /* Now the scheduler is suspended, the expected idle
4433 * time can be sampled again, and this time its value can
4435 configASSERT( xNextTaskUnblockTime >= xTickCount );
4436 xExpectedIdleTime = prvGetExpectedIdleTime();
4438 /* Define the following macro to set xExpectedIdleTime to 0
4439 * if the application does not want
4440 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
4441 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
4443 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4445 traceLOW_POWER_IDLE_BEGIN();
4446 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
4447 traceLOW_POWER_IDLE_END();
4451 mtCOVERAGE_TEST_MARKER();
4454 ( void ) xTaskResumeAll();
4458 mtCOVERAGE_TEST_MARKER();
4461 #endif /* configUSE_TICKLESS_IDLE */
4463 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
4465 extern void vApplicationMinimalIdleHook( void );
4467 /* Call the user defined function from within the idle task. This
4468 * allows the application designer to add background functionality
4469 * without the overhead of a separate task.
4471 * This hook is intended to manage core activity such as disabling cores that go idle.
4473 * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
4474 * CALL A FUNCTION THAT MIGHT BLOCK. */
4475 vApplicationMinimalIdleHook();
4477 #endif /* configUSE_MINIMAL_IDLE_HOOK */
4480 /*-----------------------------------------------------------*/
4482 #if ( configUSE_TICKLESS_IDLE != 0 )
4484 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
4486 /* The idle task exists in addition to the application tasks. */
4487 const UBaseType_t uxNonApplicationTasks = 1;
4488 eSleepModeStatus eReturn = eStandardSleep;
4490 /* This function must be called from a critical section. */
4492 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
4494 /* A task was made ready while the scheduler was suspended. */
4495 eReturn = eAbortSleep;
4497 else if( xYieldPending != pdFALSE )
4499 /* A yield was pended while the scheduler was suspended. */
4500 eReturn = eAbortSleep;
4502 else if( xPendedTicks != 0 )
4504 /* A tick interrupt has already occurred but was held pending
4505 * because the scheduler is suspended. */
4506 eReturn = eAbortSleep;
4510 /* If all the tasks are in the suspended list (which might mean they
4511 * have an infinite block time rather than actually being suspended)
4512 * then it is safe to turn all clocks off and just wait for external
4514 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
4516 eReturn = eNoTasksWaitingTimeout;
4520 mtCOVERAGE_TEST_MARKER();
4527 #endif /* configUSE_TICKLESS_IDLE */
4528 /*-----------------------------------------------------------*/
4530 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4532 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
4538 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4540 pxTCB = prvGetTCBFromHandle( xTaskToSet );
4541 configASSERT( pxTCB != NULL );
4542 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
4546 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4547 /*-----------------------------------------------------------*/
4549 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4551 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
4554 void * pvReturn = NULL;
4557 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4559 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4560 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
4570 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4571 /*-----------------------------------------------------------*/
4573 #if ( portUSING_MPU_WRAPPERS == 1 )
4575 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
4576 const MemoryRegion_t * const xRegions )
4580 /* If null is passed in here then we are modifying the MPU settings of
4581 * the calling task. */
4582 pxTCB = prvGetTCBFromHandle( xTaskToModify );
4584 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
4587 #endif /* portUSING_MPU_WRAPPERS */
4588 /*-----------------------------------------------------------*/
4590 static void prvInitialiseTaskLists( void )
4592 UBaseType_t uxPriority;
4594 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
4596 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
4599 vListInitialise( &xDelayedTaskList1 );
4600 vListInitialise( &xDelayedTaskList2 );
4601 vListInitialise( &xPendingReadyList );
4603 #if ( INCLUDE_vTaskDelete == 1 )
4605 vListInitialise( &xTasksWaitingTermination );
4607 #endif /* INCLUDE_vTaskDelete */
4609 #if ( INCLUDE_vTaskSuspend == 1 )
4611 vListInitialise( &xSuspendedTaskList );
4613 #endif /* INCLUDE_vTaskSuspend */
4615 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
4617 pxDelayedTaskList = &xDelayedTaskList1;
4618 pxOverflowDelayedTaskList = &xDelayedTaskList2;
4620 /*-----------------------------------------------------------*/
4622 static void prvCheckTasksWaitingTermination( void )
4624 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
4626 #if ( INCLUDE_vTaskDelete == 1 )
4630 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
4631 * being called too often in the idle task. */
4632 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4634 taskENTER_CRITICAL();
4636 /* Since we are SMP, multiple idles can be running simultaneously
4637 * and we need to check that other idles did not cleanup while we were
4638 * waiting to enter the critical section */
4639 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4641 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) ); /*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. */
4643 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
4645 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4646 --uxCurrentNumberOfTasks;
4647 --uxDeletedTasksWaitingCleanUp;
4648 prvDeleteTCB( pxTCB );
4652 /* The TCB to be deleted still has not yet been switched out
4653 * by the scheduler, so we will just exit this loop early and
4654 * try again next time. */
4655 taskEXIT_CRITICAL();
4660 taskEXIT_CRITICAL();
4663 #endif /* INCLUDE_vTaskDelete */
4665 /*-----------------------------------------------------------*/
4667 #if ( configUSE_TRACE_FACILITY == 1 )
4669 void vTaskGetInfo( TaskHandle_t xTask,
4670 TaskStatus_t * pxTaskStatus,
4671 BaseType_t xGetFreeStackSpace,
4676 /* xTask is NULL then get the state of the calling task. */
4677 pxTCB = prvGetTCBFromHandle( xTask );
4679 pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
4680 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
4681 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
4682 pxTaskStatus->pxStackBase = pxTCB->pxStack;
4683 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
4685 #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUM_CORES > 1 ) )
4687 pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
4691 #if ( configUSE_MUTEXES == 1 )
4693 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
4697 pxTaskStatus->uxBasePriority = 0;
4701 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4703 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
4707 pxTaskStatus->ulRunTimeCounter = 0;
4711 /* Obtaining the task state is a little fiddly, so is only done if the
4712 * value of eState passed into this function is eInvalid - otherwise the
4713 * state is just set to whatever is passed in. */
4714 if( eState != eInvalid )
4716 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
4718 pxTaskStatus->eCurrentState = eRunning;
4722 pxTaskStatus->eCurrentState = eState;
4724 #if ( INCLUDE_vTaskSuspend == 1 )
4726 /* If the task is in the suspended list then there is a
4727 * chance it is actually just blocked indefinitely - so really
4728 * it should be reported as being in the Blocked state. */
4729 if( eState == eSuspended )
4733 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4735 pxTaskStatus->eCurrentState = eBlocked;
4738 ( void ) xTaskResumeAll();
4741 #endif /* INCLUDE_vTaskSuspend */
4746 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
4749 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
4750 * parameter is provided to allow it to be skipped. */
4751 if( xGetFreeStackSpace != pdFALSE )
4753 #if ( portSTACK_GROWTH > 0 )
4755 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
4759 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
4765 pxTaskStatus->usStackHighWaterMark = 0;
4769 #endif /* configUSE_TRACE_FACILITY */
4770 /*-----------------------------------------------------------*/
4772 #if ( configUSE_TRACE_FACILITY == 1 )
4774 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
4778 configLIST_VOLATILE TCB_t * pxNextTCB, * pxFirstTCB;
4779 UBaseType_t uxTask = 0;
4781 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4783 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. */
4785 /* Populate an TaskStatus_t structure within the
4786 * pxTaskStatusArray array for each task that is referenced from
4787 * pxList. See the definition of TaskStatus_t in task.h for the
4788 * meaning of each TaskStatus_t structure member. */
4791 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. */
4792 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
4794 } while( pxNextTCB != pxFirstTCB );
4798 mtCOVERAGE_TEST_MARKER();
4804 #endif /* configUSE_TRACE_FACILITY */
4805 /*-----------------------------------------------------------*/
4807 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
4809 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
4811 uint32_t ulCount = 0U;
4813 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
4815 pucStackByte -= portSTACK_GROWTH;
4819 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
4821 return ( configSTACK_DEPTH_TYPE ) ulCount;
4824 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
4825 /*-----------------------------------------------------------*/
4827 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
4829 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
4830 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
4831 * user to determine the return type. It gets around the problem of the value
4832 * overflowing on 8-bit types without breaking backward compatibility for
4833 * applications that expect an 8-bit return type. */
4834 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
4837 uint8_t * pucEndOfStack;
4838 configSTACK_DEPTH_TYPE uxReturn;
4840 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
4841 * the same except for their return type. Using configSTACK_DEPTH_TYPE
4842 * allows the user to determine the return type. It gets around the
4843 * problem of the value overflowing on 8-bit types without breaking
4844 * backward compatibility for applications that expect an 8-bit return
4847 pxTCB = prvGetTCBFromHandle( xTask );
4849 #if portSTACK_GROWTH < 0
4851 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4855 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4859 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
4864 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
4865 /*-----------------------------------------------------------*/
4867 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
4869 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
4872 uint8_t * pucEndOfStack;
4873 UBaseType_t uxReturn;
4875 pxTCB = prvGetTCBFromHandle( xTask );
4877 #if portSTACK_GROWTH < 0
4879 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4883 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4887 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
4892 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
4893 /*-----------------------------------------------------------*/
4895 #if ( INCLUDE_vTaskDelete == 1 )
4897 static void prvDeleteTCB( TCB_t * pxTCB )
4899 /* This call is required specifically for the TriCore port. It must be
4900 * above the vPortFree() calls. The call is also used by ports/demos that
4901 * want to allocate and clean RAM statically. */
4902 portCLEAN_UP_TCB( pxTCB );
4904 /* Free up the memory allocated by the scheduler for the task. It is up
4905 * to the task to free any memory allocated at the application level.
4906 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
4907 * for additional information. */
4908 #if ( configUSE_NEWLIB_REENTRANT == 1 )
4910 _reclaim_reent( &( pxTCB->xNewLib_reent ) );
4912 #endif /* configUSE_NEWLIB_REENTRANT */
4914 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
4916 /* The task can only have been allocated dynamically - free both
4917 * the stack and TCB. */
4918 vPortFreeStack( pxTCB->pxStack );
4921 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
4923 /* The task could have been allocated statically or dynamically, so
4924 * check what was statically allocated before trying to free the
4926 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
4928 /* Both the stack and TCB were allocated dynamically, so both
4930 vPortFreeStack( pxTCB->pxStack );
4933 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4935 /* Only the stack was statically allocated, so the TCB is the
4936 * only memory that must be freed. */
4941 /* Neither the stack nor the TCB were allocated dynamically, so
4942 * nothing needs to be freed. */
4943 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
4944 mtCOVERAGE_TEST_MARKER();
4947 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
4950 #endif /* INCLUDE_vTaskDelete */
4951 /*-----------------------------------------------------------*/
4953 static void prvResetNextTaskUnblockTime( void )
4955 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4957 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
4958 * the maximum possible value so it is extremely unlikely that the
4959 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
4960 * there is an item in the delayed list. */
4961 xNextTaskUnblockTime = portMAX_DELAY;
4965 /* The new current delayed list is not empty, get the value of
4966 * the item at the head of the delayed list. This is the time at
4967 * which the task at the head of the delayed list should be removed
4968 * from the Blocked state. */
4969 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
4972 /*-----------------------------------------------------------*/
4974 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
4976 TaskHandle_t xTaskGetCurrentTaskHandle( void )
4978 TaskHandle_t xReturn;
4981 ulState = portDISABLE_INTERRUPTS();
4982 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
4983 portRESTORE_INTERRUPTS( ulState );
4988 TaskHandle_t xTaskGetCurrentTaskHandleCPU( UBaseType_t xCoreID )
4990 TaskHandle_t xReturn = NULL;
4992 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
4994 xReturn = pxCurrentTCBs[ xCoreID ];
5000 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
5001 /*-----------------------------------------------------------*/
5003 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
5005 BaseType_t xTaskGetSchedulerState( void )
5009 if( xSchedulerRunning == pdFALSE )
5011 xReturn = taskSCHEDULER_NOT_STARTED;
5015 taskENTER_CRITICAL();
5017 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
5019 xReturn = taskSCHEDULER_RUNNING;
5023 xReturn = taskSCHEDULER_SUSPENDED;
5026 taskEXIT_CRITICAL();
5032 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
5033 /*-----------------------------------------------------------*/
5035 #if ( configUSE_MUTEXES == 1 )
5037 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
5039 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
5040 BaseType_t xReturn = pdFALSE;
5042 /* If the mutex was given back by an interrupt while the queue was
5043 * locked then the mutex holder might now be NULL. _RB_ Is this still
5044 * needed as interrupts can no longer use mutexes? */
5045 if( pxMutexHolder != NULL )
5047 /* If the holder of the mutex has a priority below the priority of
5048 * the task attempting to obtain the mutex then it will temporarily
5049 * inherit the priority of the task attempting to obtain the mutex. */
5050 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
5052 /* Adjust the mutex holder state to account for its new
5053 * priority. Only reset the event list item value if the value is
5054 * not being used for anything else. */
5055 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
5057 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. */
5061 mtCOVERAGE_TEST_MARKER();
5064 /* If the task being modified is in the ready state it will need
5065 * to be moved into a new list. */
5066 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
5068 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5070 /* It is known that the task is in its ready list so
5071 * there is no need to check again and the port level
5072 * reset macro can be called directly. */
5073 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
5077 mtCOVERAGE_TEST_MARKER();
5080 /* Inherit the priority before being moved into the new list. */
5081 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
5082 prvAddTaskToReadyList( pxMutexHolderTCB );
5086 /* Just inherit the priority. */
5087 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
5090 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
5092 /* Inheritance occurred. */
5097 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
5099 /* The base priority of the mutex holder is lower than the
5100 * priority of the task attempting to take the mutex, but the
5101 * current priority of the mutex holder is not lower than the
5102 * priority of the task attempting to take the mutex.
5103 * Therefore the mutex holder must have already inherited a
5104 * priority, but inheritance would have occurred if that had
5105 * not been the case. */
5110 mtCOVERAGE_TEST_MARKER();
5116 mtCOVERAGE_TEST_MARKER();
5122 #endif /* configUSE_MUTEXES */
5123 /*-----------------------------------------------------------*/
5125 #if ( configUSE_MUTEXES == 1 )
5127 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
5129 TCB_t * const pxTCB = pxMutexHolder;
5130 BaseType_t xReturn = pdFALSE;
5132 if( pxMutexHolder != NULL )
5134 /* A task can only have an inherited priority if it holds the mutex.
5135 * If the mutex is held by a task then it cannot be given from an
5136 * interrupt, and if a mutex is given by the holding task then it must
5137 * be the running state task. */
5138 configASSERT( pxTCB == pxCurrentTCB );
5139 configASSERT( pxTCB->uxMutexesHeld );
5140 ( pxTCB->uxMutexesHeld )--;
5142 /* Has the holder of the mutex inherited the priority of another
5144 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
5146 /* Only disinherit if no other mutexes are held. */
5147 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
5149 /* A task can only have an inherited priority if it holds
5150 * the mutex. If the mutex is held by a task then it cannot be
5151 * given from an interrupt, and if a mutex is given by the
5152 * holding task then it must be the running state task. Remove
5153 * the holding task from the ready list. */
5154 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5156 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5160 mtCOVERAGE_TEST_MARKER();
5163 /* Disinherit the priority before adding the task into the
5164 * new ready list. */
5165 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
5166 pxTCB->uxPriority = pxTCB->uxBasePriority;
5168 /* Reset the event list item value. It cannot be in use for
5169 * any other purpose if this task is running, and it must be
5170 * running to give back the mutex. */
5171 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. */
5172 prvAddTaskToReadyList( pxTCB );
5174 /* Return true to indicate that a context switch is required.
5175 * This is only actually required in the corner case whereby
5176 * multiple mutexes were held and the mutexes were given back
5177 * in an order different to that in which they were taken.
5178 * If a context switch did not occur when the first mutex was
5179 * returned, even if a task was waiting on it, then a context
5180 * switch should occur when the last mutex is returned whether
5181 * a task is waiting on it or not. */
5186 mtCOVERAGE_TEST_MARKER();
5191 mtCOVERAGE_TEST_MARKER();
5196 mtCOVERAGE_TEST_MARKER();
5202 #endif /* configUSE_MUTEXES */
5203 /*-----------------------------------------------------------*/
5205 #if ( configUSE_MUTEXES == 1 )
5207 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
5208 UBaseType_t uxHighestPriorityWaitingTask )
5210 TCB_t * const pxTCB = pxMutexHolder;
5211 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
5212 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
5214 if( pxMutexHolder != NULL )
5216 /* If pxMutexHolder is not NULL then the holder must hold at least
5218 configASSERT( pxTCB->uxMutexesHeld );
5220 /* Determine the priority to which the priority of the task that
5221 * holds the mutex should be set. This will be the greater of the
5222 * holding task's base priority and the priority of the highest
5223 * priority task that is waiting to obtain the mutex. */
5224 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
5226 uxPriorityToUse = uxHighestPriorityWaitingTask;
5230 uxPriorityToUse = pxTCB->uxBasePriority;
5233 /* Does the priority need to change? */
5234 if( pxTCB->uxPriority != uxPriorityToUse )
5236 /* Only disinherit if no other mutexes are held. This is a
5237 * simplification in the priority inheritance implementation. If
5238 * the task that holds the mutex is also holding other mutexes then
5239 * the other mutexes may have caused the priority inheritance. */
5240 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
5242 /* If a task has timed out because it already holds the
5243 * mutex it was trying to obtain then it cannot of inherited
5244 * its own priority. */
5245 configASSERT( pxTCB != pxCurrentTCB );
5247 /* Disinherit the priority, remembering the previous
5248 * priority to facilitate determining the subject task's
5250 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
5251 uxPriorityUsedOnEntry = pxTCB->uxPriority;
5252 pxTCB->uxPriority = uxPriorityToUse;
5254 /* Only reset the event list item value if the value is not
5255 * being used for anything else. */
5256 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
5258 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. */
5262 mtCOVERAGE_TEST_MARKER();
5265 /* If the running task is not the task that holds the mutex
5266 * then the task that holds the mutex could be in either the
5267 * Ready, Blocked or Suspended states. Only remove the task
5268 * from its current state list if it is in the Ready state as
5269 * the task's priority is going to change and there is one
5270 * Ready list per priority. */
5271 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
5273 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5275 /* It is known that the task is in its ready list so
5276 * there is no need to check again and the port level
5277 * reset macro can be called directly. */
5278 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5282 mtCOVERAGE_TEST_MARKER();
5285 prvAddTaskToReadyList( pxTCB );
5289 mtCOVERAGE_TEST_MARKER();
5294 mtCOVERAGE_TEST_MARKER();
5299 mtCOVERAGE_TEST_MARKER();
5304 mtCOVERAGE_TEST_MARKER();
5308 #endif /* configUSE_MUTEXES */
5309 /*-----------------------------------------------------------*/
5312 * If not in a critical section then yield immediately.
5313 * Otherwise set xYieldPending to true to wait to
5314 * yield until exiting the critical section.
5316 void vTaskYieldWithinAPI( void )
5318 if( pxCurrentTCB->uxCriticalNesting == 0U )
5324 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5327 /*-----------------------------------------------------------*/
5329 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5331 void vTaskEnterCritical( void )
5333 portDISABLE_INTERRUPTS();
5335 if( xSchedulerRunning != pdFALSE )
5337 if( pxCurrentTCB->uxCriticalNesting == 0U )
5339 if( portCHECK_IF_IN_ISR() == pdFALSE )
5341 portGET_TASK_LOCK();
5347 ( pxCurrentTCB->uxCriticalNesting )++;
5349 /* This should now be interrupt safe. The only time there would be
5350 * a problem is if this is called before a context switch and
5351 * vTaskExitCritical() is called after pxCurrentTCB changes. Therefore
5352 * this should not be used within vTaskSwitchContext(). */
5354 if( ( uxSchedulerSuspended == 0U ) && ( pxCurrentTCB->uxCriticalNesting == 1U ) )
5356 prvCheckForRunStateChange();
5361 mtCOVERAGE_TEST_MARKER();
5365 #endif /* portCRITICAL_NESTING_IN_TCB */
5366 /*-----------------------------------------------------------*/
5368 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5370 void vTaskExitCritical( void )
5372 if( xSchedulerRunning != pdFALSE )
5374 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
5375 * does not match a previous call to vTaskEnterCritical(). */
5376 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
5378 if( pxCurrentTCB->uxCriticalNesting > 0U )
5380 ( pxCurrentTCB->uxCriticalNesting )--;
5382 if( pxCurrentTCB->uxCriticalNesting == 0U )
5384 portRELEASE_ISR_LOCK();
5386 if( portCHECK_IF_IN_ISR() == pdFALSE )
5388 portRELEASE_TASK_LOCK();
5389 portENABLE_INTERRUPTS();
5391 /* When a task yields in a critical section it just sets
5392 * xYieldPending to true. So now that we have exited the
5393 * critical section check if xYieldPending is true, and
5395 if( xYieldPending != pdFALSE )
5402 /* In an ISR we don't hold the task lock and don't
5403 * need to yield. Yield will happen if necessary when
5404 * the application ISR calls portEND_SWITCHING_ISR() */
5405 mtCOVERAGE_TEST_MARKER();
5410 mtCOVERAGE_TEST_MARKER();
5415 mtCOVERAGE_TEST_MARKER();
5420 mtCOVERAGE_TEST_MARKER();
5424 #endif /* portCRITICAL_NESTING_IN_TCB */
5425 /*-----------------------------------------------------------*/
5427 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
5429 static char * prvWriteNameToBuffer( char * pcBuffer,
5430 const char * pcTaskName )
5434 /* Start by copying the entire string. */
5435 strcpy( pcBuffer, pcTaskName );
5437 /* Pad the end of the string with spaces to ensure columns line up when
5439 for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
5441 pcBuffer[ x ] = ' ';
5445 pcBuffer[ x ] = ( char ) 0x00;
5447 /* Return the new end of string. */
5448 return &( pcBuffer[ x ] );
5451 #endif /* ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
5452 /*-----------------------------------------------------------*/
5454 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5456 void vTaskList( char * pcWriteBuffer )
5458 TaskStatus_t * pxTaskStatusArray;
5459 UBaseType_t uxArraySize, x;
5465 * This function is provided for convenience only, and is used by many
5466 * of the demo applications. Do not consider it to be part of the
5469 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
5470 * uxTaskGetSystemState() output into a human readable table that
5471 * displays task: names, states, priority, stack usage and task number.
5472 * Stack usage specified as the number of unused StackType_t words stack can hold
5473 * on top of stack - not the number of bytes.
5475 * vTaskList() has a dependency on the sprintf() C library function that
5476 * might bloat the code size, use a lot of stack, and provide different
5477 * results on different platforms. An alternative, tiny, third party,
5478 * and limited functionality implementation of sprintf() is provided in
5479 * many of the FreeRTOS/Demo sub-directories in a file called
5480 * printf-stdarg.c (note printf-stdarg.c does not provide a full
5481 * snprintf() implementation!).
5483 * It is recommended that production systems call uxTaskGetSystemState()
5484 * directly to get access to raw stats data, rather than indirectly
5485 * through a call to vTaskList().
5489 /* Make sure the write buffer does not contain a string. */
5490 *pcWriteBuffer = ( char ) 0x00;
5492 /* Take a snapshot of the number of tasks in case it changes while this
5493 * function is executing. */
5494 uxArraySize = uxCurrentNumberOfTasks;
5496 /* Allocate an array index for each task. NOTE! if
5497 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5498 * equate to NULL. */
5499 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. */
5501 if( pxTaskStatusArray != NULL )
5503 /* Generate the (binary) data. */
5504 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
5506 /* Create a human readable table from the binary data. */
5507 for( x = 0; x < uxArraySize; x++ )
5509 switch( pxTaskStatusArray[ x ].eCurrentState )
5512 cStatus = tskRUNNING_CHAR;
5516 cStatus = tskREADY_CHAR;
5520 cStatus = tskBLOCKED_CHAR;
5524 cStatus = tskSUSPENDED_CHAR;
5528 cStatus = tskDELETED_CHAR;
5531 case eInvalid: /* Fall through. */
5532 default: /* Should not get here, but it is included
5533 * to prevent static checking errors. */
5534 cStatus = ( char ) 0x00;
5538 /* Write the task name to the string, padding with spaces so it
5539 * can be printed in tabular form more easily. */
5540 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5542 /* Write the rest of the string. */
5543 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. */
5544 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. */
5547 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
5548 * is 0 then vPortFree() will be #defined to nothing. */
5549 vPortFree( pxTaskStatusArray );
5553 mtCOVERAGE_TEST_MARKER();
5557 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
5558 /*----------------------------------------------------------*/
5560 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5562 void vTaskGetRunTimeStats( char * pcWriteBuffer )
5564 TaskStatus_t * pxTaskStatusArray;
5565 UBaseType_t uxArraySize, x;
5566 uint32_t ulTotalTime, ulStatsAsPercentage;
5568 #if ( configUSE_TRACE_FACILITY != 1 )
5570 #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats().
5577 * This function is provided for convenience only, and is used by many
5578 * of the demo applications. Do not consider it to be part of the
5581 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
5582 * of the uxTaskGetSystemState() output into a human readable table that
5583 * displays the amount of time each task has spent in the Running state
5584 * in both absolute and percentage terms.
5586 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
5587 * function that might bloat the code size, use a lot of stack, and
5588 * provide different results on different platforms. An alternative,
5589 * tiny, third party, and limited functionality implementation of
5590 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
5591 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
5592 * a full snprintf() implementation!).
5594 * It is recommended that production systems call uxTaskGetSystemState()
5595 * directly to get access to raw stats data, rather than indirectly
5596 * through a call to vTaskGetRunTimeStats().
5599 /* Make sure the write buffer does not contain a string. */
5600 *pcWriteBuffer = ( char ) 0x00;
5602 /* Take a snapshot of the number of tasks in case it changes while this
5603 * function is executing. */
5604 uxArraySize = uxCurrentNumberOfTasks;
5606 /* Allocate an array index for each task. NOTE! If
5607 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5608 * equate to NULL. */
5609 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. */
5611 if( pxTaskStatusArray != NULL )
5613 /* Generate the (binary) data. */
5614 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
5616 /* For percentage calculations. */
5617 ulTotalTime /= 100UL;
5619 /* Avoid divide by zero errors. */
5620 if( ulTotalTime > 0UL )
5622 /* Create a human readable table from the binary data. */
5623 for( x = 0; x < uxArraySize; x++ )
5625 /* What percentage of the total run time has the task used?
5626 * This will always be rounded down to the nearest integer.
5627 * ulTotalRunTimeDiv100 has already been divided by 100. */
5628 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
5630 /* Write the task name to the string, padding with
5631 * spaces so it can be printed in tabular form more
5633 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5635 if( ulStatsAsPercentage > 0UL )
5637 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5639 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
5643 /* sizeof( int ) == sizeof( long ) so a smaller
5644 * printf() library can be used. */
5645 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. */
5651 /* If the percentage is zero here then the task has
5652 * consumed less than 1% of the total run time. */
5653 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5655 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
5659 /* sizeof( int ) == sizeof( long ) so a smaller
5660 * printf() library can be used. */
5661 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. */
5666 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. */
5671 mtCOVERAGE_TEST_MARKER();
5674 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
5675 * is 0 then vPortFree() will be #defined to nothing. */
5676 vPortFree( pxTaskStatusArray );
5680 mtCOVERAGE_TEST_MARKER();
5684 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
5685 /*-----------------------------------------------------------*/
5687 TickType_t uxTaskResetEventItemValue( void )
5689 TickType_t uxReturn;
5691 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
5693 /* Reset the event list item to its normal value - so it can be used with
5694 * queues and semaphores. */
5695 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. */
5699 /*-----------------------------------------------------------*/
5701 #if ( configUSE_MUTEXES == 1 )
5703 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
5705 /* If xSemaphoreCreateMutex() is called before any tasks have been created
5706 * then pxCurrentTCB will be NULL. */
5707 if( pxCurrentTCB != NULL )
5709 ( pxCurrentTCB->uxMutexesHeld )++;
5712 return pxCurrentTCB;
5715 #endif /* configUSE_MUTEXES */
5716 /*-----------------------------------------------------------*/
5718 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5720 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
5721 BaseType_t xClearCountOnExit,
5722 TickType_t xTicksToWait )
5726 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5728 taskENTER_CRITICAL();
5730 /* Only block if the notification count is not already non-zero. */
5731 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
5733 /* Mark this task as waiting for a notification. */
5734 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5736 if( xTicksToWait > ( TickType_t ) 0 )
5738 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5739 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
5741 /* All ports are written to allow a yield in a critical
5742 * section (some will yield immediately, others wait until the
5743 * critical section exits) - but it is not something that
5744 * application code should ever do. */
5745 vTaskYieldWithinAPI();
5749 mtCOVERAGE_TEST_MARKER();
5754 mtCOVERAGE_TEST_MARKER();
5757 taskEXIT_CRITICAL();
5759 taskENTER_CRITICAL();
5761 traceTASK_NOTIFY_TAKE( uxIndexToWait );
5762 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5764 if( ulReturn != 0UL )
5766 if( xClearCountOnExit != pdFALSE )
5768 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
5772 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
5777 mtCOVERAGE_TEST_MARKER();
5780 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5782 taskEXIT_CRITICAL();
5787 #endif /* configUSE_TASK_NOTIFICATIONS */
5788 /*-----------------------------------------------------------*/
5790 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5792 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
5793 uint32_t ulBitsToClearOnEntry,
5794 uint32_t ulBitsToClearOnExit,
5795 uint32_t * pulNotificationValue,
5796 TickType_t xTicksToWait )
5800 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5802 taskENTER_CRITICAL();
5804 /* Only block if a notification is not already pending. */
5805 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5807 /* Clear bits in the task's notification value as bits may get
5808 * set by the notifying task or interrupt. This can be used to
5809 * clear the value to zero. */
5810 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
5812 /* Mark this task as waiting for a notification. */
5813 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5815 if( xTicksToWait > ( TickType_t ) 0 )
5817 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5818 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
5820 /* All ports are written to allow a yield in a critical
5821 * section (some will yield immediately, others wait until the
5822 * critical section exits) - but it is not something that
5823 * application code should ever do. */
5824 vTaskYieldWithinAPI();
5828 mtCOVERAGE_TEST_MARKER();
5833 mtCOVERAGE_TEST_MARKER();
5836 taskEXIT_CRITICAL();
5838 taskENTER_CRITICAL();
5840 traceTASK_NOTIFY_WAIT( uxIndexToWait );
5842 if( pulNotificationValue != NULL )
5844 /* Output the current notification value, which may or may not
5846 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5849 /* If ucNotifyValue is set then either the task never entered the
5850 * blocked state (because a notification was already pending) or the
5851 * task unblocked because of a notification. Otherwise the task
5852 * unblocked because of a timeout. */
5853 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5855 /* A notification was not received. */
5860 /* A notification was already pending or a notification was
5861 * received while the task was waiting. */
5862 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
5866 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5868 taskEXIT_CRITICAL();
5873 #endif /* configUSE_TASK_NOTIFICATIONS */
5874 /*-----------------------------------------------------------*/
5876 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5878 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
5879 UBaseType_t uxIndexToNotify,
5881 eNotifyAction eAction,
5882 uint32_t * pulPreviousNotificationValue )
5885 BaseType_t xReturn = pdPASS;
5886 uint8_t ucOriginalNotifyState;
5888 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5889 configASSERT( xTaskToNotify );
5890 pxTCB = xTaskToNotify;
5892 taskENTER_CRITICAL();
5894 if( pulPreviousNotificationValue != NULL )
5896 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5899 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5901 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5906 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5910 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5913 case eSetValueWithOverwrite:
5914 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5917 case eSetValueWithoutOverwrite:
5919 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5921 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5925 /* The value could not be written to the task. */
5933 /* The task is being notified without its notify value being
5939 /* Should not get here if all enums are handled.
5940 * Artificially force an assert by testing a value the
5941 * compiler can't assume is const. */
5942 configASSERT( xTickCount == ( TickType_t ) 0 );
5947 traceTASK_NOTIFY( uxIndexToNotify );
5949 /* If the task is in the blocked state specifically to wait for a
5950 * notification then unblock it now. */
5951 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5953 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5954 prvAddTaskToReadyList( pxTCB );
5956 /* The task should not have been on an event list. */
5957 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5959 #if ( configUSE_TICKLESS_IDLE != 0 )
5961 /* If a task is blocked waiting for a notification then
5962 * xNextTaskUnblockTime might be set to the blocked task's time
5963 * out time. If the task is unblocked for a reason other than
5964 * a timeout xNextTaskUnblockTime is normally left unchanged,
5965 * because it will automatically get reset to a new value when
5966 * the tick count equals xNextTaskUnblockTime. However if
5967 * tickless idling is used it might be more important to enter
5968 * sleep mode at the earliest possible time - so reset
5969 * xNextTaskUnblockTime here to ensure it is updated at the
5970 * earliest possible time. */
5971 prvResetNextTaskUnblockTime();
5975 #if ( configUSE_PREEMPTION == 1 )
5977 prvYieldForTask( pxTCB, pdFALSE );
5983 mtCOVERAGE_TEST_MARKER();
5986 taskEXIT_CRITICAL();
5991 #endif /* configUSE_TASK_NOTIFICATIONS */
5992 /*-----------------------------------------------------------*/
5994 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5996 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
5997 UBaseType_t uxIndexToNotify,
5999 eNotifyAction eAction,
6000 uint32_t * pulPreviousNotificationValue,
6001 BaseType_t * pxHigherPriorityTaskWoken )
6004 uint8_t ucOriginalNotifyState;
6005 BaseType_t xReturn = pdPASS;
6006 UBaseType_t uxSavedInterruptStatus;
6008 configASSERT( xTaskToNotify );
6009 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6011 /* RTOS ports that support interrupt nesting have the concept of a
6012 * maximum system call (or maximum API call) interrupt priority.
6013 * Interrupts that are above the maximum system call priority are keep
6014 * permanently enabled, even when the RTOS kernel is in a critical section,
6015 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
6016 * is defined in FreeRTOSConfig.h then
6017 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
6018 * failure if a FreeRTOS API function is called from an interrupt that has
6019 * been assigned a priority above the configured maximum system call
6020 * priority. Only FreeRTOS functions that end in FromISR can be called
6021 * from interrupts that have been assigned a priority at or (logically)
6022 * below the maximum system call interrupt priority. FreeRTOS maintains a
6023 * separate interrupt safe API to ensure interrupt entry is as fast and as
6024 * simple as possible. More information (albeit Cortex-M specific) is
6025 * provided on the following link:
6026 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
6027 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
6029 pxTCB = xTaskToNotify;
6031 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6033 if( pulPreviousNotificationValue != NULL )
6035 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
6038 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
6039 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
6044 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
6048 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
6051 case eSetValueWithOverwrite:
6052 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
6055 case eSetValueWithoutOverwrite:
6057 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
6059 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
6063 /* The value could not be written to the task. */
6071 /* The task is being notified without its notify value being
6077 /* Should not get here if all enums are handled.
6078 * Artificially force an assert by testing a value the
6079 * compiler can't assume is const. */
6080 configASSERT( xTickCount == ( TickType_t ) 0 );
6084 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
6086 /* If the task is in the blocked state specifically to wait for a
6087 * notification then unblock it now. */
6088 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
6090 /* The task should not have been on an event list. */
6091 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
6093 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
6095 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6096 prvAddTaskToReadyList( pxTCB );
6100 /* The delayed and ready lists cannot be accessed, so hold
6101 * this task pending until the scheduler is resumed. */
6102 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
6105 #if ( configUSE_PREEMPTION == 1 )
6106 prvYieldForTask( pxTCB, pdFALSE );
6108 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6110 if( pxHigherPriorityTaskWoken != NULL )
6112 *pxHigherPriorityTaskWoken = pdTRUE;
6118 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6123 #endif /* configUSE_TASK_NOTIFICATIONS */
6124 /*-----------------------------------------------------------*/
6126 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6128 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
6129 UBaseType_t uxIndexToNotify,
6130 BaseType_t * pxHigherPriorityTaskWoken )
6133 uint8_t ucOriginalNotifyState;
6134 UBaseType_t uxSavedInterruptStatus;
6136 configASSERT( xTaskToNotify );
6137 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6139 /* RTOS ports that support interrupt nesting have the concept of a
6140 * maximum system call (or maximum API call) interrupt priority.
6141 * Interrupts that are above the maximum system call priority are keep
6142 * permanently enabled, even when the RTOS kernel is in a critical section,
6143 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
6144 * is defined in FreeRTOSConfig.h then
6145 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
6146 * failure if a FreeRTOS API function is called from an interrupt that has
6147 * been assigned a priority above the configured maximum system call
6148 * priority. Only FreeRTOS functions that end in FromISR can be called
6149 * from interrupts that have been assigned a priority at or (logically)
6150 * below the maximum system call interrupt priority. FreeRTOS maintains a
6151 * separate interrupt safe API to ensure interrupt entry is as fast and as
6152 * simple as possible. More information (albeit Cortex-M specific) is
6153 * provided on the following link:
6154 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
6155 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
6157 pxTCB = xTaskToNotify;
6159 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6161 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
6162 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
6164 /* 'Giving' is equivalent to incrementing a count in a counting
6166 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
6168 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
6170 /* If the task is in the blocked state specifically to wait for a
6171 * notification then unblock it now. */
6172 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
6174 /* The task should not have been on an event list. */
6175 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
6177 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
6179 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6180 prvAddTaskToReadyList( pxTCB );
6184 /* The delayed and ready lists cannot be accessed, so hold
6185 * this task pending until the scheduler is resumed. */
6186 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
6189 #if ( configUSE_PREEMPTION == 1 )
6190 prvYieldForTask( pxTCB, pdFALSE );
6192 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6194 if( pxHigherPriorityTaskWoken != NULL )
6196 *pxHigherPriorityTaskWoken = pdTRUE;
6202 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6205 #endif /* configUSE_TASK_NOTIFICATIONS */
6206 /*-----------------------------------------------------------*/
6208 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6210 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
6211 UBaseType_t uxIndexToClear )
6216 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6218 /* If null is passed in here then it is the calling task that is having
6219 * its notification state cleared. */
6220 pxTCB = prvGetTCBFromHandle( xTask );
6222 taskENTER_CRITICAL();
6224 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
6226 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
6234 taskEXIT_CRITICAL();
6239 #endif /* configUSE_TASK_NOTIFICATIONS */
6240 /*-----------------------------------------------------------*/
6242 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6244 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
6245 UBaseType_t uxIndexToClear,
6246 uint32_t ulBitsToClear )
6251 /* If null is passed in here then it is the calling task that is having
6252 * its notification state cleared. */
6253 pxTCB = prvGetTCBFromHandle( xTask );
6255 taskENTER_CRITICAL();
6257 /* Return the notification as it was before the bits were cleared,
6258 * then clear the bit mask. */
6259 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
6260 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
6262 taskEXIT_CRITICAL();
6267 #endif /* configUSE_TASK_NOTIFICATIONS */
6268 /*-----------------------------------------------------------*/
6270 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
6272 uint32_t ulTaskGetIdleRunTimeCounter( void )
6274 uint32_t ulReturn = 0;
6276 for( BaseType_t i = 0; i < configNUM_CORES; i++ )
6278 ulReturn += xIdleTaskHandle[ i ]->ulRunTimeCounter;
6284 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
6285 /*-----------------------------------------------------------*/
6287 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
6288 const BaseType_t xCanBlockIndefinitely )
6290 TickType_t xTimeToWake;
6291 const TickType_t xConstTickCount = xTickCount;
6293 #if ( INCLUDE_xTaskAbortDelay == 1 )
6295 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
6296 * reset to pdFALSE so it can be detected as having been set to pdTRUE
6297 * when the task leaves the Blocked state. */
6298 pxCurrentTCB->ucDelayAborted = pdFALSE;
6302 /* Remove the task from the ready list before adding it to the blocked list
6303 * as the same list item is used for both lists. */
6304 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6306 /* The current task must be in a ready list, so there is no need to
6307 * check, and the port reset macro can be called directly. */
6308 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. */
6312 mtCOVERAGE_TEST_MARKER();
6315 #if ( INCLUDE_vTaskSuspend == 1 )
6317 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
6319 /* Add the task to the suspended task list instead of a delayed task
6320 * list to ensure it is not woken by a timing event. It will block
6322 vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
6326 /* Calculate the time at which the task should be woken if the event
6327 * does not occur. This may overflow but this doesn't matter, the
6328 * kernel will manage it correctly. */
6329 xTimeToWake = xConstTickCount + xTicksToWait;
6331 /* The list item will be inserted in wake time order. */
6332 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6334 if( xTimeToWake < xConstTickCount )
6336 /* Wake time has overflowed. Place this item in the overflow
6338 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6342 /* The wake time has not overflowed, so the current block list
6344 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6346 /* If the task entering the blocked state was placed at the
6347 * head of the list of blocked tasks then xNextTaskUnblockTime
6348 * needs to be updated too. */
6349 if( xTimeToWake < xNextTaskUnblockTime )
6351 xNextTaskUnblockTime = xTimeToWake;
6355 mtCOVERAGE_TEST_MARKER();
6360 #else /* INCLUDE_vTaskSuspend */
6362 /* Calculate the time at which the task should be woken if the event
6363 * does not occur. This may overflow but this doesn't matter, the kernel
6364 * will manage it correctly. */
6365 xTimeToWake = xConstTickCount + xTicksToWait;
6367 /* The list item will be inserted in wake time order. */
6368 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6370 if( xTimeToWake < xConstTickCount )
6372 /* Wake time has overflowed. Place this item in the overflow list. */
6373 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6377 /* The wake time has not overflowed, so the current block list is used. */
6378 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6380 /* If the task entering the blocked state was placed at the head of the
6381 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
6383 if( xTimeToWake < xNextTaskUnblockTime )
6385 xNextTaskUnblockTime = xTimeToWake;
6389 mtCOVERAGE_TEST_MARKER();
6393 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
6394 ( void ) xCanBlockIndefinitely;
6396 #endif /* INCLUDE_vTaskSuspend */
6399 /* Code below here allows additional code to be inserted into this source file,
6400 * especially where access to file scope functions and data is needed (for example
6401 * when performing module tests). */
6403 #ifdef FREERTOS_MODULE_TEST
6404 #include "tasks_test_access_functions.h"
6408 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
6410 #include "freertos_tasks_c_additions.h"
6412 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
6413 static void freertos_tasks_c_additions_init( void )
6415 FREERTOS_TASKS_C_ADDITIONS_INIT();
6419 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */