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 = 0UL; /*< Holds the value of a timer/counter the last time a task was switched in. */
400 PRIVILEGED_DATA static volatile uint32_t ulTotalRunTime = 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 BaseType_t 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 BaseType_t 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 while( xTaskScheduled == pdFALSE )
837 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
839 if( uxCurrentPriority < uxTopReadyPriority )
841 /* We can't schedule any tasks, other than idle, that have a
842 * priority lower than the priority of a task currently running
843 * on another core. */
844 uxCurrentPriority = tskIDLE_PRIORITY;
849 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
851 List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
852 ListItem_t * pxLastTaskItem = pxReadyList->pxIndex->pxPrevious;
853 ListItem_t * pxTaskItem = pxLastTaskItem;
855 if( ( void * ) pxLastTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
857 pxLastTaskItem = pxLastTaskItem->pxPrevious;
860 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
861 * must not be decremented any further */
862 xDecrementTopPriority = pdFALSE;
868 pxTaskItem = pxTaskItem->pxNext;
870 if( ( void * ) pxTaskItem == ( void * ) &( pxReadyList->xListEnd ) )
872 pxTaskItem = pxTaskItem->pxNext;
875 pxTCB = pxTaskItem->pvOwner;
877 /*debug_printf("Attempting to schedule %s on core %d\n", pxTCB->pcTaskName, portGET_CORE_ID() ); */
879 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
881 /* When falling back to the idle priority because only one priority
882 * level is allowed to run at a time, we should ONLY schedule the true
883 * idle tasks, not user tasks at the idle priority. */
884 if( uxCurrentPriority < uxTopReadyPriority )
886 if( pxTCB->xIsIdle == pdFALSE )
892 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
894 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
896 #if ( configNUM_CORES > 1 )
897 #if ( configUSE_CORE_AFFINITY == 1 )
898 if( ( pxTCB->uxCoreAffinityMask & ( 1 << xCoreID ) ) != 0 )
902 /* If the task is not being executed by any core swap it in */
903 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
904 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
905 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
907 pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
908 pxCurrentTCBs[ xCoreID ] = pxTCB;
909 xTaskScheduled = pdTRUE;
912 else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
914 configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_YIELDING ) );
915 #if ( configNUM_CORES > 1 )
916 #if ( configUSE_CORE_AFFINITY == 1 )
917 if( ( pxTCB->uxCoreAffinityMask & ( 1 << xCoreID ) ) != 0 )
921 /* The task is already running on this core, mark it as scheduled */
922 pxTCB->xTaskRunState = ( TaskRunning_t ) xCoreID;
923 xTaskScheduled = pdTRUE;
927 if( xTaskScheduled != pdFALSE )
929 /* Once a task has been selected to run on this core,
930 * move it to the end of the ready task list. */
931 uxListRemove( pxTaskItem );
932 vListInsertEnd( pxReadyList, pxTaskItem );
935 } while( pxTaskItem != pxLastTaskItem );
939 if( xDecrementTopPriority != pdFALSE )
941 uxTopReadyPriority--;
942 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
944 xPriorityDropped = pdTRUE;
950 /* This function can get called by vTaskSuspend() before the scheduler is started.
951 * In that case, since the idle tasks have not yet been created it is possible that we
952 * won't find a new task to schedule. Return pdFALSE in this case. */
953 if( ( xSchedulerRunning == pdFALSE ) && ( uxCurrentPriority == tskIDLE_PRIORITY ) && ( xTaskScheduled == pdFALSE ) )
958 configASSERT( ( uxCurrentPriority > tskIDLE_PRIORITY ) || ( xTaskScheduled == pdTRUE ) );
962 configASSERT( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ]->xTaskRunState ) );
964 #if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) )
965 if( xPriorityDropped != pdFALSE )
967 /* There may be several ready tasks that were being prevented from running because there was
968 * a higher priority task running. Now that the last of the higher priority tasks is no longer
969 * running, make sure all the other idle tasks yield. */
972 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUM_CORES; x++ )
974 if( pxCurrentTCBs[ x ]->xIsIdle != pdFALSE )
980 #endif /* if ( ( configRUN_MULTIPLE_PRIORITIES == 0 ) && ( configNUM_CORES > 1 ) ) */
982 #if ( configNUM_CORES > 1 )
983 #if ( configUSE_CORE_AFFINITY == 1 )
984 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
986 /* A ready task was just bumped off this core. Look at the cores it can run from
987 * from to see if it is able to run on any of them */
988 UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
989 BaseType_t xLowestPriority = pxPreviousTCB->uxPriority - pxPreviousTCB->xIsIdle;
990 BaseType_t xLowestPriorityCore = -1;
992 if( ( uxCoreMap & ( 1 << xCoreID ) ) != 0 )
994 /* The ready task that was removed from this core is not excluded from it.
995 * Only look at the intersection of the cores the removed task is allowed to run
996 * on with the cores that the new task is excluded from. It is possible that the
997 * new task was only placed onto this core because it is excluded from another.
998 * Check to see if the previous task could run on one of those cores. */
999 uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1003 /* The ready task that was removed from this core is excluded from it. */
1006 uxCoreMap &= ( ( 1 << configNUM_CORES ) - 1 );
1008 while( uxCoreMap != 0 )
1010 int uxCore = 31UL - ( uint32_t ) __builtin_clz( uxCoreMap );
1012 configASSERT( taskVALID_CORE_ID( uxCore ) );
1014 uxCoreMap &= ~( 1 << uxCore );
1016 BaseType_t xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority - pxCurrentTCBs[ uxCore ]->xIsIdle;
1018 if( ( xTaskPriority < xLowestPriority ) && ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ]->xTaskRunState ) != pdFALSE ) && ( xYieldPendings[ uxCore ] == pdFALSE ) )
1020 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1021 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1024 xLowestPriority = xTaskPriority;
1025 xLowestPriorityCore = uxCore;
1030 if( taskVALID_CORE_ID( xLowestPriorityCore ) )
1032 prvYieldCore( xLowestPriorityCore );
1035 #endif /* if ( configUSE_CORE_AFFINITY == 1 ) */
1036 #endif /* if ( configNUM_CORES > 1 ) */
1041 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1043 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
1045 UBaseType_t uxTopPriority;
1047 /* Find the highest priority list that contains ready tasks. */
1048 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority );
1049 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 );
1050 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) );
1053 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1054 /*-----------------------------------------------------------*/
1056 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1058 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1059 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1060 const uint32_t ulStackDepth,
1061 void * const pvParameters,
1062 UBaseType_t uxPriority,
1063 StackType_t * const puxStackBuffer,
1064 StaticTask_t * const pxTaskBuffer )
1065 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1067 return xTaskCreateStaticAffinitySet(pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, tskNO_AFFINITY);
1070 TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1071 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1072 const uint32_t ulStackDepth,
1073 void * const pvParameters,
1074 UBaseType_t uxPriority,
1075 StackType_t * const puxStackBuffer,
1076 StaticTask_t * const pxTaskBuffer,
1077 UBaseType_t uxCoreAffinityMask )
1078 #endif /* ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) */
1081 TaskHandle_t xReturn;
1083 configASSERT( puxStackBuffer != NULL );
1084 configASSERT( pxTaskBuffer != NULL );
1086 #if ( configASSERT_DEFINED == 1 )
1088 /* Sanity check that the size of the structure used to declare a
1089 * variable of type StaticTask_t equals the size of the real task
1091 volatile size_t xSize = sizeof( StaticTask_t );
1092 configASSERT( xSize == sizeof( TCB_t ) );
1093 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1095 #endif /* configASSERT_DEFINED */
1097 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1099 /* The memory used for the task's TCB and stack are passed into this
1100 * function - use them. */
1101 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. */
1102 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1104 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1106 /* Tasks can be created statically or dynamically, so note this
1107 * task was created statically in case the task is later deleted. */
1108 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1110 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1112 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1114 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1116 /* Set the task's affinity before scheduling it */
1117 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1121 prvAddNewTaskToReadyList( pxNewTCB );
1131 #endif /* SUPPORT_STATIC_ALLOCATION */
1132 /*-----------------------------------------------------------*/
1134 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1136 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1137 TaskHandle_t * pxCreatedTask )
1138 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1140 return xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1143 BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1144 UBaseType_t uxCoreAffinityMask,
1145 TaskHandle_t * pxCreatedTask )
1146 #endif /* ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) */
1149 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1151 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1152 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1154 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1156 /* Allocate space for the TCB. Where the memory comes from depends
1157 * on the implementation of the port malloc function and whether or
1158 * not static allocation is being used. */
1159 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1161 /* Store the stack location in the TCB. */
1162 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1164 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1166 /* Tasks can be created statically or dynamically, so note this
1167 * task was created statically in case the task is later deleted. */
1168 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1170 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1172 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1173 pxTaskDefinition->pcName,
1174 ( uint32_t ) pxTaskDefinition->usStackDepth,
1175 pxTaskDefinition->pvParameters,
1176 pxTaskDefinition->uxPriority,
1177 pxCreatedTask, pxNewTCB,
1178 pxTaskDefinition->xRegions );
1180 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1182 /* Set the task's affinity before scheduling it */
1183 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1187 prvAddNewTaskToReadyList( pxNewTCB );
1194 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1195 /*-----------------------------------------------------------*/
1197 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1199 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1200 TaskHandle_t * pxCreatedTask )
1201 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1203 return xTaskCreateRestrictedAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1206 BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1207 UBaseType_t uxCoreAffinityMask,
1208 TaskHandle_t * pxCreatedTask )
1209 #endif /* ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) */
1212 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1214 configASSERT( pxTaskDefinition->puxStackBuffer );
1216 if( pxTaskDefinition->puxStackBuffer != NULL )
1218 /* Allocate space for the TCB. Where the memory comes from depends
1219 * on the implementation of the port malloc function and whether or
1220 * not static allocation is being used. */
1221 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1223 if( pxNewTCB != NULL )
1225 /* Store the stack location in the TCB. */
1226 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1228 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1230 /* Tasks can be created statically or dynamically, so note
1231 * this task had a statically allocated stack in case it is
1232 * later deleted. The TCB was allocated dynamically. */
1233 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1235 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1237 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1238 pxTaskDefinition->pcName,
1239 ( uint32_t ) pxTaskDefinition->usStackDepth,
1240 pxTaskDefinition->pvParameters,
1241 pxTaskDefinition->uxPriority,
1242 pxCreatedTask, pxNewTCB,
1243 pxTaskDefinition->xRegions );
1245 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1247 /* Set the task's affinity before scheduling it */
1248 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1252 prvAddNewTaskToReadyList( pxNewTCB );
1260 #endif /* portUSING_MPU_WRAPPERS */
1261 /*-----------------------------------------------------------*/
1263 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1265 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1266 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1267 const configSTACK_DEPTH_TYPE usStackDepth,
1268 void * const pvParameters,
1269 UBaseType_t uxPriority,
1270 TaskHandle_t * const pxCreatedTask )
1271 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1273 return xTaskCreateAffinitySet(pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, tskNO_AFFINITY, pxCreatedTask);
1276 BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1277 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1278 const configSTACK_DEPTH_TYPE usStackDepth,
1279 void * const pvParameters,
1280 UBaseType_t uxPriority,
1281 UBaseType_t uxCoreAffinityMask,
1282 TaskHandle_t * const pxCreatedTask )
1283 #endif /* ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) */
1288 /* If the stack grows down then allocate the stack then the TCB so the stack
1289 * does not grow into the TCB. Likewise if the stack grows up then allocate
1290 * the TCB then the stack. */
1291 #if ( portSTACK_GROWTH > 0 )
1293 /* Allocate space for the TCB. Where the memory comes from depends on
1294 * the implementation of the port malloc function and whether or not static
1295 * allocation is being used. */
1296 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1298 if( pxNewTCB != NULL )
1300 /* Allocate space for the stack used by the task being created.
1301 * The base of the stack memory stored in the TCB so the task can
1302 * be deleted later if required. */
1303 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1305 if( pxNewTCB->pxStack == NULL )
1307 /* Could not allocate the stack. Delete the allocated TCB. */
1308 vPortFree( pxNewTCB );
1313 #else /* portSTACK_GROWTH */
1315 StackType_t * pxStack;
1317 /* Allocate space for the stack used by the task being created. */
1318 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. */
1320 if( pxStack != NULL )
1322 /* Allocate space for the TCB. */
1323 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. */
1325 if( pxNewTCB != NULL )
1327 /* Store the stack location in the TCB. */
1328 pxNewTCB->pxStack = pxStack;
1332 /* The stack cannot be used as the TCB was not created. Free
1334 vPortFreeStack( pxStack );
1342 #endif /* portSTACK_GROWTH */
1344 if( pxNewTCB != NULL )
1346 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1348 /* Tasks can be created statically or dynamically, so note this
1349 * task was created dynamically in case it is later deleted. */
1350 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1352 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1354 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1356 #if ( ( configNUM_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1358 /* Set the task's affinity before scheduling it */
1359 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1363 prvAddNewTaskToReadyList( pxNewTCB );
1368 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1374 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1375 /*-----------------------------------------------------------*/
1377 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1378 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1379 const uint32_t ulStackDepth,
1380 void * const pvParameters,
1381 UBaseType_t uxPriority,
1382 TaskHandle_t * const pxCreatedTask,
1384 const MemoryRegion_t * const xRegions )
1386 StackType_t * pxTopOfStack;
1389 #if ( portUSING_MPU_WRAPPERS == 1 )
1390 /* Should the task be created in privileged mode? */
1391 BaseType_t xRunPrivileged;
1393 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1395 xRunPrivileged = pdTRUE;
1399 xRunPrivileged = pdFALSE;
1401 uxPriority &= ~portPRIVILEGE_BIT;
1402 #endif /* portUSING_MPU_WRAPPERS == 1 */
1404 /* Avoid dependency on memset() if it is not required. */
1405 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1407 /* Fill the stack with a known value to assist debugging. */
1408 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1410 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1412 /* Calculate the top of stack address. This depends on whether the stack
1413 * grows from high memory to low (as per the 80x86) or vice versa.
1414 * portSTACK_GROWTH is used to make the result positive or negative as required
1416 #if ( portSTACK_GROWTH < 0 )
1418 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1419 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(). */
1421 /* Check the alignment of the calculated top of stack is correct. */
1422 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1424 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1426 /* Also record the stack's high address, which may assist
1428 pxNewTCB->pxEndOfStack = pxTopOfStack;
1430 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1432 #else /* portSTACK_GROWTH */
1434 pxTopOfStack = pxNewTCB->pxStack;
1436 /* Check the alignment of the stack buffer is correct. */
1437 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1439 /* The other extreme of the stack space is required if stack checking is
1441 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1443 #endif /* portSTACK_GROWTH */
1445 /* Store the task name in the TCB. */
1446 if( pcName != NULL )
1448 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1450 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1452 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1453 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1454 * string is not accessible (extremely unlikely). */
1455 if( pcName[ x ] == ( char ) 0x00 )
1461 mtCOVERAGE_TEST_MARKER();
1465 /* Ensure the name string is terminated in the case that the string length
1466 * was greater or equal to configMAX_TASK_NAME_LEN. */
1467 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1471 /* The task has not been given a name, so just ensure there is a NULL
1472 * terminator when it is read out. */
1473 pxNewTCB->pcTaskName[ 0 ] = 0x00;
1476 /* This is used as an array index so must ensure it's not too large. First
1477 * remove the privilege bit if one is present. */
1478 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1480 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1484 mtCOVERAGE_TEST_MARKER();
1487 pxNewTCB->uxPriority = uxPriority;
1488 #if ( configUSE_MUTEXES == 1 )
1490 pxNewTCB->uxBasePriority = uxPriority;
1491 pxNewTCB->uxMutexesHeld = 0;
1493 #endif /* configUSE_MUTEXES */
1495 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1496 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1498 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1499 * back to the containing TCB from a generic item in a list. */
1500 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1502 /* Event lists are always in priority order. */
1503 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. */
1504 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1506 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
1508 pxNewTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
1510 #endif /* portCRITICAL_NESTING_IN_TCB */
1512 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
1514 pxNewTCB->pxTaskTag = NULL;
1516 #endif /* configUSE_APPLICATION_TASK_TAG */
1518 #if ( configGENERATE_RUN_TIME_STATS == 1 )
1520 pxNewTCB->ulRunTimeCounter = 0UL;
1522 #endif /* configGENERATE_RUN_TIME_STATS */
1524 #if ( portUSING_MPU_WRAPPERS == 1 )
1526 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1530 /* Avoid compiler warning about unreferenced parameter. */
1535 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
1537 memset( ( void * ) &( pxNewTCB->pvThreadLocalStoragePointers[ 0 ] ), 0x00, sizeof( pxNewTCB->pvThreadLocalStoragePointers ) );
1541 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1543 memset( ( void * ) &( pxNewTCB->ulNotifiedValue[ 0 ] ), 0x00, sizeof( pxNewTCB->ulNotifiedValue ) );
1544 memset( ( void * ) &( pxNewTCB->ucNotifyState[ 0 ] ), 0x00, sizeof( pxNewTCB->ucNotifyState ) );
1548 #if ( configUSE_NEWLIB_REENTRANT == 1 )
1550 /* Initialise this task's Newlib reent structure.
1551 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
1552 * for additional information. */
1553 _REENT_INIT_PTR( ( &( pxNewTCB->xNewLib_reent ) ) );
1557 #if ( INCLUDE_xTaskAbortDelay == 1 )
1559 pxNewTCB->ucDelayAborted = pdFALSE;
1563 #if ( configNUM_CORES > 1 )
1564 #if ( configUSE_CORE_AFFINITY == 1 )
1566 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1570 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1572 pxNewTCB->xPreemptionDisable = 0;
1576 /* Initialize the TCB stack to look as if the task was already running,
1577 * but had been interrupted by the scheduler. The return address is set
1578 * to the start of the task function. Once the stack has been initialised
1579 * the top of stack variable is updated. */
1580 #if ( portUSING_MPU_WRAPPERS == 1 )
1582 /* If the port has capability to detect stack overflow,
1583 * pass the stack end address to the stack initialization
1584 * function as well. */
1585 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1587 #if ( portSTACK_GROWTH < 0 )
1589 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
1591 #else /* portSTACK_GROWTH */
1593 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1595 #endif /* portSTACK_GROWTH */
1597 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1599 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1601 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1603 #else /* portUSING_MPU_WRAPPERS */
1605 /* If the port has capability to detect stack overflow,
1606 * pass the stack end address to the stack initialization
1607 * function as well. */
1608 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1610 #if ( portSTACK_GROWTH < 0 )
1612 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1614 #else /* portSTACK_GROWTH */
1616 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1618 #endif /* portSTACK_GROWTH */
1620 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1622 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1624 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1626 #endif /* portUSING_MPU_WRAPPERS */
1628 /* Initialize to not running */
1629 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1631 /* Is this an idle task? */
1632 if( pxTaskCode == prvIdleTask )
1634 pxNewTCB->xIsIdle = pdTRUE;
1637 #if ( configNUM_CORES > 1 )
1638 else if( pxTaskCode == prvMinimalIdleTask )
1640 pxNewTCB->xIsIdle = pdTRUE;
1645 pxNewTCB->xIsIdle = pdFALSE;
1648 if( pxCreatedTask != NULL )
1650 /* Pass the handle out in an anonymous way. The handle can be used to
1651 * change the created task's priority, delete the created task, etc.*/
1652 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1656 mtCOVERAGE_TEST_MARKER();
1659 /*-----------------------------------------------------------*/
1661 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1663 /* Ensure interrupts don't access the task lists while the lists are being
1665 taskENTER_CRITICAL();
1667 uxCurrentNumberOfTasks++;
1669 if( xSchedulerRunning == pdFALSE )
1671 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1673 /* This is the first task to be created so do the preliminary
1674 * initialisation required. We will not recover if this call
1675 * fails, but we will report the failure. */
1676 prvInitialiseTaskLists();
1680 mtCOVERAGE_TEST_MARKER();
1683 if( pxNewTCB->xIsIdle != pdFALSE )
1687 /* Check if a core is free. */
1688 for( xCoreID = ( UBaseType_t ) 0; xCoreID < ( UBaseType_t ) configNUM_CORES; xCoreID++ )
1690 if( pxCurrentTCBs[ xCoreID ] == NULL )
1692 pxNewTCB->xTaskRunState = xCoreID;
1693 pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1701 mtCOVERAGE_TEST_MARKER();
1706 #if ( configUSE_TRACE_FACILITY == 1 )
1708 /* Add a counter into the TCB for tracing only. */
1709 pxNewTCB->uxTCBNumber = uxTaskNumber;
1711 #endif /* configUSE_TRACE_FACILITY */
1712 traceTASK_CREATE( pxNewTCB );
1714 prvAddTaskToReadyList( pxNewTCB );
1716 portSETUP_TCB( pxNewTCB );
1718 if( xSchedulerRunning != pdFALSE )
1720 /* If the created task is of a higher priority than another
1721 * currently running task and preemption is on then it should
1723 #if ( configUSE_PREEMPTION == 1 )
1724 prvYieldForTask( pxNewTCB, pdFALSE );
1729 mtCOVERAGE_TEST_MARKER();
1732 taskEXIT_CRITICAL();
1734 /*-----------------------------------------------------------*/
1736 #if ( INCLUDE_vTaskDelete == 1 )
1738 void vTaskDelete( TaskHandle_t xTaskToDelete )
1741 TaskRunning_t xTaskRunningOnCore;
1743 taskENTER_CRITICAL();
1745 /* If null is passed in here then it is the calling task that is
1747 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1749 xTaskRunningOnCore = pxTCB->xTaskRunState;
1751 /* Remove task from the ready/delayed list. */
1752 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1754 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1758 mtCOVERAGE_TEST_MARKER();
1761 /* Is the task waiting on an event also? */
1762 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1764 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1768 mtCOVERAGE_TEST_MARKER();
1771 /* Increment the uxTaskNumber also so kernel aware debuggers can
1772 * detect that the task lists need re-generating. This is done before
1773 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1777 /* If the task is running (or yielding), we must add it to the
1778 * termination list so that an idle task can delete it when it is
1779 * no longer running. */
1780 if( xTaskRunningOnCore != taskTASK_NOT_RUNNING )
1782 /* A running task is being deleted. This cannot complete within the
1783 * task itself, as a context switch to another task is required.
1784 * Place the task in the termination list. The idle task will
1785 * check the termination list and free up any memory allocated by
1786 * the scheduler for the TCB and stack of the deleted task. */
1787 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1789 /* Increment the ucTasksDeleted variable so the idle task knows
1790 * there is a task that has been deleted and that it should therefore
1791 * check the xTasksWaitingTermination list. */
1792 ++uxDeletedTasksWaitingCleanUp;
1794 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1795 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1796 traceTASK_DELETE( pxTCB );
1798 /* The pre-delete hook is primarily for the Windows simulator,
1799 * in which Windows specific clean up operations are performed,
1800 * after which it is not possible to yield away from this task -
1801 * hence xYieldPending is used to latch that a context switch is
1803 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPendings[ pxTCB->xTaskRunState ] );
1807 --uxCurrentNumberOfTasks;
1808 traceTASK_DELETE( pxTCB );
1809 prvDeleteTCB( pxTCB );
1811 /* Reset the next expected unblock time in case it referred to
1812 * the task that has just been deleted. */
1813 prvResetNextTaskUnblockTime();
1816 /* Force a reschedule if the task that has just been deleted was running. */
1817 if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( xTaskRunningOnCore ) ) )
1821 xCoreID = portGET_CORE_ID();
1823 if( xTaskRunningOnCore == xCoreID )
1825 configASSERT( uxSchedulerSuspended == 0 );
1826 vTaskYieldWithinAPI();
1830 prvYieldCore( xTaskRunningOnCore );
1834 taskEXIT_CRITICAL();
1837 #endif /* INCLUDE_vTaskDelete */
1838 /*-----------------------------------------------------------*/
1840 #if ( INCLUDE_xTaskDelayUntil == 1 )
1842 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
1843 const TickType_t xTimeIncrement )
1845 TickType_t xTimeToWake;
1846 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
1848 configASSERT( pxPreviousWakeTime );
1849 configASSERT( ( xTimeIncrement > 0U ) );
1853 configASSERT( uxSchedulerSuspended == 1 );
1855 /* Minor optimisation. The tick count cannot change in this
1857 const TickType_t xConstTickCount = xTickCount;
1859 /* Generate the tick time at which the task wants to wake. */
1860 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
1862 if( xConstTickCount < *pxPreviousWakeTime )
1864 /* The tick count has overflowed since this function was
1865 * lasted called. In this case the only time we should ever
1866 * actually delay is if the wake time has also overflowed,
1867 * and the wake time is greater than the tick time. When this
1868 * is the case it is as if neither time had overflowed. */
1869 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1871 xShouldDelay = pdTRUE;
1875 mtCOVERAGE_TEST_MARKER();
1880 /* The tick time has not overflowed. In this case we will
1881 * delay if either the wake time has overflowed, and/or the
1882 * tick time is less than the wake time. */
1883 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1885 xShouldDelay = pdTRUE;
1889 mtCOVERAGE_TEST_MARKER();
1893 /* Update the wake time ready for the next call. */
1894 *pxPreviousWakeTime = xTimeToWake;
1896 if( xShouldDelay != pdFALSE )
1898 traceTASK_DELAY_UNTIL( xTimeToWake );
1900 /* prvAddCurrentTaskToDelayedList() needs the block time, not
1901 * the time to wake, so subtract the current tick count. */
1902 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
1906 mtCOVERAGE_TEST_MARKER();
1909 xAlreadyYielded = xTaskResumeAll();
1911 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1912 * have put ourselves to sleep. */
1913 if( xAlreadyYielded == pdFALSE )
1915 vTaskYieldWithinAPI();
1919 mtCOVERAGE_TEST_MARKER();
1922 return xShouldDelay;
1925 #endif /* INCLUDE_xTaskDelayUntil */
1926 /*-----------------------------------------------------------*/
1928 #if ( INCLUDE_vTaskDelay == 1 )
1930 void vTaskDelay( const TickType_t xTicksToDelay )
1932 BaseType_t xAlreadyYielded = pdFALSE;
1934 /* A delay time of zero just forces a reschedule. */
1935 if( xTicksToDelay > ( TickType_t ) 0U )
1939 configASSERT( uxSchedulerSuspended == 1 );
1942 /* A task that is removed from the event list while the
1943 * scheduler is suspended will not get placed in the ready
1944 * list or removed from the blocked list until the scheduler
1947 * This task cannot be in an event list as it is the currently
1948 * executing task. */
1949 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
1951 xAlreadyYielded = xTaskResumeAll();
1955 mtCOVERAGE_TEST_MARKER();
1958 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1959 * have put ourselves to sleep. */
1960 if( xAlreadyYielded == pdFALSE )
1962 vTaskYieldWithinAPI();
1966 mtCOVERAGE_TEST_MARKER();
1970 #endif /* INCLUDE_vTaskDelay */
1971 /*-----------------------------------------------------------*/
1973 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
1975 eTaskState eTaskGetState( TaskHandle_t xTask )
1978 List_t const * pxStateList, * pxDelayedList, * pxOverflowedDelayedList;
1979 const TCB_t * const pxTCB = xTask;
1981 configASSERT( pxTCB );
1983 taskENTER_CRITICAL();
1985 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
1986 pxDelayedList = pxDelayedTaskList;
1987 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
1989 taskEXIT_CRITICAL();
1991 if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
1993 /* The task being queried is referenced from one of the Blocked
1998 #if ( INCLUDE_vTaskSuspend == 1 )
1999 else if( pxStateList == &xSuspendedTaskList )
2001 /* The task being queried is referenced from the suspended
2002 * list. Is it genuinely suspended or is it blocked
2004 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2006 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2010 /* The task does not appear on the event list item of
2011 * and of the RTOS objects, but could still be in the
2012 * blocked state if it is waiting on its notification
2013 * rather than waiting on an object. If not, is
2015 eReturn = eSuspended;
2017 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2019 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2026 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2028 eReturn = eSuspended;
2030 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2037 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2039 #if ( INCLUDE_vTaskDelete == 1 )
2040 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2042 /* The task being queried is referenced from the deleted
2043 * tasks list, or it is not referenced from any lists at
2049 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
2051 /* If the task is not in any other state, it must be in the
2052 * Ready (including pending ready) state. */
2053 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2055 /* Is it actively running on a core? */
2065 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2067 #endif /* INCLUDE_eTaskGetState */
2068 /*-----------------------------------------------------------*/
2070 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2072 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2074 TCB_t const * pxTCB;
2075 UBaseType_t uxReturn;
2077 taskENTER_CRITICAL();
2079 /* If null is passed in here then it is the priority of the task
2080 * that called uxTaskPriorityGet() that is being queried. */
2081 pxTCB = prvGetTCBFromHandle( xTask );
2082 uxReturn = pxTCB->uxPriority;
2084 taskEXIT_CRITICAL();
2089 #endif /* INCLUDE_uxTaskPriorityGet */
2090 /*-----------------------------------------------------------*/
2092 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2094 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2096 TCB_t const * pxTCB;
2097 UBaseType_t uxReturn, uxSavedInterruptState;
2099 /* RTOS ports that support interrupt nesting have the concept of a
2100 * maximum system call (or maximum API call) interrupt priority.
2101 * Interrupts that are above the maximum system call priority are keep
2102 * permanently enabled, even when the RTOS kernel is in a critical section,
2103 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2104 * is defined in FreeRTOSConfig.h then
2105 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2106 * failure if a FreeRTOS API function is called from an interrupt that has
2107 * been assigned a priority above the configured maximum system call
2108 * priority. Only FreeRTOS functions that end in FromISR can be called
2109 * from interrupts that have been assigned a priority at or (logically)
2110 * below the maximum system call interrupt priority. FreeRTOS maintains a
2111 * separate interrupt safe API to ensure interrupt entry is as fast and as
2112 * simple as possible. More information (albeit Cortex-M specific) is
2113 * provided on the following link:
2114 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2115 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2117 uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
2119 /* If null is passed in here then it is the priority of the calling
2120 * task that is being queried. */
2121 pxTCB = prvGetTCBFromHandle( xTask );
2122 uxReturn = pxTCB->uxPriority;
2124 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
2129 #endif /* INCLUDE_uxTaskPriorityGet */
2130 /*-----------------------------------------------------------*/
2132 #if ( INCLUDE_vTaskPrioritySet == 1 )
2134 void vTaskPrioritySet( TaskHandle_t xTask,
2135 UBaseType_t uxNewPriority )
2138 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2139 BaseType_t xYieldRequired = pdFALSE;
2140 BaseType_t xYieldForTask = pdFALSE;
2143 configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
2145 /* Ensure the new priority is valid. */
2146 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2148 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2152 mtCOVERAGE_TEST_MARKER();
2155 taskENTER_CRITICAL();
2157 /* If null is passed in here then it is the priority of the calling
2158 * task that is being changed. */
2159 pxTCB = prvGetTCBFromHandle( xTask );
2161 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2163 #if ( configUSE_MUTEXES == 1 )
2165 uxCurrentBasePriority = pxTCB->uxBasePriority;
2169 uxCurrentBasePriority = pxTCB->uxPriority;
2173 if( uxCurrentBasePriority != uxNewPriority )
2175 /* The priority change may have readied a task of higher
2176 * priority than a running task. */
2177 if( uxNewPriority > uxCurrentBasePriority )
2179 /* The priority of a task is being raised so
2180 * perform a yield for this task later. */
2181 xYieldForTask = pdTRUE;
2183 else if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2185 /* Setting the priority of a running task down means
2186 * there may now be another task of higher priority that
2187 * is ready to execute. */
2188 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2189 if( pxTCB->xPreemptionDisable == pdFALSE )
2192 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2193 xYieldRequired = pdTRUE;
2198 /* Setting the priority of any other task down does not
2199 * require a yield as the running task must be above the
2200 * new priority of the task being modified. */
2203 /* Remember the ready list the task might be referenced from
2204 * before its uxPriority member is changed so the
2205 * taskRESET_READY_PRIORITY() macro can function correctly. */
2206 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2208 #if ( configUSE_MUTEXES == 1 )
2210 /* Only change the priority being used if the task is not
2211 * currently using an inherited priority. */
2212 if( pxTCB->uxBasePriority == pxTCB->uxPriority )
2214 pxTCB->uxPriority = uxNewPriority;
2218 mtCOVERAGE_TEST_MARKER();
2221 /* The base priority gets set whatever. */
2222 pxTCB->uxBasePriority = uxNewPriority;
2224 #else /* if ( configUSE_MUTEXES == 1 ) */
2226 pxTCB->uxPriority = uxNewPriority;
2228 #endif /* if ( configUSE_MUTEXES == 1 ) */
2230 /* Only reset the event list item value if the value is not
2231 * being used for anything else. */
2232 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2234 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. */
2238 mtCOVERAGE_TEST_MARKER();
2241 /* If the task is in the blocked or suspended list we need do
2242 * nothing more than change its priority variable. However, if
2243 * the task is in a ready list it needs to be removed and placed
2244 * in the list appropriate to its new priority. */
2245 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2247 /* The task is currently in its ready list - remove before
2248 * adding it to its new ready list. As we are in a critical
2249 * section we can do this even if the scheduler is suspended. */
2250 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2252 /* It is known that the task is in its ready list so
2253 * there is no need to check again and the port level
2254 * reset macro can be called directly. */
2255 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2259 mtCOVERAGE_TEST_MARKER();
2262 prvAddTaskToReadyList( pxTCB );
2266 /* It's possible that xYieldForTask was already set to pdTRUE because
2267 * its priority is being raised. However, since it is not in a ready list
2268 * we don't actually need to yield for it. */
2269 xYieldForTask = pdFALSE;
2272 #if ( configUSE_PREEMPTION == 1 )
2273 if( xYieldRequired != pdFALSE )
2275 prvYieldCore( xCoreID );
2277 else if( xYieldForTask != pdFALSE )
2279 prvYieldForTask( pxTCB, pdTRUE );
2283 mtCOVERAGE_TEST_MARKER();
2285 #endif /* if ( configUSE_PREEMPTION == 1 ) */
2287 /* Remove compiler warning about unused variables when the port
2288 * optimised task selection is not being used. */
2289 ( void ) uxPriorityUsedOnEntry;
2292 taskEXIT_CRITICAL();
2295 #endif /* INCLUDE_vTaskPrioritySet */
2296 /*-----------------------------------------------------------*/
2298 #if ( configNUM_CORES > 1 )
2299 #if ( configUSE_CORE_AFFINITY == 1 )
2301 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2302 UBaseType_t uxCoreAffinityMask )
2307 taskENTER_CRITICAL();
2309 pxTCB = prvGetTCBFromHandle( xTask );
2311 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2313 if( xSchedulerRunning != pdFALSE )
2315 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2317 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2319 if( ( uxCoreAffinityMask & ( 1 << xCoreID ) ) == 0 )
2321 prvYieldCore( xCoreID );
2326 taskEXIT_CRITICAL();
2329 #endif /* configUSE_CORE_AFFINITY */
2330 #endif /* if ( configNUM_CORES > 1 ) */
2331 /*-----------------------------------------------------------*/
2333 #if ( configNUM_CORES > 1 )
2334 #if ( configUSE_CORE_AFFINITY == 1 )
2336 UBaseType_t vTaskCoreAffinityGet( const TaskHandle_t xTask )
2339 UBaseType_t uxCoreAffinityMask;
2341 taskENTER_CRITICAL();
2343 pxTCB = prvGetTCBFromHandle( xTask );
2344 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2346 taskEXIT_CRITICAL();
2348 return uxCoreAffinityMask;
2351 #endif /* configUSE_CORE_AFFINITY */
2352 #endif /* if ( configNUM_CORES > 1 ) */
2354 /*-----------------------------------------------------------*/
2356 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2358 void vTaskPreemptionDisable( const TaskHandle_t xTask )
2362 taskENTER_CRITICAL();
2364 pxTCB = prvGetTCBFromHandle( xTask );
2366 pxTCB->xPreemptionDisable = pdTRUE;
2368 taskEXIT_CRITICAL();
2371 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2372 /*-----------------------------------------------------------*/
2374 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2376 void vTaskPreemptionEnable( const TaskHandle_t xTask )
2381 taskENTER_CRITICAL();
2383 pxTCB = prvGetTCBFromHandle( xTask );
2385 pxTCB->xPreemptionDisable = pdFALSE;
2387 if( xSchedulerRunning != pdFALSE )
2389 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2391 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2392 prvYieldCore( xCoreID );
2396 taskEXIT_CRITICAL();
2399 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2400 /*-----------------------------------------------------------*/
2402 #if ( INCLUDE_vTaskSuspend == 1 )
2404 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2407 TaskRunning_t xTaskRunningOnCore;
2409 taskENTER_CRITICAL();
2411 /* If null is passed in here then it is the running task that is
2412 * being suspended. */
2413 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2415 traceTASK_SUSPEND( pxTCB );
2417 xTaskRunningOnCore = pxTCB->xTaskRunState;
2419 /* Remove task from the ready/delayed list and place in the
2420 * suspended list. */
2421 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2423 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2427 mtCOVERAGE_TEST_MARKER();
2430 /* Is the task waiting on an event also? */
2431 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2433 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2437 mtCOVERAGE_TEST_MARKER();
2440 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2442 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2446 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2448 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2450 /* The task was blocked to wait for a notification, but is
2451 * now suspended, so no notification was received. */
2452 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2456 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2458 if( xSchedulerRunning != pdFALSE )
2460 /* Reset the next expected unblock time in case it referred to the
2461 * task that is now in the Suspended state. */
2462 prvResetNextTaskUnblockTime();
2466 mtCOVERAGE_TEST_MARKER();
2469 if( taskTASK_IS_RUNNING( xTaskRunningOnCore ) )
2471 if( xSchedulerRunning != pdFALSE )
2473 if( xTaskRunningOnCore == portGET_CORE_ID() )
2475 /* The current task has just been suspended. */
2476 configASSERT( uxSchedulerSuspended == 0 );
2477 vTaskYieldWithinAPI();
2481 prvYieldCore( xTaskRunningOnCore );
2484 taskEXIT_CRITICAL();
2488 taskEXIT_CRITICAL();
2490 configASSERT( pxTCB == pxCurrentTCBs[ xTaskRunningOnCore ] );
2492 /* The scheduler is not running, but the task that was pointed
2493 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
2494 * must be adjusted to point to a different task. */
2495 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
2497 /* No other tasks are ready, so set the core's TCB back to
2498 * NULL so when the next task is created the core's TCB will
2499 * be able to be set to point to it no matter what its relative
2501 pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2502 pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2506 /* Attempt to switch in a new task. This could fail since the idle tasks
2507 * haven't been created yet. If it does then set the core's TCB back to
2509 if( prvSelectHighestPriorityTask( xTaskRunningOnCore ) == pdFALSE )
2511 pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2512 pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2519 taskEXIT_CRITICAL();
2521 } /* taskEXIT_CRITICAL() - already exited in one of three cases above */
2524 #endif /* INCLUDE_vTaskSuspend */
2525 /*-----------------------------------------------------------*/
2527 #if ( INCLUDE_vTaskSuspend == 1 )
2529 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
2531 BaseType_t xReturn = pdFALSE;
2532 const TCB_t * const pxTCB = xTask;
2534 /* Accesses xPendingReadyList so must be called from a critical section. */
2536 /* It does not make sense to check if the calling task is suspended. */
2537 configASSERT( xTask );
2539 /* Is the task being resumed actually in the suspended list? */
2540 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
2542 /* Has the task already been resumed from within an ISR? */
2543 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
2545 /* Is it in the suspended list because it is in the Suspended
2546 * state, or because is is blocked with no timeout? */
2547 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
2553 mtCOVERAGE_TEST_MARKER();
2558 mtCOVERAGE_TEST_MARKER();
2563 mtCOVERAGE_TEST_MARKER();
2567 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2569 #endif /* INCLUDE_vTaskSuspend */
2570 /*-----------------------------------------------------------*/
2572 #if ( INCLUDE_vTaskSuspend == 1 )
2574 void vTaskResume( TaskHandle_t xTaskToResume )
2576 TCB_t * const pxTCB = xTaskToResume;
2578 /* It does not make sense to resume the calling task. */
2579 configASSERT( xTaskToResume );
2581 /* The parameter cannot be NULL as it is impossible to resume the
2582 * currently executing task. It is also impossible to resume a task
2583 * that is actively running on another core but it is too dangerous
2584 * to check their run state here. Safer to get into a critical section
2585 * and check if it is actually suspended or not below. */
2588 taskENTER_CRITICAL();
2590 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2592 traceTASK_RESUME( pxTCB );
2594 /* The ready list can be accessed even if the scheduler is
2595 * suspended because this is inside a critical section. */
2596 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2597 prvAddTaskToReadyList( pxTCB );
2599 /* A higher priority task may have just been resumed. */
2600 #if ( configUSE_PREEMPTION == 1 )
2602 prvYieldForTask( pxTCB, pdTRUE );
2608 mtCOVERAGE_TEST_MARKER();
2611 taskEXIT_CRITICAL();
2615 mtCOVERAGE_TEST_MARKER();
2619 #endif /* INCLUDE_vTaskSuspend */
2621 /*-----------------------------------------------------------*/
2623 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
2625 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
2627 BaseType_t xYieldRequired = pdFALSE;
2628 TCB_t * const pxTCB = xTaskToResume;
2629 UBaseType_t uxSavedInterruptStatus;
2631 configASSERT( xTaskToResume );
2633 /* RTOS ports that support interrupt nesting have the concept of a
2634 * maximum system call (or maximum API call) interrupt priority.
2635 * Interrupts that are above the maximum system call priority are keep
2636 * permanently enabled, even when the RTOS kernel is in a critical section,
2637 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2638 * is defined in FreeRTOSConfig.h then
2639 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2640 * failure if a FreeRTOS API function is called from an interrupt that has
2641 * been assigned a priority above the configured maximum system call
2642 * priority. Only FreeRTOS functions that end in FromISR can be called
2643 * from interrupts that have been assigned a priority at or (logically)
2644 * below the maximum system call interrupt priority. FreeRTOS maintains a
2645 * separate interrupt safe API to ensure interrupt entry is as fast and as
2646 * simple as possible. More information (albeit Cortex-M specific) is
2647 * provided on the following link:
2648 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2649 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2651 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2653 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2655 traceTASK_RESUME_FROM_ISR( pxTCB );
2657 /* Check the ready lists can be accessed. */
2658 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2660 /* Ready lists can be accessed so move the task from the
2661 * suspended list to the ready list directly. */
2663 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2664 prvAddTaskToReadyList( pxTCB );
2668 /* The delayed or ready lists cannot be accessed so the task
2669 * is held in the pending ready list until the scheduler is
2671 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
2674 #if ( configUSE_PREEMPTION == 1 )
2675 prvYieldForTask( pxTCB, pdTRUE );
2677 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
2679 xYieldRequired = pdTRUE;
2685 mtCOVERAGE_TEST_MARKER();
2688 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2690 return xYieldRequired;
2693 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
2694 /*-----------------------------------------------------------*/
2696 static BaseType_t prvCreateIdleTasks( void )
2698 BaseType_t xReturn = pdPASS;
2700 char cIdleName[ configMAX_TASK_NAME_LEN ];
2702 /* Add each idle task at the lowest priority. */
2703 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUM_CORES; xCoreID++ )
2707 if( xReturn == pdFAIL )
2713 mtCOVERAGE_TEST_MARKER();
2716 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
2718 cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
2720 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
2721 * configMAX_TASK_NAME_LEN characters just in case the memory after the
2722 * string is not accessible (extremely unlikely). */
2723 if( cIdleName[ x ] == ( char ) 0x00 )
2729 mtCOVERAGE_TEST_MARKER();
2733 /* Append the idle task number to the end of the name if there is space */
2734 if( x < configMAX_TASK_NAME_LEN )
2736 cIdleName[ x++ ] = xCoreID + '0';
2738 /* And append a null character if there is space */
2739 if( x < configMAX_TASK_NAME_LEN )
2741 cIdleName[ x ] = '\0';
2745 mtCOVERAGE_TEST_MARKER();
2750 mtCOVERAGE_TEST_MARKER();
2753 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
2757 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
2758 StackType_t * pxIdleTaskStackBuffer = NULL;
2759 uint32_t ulIdleTaskStackSize;
2761 /* The Idle task is created using user provided RAM - obtain the
2762 * address of the RAM then create the idle task. */
2763 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
2764 xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
2766 ulIdleTaskStackSize,
2767 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
2768 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2769 pxIdleTaskStackBuffer,
2770 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2773 #if ( configNUM_CORES > 1 )
2776 static StaticTask_t xIdleTCBBuffers[ configNUM_CORES - 1 ];
2777 static StackType_t xIdleTaskStackBuffers[ configNUM_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
2779 xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvMinimalIdleTask,
2781 configMINIMAL_STACK_SIZE,
2782 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
2783 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2784 xIdleTaskStackBuffers[ xCoreID - 1 ],
2785 &xIdleTCBBuffers[ xCoreID - 1 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2787 #endif /* if ( configNUM_CORES > 1 ) */
2789 if( xIdleTaskHandle[ xCoreID ] != NULL )
2798 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
2802 /* The Idle task is being created using dynamically allocated RAM. */
2803 xReturn = xTaskCreate( prvIdleTask,
2805 configMINIMAL_STACK_SIZE,
2807 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2808 &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2811 #if ( configNUM_CORES > 1 )
2814 xReturn = xTaskCreate( prvMinimalIdleTask,
2816 configMINIMAL_STACK_SIZE,
2818 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2819 &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2823 #endif /* configSUPPORT_STATIC_ALLOCATION */
2829 void vTaskStartScheduler( void )
2833 #if ( configUSE_TIMERS == 1 )
2835 xReturn = xTimerCreateTimerTask();
2837 #endif /* configUSE_TIMERS */
2839 xReturn = prvCreateIdleTasks();
2841 if( xReturn == pdPASS )
2843 /* freertos_tasks_c_additions_init() should only be called if the user
2844 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
2845 * the only macro called by the function. */
2846 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
2848 freertos_tasks_c_additions_init();
2852 /* Interrupts are turned off here, to ensure a tick does not occur
2853 * before or during the call to xPortStartScheduler(). The stacks of
2854 * the created tasks contain a status word with interrupts switched on
2855 * so interrupts will automatically get re-enabled when the first task
2857 portDISABLE_INTERRUPTS();
2859 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) && ( configNEWLIB_REENTRANT_IS_DYNAMIC == 0 ) )
2861 /* Switch Newlib's _impure_ptr variable to point to the _reent
2862 * structure specific to the task that will run first.
2863 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
2864 * for additional information.
2866 * Note: Updating the _impure_ptr is not required when Newlib is compiled with
2867 * __DYNAMIC_REENT__ enabled. The port should provide __getreent() instead. */
2868 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
2870 #endif /* ( configUSE_NEWLIB_REENTRANT == 1 ) && ( configNEWLIB_REENTRANT_IS_DYNAMIC == 0 ) */
2872 xNextTaskUnblockTime = portMAX_DELAY;
2873 xSchedulerRunning = pdTRUE;
2874 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
2876 /* If configGENERATE_RUN_TIME_STATS is defined then the following
2877 * macro must be defined to configure the timer/counter used to generate
2878 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
2879 * is set to 0 and the following line fails to build then ensure you do not
2880 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
2881 * FreeRTOSConfig.h file. */
2882 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
2884 traceTASK_SWITCHED_IN();
2886 /* Setting up the timer tick is hardware specific and thus in the
2887 * portable interface. */
2888 if( xPortStartScheduler() != pdFALSE )
2890 /* Should not reach here as if the scheduler is running the
2891 * function will not return. */
2895 /* Should only reach here if a task calls xTaskEndScheduler(). */
2900 /* This line will only be reached if the kernel could not be started,
2901 * because there was not enough FreeRTOS heap to create the idle task
2902 * or the timer task. */
2903 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
2906 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
2907 * meaning xIdleTaskHandle is not used anywhere else. */
2908 ( void ) xIdleTaskHandle;
2910 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
2911 * from getting optimized out as it is no longer used by the kernel. */
2912 ( void ) uxTopUsedPriority;
2914 /*-----------------------------------------------------------*/
2916 void vTaskEndScheduler( void )
2918 /* Stop the scheduler interrupts and call the portable scheduler end
2919 * routine so the original ISRs can be restored if necessary. The port
2920 * layer must ensure interrupts enable bit is left in the correct state. */
2921 portDISABLE_INTERRUPTS();
2922 xSchedulerRunning = pdFALSE;
2923 vPortEndScheduler();
2925 /*----------------------------------------------------------*/
2927 void vTaskSuspendAll( void )
2929 UBaseType_t ulState;
2931 /* This must only be called from within a task */
2932 portASSERT_IF_IN_ISR();
2934 if( xSchedulerRunning != pdFALSE )
2936 /* writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
2937 * We must disable interrupts before we grab the locks in the event that this task is
2938 * interrupted and switches context before incrementing uxSchedulerSuspended.
2939 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
2940 * uxSchedulerSuspended since that will prevent context switches. */
2941 ulState = portDISABLE_INTERRUPTS();
2943 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
2944 * do not otherwise exhibit real time behaviour. */
2945 portSOFTWARE_BARRIER();
2947 portGET_TASK_LOCK();
2950 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
2951 * is used to allow calls to vTaskSuspendAll() to nest. */
2952 ++uxSchedulerSuspended;
2953 portRELEASE_ISR_LOCK();
2955 if( ( uxSchedulerSuspended == 1U ) && ( pxCurrentTCB->uxCriticalNesting == 0U ) )
2957 prvCheckForRunStateChange();
2960 portRESTORE_INTERRUPTS( ulState );
2964 mtCOVERAGE_TEST_MARKER();
2967 /*----------------------------------------------------------*/
2969 #if ( configUSE_TICKLESS_IDLE != 0 )
2971 static TickType_t prvGetExpectedIdleTime( void )
2974 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
2976 /* uxHigherPriorityReadyTasks takes care of the case where
2977 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
2978 * task that are in the Ready state, even though the idle task is
2980 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
2982 if( uxTopReadyPriority > tskIDLE_PRIORITY )
2984 uxHigherPriorityReadyTasks = pdTRUE;
2989 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
2991 /* When port optimised task selection is used the uxTopReadyPriority
2992 * variable is used as a bit map. If bits other than the least
2993 * significant bit are set then there are tasks that have a priority
2994 * above the idle priority that are in the Ready state. This takes
2995 * care of the case where the co-operative scheduler is in use. */
2996 if( uxTopReadyPriority > uxLeastSignificantBit )
2998 uxHigherPriorityReadyTasks = pdTRUE;
3001 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3003 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3007 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
3009 /* There are other idle priority tasks in the ready state. If
3010 * time slicing is used then the very next tick interrupt must be
3014 else if( uxHigherPriorityReadyTasks != pdFALSE )
3016 /* There are tasks in the Ready state that have a priority above the
3017 * idle priority. This path can only be reached if
3018 * configUSE_PREEMPTION is 0. */
3023 xReturn = xNextTaskUnblockTime - xTickCount;
3029 #endif /* configUSE_TICKLESS_IDLE */
3030 /*----------------------------------------------------------*/
3032 BaseType_t xTaskResumeAll( void )
3034 TCB_t * pxTCB = NULL;
3035 BaseType_t xAlreadyYielded = pdFALSE;
3037 if( xSchedulerRunning != pdFALSE )
3039 /* It is possible that an ISR caused a task to be removed from an event
3040 * list while the scheduler was suspended. If this was the case then the
3041 * removed task will have been added to the xPendingReadyList. Once the
3042 * scheduler has been resumed it is safe to move all the pending ready
3043 * tasks from this list into their appropriate ready list. */
3044 taskENTER_CRITICAL();
3048 xCoreID = portGET_CORE_ID();
3050 /* If uxSchedulerSuspended is zero then this function does not match a
3051 * previous call to vTaskSuspendAll(). */
3052 configASSERT( uxSchedulerSuspended );
3054 --uxSchedulerSuspended;
3055 portRELEASE_TASK_LOCK();
3057 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3059 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3061 /* Move any readied tasks from the pending list into the
3062 * appropriate ready list. */
3063 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3065 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. */
3066 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3067 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3068 prvAddTaskToReadyList( pxTCB );
3070 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
3071 * If the current core yielded then vTaskSwitchContext() has already been called
3072 * which sets xYieldPendings for the current core to pdTRUE. */
3077 /* A task was unblocked while the scheduler was suspended,
3078 * which may have prevented the next unblock time from being
3079 * re-calculated, in which case re-calculate it now. Mainly
3080 * important for low power tickless implementations, where
3081 * this can prevent an unnecessary exit from low power
3083 prvResetNextTaskUnblockTime();
3086 /* If any ticks occurred while the scheduler was suspended then
3087 * they should be processed now. This ensures the tick count does
3088 * not slip, and that any delayed tasks are resumed at the correct
3091 * It should be safe to call xTaskIncrementTick here from any core
3092 * since we are in a critical section and xTaskIncrementTick itself
3093 * protects itself within a critical section. Suspending the scheduler
3094 * from any core causes xTaskIncrementTick to increment uxPendedCounts.*/
3096 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3098 if( xPendedCounts > ( TickType_t ) 0U )
3102 if( xTaskIncrementTick() != pdFALSE )
3104 /* other cores are interrupted from
3105 * within xTaskIncrementTick(). */
3106 xYieldPendings[ xCoreID ] = pdTRUE;
3110 mtCOVERAGE_TEST_MARKER();
3114 } while( xPendedCounts > ( TickType_t ) 0U );
3120 mtCOVERAGE_TEST_MARKER();
3124 if( xYieldPendings[ xCoreID ] != pdFALSE )
3126 /* If xYieldPendings is true then taskEXIT_CRITICAL()
3127 * will yield, so make sure we return true to let the
3128 * caller know a yield has already happened. */
3129 xAlreadyYielded = pdTRUE;
3135 mtCOVERAGE_TEST_MARKER();
3138 taskEXIT_CRITICAL();
3142 mtCOVERAGE_TEST_MARKER();
3145 return xAlreadyYielded;
3147 /*-----------------------------------------------------------*/
3149 TickType_t xTaskGetTickCount( void )
3153 /* Critical section required if running on a 16 bit processor. */
3154 portTICK_TYPE_ENTER_CRITICAL();
3156 xTicks = xTickCount;
3158 portTICK_TYPE_EXIT_CRITICAL();
3162 /*-----------------------------------------------------------*/
3164 TickType_t xTaskGetTickCountFromISR( void )
3167 UBaseType_t uxSavedInterruptStatus;
3169 /* RTOS ports that support interrupt nesting have the concept of a maximum
3170 * system call (or maximum API call) interrupt priority. Interrupts that are
3171 * above the maximum system call priority are kept permanently enabled, even
3172 * when the RTOS kernel is in a critical section, but cannot make any calls to
3173 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
3174 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3175 * failure if a FreeRTOS API function is called from an interrupt that has been
3176 * assigned a priority above the configured maximum system call priority.
3177 * Only FreeRTOS functions that end in FromISR can be called from interrupts
3178 * that have been assigned a priority at or (logically) below the maximum
3179 * system call interrupt priority. FreeRTOS maintains a separate interrupt
3180 * safe API to ensure interrupt entry is as fast and as simple as possible.
3181 * More information (albeit Cortex-M specific) is provided on the following
3182 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3183 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3185 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
3187 xReturn = xTickCount;
3189 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3193 /*-----------------------------------------------------------*/
3195 UBaseType_t uxTaskGetNumberOfTasks( void )
3197 /* A critical section is not required because the variables are of type
3199 return uxCurrentNumberOfTasks;
3201 /*-----------------------------------------------------------*/
3203 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3207 /* If null is passed in here then the name of the calling task is being
3209 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3210 configASSERT( pxTCB );
3211 return &( pxTCB->pcTaskName[ 0 ] );
3213 /*-----------------------------------------------------------*/
3215 #if ( INCLUDE_xTaskGetHandle == 1 )
3217 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3218 const char pcNameToQuery[] )
3220 TCB_t * pxNextTCB, * pxFirstTCB, * pxReturn = NULL;
3223 BaseType_t xBreakLoop;
3225 /* This function is called with the scheduler suspended. */
3227 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3229 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. */
3233 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. */
3235 /* Check each character in the name looking for a match or
3237 xBreakLoop = pdFALSE;
3239 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3241 cNextChar = pxNextTCB->pcTaskName[ x ];
3243 if( cNextChar != pcNameToQuery[ x ] )
3245 /* Characters didn't match. */
3246 xBreakLoop = pdTRUE;
3248 else if( cNextChar == ( char ) 0x00 )
3250 /* Both strings terminated, a match must have been
3252 pxReturn = pxNextTCB;
3253 xBreakLoop = pdTRUE;
3257 mtCOVERAGE_TEST_MARKER();
3260 if( xBreakLoop != pdFALSE )
3266 if( pxReturn != NULL )
3268 /* The handle has been found. */
3271 } while( pxNextTCB != pxFirstTCB );
3275 mtCOVERAGE_TEST_MARKER();
3281 #endif /* INCLUDE_xTaskGetHandle */
3282 /*-----------------------------------------------------------*/
3284 #if ( INCLUDE_xTaskGetHandle == 1 )
3286 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3288 UBaseType_t uxQueue = configMAX_PRIORITIES;
3291 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
3292 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
3296 /* Search the ready lists. */
3300 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
3304 /* Found the handle. */
3307 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3309 /* Search the delayed lists. */
3312 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
3317 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
3320 #if ( INCLUDE_vTaskSuspend == 1 )
3324 /* Search the suspended list. */
3325 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
3330 #if ( INCLUDE_vTaskDelete == 1 )
3334 /* Search the deleted list. */
3335 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
3340 ( void ) xTaskResumeAll();
3345 #endif /* INCLUDE_xTaskGetHandle */
3346 /*-----------------------------------------------------------*/
3348 #if ( configUSE_TRACE_FACILITY == 1 )
3350 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
3351 const UBaseType_t uxArraySize,
3352 uint32_t * const pulTotalRunTime )
3354 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
3358 /* Is there a space in the array for each task in the system? */
3359 if( uxArraySize >= uxCurrentNumberOfTasks )
3361 /* Fill in an TaskStatus_t structure with information on each
3362 * task in the Ready state. */
3366 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
3367 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3369 /* Fill in an TaskStatus_t structure with information on each
3370 * task in the Blocked state. */
3371 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
3372 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
3374 #if ( INCLUDE_vTaskDelete == 1 )
3376 /* Fill in an TaskStatus_t structure with information on
3377 * each task that has been deleted but not yet cleaned up. */
3378 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
3382 #if ( INCLUDE_vTaskSuspend == 1 )
3384 /* Fill in an TaskStatus_t structure with information on
3385 * each task in the Suspended state. */
3386 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
3390 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3392 if( pulTotalRunTime != NULL )
3394 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3395 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
3397 *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3401 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3403 if( pulTotalRunTime != NULL )
3405 *pulTotalRunTime = 0;
3408 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3412 mtCOVERAGE_TEST_MARKER();
3415 ( void ) xTaskResumeAll();
3420 #endif /* configUSE_TRACE_FACILITY */
3421 /*----------------------------------------------------------*/
3423 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
3425 TaskHandle_t * xTaskGetIdleTaskHandle( void )
3427 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
3428 * started, then xIdleTaskHandle will be NULL. */
3429 configASSERT( ( xIdleTaskHandle != NULL ) );
3430 return &( xIdleTaskHandle[ 0 ] );
3433 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
3434 /*----------------------------------------------------------*/
3436 /* This conditional compilation should use inequality to 0, not equality to 1.
3437 * This is to ensure vTaskStepTick() is available when user defined low power mode
3438 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
3440 #if ( configUSE_TICKLESS_IDLE != 0 )
3442 void vTaskStepTick( const TickType_t xTicksToJump )
3444 /* Correct the tick count value after a period during which the tick
3445 * was suppressed. Note this does *not* call the tick hook function for
3446 * each stepped tick. */
3447 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
3448 xTickCount += xTicksToJump;
3449 traceINCREASE_TICK_COUNT( xTicksToJump );
3452 #endif /* configUSE_TICKLESS_IDLE */
3453 /*----------------------------------------------------------*/
3455 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
3457 BaseType_t xYieldOccurred;
3459 /* Must not be called with the scheduler suspended as the implementation
3460 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
3461 configASSERT( uxSchedulerSuspended == 0 );
3463 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
3464 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
3466 xPendedTicks += xTicksToCatchUp;
3467 xYieldOccurred = xTaskResumeAll();
3469 return xYieldOccurred;
3471 /*----------------------------------------------------------*/
3473 #if ( INCLUDE_xTaskAbortDelay == 1 )
3475 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
3477 TCB_t * pxTCB = xTask;
3480 configASSERT( pxTCB );
3484 /* A task can only be prematurely removed from the Blocked state if
3485 * it is actually in the Blocked state. */
3486 if( eTaskGetState( xTask ) == eBlocked )
3490 /* Remove the reference to the task from the blocked list. An
3491 * interrupt won't touch the xStateListItem because the
3492 * scheduler is suspended. */
3493 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3495 /* Is the task waiting on an event also? If so remove it from
3496 * the event list too. Interrupts can touch the event list item,
3497 * even though the scheduler is suspended, so a critical section
3499 taskENTER_CRITICAL();
3501 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3503 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3505 /* This lets the task know it was forcibly removed from the
3506 * blocked state so it should not re-evaluate its block time and
3507 * then block again. */
3508 pxTCB->ucDelayAborted = pdTRUE;
3512 mtCOVERAGE_TEST_MARKER();
3515 taskEXIT_CRITICAL();
3517 /* Place the unblocked task into the appropriate ready list. */
3518 prvAddTaskToReadyList( pxTCB );
3520 /* A task being unblocked cannot cause an immediate context
3521 * switch if preemption is turned off. */
3522 #if ( configUSE_PREEMPTION == 1 )
3524 taskENTER_CRITICAL();
3526 prvYieldForTask( pxTCB, pdFALSE );
3528 taskEXIT_CRITICAL();
3530 #endif /* configUSE_PREEMPTION */
3537 ( void ) xTaskResumeAll();
3542 #endif /* INCLUDE_xTaskAbortDelay */
3543 /*----------------------------------------------------------*/
3545 BaseType_t xTaskIncrementTick( void )
3548 TickType_t xItemValue;
3549 BaseType_t xSwitchRequired = pdFALSE;
3551 #if ( configUSE_PREEMPTION == 1 )
3553 BaseType_t xCoreYieldList[ configNUM_CORES ] = { pdFALSE };
3554 #endif /* configUSE_PREEMPTION */
3556 taskENTER_CRITICAL();
3558 /* Called by the portable layer each time a tick interrupt occurs.
3559 * Increments the tick then checks to see if the new tick value will cause any
3560 * tasks to be unblocked. */
3561 traceTASK_INCREMENT_TICK( xTickCount );
3563 /* Tick increment should occur on every kernel timer event. Core 0 has the
3564 * responsibility to increment the tick, or increment the pended ticks if the
3565 * scheduler is suspended. If pended ticks is greater than zero, the core that
3566 * calls xTaskResumeAll has the responsibility to increment the tick. */
3567 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3569 /* Minor optimisation. The tick count cannot change in this
3571 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
3573 /* Increment the RTOS tick, switching the delayed and overflowed
3574 * delayed lists if it wraps to 0. */
3575 xTickCount = xConstTickCount;
3577 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
3579 taskSWITCH_DELAYED_LISTS();
3583 mtCOVERAGE_TEST_MARKER();
3586 /* See if this tick has made a timeout expire. Tasks are stored in
3587 * the queue in the order of their wake time - meaning once one task
3588 * has been found whose block time has not expired there is no need to
3589 * look any further down the list. */
3590 if( xConstTickCount >= xNextTaskUnblockTime )
3594 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
3596 /* The delayed list is empty. Set xNextTaskUnblockTime
3597 * to the maximum possible value so it is extremely
3599 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
3600 * next time through. */
3601 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3606 /* The delayed list is not empty, get the value of the
3607 * item at the head of the delayed list. This is the time
3608 * at which the task at the head of the delayed list must
3609 * be removed from the Blocked state. */
3610 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. */
3611 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
3613 if( xConstTickCount < xItemValue )
3615 /* It is not time to unblock this item yet, but the
3616 * item value is the time at which the task at the head
3617 * of the blocked list must be removed from the Blocked
3618 * state - so record the item value in
3619 * xNextTaskUnblockTime. */
3620 xNextTaskUnblockTime = xItemValue;
3621 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
3625 mtCOVERAGE_TEST_MARKER();
3628 /* It is time to remove the item from the Blocked state. */
3629 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3631 /* Is the task waiting on an event also? If so remove
3632 * it from the event list. */
3633 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3635 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3639 mtCOVERAGE_TEST_MARKER();
3642 /* Place the unblocked task into the appropriate ready
3644 prvAddTaskToReadyList( pxTCB );
3646 /* A task being unblocked cannot cause an immediate
3647 * context switch if preemption is turned off. */
3648 #if ( configUSE_PREEMPTION == 1 )
3650 prvYieldForTask( pxTCB, pdTRUE );
3652 #endif /* configUSE_PREEMPTION */
3657 /* Tasks of equal priority to the currently running task will share
3658 * processing time (time slice) if preemption is on, and the application
3659 * writer has not explicitly turned time slicing off. */
3660 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
3662 /* TODO: If there are fewer "non-IDLE" READY tasks than cores, do not
3663 * force a context switch that would just shuffle tasks around cores */
3664 /* TODO: There are certainly better ways of doing this that would reduce
3665 * the number of interrupts and also potentially help prevent tasks from
3666 * moving between cores as often. This, however, works for now. */
3667 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3669 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ x ]->uxPriority ] ) ) > ( UBaseType_t ) 1 )
3671 xCoreYieldList[ x ] = pdTRUE;
3675 mtCOVERAGE_TEST_MARKER();
3679 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
3681 #if ( configUSE_TICK_HOOK == 1 )
3683 /* Guard against the tick hook being called when the pended tick
3684 * count is being unwound (when the scheduler is being unlocked). */
3685 if( xPendedTicks == ( TickType_t ) 0 )
3687 vApplicationTickHook();
3691 mtCOVERAGE_TEST_MARKER();
3694 #endif /* configUSE_TICK_HOOK */
3696 #if ( configUSE_PREEMPTION == 1 )
3698 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3700 if( xYieldPendings[ x ] != pdFALSE )
3702 xCoreYieldList[ x ] = pdTRUE;
3706 mtCOVERAGE_TEST_MARKER();
3710 #endif /* configUSE_PREEMPTION */
3712 #if ( configUSE_PREEMPTION == 1 )
3716 xCoreID = portGET_CORE_ID();
3718 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3720 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3721 if( pxCurrentTCBs[ x ]->xPreemptionDisable == pdFALSE )
3724 if( xCoreYieldList[ x ] != pdFALSE )
3728 xSwitchRequired = pdTRUE;
3737 mtCOVERAGE_TEST_MARKER();
3742 #endif /* configUSE_PREEMPTION */
3748 /* The tick hook gets called at regular intervals, even if the
3749 * scheduler is locked. */
3750 #if ( configUSE_TICK_HOOK == 1 )
3752 vApplicationTickHook();
3757 taskEXIT_CRITICAL();
3759 return xSwitchRequired;
3761 /*-----------------------------------------------------------*/
3763 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3765 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
3766 TaskHookFunction_t pxHookFunction )
3770 /* If xTask is NULL then it is the task hook of the calling task that is
3774 xTCB = ( TCB_t * ) pxCurrentTCB;
3781 /* Save the hook function in the TCB. A critical section is required as
3782 * the value can be accessed from an interrupt. */
3783 taskENTER_CRITICAL();
3785 xTCB->pxTaskTag = pxHookFunction;
3787 taskEXIT_CRITICAL();
3790 #endif /* configUSE_APPLICATION_TASK_TAG */
3791 /*-----------------------------------------------------------*/
3793 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3795 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
3798 TaskHookFunction_t xReturn;
3800 /* If xTask is NULL then set the calling task's hook. */
3801 pxTCB = prvGetTCBFromHandle( xTask );
3803 /* Save the hook function in the TCB. A critical section is required as
3804 * the value can be accessed from an interrupt. */
3805 taskENTER_CRITICAL();
3807 xReturn = pxTCB->pxTaskTag;
3809 taskEXIT_CRITICAL();
3814 #endif /* configUSE_APPLICATION_TASK_TAG */
3815 /*-----------------------------------------------------------*/
3817 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3819 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
3822 TaskHookFunction_t xReturn;
3823 UBaseType_t uxSavedInterruptStatus;
3825 /* If xTask is NULL then set the calling task's hook. */
3826 pxTCB = prvGetTCBFromHandle( xTask );
3828 /* Save the hook function in the TCB. A critical section is required as
3829 * the value can be accessed from an interrupt. */
3830 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
3832 xReturn = pxTCB->pxTaskTag;
3834 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3839 #endif /* configUSE_APPLICATION_TASK_TAG */
3840 /*-----------------------------------------------------------*/
3842 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3844 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
3845 void * pvParameter )
3850 /* If xTask is NULL then we are calling our own task hook. */
3853 xTCB = pxCurrentTCB;
3860 if( xTCB->pxTaskTag != NULL )
3862 xReturn = xTCB->pxTaskTag( pvParameter );
3872 #endif /* configUSE_APPLICATION_TASK_TAG */
3873 /*-----------------------------------------------------------*/
3875 void vTaskSwitchContext( BaseType_t xCoreID )
3877 /* Acquire both locks:
3878 * - The ISR lock protects the ready list from simultaneous access by
3879 * both other ISRs and tasks.
3880 * - We also take the task lock to pause here in case another core has
3881 * suspended the scheduler. We don't want to simply set xYieldPending
3882 * and move on if another core suspended the scheduler. We should only
3883 * do that if the current core has suspended the scheduler. */
3885 portGET_TASK_LOCK(); /* Must always acquire the task lock first */
3888 /* vTaskSwitchContext() must never be called from within a critical section.
3889 * This is not necessarily true for vanilla FreeRTOS, but it is for this SMP port. */
3890 configASSERT( pxCurrentTCB->uxCriticalNesting == 0 );
3892 if( uxSchedulerSuspended != ( UBaseType_t ) pdFALSE )
3894 /* The scheduler is currently suspended - do not allow a context
3896 xYieldPendings[ xCoreID ] = pdTRUE;
3900 xYieldPendings[ xCoreID ] = pdFALSE;
3901 traceTASK_SWITCHED_OUT();
3903 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3905 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3906 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
3908 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3911 /* Add the amount of time the task has been running to the
3912 * accumulated time so far. The time the task started running was
3913 * stored in ulTaskSwitchedInTime. Note that there is no overflow
3914 * protection here so count values are only valid until the timer
3915 * overflows. The guard against negative values is to protect
3916 * against suspect run time stat counter implementations - which
3917 * are provided by the application, not the kernel. */
3918 if( ulTotalRunTime > ulTaskSwitchedInTime )
3920 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime );
3924 mtCOVERAGE_TEST_MARKER();
3927 ulTaskSwitchedInTime = ulTotalRunTime;
3929 #endif /* configGENERATE_RUN_TIME_STATS */
3931 /* Check for stack overflow, if configured. */
3932 taskCHECK_FOR_STACK_OVERFLOW();
3934 /* Before the currently running task is switched out, save its errno. */
3935 #if ( configUSE_POSIX_ERRNO == 1 )
3937 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
3941 /* Select a new task to run using either the generic C or port
3942 * optimised asm code. */
3943 ( void ) prvSelectHighestPriorityTask( xCoreID );
3944 traceTASK_SWITCHED_IN();
3946 /* After the new task is switched in, update the global errno. */
3947 #if ( configUSE_POSIX_ERRNO == 1 )
3949 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
3953 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) && ( configNEWLIB_REENTRANT_IS_DYNAMIC == 0 ) )
3955 /* Switch Newlib's _impure_ptr variable to point to the _reent
3956 * structure specific to this task.
3957 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
3958 * for additional information.
3960 * Note: Updating the _impure_ptr is not required when Newlib is compiled with
3961 * __DYNAMIC_REENT__ enabled. The the port should provide __getreent() instead. */
3962 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
3964 #endif /* ( configUSE_NEWLIB_REENTRANT == 1 ) && ( configNEWLIB_REENTRANT_IS_DYNAMIC == 0 ) */
3967 portRELEASE_ISR_LOCK();
3968 portRELEASE_TASK_LOCK();
3970 /*-----------------------------------------------------------*/
3972 void vTaskPlaceOnEventList( List_t * const pxEventList,
3973 const TickType_t xTicksToWait )
3975 configASSERT( pxEventList );
3977 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
3978 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
3980 /* Place the event list item of the TCB in the appropriate event list.
3981 * This is placed in the list in priority order so the highest priority task
3982 * is the first to be woken by the event. The queue that contains the event
3983 * list is locked, preventing simultaneous access from interrupts. */
3984 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3986 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3988 /*-----------------------------------------------------------*/
3990 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
3991 const TickType_t xItemValue,
3992 const TickType_t xTicksToWait )
3994 configASSERT( pxEventList );
3996 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3997 * the event groups implementation. */
3998 configASSERT( uxSchedulerSuspended != 0 );
4000 /* Store the item value in the event list item. It is safe to access the
4001 * event list item here as interrupts won't access the event list item of a
4002 * task that is not in the Blocked state. */
4003 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4005 /* Place the event list item of the TCB at the end of the appropriate event
4006 * list. It is safe to access the event list here because it is part of an
4007 * event group implementation - and interrupts don't access event groups
4008 * directly (instead they access them indirectly by pending function calls to
4009 * the task level). */
4010 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4012 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4014 /*-----------------------------------------------------------*/
4016 #if ( configUSE_TIMERS == 1 )
4018 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
4019 TickType_t xTicksToWait,
4020 const BaseType_t xWaitIndefinitely )
4022 configASSERT( pxEventList );
4024 /* This function should not be called by application code hence the
4025 * 'Restricted' in its name. It is not part of the public API. It is
4026 * designed for use by kernel code, and has special calling requirements -
4027 * it should be called with the scheduler suspended. */
4030 /* Place the event list item of the TCB in the appropriate event list.
4031 * In this case it is assume that this is the only task that is going to
4032 * be waiting on this event list, so the faster vListInsertEnd() function
4033 * can be used in place of vListInsert. */
4034 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4036 /* If the task should block indefinitely then set the block time to a
4037 * value that will be recognised as an indefinite delay inside the
4038 * prvAddCurrentTaskToDelayedList() function. */
4039 if( xWaitIndefinitely != pdFALSE )
4041 xTicksToWait = portMAX_DELAY;
4044 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
4045 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
4048 #endif /* configUSE_TIMERS */
4049 /*-----------------------------------------------------------*/
4051 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
4053 TCB_t * pxUnblockedTCB;
4056 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
4057 * called from a critical section within an ISR. */
4059 /* The event list is sorted in priority order, so the first in the list can
4060 * be removed as it is known to be the highest priority. Remove the TCB from
4061 * the delayed list, and add it to the ready list.
4063 * If an event is for a queue that is locked then this function will never
4064 * get called - the lock count on the queue will get modified instead. This
4065 * means exclusive access to the event list is guaranteed here.
4067 * This function assumes that a check has already been made to ensure that
4068 * pxEventList is not empty. */
4069 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. */
4070 configASSERT( pxUnblockedTCB );
4071 ( void ) uxListRemove( &( pxUnblockedTCB->xEventListItem ) );
4073 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4075 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
4076 prvAddTaskToReadyList( pxUnblockedTCB );
4078 #if ( configUSE_TICKLESS_IDLE != 0 )
4080 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4081 * might be set to the blocked task's time out time. If the task is
4082 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4083 * normally left unchanged, because it is automatically reset to a new
4084 * value when the tick count equals xNextTaskUnblockTime. However if
4085 * tickless idling is used it might be more important to enter sleep mode
4086 * at the earliest possible time - so reset xNextTaskUnblockTime here to
4087 * ensure it is updated at the earliest possible time. */
4088 prvResetNextTaskUnblockTime();
4094 /* The delayed and ready lists cannot be accessed, so hold this task
4095 * pending until the scheduler is resumed. */
4096 vListInsertEnd( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
4100 #if ( configUSE_PREEMPTION == 1 )
4101 prvYieldForTask( pxUnblockedTCB, pdFALSE );
4103 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
4111 /*-----------------------------------------------------------*/
4113 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
4114 const TickType_t xItemValue )
4116 TCB_t * pxUnblockedTCB;
4118 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
4119 * the event flags implementation. */
4120 configASSERT( uxSchedulerSuspended != pdFALSE );
4122 /* Store the new item value in the event list. */
4123 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4125 /* Remove the event list form the event flag. Interrupts do not access
4127 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. */
4128 configASSERT( pxUnblockedTCB );
4129 ( void ) uxListRemove( pxEventListItem );
4131 #if ( configUSE_TICKLESS_IDLE != 0 )
4133 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4134 * might be set to the blocked task's time out time. If the task is
4135 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4136 * normally left unchanged, because it is automatically reset to a new
4137 * value when the tick count equals xNextTaskUnblockTime. However if
4138 * tickless idling is used it might be more important to enter sleep mode
4139 * at the earliest possible time - so reset xNextTaskUnblockTime here to
4140 * ensure it is updated at the earliest possible time. */
4141 prvResetNextTaskUnblockTime();
4145 /* Remove the task from the delayed list and add it to the ready list. The
4146 * scheduler is suspended so interrupts will not be accessing the ready
4148 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
4149 prvAddTaskToReadyList( pxUnblockedTCB );
4151 #if ( configUSE_PREEMPTION == 1 )
4152 taskENTER_CRITICAL();
4154 prvYieldForTask( pxUnblockedTCB, pdFALSE );
4156 taskEXIT_CRITICAL();
4159 /*-----------------------------------------------------------*/
4161 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
4163 configASSERT( pxTimeOut );
4164 taskENTER_CRITICAL();
4166 pxTimeOut->xOverflowCount = xNumOfOverflows;
4167 pxTimeOut->xTimeOnEntering = xTickCount;
4169 taskEXIT_CRITICAL();
4171 /*-----------------------------------------------------------*/
4173 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
4175 /* For internal use only as it does not use a critical section. */
4176 pxTimeOut->xOverflowCount = xNumOfOverflows;
4177 pxTimeOut->xTimeOnEntering = xTickCount;
4179 /*-----------------------------------------------------------*/
4181 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
4182 TickType_t * const pxTicksToWait )
4186 configASSERT( pxTimeOut );
4187 configASSERT( pxTicksToWait );
4189 taskENTER_CRITICAL();
4191 /* Minor optimisation. The tick count cannot change in this block. */
4192 const TickType_t xConstTickCount = xTickCount;
4193 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
4195 #if ( INCLUDE_xTaskAbortDelay == 1 )
4196 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
4198 /* The delay was aborted, which is not the same as a time out,
4199 * but has the same result. */
4200 pxCurrentTCB->ucDelayAborted = pdFALSE;
4206 #if ( INCLUDE_vTaskSuspend == 1 )
4207 if( *pxTicksToWait == portMAX_DELAY )
4209 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
4210 * specified is the maximum block time then the task should block
4211 * indefinitely, and therefore never time out. */
4217 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
4219 /* The tick count is greater than the time at which
4220 * vTaskSetTimeout() was called, but has also overflowed since
4221 * vTaskSetTimeOut() was called. It must have wrapped all the way
4222 * around and gone past again. This passed since vTaskSetTimeout()
4225 *pxTicksToWait = ( TickType_t ) 0;
4227 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
4229 /* Not a genuine timeout. Adjust parameters for time remaining. */
4230 *pxTicksToWait -= xElapsedTime;
4231 vTaskInternalSetTimeOutState( pxTimeOut );
4236 *pxTicksToWait = ( TickType_t ) 0;
4240 taskEXIT_CRITICAL();
4244 /*-----------------------------------------------------------*/
4246 void vTaskMissedYield( void )
4248 /* Must be called from within a critical section */
4249 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
4251 /*-----------------------------------------------------------*/
4253 #if ( configUSE_TRACE_FACILITY == 1 )
4255 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
4257 UBaseType_t uxReturn;
4258 TCB_t const * pxTCB;
4263 uxReturn = pxTCB->uxTaskNumber;
4273 #endif /* configUSE_TRACE_FACILITY */
4274 /*-----------------------------------------------------------*/
4276 #if ( configUSE_TRACE_FACILITY == 1 )
4278 void vTaskSetTaskNumber( TaskHandle_t xTask,
4279 const UBaseType_t uxHandle )
4286 pxTCB->uxTaskNumber = uxHandle;
4290 #endif /* configUSE_TRACE_FACILITY */
4293 * -----------------------------------------------------------
4294 * The MinimalIdle task.
4295 * ----------------------------------------------------------
4297 * The minimal idle task is used for all the additional Cores in a SMP system.
4298 * There must be only 1 idle task and the rest are minimal idle tasks.
4300 * @todo additional conditional compiles to remove this function.
4303 #if ( configNUM_CORES > 1 )
4304 static portTASK_FUNCTION( prvMinimalIdleTask, pvParameters )
4310 #if ( configUSE_PREEMPTION == 0 )
4312 /* If we are not using preemption we keep forcing a task switch to
4313 * see if any other task has become available. If we are using
4314 * preemption we don't need to do this as any task becoming available
4315 * will automatically get the processor anyway. */
4318 #endif /* configUSE_PREEMPTION */
4320 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4322 /* When using preemption tasks of equal priority will be
4323 * timesliced. If a task that is sharing the idle priority is ready
4324 * to run then the idle task should yield before the end of the
4327 * A critical region is not required here as we are just reading from
4328 * the list, and an occasional incorrect value will not matter. If
4329 * the ready list at the idle priority contains one more task than the
4330 * number of idle tasks, which is equal to the configured numbers of cores
4331 * then a task other than the idle task is ready to execute. */
4332 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4338 mtCOVERAGE_TEST_MARKER();
4341 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4343 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
4345 extern void vApplicationMinimalIdleHook( void );
4347 /* Call the user defined function from within the idle task. This
4348 * allows the application designer to add background functionality
4349 * without the overhead of a separate task.
4351 * This hook is intended to manage core activity such as disabling cores that go idle.
4353 * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
4354 * CALL A FUNCTION THAT MIGHT BLOCK. */
4355 vApplicationMinimalIdleHook();
4357 #endif /* configUSE_MINIMAL_IDLE_HOOK */
4360 #endif /* if ( configNUM_CORES > 1 ) */
4363 * -----------------------------------------------------------
4365 * ----------------------------------------------------------
4369 static portTASK_FUNCTION( prvIdleTask, pvParameters )
4371 /* Stop warnings. */
4372 ( void ) pvParameters;
4374 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
4375 * SCHEDULER IS STARTED. **/
4377 /* In case a task that has a secure context deletes itself, in which case
4378 * the idle task is responsible for deleting the task's secure context, if
4380 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
4382 /* All cores start up in the idle task. This initial yield gets the application
4388 /* See if any tasks have deleted themselves - if so then the idle task
4389 * is responsible for freeing the deleted task's TCB and stack. */
4390 prvCheckTasksWaitingTermination();
4392 #if ( configUSE_PREEMPTION == 0 )
4394 /* If we are not using preemption we keep forcing a task switch to
4395 * see if any other task has become available. If we are using
4396 * preemption we don't need to do this as any task becoming available
4397 * will automatically get the processor anyway. */
4400 #endif /* configUSE_PREEMPTION */
4402 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4404 /* When using preemption tasks of equal priority will be
4405 * timesliced. If a task that is sharing the idle priority is ready
4406 * to run then the idle task should yield before the end of the
4409 * A critical region is not required here as we are just reading from
4410 * the list, and an occasional incorrect value will not matter. If
4411 * the ready list at the idle priority contains one more task than the
4412 * number of idle tasks, which is equal to the configured numbers of cores
4413 * then a task other than the idle task is ready to execute. */
4414 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4420 mtCOVERAGE_TEST_MARKER();
4423 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4425 #if ( configUSE_IDLE_HOOK == 1 )
4427 extern void vApplicationIdleHook( void );
4429 /* Call the user defined function from within the idle task. This
4430 * allows the application designer to add background functionality
4431 * without the overhead of a separate task.
4433 * NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
4434 * CALL A FUNCTION THAT MIGHT BLOCK. */
4435 vApplicationIdleHook();
4437 #endif /* configUSE_IDLE_HOOK */
4439 /* This conditional compilation should use inequality to 0, not equality
4440 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
4441 * user defined low power mode implementations require
4442 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
4443 #if ( configUSE_TICKLESS_IDLE != 0 )
4445 TickType_t xExpectedIdleTime;
4447 /* It is not desirable to suspend then resume the scheduler on
4448 * each iteration of the idle task. Therefore, a preliminary
4449 * test of the expected idle time is performed without the
4450 * scheduler suspended. The result here is not necessarily
4452 xExpectedIdleTime = prvGetExpectedIdleTime();
4454 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4458 /* Now the scheduler is suspended, the expected idle
4459 * time can be sampled again, and this time its value can
4461 configASSERT( xNextTaskUnblockTime >= xTickCount );
4462 xExpectedIdleTime = prvGetExpectedIdleTime();
4464 /* Define the following macro to set xExpectedIdleTime to 0
4465 * if the application does not want
4466 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
4467 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
4469 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4471 traceLOW_POWER_IDLE_BEGIN();
4472 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
4473 traceLOW_POWER_IDLE_END();
4477 mtCOVERAGE_TEST_MARKER();
4480 ( void ) xTaskResumeAll();
4484 mtCOVERAGE_TEST_MARKER();
4487 #endif /* configUSE_TICKLESS_IDLE */
4489 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
4491 extern void vApplicationMinimalIdleHook( void );
4493 /* Call the user defined function from within the idle task. This
4494 * allows the application designer to add background functionality
4495 * without the overhead of a separate task.
4497 * This hook is intended to manage core activity such as disabling cores that go idle.
4499 * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
4500 * CALL A FUNCTION THAT MIGHT BLOCK. */
4501 vApplicationMinimalIdleHook();
4503 #endif /* configUSE_MINIMAL_IDLE_HOOK */
4506 /*-----------------------------------------------------------*/
4508 #if ( configUSE_TICKLESS_IDLE != 0 )
4510 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
4512 /* The idle task exists in addition to the application tasks. */
4513 const UBaseType_t uxNonApplicationTasks = 1;
4514 eSleepModeStatus eReturn = eStandardSleep;
4516 /* This function must be called from a critical section. */
4518 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
4520 /* A task was made ready while the scheduler was suspended. */
4521 eReturn = eAbortSleep;
4523 else if( xYieldPending != pdFALSE )
4525 /* A yield was pended while the scheduler was suspended. */
4526 eReturn = eAbortSleep;
4528 else if( xPendedTicks != 0 )
4530 /* A tick interrupt has already occurred but was held pending
4531 * because the scheduler is suspended. */
4532 eReturn = eAbortSleep;
4536 /* If all the tasks are in the suspended list (which might mean they
4537 * have an infinite block time rather than actually being suspended)
4538 * then it is safe to turn all clocks off and just wait for external
4540 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
4542 eReturn = eNoTasksWaitingTimeout;
4546 mtCOVERAGE_TEST_MARKER();
4553 #endif /* configUSE_TICKLESS_IDLE */
4554 /*-----------------------------------------------------------*/
4556 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4558 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
4564 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4566 pxTCB = prvGetTCBFromHandle( xTaskToSet );
4567 configASSERT( pxTCB != NULL );
4568 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
4572 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4573 /*-----------------------------------------------------------*/
4575 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4577 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
4580 void * pvReturn = NULL;
4583 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4585 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4586 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
4596 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4597 /*-----------------------------------------------------------*/
4599 #if ( portUSING_MPU_WRAPPERS == 1 )
4601 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
4602 const MemoryRegion_t * const xRegions )
4606 /* If null is passed in here then we are modifying the MPU settings of
4607 * the calling task. */
4608 pxTCB = prvGetTCBFromHandle( xTaskToModify );
4610 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
4613 #endif /* portUSING_MPU_WRAPPERS */
4614 /*-----------------------------------------------------------*/
4616 static void prvInitialiseTaskLists( void )
4618 UBaseType_t uxPriority;
4620 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
4622 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
4625 vListInitialise( &xDelayedTaskList1 );
4626 vListInitialise( &xDelayedTaskList2 );
4627 vListInitialise( &xPendingReadyList );
4629 #if ( INCLUDE_vTaskDelete == 1 )
4631 vListInitialise( &xTasksWaitingTermination );
4633 #endif /* INCLUDE_vTaskDelete */
4635 #if ( INCLUDE_vTaskSuspend == 1 )
4637 vListInitialise( &xSuspendedTaskList );
4639 #endif /* INCLUDE_vTaskSuspend */
4641 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
4643 pxDelayedTaskList = &xDelayedTaskList1;
4644 pxOverflowDelayedTaskList = &xDelayedTaskList2;
4646 /*-----------------------------------------------------------*/
4648 static void prvCheckTasksWaitingTermination( void )
4650 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
4652 #if ( INCLUDE_vTaskDelete == 1 )
4656 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
4657 * being called too often in the idle task. */
4658 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4660 taskENTER_CRITICAL();
4662 /* Since we are SMP, multiple idles can be running simultaneously
4663 * and we need to check that other idles did not cleanup while we were
4664 * waiting to enter the critical section */
4665 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4667 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. */
4669 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
4671 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4672 --uxCurrentNumberOfTasks;
4673 --uxDeletedTasksWaitingCleanUp;
4674 prvDeleteTCB( pxTCB );
4678 /* The TCB to be deleted still has not yet been switched out
4679 * by the scheduler, so we will just exit this loop early and
4680 * try again next time. */
4681 taskEXIT_CRITICAL();
4686 taskEXIT_CRITICAL();
4689 #endif /* INCLUDE_vTaskDelete */
4691 /*-----------------------------------------------------------*/
4693 #if ( configUSE_TRACE_FACILITY == 1 )
4695 void vTaskGetInfo( TaskHandle_t xTask,
4696 TaskStatus_t * pxTaskStatus,
4697 BaseType_t xGetFreeStackSpace,
4702 /* xTask is NULL then get the state of the calling task. */
4703 pxTCB = prvGetTCBFromHandle( xTask );
4705 pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
4706 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
4707 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
4708 pxTaskStatus->pxStackBase = pxTCB->pxStack;
4709 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
4711 #if ( configUSE_MUTEXES == 1 )
4713 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
4717 pxTaskStatus->uxBasePriority = 0;
4721 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4723 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
4727 pxTaskStatus->ulRunTimeCounter = 0;
4731 /* Obtaining the task state is a little fiddly, so is only done if the
4732 * value of eState passed into this function is eInvalid - otherwise the
4733 * state is just set to whatever is passed in. */
4734 if( eState != eInvalid )
4736 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
4738 pxTaskStatus->eCurrentState = eRunning;
4742 pxTaskStatus->eCurrentState = eState;
4744 #if ( INCLUDE_vTaskSuspend == 1 )
4746 /* If the task is in the suspended list then there is a
4747 * chance it is actually just blocked indefinitely - so really
4748 * it should be reported as being in the Blocked state. */
4749 if( eState == eSuspended )
4753 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4755 pxTaskStatus->eCurrentState = eBlocked;
4758 ( void ) xTaskResumeAll();
4761 #endif /* INCLUDE_vTaskSuspend */
4766 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
4769 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
4770 * parameter is provided to allow it to be skipped. */
4771 if( xGetFreeStackSpace != pdFALSE )
4773 #if ( portSTACK_GROWTH > 0 )
4775 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
4779 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
4785 pxTaskStatus->usStackHighWaterMark = 0;
4789 #endif /* configUSE_TRACE_FACILITY */
4790 /*-----------------------------------------------------------*/
4792 #if ( configUSE_TRACE_FACILITY == 1 )
4794 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
4798 configLIST_VOLATILE TCB_t * pxNextTCB, * pxFirstTCB;
4799 UBaseType_t uxTask = 0;
4801 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4803 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. */
4805 /* Populate an TaskStatus_t structure within the
4806 * pxTaskStatusArray array for each task that is referenced from
4807 * pxList. See the definition of TaskStatus_t in task.h for the
4808 * meaning of each TaskStatus_t structure member. */
4811 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. */
4812 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
4814 } while( pxNextTCB != pxFirstTCB );
4818 mtCOVERAGE_TEST_MARKER();
4824 #endif /* configUSE_TRACE_FACILITY */
4825 /*-----------------------------------------------------------*/
4827 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
4829 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
4831 uint32_t ulCount = 0U;
4833 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
4835 pucStackByte -= portSTACK_GROWTH;
4839 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
4841 return ( configSTACK_DEPTH_TYPE ) ulCount;
4844 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
4845 /*-----------------------------------------------------------*/
4847 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
4849 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
4850 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
4851 * user to determine the return type. It gets around the problem of the value
4852 * overflowing on 8-bit types without breaking backward compatibility for
4853 * applications that expect an 8-bit return type. */
4854 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
4857 uint8_t * pucEndOfStack;
4858 configSTACK_DEPTH_TYPE uxReturn;
4860 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
4861 * the same except for their return type. Using configSTACK_DEPTH_TYPE
4862 * allows the user to determine the return type. It gets around the
4863 * problem of the value overflowing on 8-bit types without breaking
4864 * backward compatibility for applications that expect an 8-bit return
4867 pxTCB = prvGetTCBFromHandle( xTask );
4869 #if portSTACK_GROWTH < 0
4871 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4875 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4879 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
4884 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
4885 /*-----------------------------------------------------------*/
4887 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
4889 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
4892 uint8_t * pucEndOfStack;
4893 UBaseType_t uxReturn;
4895 pxTCB = prvGetTCBFromHandle( xTask );
4897 #if portSTACK_GROWTH < 0
4899 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4903 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4907 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
4912 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
4913 /*-----------------------------------------------------------*/
4915 #if ( INCLUDE_vTaskDelete == 1 )
4917 static void prvDeleteTCB( TCB_t * pxTCB )
4919 /* This call is required specifically for the TriCore port. It must be
4920 * above the vPortFree() calls. The call is also used by ports/demos that
4921 * want to allocate and clean RAM statically. */
4922 portCLEAN_UP_TCB( pxTCB );
4924 /* Free up the memory allocated by the scheduler for the task. It is up
4925 * to the task to free any memory allocated at the application level.
4926 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
4927 * for additional information. */
4928 #if ( configUSE_NEWLIB_REENTRANT == 1 )
4930 _reclaim_reent( &( pxTCB->xNewLib_reent ) );
4932 #endif /* configUSE_NEWLIB_REENTRANT */
4934 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
4936 /* The task can only have been allocated dynamically - free both
4937 * the stack and TCB. */
4938 vPortFreeStack( pxTCB->pxStack );
4941 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
4943 /* The task could have been allocated statically or dynamically, so
4944 * check what was statically allocated before trying to free the
4946 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
4948 /* Both the stack and TCB were allocated dynamically, so both
4950 vPortFreeStack( pxTCB->pxStack );
4953 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4955 /* Only the stack was statically allocated, so the TCB is the
4956 * only memory that must be freed. */
4961 /* Neither the stack nor the TCB were allocated dynamically, so
4962 * nothing needs to be freed. */
4963 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
4964 mtCOVERAGE_TEST_MARKER();
4967 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
4970 #endif /* INCLUDE_vTaskDelete */
4971 /*-----------------------------------------------------------*/
4973 static void prvResetNextTaskUnblockTime( void )
4975 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4977 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
4978 * the maximum possible value so it is extremely unlikely that the
4979 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
4980 * there is an item in the delayed list. */
4981 xNextTaskUnblockTime = portMAX_DELAY;
4985 /* The new current delayed list is not empty, get the value of
4986 * the item at the head of the delayed list. This is the time at
4987 * which the task at the head of the delayed list should be removed
4988 * from the Blocked state. */
4989 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
4992 /*-----------------------------------------------------------*/
4994 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
4996 TaskHandle_t xTaskGetCurrentTaskHandle( void )
4998 TaskHandle_t xReturn;
5001 ulState = portDISABLE_INTERRUPTS();
5002 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
5003 portRESTORE_INTERRUPTS( ulState );
5008 TaskHandle_t xTaskGetCurrentTaskHandleCPU( UBaseType_t xCoreID )
5010 TaskHandle_t xReturn = NULL;
5012 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
5014 xReturn = pxCurrentTCBs[ xCoreID ];
5020 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
5021 /*-----------------------------------------------------------*/
5023 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
5025 BaseType_t xTaskGetSchedulerState( void )
5029 if( xSchedulerRunning == pdFALSE )
5031 xReturn = taskSCHEDULER_NOT_STARTED;
5035 taskENTER_CRITICAL();
5037 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
5039 xReturn = taskSCHEDULER_RUNNING;
5043 xReturn = taskSCHEDULER_SUSPENDED;
5046 taskEXIT_CRITICAL();
5052 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
5053 /*-----------------------------------------------------------*/
5055 #if ( configUSE_MUTEXES == 1 )
5057 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
5059 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
5060 BaseType_t xReturn = pdFALSE;
5062 /* If the mutex was given back by an interrupt while the queue was
5063 * locked then the mutex holder might now be NULL. _RB_ Is this still
5064 * needed as interrupts can no longer use mutexes? */
5065 if( pxMutexHolder != NULL )
5067 /* If the holder of the mutex has a priority below the priority of
5068 * the task attempting to obtain the mutex then it will temporarily
5069 * inherit the priority of the task attempting to obtain the mutex. */
5070 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
5072 /* Adjust the mutex holder state to account for its new
5073 * priority. Only reset the event list item value if the value is
5074 * not being used for anything else. */
5075 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
5077 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. */
5081 mtCOVERAGE_TEST_MARKER();
5084 /* If the task being modified is in the ready state it will need
5085 * to be moved into a new list. */
5086 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
5088 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5090 /* It is known that the task is in its ready list so
5091 * there is no need to check again and the port level
5092 * reset macro can be called directly. */
5093 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
5097 mtCOVERAGE_TEST_MARKER();
5100 /* Inherit the priority before being moved into the new list. */
5101 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
5102 prvAddTaskToReadyList( pxMutexHolderTCB );
5106 /* Just inherit the priority. */
5107 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
5110 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
5112 /* Inheritance occurred. */
5117 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
5119 /* The base priority of the mutex holder is lower than the
5120 * priority of the task attempting to take the mutex, but the
5121 * current priority of the mutex holder is not lower than the
5122 * priority of the task attempting to take the mutex.
5123 * Therefore the mutex holder must have already inherited a
5124 * priority, but inheritance would have occurred if that had
5125 * not been the case. */
5130 mtCOVERAGE_TEST_MARKER();
5136 mtCOVERAGE_TEST_MARKER();
5142 #endif /* configUSE_MUTEXES */
5143 /*-----------------------------------------------------------*/
5145 #if ( configUSE_MUTEXES == 1 )
5147 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
5149 TCB_t * const pxTCB = pxMutexHolder;
5150 BaseType_t xReturn = pdFALSE;
5152 if( pxMutexHolder != NULL )
5154 /* A task can only have an inherited priority if it holds the mutex.
5155 * If the mutex is held by a task then it cannot be given from an
5156 * interrupt, and if a mutex is given by the holding task then it must
5157 * be the running state task. */
5158 configASSERT( pxTCB == pxCurrentTCB );
5159 configASSERT( pxTCB->uxMutexesHeld );
5160 ( pxTCB->uxMutexesHeld )--;
5162 /* Has the holder of the mutex inherited the priority of another
5164 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
5166 /* Only disinherit if no other mutexes are held. */
5167 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
5169 /* A task can only have an inherited priority if it holds
5170 * the mutex. If the mutex is held by a task then it cannot be
5171 * given from an interrupt, and if a mutex is given by the
5172 * holding task then it must be the running state task. Remove
5173 * the holding task from the ready list. */
5174 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5176 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5180 mtCOVERAGE_TEST_MARKER();
5183 /* Disinherit the priority before adding the task into the
5184 * new ready list. */
5185 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
5186 pxTCB->uxPriority = pxTCB->uxBasePriority;
5188 /* Reset the event list item value. It cannot be in use for
5189 * any other purpose if this task is running, and it must be
5190 * running to give back the mutex. */
5191 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. */
5192 prvAddTaskToReadyList( pxTCB );
5194 /* Return true to indicate that a context switch is required.
5195 * This is only actually required in the corner case whereby
5196 * multiple mutexes were held and the mutexes were given back
5197 * in an order different to that in which they were taken.
5198 * If a context switch did not occur when the first mutex was
5199 * returned, even if a task was waiting on it, then a context
5200 * switch should occur when the last mutex is returned whether
5201 * a task is waiting on it or not. */
5206 mtCOVERAGE_TEST_MARKER();
5211 mtCOVERAGE_TEST_MARKER();
5216 mtCOVERAGE_TEST_MARKER();
5222 #endif /* configUSE_MUTEXES */
5223 /*-----------------------------------------------------------*/
5225 #if ( configUSE_MUTEXES == 1 )
5227 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
5228 UBaseType_t uxHighestPriorityWaitingTask )
5230 TCB_t * const pxTCB = pxMutexHolder;
5231 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
5232 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
5234 if( pxMutexHolder != NULL )
5236 /* If pxMutexHolder is not NULL then the holder must hold at least
5238 configASSERT( pxTCB->uxMutexesHeld );
5240 /* Determine the priority to which the priority of the task that
5241 * holds the mutex should be set. This will be the greater of the
5242 * holding task's base priority and the priority of the highest
5243 * priority task that is waiting to obtain the mutex. */
5244 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
5246 uxPriorityToUse = uxHighestPriorityWaitingTask;
5250 uxPriorityToUse = pxTCB->uxBasePriority;
5253 /* Does the priority need to change? */
5254 if( pxTCB->uxPriority != uxPriorityToUse )
5256 /* Only disinherit if no other mutexes are held. This is a
5257 * simplification in the priority inheritance implementation. If
5258 * the task that holds the mutex is also holding other mutexes then
5259 * the other mutexes may have caused the priority inheritance. */
5260 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
5262 /* If a task has timed out because it already holds the
5263 * mutex it was trying to obtain then it cannot of inherited
5264 * its own priority. */
5265 configASSERT( pxTCB != pxCurrentTCB );
5267 /* Disinherit the priority, remembering the previous
5268 * priority to facilitate determining the subject task's
5270 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
5271 uxPriorityUsedOnEntry = pxTCB->uxPriority;
5272 pxTCB->uxPriority = uxPriorityToUse;
5274 /* Only reset the event list item value if the value is not
5275 * being used for anything else. */
5276 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
5278 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. */
5282 mtCOVERAGE_TEST_MARKER();
5285 /* If the running task is not the task that holds the mutex
5286 * then the task that holds the mutex could be in either the
5287 * Ready, Blocked or Suspended states. Only remove the task
5288 * from its current state list if it is in the Ready state as
5289 * the task's priority is going to change and there is one
5290 * Ready list per priority. */
5291 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
5293 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5295 /* It is known that the task is in its ready list so
5296 * there is no need to check again and the port level
5297 * reset macro can be called directly. */
5298 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5302 mtCOVERAGE_TEST_MARKER();
5305 prvAddTaskToReadyList( pxTCB );
5309 mtCOVERAGE_TEST_MARKER();
5314 mtCOVERAGE_TEST_MARKER();
5319 mtCOVERAGE_TEST_MARKER();
5324 mtCOVERAGE_TEST_MARKER();
5328 #endif /* configUSE_MUTEXES */
5329 /*-----------------------------------------------------------*/
5332 * If not in a critical section then yield immediately.
5333 * Otherwise set xYieldPending to true to wait to
5334 * yield until exiting the critical section.
5336 void vTaskYieldWithinAPI( void )
5338 if( pxCurrentTCB->uxCriticalNesting == 0U )
5344 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5347 /*-----------------------------------------------------------*/
5349 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5351 void vTaskEnterCritical( void )
5353 portDISABLE_INTERRUPTS();
5355 if( xSchedulerRunning != pdFALSE )
5357 if( pxCurrentTCB->uxCriticalNesting == 0U )
5359 if( portCHECK_IF_IN_ISR() == pdFALSE )
5361 portGET_TASK_LOCK();
5367 ( pxCurrentTCB->uxCriticalNesting )++;
5369 /* This should now be interrupt safe. The only time there would be
5370 * a problem is if this is called before a context switch and
5371 * vTaskExitCritical() is called after pxCurrentTCB changes. Therefore
5372 * this should not be used within vTaskSwitchContext(). */
5374 if( ( uxSchedulerSuspended == 0U ) && ( pxCurrentTCB->uxCriticalNesting == 1U ) )
5376 prvCheckForRunStateChange();
5381 mtCOVERAGE_TEST_MARKER();
5385 #endif /* portCRITICAL_NESTING_IN_TCB */
5386 /*-----------------------------------------------------------*/
5388 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5390 void vTaskExitCritical( void )
5392 if( xSchedulerRunning != pdFALSE )
5394 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
5395 * does not match a previous call to vTaskEnterCritical(). */
5396 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
5398 if( pxCurrentTCB->uxCriticalNesting > 0U )
5400 ( pxCurrentTCB->uxCriticalNesting )--;
5402 if( pxCurrentTCB->uxCriticalNesting == 0U )
5404 portRELEASE_ISR_LOCK();
5406 if( portCHECK_IF_IN_ISR() == pdFALSE )
5408 portRELEASE_TASK_LOCK();
5409 portENABLE_INTERRUPTS();
5411 /* When a task yields in a critical section it just sets
5412 * xYieldPending to true. So now that we have exited the
5413 * critical section check if xYieldPending is true, and
5415 if( xYieldPending != pdFALSE )
5422 /* In an ISR we don't hold the task lock and don't
5423 * need to yield. Yield will happen if necessary when
5424 * the application ISR calls portEND_SWITCHING_ISR() */
5425 mtCOVERAGE_TEST_MARKER();
5430 mtCOVERAGE_TEST_MARKER();
5435 mtCOVERAGE_TEST_MARKER();
5440 mtCOVERAGE_TEST_MARKER();
5444 #endif /* portCRITICAL_NESTING_IN_TCB */
5445 /*-----------------------------------------------------------*/
5447 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
5449 static char * prvWriteNameToBuffer( char * pcBuffer,
5450 const char * pcTaskName )
5454 /* Start by copying the entire string. */
5455 strcpy( pcBuffer, pcTaskName );
5457 /* Pad the end of the string with spaces to ensure columns line up when
5459 for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
5461 pcBuffer[ x ] = ' ';
5465 pcBuffer[ x ] = ( char ) 0x00;
5467 /* Return the new end of string. */
5468 return &( pcBuffer[ x ] );
5471 #endif /* ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
5472 /*-----------------------------------------------------------*/
5474 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5476 void vTaskList( char * pcWriteBuffer )
5478 TaskStatus_t * pxTaskStatusArray;
5479 UBaseType_t uxArraySize, x;
5485 * This function is provided for convenience only, and is used by many
5486 * of the demo applications. Do not consider it to be part of the
5489 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
5490 * uxTaskGetSystemState() output into a human readable table that
5491 * displays task: names, states, priority, stack usage and task number.
5492 * Stack usage specified as the number of unused StackType_t words stack can hold
5493 * on top of stack - not the number of bytes.
5495 * vTaskList() has a dependency on the sprintf() C library function that
5496 * might bloat the code size, use a lot of stack, and provide different
5497 * results on different platforms. An alternative, tiny, third party,
5498 * and limited functionality implementation of sprintf() is provided in
5499 * many of the FreeRTOS/Demo sub-directories in a file called
5500 * printf-stdarg.c (note printf-stdarg.c does not provide a full
5501 * snprintf() implementation!).
5503 * It is recommended that production systems call uxTaskGetSystemState()
5504 * directly to get access to raw stats data, rather than indirectly
5505 * through a call to vTaskList().
5509 /* Make sure the write buffer does not contain a string. */
5510 *pcWriteBuffer = ( char ) 0x00;
5512 /* Take a snapshot of the number of tasks in case it changes while this
5513 * function is executing. */
5514 uxArraySize = uxCurrentNumberOfTasks;
5516 /* Allocate an array index for each task. NOTE! if
5517 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5518 * equate to NULL. */
5519 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. */
5521 if( pxTaskStatusArray != NULL )
5523 /* Generate the (binary) data. */
5524 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
5526 /* Create a human readable table from the binary data. */
5527 for( x = 0; x < uxArraySize; x++ )
5529 switch( pxTaskStatusArray[ x ].eCurrentState )
5532 cStatus = tskRUNNING_CHAR;
5536 cStatus = tskREADY_CHAR;
5540 cStatus = tskBLOCKED_CHAR;
5544 cStatus = tskSUSPENDED_CHAR;
5548 cStatus = tskDELETED_CHAR;
5551 case eInvalid: /* Fall through. */
5552 default: /* Should not get here, but it is included
5553 * to prevent static checking errors. */
5554 cStatus = ( char ) 0x00;
5558 /* Write the task name to the string, padding with spaces so it
5559 * can be printed in tabular form more easily. */
5560 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5562 /* Write the rest of the string. */
5563 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. */
5564 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. */
5567 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
5568 * is 0 then vPortFree() will be #defined to nothing. */
5569 vPortFree( pxTaskStatusArray );
5573 mtCOVERAGE_TEST_MARKER();
5577 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
5578 /*----------------------------------------------------------*/
5580 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5582 void vTaskGetRunTimeStats( char * pcWriteBuffer )
5584 TaskStatus_t * pxTaskStatusArray;
5585 UBaseType_t uxArraySize, x;
5586 uint32_t ulTotalTime, ulStatsAsPercentage;
5588 #if ( configUSE_TRACE_FACILITY != 1 )
5590 #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats().
5597 * This function is provided for convenience only, and is used by many
5598 * of the demo applications. Do not consider it to be part of the
5601 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
5602 * of the uxTaskGetSystemState() output into a human readable table that
5603 * displays the amount of time each task has spent in the Running state
5604 * in both absolute and percentage terms.
5606 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
5607 * function that might bloat the code size, use a lot of stack, and
5608 * provide different results on different platforms. An alternative,
5609 * tiny, third party, and limited functionality implementation of
5610 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
5611 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
5612 * a full snprintf() implementation!).
5614 * It is recommended that production systems call uxTaskGetSystemState()
5615 * directly to get access to raw stats data, rather than indirectly
5616 * through a call to vTaskGetRunTimeStats().
5619 /* Make sure the write buffer does not contain a string. */
5620 *pcWriteBuffer = ( char ) 0x00;
5622 /* Take a snapshot of the number of tasks in case it changes while this
5623 * function is executing. */
5624 uxArraySize = uxCurrentNumberOfTasks;
5626 /* Allocate an array index for each task. NOTE! If
5627 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5628 * equate to NULL. */
5629 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. */
5631 if( pxTaskStatusArray != NULL )
5633 /* Generate the (binary) data. */
5634 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
5636 /* For percentage calculations. */
5637 ulTotalTime /= 100UL;
5639 /* Avoid divide by zero errors. */
5640 if( ulTotalTime > 0UL )
5642 /* Create a human readable table from the binary data. */
5643 for( x = 0; x < uxArraySize; x++ )
5645 /* What percentage of the total run time has the task used?
5646 * This will always be rounded down to the nearest integer.
5647 * ulTotalRunTimeDiv100 has already been divided by 100. */
5648 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
5650 /* Write the task name to the string, padding with
5651 * spaces so it can be printed in tabular form more
5653 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5655 if( ulStatsAsPercentage > 0UL )
5657 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5659 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
5663 /* sizeof( int ) == sizeof( long ) so a smaller
5664 * printf() library can be used. */
5665 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. */
5671 /* If the percentage is zero here then the task has
5672 * consumed less than 1% of the total run time. */
5673 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5675 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
5679 /* sizeof( int ) == sizeof( long ) so a smaller
5680 * printf() library can be used. */
5681 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. */
5686 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. */
5691 mtCOVERAGE_TEST_MARKER();
5694 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
5695 * is 0 then vPortFree() will be #defined to nothing. */
5696 vPortFree( pxTaskStatusArray );
5700 mtCOVERAGE_TEST_MARKER();
5704 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
5705 /*-----------------------------------------------------------*/
5707 TickType_t uxTaskResetEventItemValue( void )
5709 TickType_t uxReturn;
5711 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
5713 /* Reset the event list item to its normal value - so it can be used with
5714 * queues and semaphores. */
5715 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. */
5719 /*-----------------------------------------------------------*/
5721 #if ( configUSE_MUTEXES == 1 )
5723 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
5725 /* If xSemaphoreCreateMutex() is called before any tasks have been created
5726 * then pxCurrentTCB will be NULL. */
5727 if( pxCurrentTCB != NULL )
5729 ( pxCurrentTCB->uxMutexesHeld )++;
5732 return pxCurrentTCB;
5735 #endif /* configUSE_MUTEXES */
5736 /*-----------------------------------------------------------*/
5738 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5740 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
5741 BaseType_t xClearCountOnExit,
5742 TickType_t xTicksToWait )
5746 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5748 taskENTER_CRITICAL();
5750 /* Only block if the notification count is not already non-zero. */
5751 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
5753 /* Mark this task as waiting for a notification. */
5754 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5756 if( xTicksToWait > ( TickType_t ) 0 )
5758 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5759 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
5761 /* All ports are written to allow a yield in a critical
5762 * section (some will yield immediately, others wait until the
5763 * critical section exits) - but it is not something that
5764 * application code should ever do. */
5765 vTaskYieldWithinAPI();
5769 mtCOVERAGE_TEST_MARKER();
5774 mtCOVERAGE_TEST_MARKER();
5777 taskEXIT_CRITICAL();
5779 taskENTER_CRITICAL();
5781 traceTASK_NOTIFY_TAKE( uxIndexToWait );
5782 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5784 if( ulReturn != 0UL )
5786 if( xClearCountOnExit != pdFALSE )
5788 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
5792 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
5797 mtCOVERAGE_TEST_MARKER();
5800 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5802 taskEXIT_CRITICAL();
5807 #endif /* configUSE_TASK_NOTIFICATIONS */
5808 /*-----------------------------------------------------------*/
5810 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5812 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
5813 uint32_t ulBitsToClearOnEntry,
5814 uint32_t ulBitsToClearOnExit,
5815 uint32_t * pulNotificationValue,
5816 TickType_t xTicksToWait )
5820 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5822 taskENTER_CRITICAL();
5824 /* Only block if a notification is not already pending. */
5825 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5827 /* Clear bits in the task's notification value as bits may get
5828 * set by the notifying task or interrupt. This can be used to
5829 * clear the value to zero. */
5830 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
5832 /* Mark this task as waiting for a notification. */
5833 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5835 if( xTicksToWait > ( TickType_t ) 0 )
5837 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5838 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
5840 /* All ports are written to allow a yield in a critical
5841 * section (some will yield immediately, others wait until the
5842 * critical section exits) - but it is not something that
5843 * application code should ever do. */
5844 vTaskYieldWithinAPI();
5848 mtCOVERAGE_TEST_MARKER();
5853 mtCOVERAGE_TEST_MARKER();
5856 taskEXIT_CRITICAL();
5858 taskENTER_CRITICAL();
5860 traceTASK_NOTIFY_WAIT( uxIndexToWait );
5862 if( pulNotificationValue != NULL )
5864 /* Output the current notification value, which may or may not
5866 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5869 /* If ucNotifyValue is set then either the task never entered the
5870 * blocked state (because a notification was already pending) or the
5871 * task unblocked because of a notification. Otherwise the task
5872 * unblocked because of a timeout. */
5873 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5875 /* A notification was not received. */
5880 /* A notification was already pending or a notification was
5881 * received while the task was waiting. */
5882 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
5886 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5888 taskEXIT_CRITICAL();
5893 #endif /* configUSE_TASK_NOTIFICATIONS */
5894 /*-----------------------------------------------------------*/
5896 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5898 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
5899 UBaseType_t uxIndexToNotify,
5901 eNotifyAction eAction,
5902 uint32_t * pulPreviousNotificationValue )
5905 BaseType_t xReturn = pdPASS;
5906 uint8_t ucOriginalNotifyState;
5908 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5909 configASSERT( xTaskToNotify );
5910 pxTCB = xTaskToNotify;
5912 taskENTER_CRITICAL();
5914 if( pulPreviousNotificationValue != NULL )
5916 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5919 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5921 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5926 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5930 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5933 case eSetValueWithOverwrite:
5934 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5937 case eSetValueWithoutOverwrite:
5939 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5941 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5945 /* The value could not be written to the task. */
5953 /* The task is being notified without its notify value being
5959 /* Should not get here if all enums are handled.
5960 * Artificially force an assert by testing a value the
5961 * compiler can't assume is const. */
5962 configASSERT( xTickCount == ( TickType_t ) 0 );
5967 traceTASK_NOTIFY( uxIndexToNotify );
5969 /* If the task is in the blocked state specifically to wait for a
5970 * notification then unblock it now. */
5971 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5973 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5974 prvAddTaskToReadyList( pxTCB );
5976 /* The task should not have been on an event list. */
5977 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5979 #if ( configUSE_TICKLESS_IDLE != 0 )
5981 /* If a task is blocked waiting for a notification then
5982 * xNextTaskUnblockTime might be set to the blocked task's time
5983 * out time. If the task is unblocked for a reason other than
5984 * a timeout xNextTaskUnblockTime is normally left unchanged,
5985 * because it will automatically get reset to a new value when
5986 * the tick count equals xNextTaskUnblockTime. However if
5987 * tickless idling is used it might be more important to enter
5988 * sleep mode at the earliest possible time - so reset
5989 * xNextTaskUnblockTime here to ensure it is updated at the
5990 * earliest possible time. */
5991 prvResetNextTaskUnblockTime();
5995 #if ( configUSE_PREEMPTION == 1 )
5997 prvYieldForTask( pxTCB, pdFALSE );
6003 mtCOVERAGE_TEST_MARKER();
6006 taskEXIT_CRITICAL();
6011 #endif /* configUSE_TASK_NOTIFICATIONS */
6012 /*-----------------------------------------------------------*/
6014 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6016 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
6017 UBaseType_t uxIndexToNotify,
6019 eNotifyAction eAction,
6020 uint32_t * pulPreviousNotificationValue,
6021 BaseType_t * pxHigherPriorityTaskWoken )
6024 uint8_t ucOriginalNotifyState;
6025 BaseType_t xReturn = pdPASS;
6026 UBaseType_t uxSavedInterruptStatus;
6028 configASSERT( xTaskToNotify );
6029 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6031 /* RTOS ports that support interrupt nesting have the concept of a
6032 * maximum system call (or maximum API call) interrupt priority.
6033 * Interrupts that are above the maximum system call priority are keep
6034 * permanently enabled, even when the RTOS kernel is in a critical section,
6035 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
6036 * is defined in FreeRTOSConfig.h then
6037 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
6038 * failure if a FreeRTOS API function is called from an interrupt that has
6039 * been assigned a priority above the configured maximum system call
6040 * priority. Only FreeRTOS functions that end in FromISR can be called
6041 * from interrupts that have been assigned a priority at or (logically)
6042 * below the maximum system call interrupt priority. FreeRTOS maintains a
6043 * separate interrupt safe API to ensure interrupt entry is as fast and as
6044 * simple as possible. More information (albeit Cortex-M specific) is
6045 * provided on the following link:
6046 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
6047 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
6049 pxTCB = xTaskToNotify;
6051 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6053 if( pulPreviousNotificationValue != NULL )
6055 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
6058 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
6059 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
6064 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
6068 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
6071 case eSetValueWithOverwrite:
6072 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
6075 case eSetValueWithoutOverwrite:
6077 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
6079 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
6083 /* The value could not be written to the task. */
6091 /* The task is being notified without its notify value being
6097 /* Should not get here if all enums are handled.
6098 * Artificially force an assert by testing a value the
6099 * compiler can't assume is const. */
6100 configASSERT( xTickCount == ( TickType_t ) 0 );
6104 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
6106 /* If the task is in the blocked state specifically to wait for a
6107 * notification then unblock it now. */
6108 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
6110 /* The task should not have been on an event list. */
6111 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
6113 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
6115 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6116 prvAddTaskToReadyList( pxTCB );
6120 /* The delayed and ready lists cannot be accessed, so hold
6121 * this task pending until the scheduler is resumed. */
6122 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
6125 #if ( configUSE_PREEMPTION == 1 )
6126 prvYieldForTask( pxTCB, pdFALSE );
6128 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6130 if( pxHigherPriorityTaskWoken != NULL )
6132 *pxHigherPriorityTaskWoken = pdTRUE;
6138 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6143 #endif /* configUSE_TASK_NOTIFICATIONS */
6144 /*-----------------------------------------------------------*/
6146 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6148 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
6149 UBaseType_t uxIndexToNotify,
6150 BaseType_t * pxHigherPriorityTaskWoken )
6153 uint8_t ucOriginalNotifyState;
6154 UBaseType_t uxSavedInterruptStatus;
6156 configASSERT( xTaskToNotify );
6157 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6159 /* RTOS ports that support interrupt nesting have the concept of a
6160 * maximum system call (or maximum API call) interrupt priority.
6161 * Interrupts that are above the maximum system call priority are keep
6162 * permanently enabled, even when the RTOS kernel is in a critical section,
6163 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
6164 * is defined in FreeRTOSConfig.h then
6165 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
6166 * failure if a FreeRTOS API function is called from an interrupt that has
6167 * been assigned a priority above the configured maximum system call
6168 * priority. Only FreeRTOS functions that end in FromISR can be called
6169 * from interrupts that have been assigned a priority at or (logically)
6170 * below the maximum system call interrupt priority. FreeRTOS maintains a
6171 * separate interrupt safe API to ensure interrupt entry is as fast and as
6172 * simple as possible. More information (albeit Cortex-M specific) is
6173 * provided on the following link:
6174 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
6175 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
6177 pxTCB = xTaskToNotify;
6179 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6181 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
6182 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
6184 /* 'Giving' is equivalent to incrementing a count in a counting
6186 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
6188 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
6190 /* If the task is in the blocked state specifically to wait for a
6191 * notification then unblock it now. */
6192 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
6194 /* The task should not have been on an event list. */
6195 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
6197 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
6199 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6200 prvAddTaskToReadyList( pxTCB );
6204 /* The delayed and ready lists cannot be accessed, so hold
6205 * this task pending until the scheduler is resumed. */
6206 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
6209 #if ( configUSE_PREEMPTION == 1 )
6210 prvYieldForTask( pxTCB, pdFALSE );
6212 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6214 if( pxHigherPriorityTaskWoken != NULL )
6216 *pxHigherPriorityTaskWoken = pdTRUE;
6222 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6225 #endif /* configUSE_TASK_NOTIFICATIONS */
6226 /*-----------------------------------------------------------*/
6228 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6230 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
6231 UBaseType_t uxIndexToClear )
6236 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6238 /* If null is passed in here then it is the calling task that is having
6239 * its notification state cleared. */
6240 pxTCB = prvGetTCBFromHandle( xTask );
6242 taskENTER_CRITICAL();
6244 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
6246 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
6254 taskEXIT_CRITICAL();
6259 #endif /* configUSE_TASK_NOTIFICATIONS */
6260 /*-----------------------------------------------------------*/
6262 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6264 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
6265 UBaseType_t uxIndexToClear,
6266 uint32_t ulBitsToClear )
6271 /* If null is passed in here then it is the calling task that is having
6272 * its notification state cleared. */
6273 pxTCB = prvGetTCBFromHandle( xTask );
6275 taskENTER_CRITICAL();
6277 /* Return the notification as it was before the bits were cleared,
6278 * then clear the bit mask. */
6279 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
6280 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
6282 taskEXIT_CRITICAL();
6287 #endif /* configUSE_TASK_NOTIFICATIONS */
6288 /*-----------------------------------------------------------*/
6290 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
6292 uint32_t ulTaskGetIdleRunTimeCounter( void )
6294 uint32_t ulReturn = 0;
6296 for( BaseType_t i = 0; i < configNUM_CORES; i++ )
6298 ulReturn += xIdleTaskHandle[ i ]->ulRunTimeCounter;
6304 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
6305 /*-----------------------------------------------------------*/
6307 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
6308 const BaseType_t xCanBlockIndefinitely )
6310 TickType_t xTimeToWake;
6311 const TickType_t xConstTickCount = xTickCount;
6313 #if ( INCLUDE_xTaskAbortDelay == 1 )
6315 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
6316 * reset to pdFALSE so it can be detected as having been set to pdTRUE
6317 * when the task leaves the Blocked state. */
6318 pxCurrentTCB->ucDelayAborted = pdFALSE;
6322 /* Remove the task from the ready list before adding it to the blocked list
6323 * as the same list item is used for both lists. */
6324 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6326 /* The current task must be in a ready list, so there is no need to
6327 * check, and the port reset macro can be called directly. */
6328 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. */
6332 mtCOVERAGE_TEST_MARKER();
6335 #if ( INCLUDE_vTaskSuspend == 1 )
6337 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
6339 /* Add the task to the suspended task list instead of a delayed task
6340 * list to ensure it is not woken by a timing event. It will block
6342 vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
6346 /* Calculate the time at which the task should be woken if the event
6347 * does not occur. This may overflow but this doesn't matter, the
6348 * kernel will manage it correctly. */
6349 xTimeToWake = xConstTickCount + xTicksToWait;
6351 /* The list item will be inserted in wake time order. */
6352 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6354 if( xTimeToWake < xConstTickCount )
6356 /* Wake time has overflowed. Place this item in the overflow
6358 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6362 /* The wake time has not overflowed, so the current block list
6364 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6366 /* If the task entering the blocked state was placed at the
6367 * head of the list of blocked tasks then xNextTaskUnblockTime
6368 * needs to be updated too. */
6369 if( xTimeToWake < xNextTaskUnblockTime )
6371 xNextTaskUnblockTime = xTimeToWake;
6375 mtCOVERAGE_TEST_MARKER();
6380 #else /* INCLUDE_vTaskSuspend */
6382 /* Calculate the time at which the task should be woken if the event
6383 * does not occur. This may overflow but this doesn't matter, the kernel
6384 * will manage it correctly. */
6385 xTimeToWake = xConstTickCount + xTicksToWait;
6387 /* The list item will be inserted in wake time order. */
6388 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6390 if( xTimeToWake < xConstTickCount )
6392 /* Wake time has overflowed. Place this item in the overflow list. */
6393 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6397 /* The wake time has not overflowed, so the current block list is used. */
6398 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6400 /* If the task entering the blocked state was placed at the head of the
6401 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
6403 if( xTimeToWake < xNextTaskUnblockTime )
6405 xNextTaskUnblockTime = xTimeToWake;
6409 mtCOVERAGE_TEST_MARKER();
6413 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
6414 ( void ) xCanBlockIndefinitely;
6416 #endif /* INCLUDE_vTaskSuspend */
6419 /* Code below here allows additional code to be inserted into this source file,
6420 * especially where access to file scope functions and data is needed (for example
6421 * when performing module tests). */
6423 #ifdef FREERTOS_MODULE_TEST
6424 #include "tasks_test_access_functions.h"
6428 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
6430 #include "freertos_tasks_c_additions.h"
6432 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
6433 static void freertos_tasks_c_additions_init( void )
6435 FREERTOS_TASKS_C_ADDITIONS_INIT();
6439 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */