2 * FreeRTOS Kernel V10.4.3
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 #define DEBUG_UNIT FREERTOS_TASKS
38 /* FreeRTOS includes. */
42 #include "stack_macros.h"
44 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
45 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
46 * for the header files above, but not in this file, in order to generate the
47 * correct privileged Vs unprivileged linkage and placement. */
48 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
50 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
51 * functions but without including stdio.h here. */
52 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
54 /* At the bottom of this file are two optional functions that can be used
55 * to generate human readable text from the raw data generated by the
56 * uxTaskGetSystemState() function. Note the formatting functions are provided
57 * for convenience only, and are NOT considered part of the kernel. */
59 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
61 #if ( configUSE_PREEMPTION == 0 )
63 /* If the cooperative scheduler is being used then a yield should not be
64 * performed just because a higher priority task has been woken. */
65 #define taskYIELD_IF_USING_PREEMPTION()
67 #define taskYIELD_IF_USING_PREEMPTION() vTaskYieldWithinAPI()
70 /* Values that can be assigned to the ucNotifyState member of the TCB. */
71 #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
72 #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
73 #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
76 * The value used to fill the stack of a task when the task is created. This
77 * is used purely for checking the high water mark for tasks.
79 #define tskSTACK_FILL_BYTE ( 0xa5U )
81 /* Bits used to record how a task's stack and TCB were allocated. */
82 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
83 #define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
84 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
86 /* If any of the following are set then task stacks are filled with a known
87 * value so the high water mark can be determined. If none of the following are
88 * set then don't fill the stack so there is no unnecessary dependency on memset. */
89 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
90 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
92 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
96 * Macros used by vListTask to indicate which state a task is in.
98 #define tskRUNNING_CHAR ( 'X' )
99 #define tskBLOCKED_CHAR ( 'B' )
100 #define tskREADY_CHAR ( 'R' )
101 #define tskDELETED_CHAR ( 'D' )
102 #define tskSUSPENDED_CHAR ( 'S' )
105 * Some kernel aware debuggers require the data the debugger needs access to to
106 * be global, rather than file scope.
108 #ifdef portREMOVE_STATIC_QUALIFIER
112 /* The name allocated to the Idle task. This can be overridden by defining
113 * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
114 #ifndef configIDLE_TASK_NAME
115 #define configIDLE_TASK_NAME "IDLE"
118 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
120 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
121 * performed in a generic way that is not optimised to any particular
122 * microcontroller architecture. */
124 /* uxTopReadyPriority holds the priority of the highest priority ready
126 #define taskRECORD_READY_PRIORITY( uxPriority ) \
128 if( ( uxPriority ) > uxTopReadyPriority ) \
130 uxTopReadyPriority = ( uxPriority ); \
132 } /* taskRECORD_READY_PRIORITY */
134 /*-----------------------------------------------------------*/
136 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
137 * they are only required when a port optimised method of task selection is
139 #define taskRESET_READY_PRIORITY( uxPriority )
140 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
142 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
144 #error configUSE_PORT_OPTIMISED_TASK_SELECTION not yet supported in SMP
146 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
147 * performed in a way that is tailored to the particular microcontroller
148 * architecture being used. */
150 /* A port optimised version is provided. Call the port defined macros. */
151 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( uxPriority, uxTopReadyPriority )
153 /*-----------------------------------------------------------*/
155 /* A port optimised version is provided, call it only if the TCB being reset
156 * is being referenced from a ready list. If it is referenced from a delayed
157 * or suspended list then it won't be in a ready list. */
158 #define taskRESET_READY_PRIORITY( uxPriority ) \
160 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
162 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
166 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
168 /*-----------------------------------------------------------*/
170 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
171 * count overflows. */
172 #define taskSWITCH_DELAYED_LISTS() \
176 /* The delayed tasks list should be empty when the lists are switched. */ \
177 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
179 pxTemp = pxDelayedTaskList; \
180 pxDelayedTaskList = pxOverflowDelayedTaskList; \
181 pxOverflowDelayedTaskList = pxTemp; \
183 prvResetNextTaskUnblockTime(); \
186 /*-----------------------------------------------------------*/
189 * Place the task represented by pxTCB into the appropriate ready list for
190 * the task. It is inserted at the end of the list.
192 #define prvAddTaskToReadyList( pxTCB ) \
193 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
194 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
195 vListInsertEnd( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
196 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB )
197 /*-----------------------------------------------------------*/
200 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
201 * where NULL is used to indicate that the handle of the currently executing
202 * task should be used in place of the parameter. This macro simply checks to
203 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
205 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
207 /* The item value of the event list item is normally used to hold the priority
208 * of the task to which it belongs (coded to allow it to be held in reverse
209 * priority order). However, it is occasionally borrowed for other purposes. It
210 * is important its value is not updated due to a task priority change while it is
211 * being used for another purpose. The following bit definition is used to inform
212 * the scheduler that the value should not be changed - in which case it is the
213 * responsibility of whichever module is using the value to ensure it gets set back
214 * to its original value when it is released. */
215 #if ( configUSE_16_BIT_TICKS == 1 )
216 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U
218 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
221 /* Indicates that the task is not actively running on any core. */
222 #define taskTASK_NOT_RUNNING ( TaskRunning_t ) ( -1 )
224 /* Indicates that the task is actively running but scheduled to yield. */
225 #define taskTASK_YIELDING ( TaskRunning_t ) ( -2 )
227 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
228 #define taskTASK_IS_RUNNING( xTaskRunState ) ( ( 0 <= xTaskRunState ) && ( xTaskRunState < configNUM_CORES ) )
230 typedef BaseType_t TaskRunning_t;
233 * Task control block. A task control block (TCB) is allocated for each task,
234 * and stores task state information, including a pointer to the task's context
235 * (the task's run time environment, including register values)
237 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
239 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. */
241 #if ( portUSING_MPU_WRAPPERS == 1 )
242 xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
245 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 ). */
246 ListItem_t xEventListItem; /*< Used to reference a task from an event list. */
247 UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */
248 StackType_t * pxStack; /*< Points to the start of the stack. */
249 volatile TaskRunning_t xTaskRunState; /*< Used to identify the core the task is running on, if any. */
250 BaseType_t xIsIdle; /*< Used to identify the idle tasks. */
251 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. */
253 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
254 BaseType_t xPreemptionDisable; /*< Used to prevent the task from being preempted */
257 #if ( configUSE_CORE_AFFINITY == 1 && configNUM_CORES > 1 )
258 UBaseType_t uxCoreAffinityMask; /*< Used to link the task to certain cores. UBaseType_t must have >= the same number of bits as SMP confNUM_CORES */
261 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
262 StackType_t * pxEndOfStack; /*< Points to the highest valid address for the stack. */
265 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
266 UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
269 #if ( configUSE_TRACE_FACILITY == 1 )
270 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. */
271 UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
274 #if ( configUSE_MUTEXES == 1 )
275 UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
276 UBaseType_t uxMutexesHeld;
279 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
280 TaskHookFunction_t pxTaskTag;
283 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
284 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
287 #if ( configGENERATE_RUN_TIME_STATS == 1 )
288 uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
291 #if ( configUSE_NEWLIB_REENTRANT == 1 )
292 /* Allocate a Newlib reent structure that is specific to this task.
293 * Note Newlib support has been included by popular demand, but is not
294 * used by the FreeRTOS maintainers themselves. FreeRTOS is not
295 * responsible for resulting newlib operation. User must be familiar with
296 * newlib and must provide system-wide implementations of the necessary
297 * stubs. Be warned that (at the time of writing) the current newlib design
298 * implements a system-wide malloc() that must be provided with locks.
300 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
301 * for additional information. */
302 struct _reent xNewLib_reent;
305 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
306 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
307 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
310 /* See the comments in FreeRTOS.h with the definition of
311 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
312 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
313 uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
316 #if ( INCLUDE_xTaskAbortDelay == 1 )
317 uint8_t ucDelayAborted;
320 #if ( configUSE_POSIX_ERRNO == 1 )
325 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
326 * below to enable the use of older kernel aware debuggers. */
327 typedef tskTCB TCB_t;
329 /*lint -save -e956 A manual analysis and inspection has been used to determine
330 * which static variables must be declared volatile. */
331 PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUM_CORES ] = { NULL };
332 #define pxCurrentTCB xTaskGetCurrentTaskHandle()
334 /* Lists for ready and blocked tasks. --------------------
335 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
336 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
337 * the static qualifier. */
338 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */
339 PRIVILEGED_DATA static List_t xDelayedTaskList1; /*< Delayed tasks. */
340 PRIVILEGED_DATA static List_t xDelayedTaskList2; /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
341 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /*< Points to the delayed task list currently being used. */
342 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. */
343 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. */
345 #if ( INCLUDE_vTaskDelete == 1 )
347 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /*< Tasks that have been deleted - but their memory not yet freed. */
348 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
352 #if ( INCLUDE_vTaskSuspend == 1 )
354 PRIVILEGED_DATA static List_t xSuspendedTaskList; /*< Tasks that are currently suspended. */
358 /* Global POSIX errno. Its value is changed upon context switching to match
359 * the errno of the currently running task. */
360 #if ( configUSE_POSIX_ERRNO == 1 )
361 int FreeRTOS_errno = 0;
364 /* Other file private variables. --------------------------------*/
365 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
366 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
367 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
368 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
369 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
370 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUM_CORES ] = { pdFALSE };
371 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
372 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
373 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
374 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. */
376 #define xYieldPending prvGetCurrentYieldPending()
378 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
379 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
380 * to determine the number of priority lists to read back from the remote target. */
381 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
383 /* Context switches are held pending while the scheduler is suspended. Also,
384 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
385 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
386 * If an interrupt needs to unblock a task while the scheduler is suspended then it
387 * moves the task's event list item into the xPendingReadyList, ready for the
388 * kernel to move the task from the pending ready list into the real ready list
389 * when the scheduler is unsuspended. The pending ready list itself can only be
390 * accessed from a critical section.
392 * Updates to uxSchedulerSuspended must be protected by both the task and ISR locks and
393 * must not be done by an ISR. Reads must be protected by either lock and may be done by
394 * either an ISR or a task. */
395 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) pdFALSE;
397 #if ( configGENERATE_RUN_TIME_STATS == 1 )
399 /* Do not move these variables to function scope as doing so prevents the
400 * code working with debuggers that need to remove the static qualifier. */
401 PRIVILEGED_DATA static uint32_t ulTaskSwitchedInTime = 0UL; /*< Holds the value of a timer/counter the last time a task was switched in. */
402 PRIVILEGED_DATA static volatile uint32_t ulTotalRunTime = 0UL; /*< Holds the total amount of execution time as defined by the run time counter clock. */
408 /*-----------------------------------------------------------*/
410 /* File private functions. --------------------------------*/
413 * Creates the idle tasks during scheduler start
415 static BaseType_t prvCreateIdleTasks( void );
418 * Returns the yield pending count for the calling core.
420 static BaseType_t prvGetCurrentYieldPending( void );
423 * Checks to see if another task moved the current task out of the ready
424 * list while it was waiting to enter a critical section and yields if so.
426 static void prvCheckForRunStateChange( void );
429 * Yields the given core.
431 static void prvYieldCore( BaseType_t xCoreID );
434 * Yields a core, or cores if multiple priorities are not allowed to run
435 * simultaneously, to allow the task pxTCB to run.
437 static void prvYieldForTask( TCB_t * pxTCB,
438 const BaseType_t xPreemptEqualPriority );
441 * Selects the highest priority available task
443 static BaseType_t prvSelectHighestPriorityTask( const BaseType_t xCoreID );
446 * Utility task that simply returns pdTRUE if the task referenced by xTask is
447 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
448 * is in any other state.
450 #if ( INCLUDE_vTaskSuspend == 1 )
452 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
454 #endif /* INCLUDE_vTaskSuspend */
457 * Utility to ready all the lists used by the scheduler. This is called
458 * automatically upon the creation of the first task.
460 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
463 * The idle task, which as all tasks is implemented as a never ending loop.
464 * The idle task is automatically created and added to the ready lists upon
465 * creation of the first user task.
468 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
469 #if ( configNUM_CORES > 1 )
470 static portTASK_FUNCTION_PROTO( prvMinimalIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
474 * Utility to free all memory allocated by the scheduler to hold a TCB,
475 * including the stack pointed to by the TCB.
477 * This does not free memory allocated by the task itself (i.e. memory
478 * allocated by calls to pvPortMalloc from within the tasks application code).
480 #if ( INCLUDE_vTaskDelete == 1 )
482 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
487 * Used only by the idle task. This checks to see if anything has been placed
488 * in the list of tasks waiting to be deleted. If so the task is cleaned up
489 * and its TCB deleted.
491 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
494 * The currently executing task is entering the Blocked state. Add the task to
495 * either the current or the overflow delayed task list.
497 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
498 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
501 * Fills an TaskStatus_t structure with information on each task that is
502 * referenced from the pxList list (which may be a ready list, a delayed list,
503 * a suspended list, etc.).
505 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
506 * NORMAL APPLICATION CODE.
508 #if ( configUSE_TRACE_FACILITY == 1 )
510 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
512 eTaskState eState ) PRIVILEGED_FUNCTION;
517 * Searches pxList for a task with name pcNameToQuery - returning a handle to
518 * the task if it is found, or NULL if the task is not found.
520 #if ( INCLUDE_xTaskGetHandle == 1 )
522 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
523 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
528 * When a task is created, the stack of the task is filled with a known value.
529 * This function determines the 'high water mark' of the task stack by
530 * determining how much of the stack remains at the original preset value.
532 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
534 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
539 * Return the amount of time, in ticks, that will pass before the kernel will
540 * next move a task from the Blocked state to the Running state.
542 * This conditional compilation should use inequality to 0, not equality to 1.
543 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
544 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
545 * set to a value other than 1.
547 #if ( configUSE_TICKLESS_IDLE != 0 )
549 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
554 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
555 * will exit the Blocked state.
557 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
559 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
562 * Helper function used to pad task names with spaces when printing out
563 * human readable tables of task information.
565 static char * prvWriteNameToBuffer( char * pcBuffer,
566 const char * pcTaskName ) PRIVILEGED_FUNCTION;
571 * Called after a Task_t structure has been allocated either statically or
572 * dynamically to fill in the structure's members.
574 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
575 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
576 const uint32_t ulStackDepth,
577 void * const pvParameters,
578 UBaseType_t uxPriority,
579 TaskHandle_t * const pxCreatedTask,
581 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
584 * Called after a new task has been created and initialised to place the task
585 * under the control of the scheduler.
587 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
590 * freertos_tasks_c_additions_init() should only be called if the user definable
591 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
592 * called by the function.
594 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
596 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
600 /*-----------------------------------------------------------*/
602 static BaseType_t prvGetCurrentYieldPending( void )
607 ulState = portDISABLE_INTERRUPTS();
608 xReturn = xYieldPendings[ portGET_CORE_ID() ];
609 portRESTORE_INTERRUPTS( ulState );
614 /*-----------------------------------------------------------*/
616 static void prvCheckForRunStateChange( void )
618 UBaseType_t uxPrevCriticalNesting;
619 UBaseType_t uxPrevSchedulerSuspended;
622 /* This should be skipped when entering a critical section within
623 * an ISR. If the task on the current core is no longer running, then
624 * vTaskSwitchContext() probably should be run before returning, but
625 * we don't have a way to force that to happen from here. */
626 if( portCHECK_IF_IN_ISR() == pdFALSE )
628 /* This function is always called with interrupts disabled
629 * so this is safe. */
630 pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
632 while( pxThisTCB->xTaskRunState == taskTASK_YIELDING )
634 /* We are only here if we just entered a critical section
635 * or if we just suspended the scheduler, and another task
636 * has requested that we yield.
638 * This is slightly complicated since we need to save and restore
639 * the suspension and critical nesting counts, as well as release
640 * and reacquire the correct locks. And then do it all over again
641 * if our state changed again during the reacquisition. */
643 uxPrevCriticalNesting = pxThisTCB->uxCriticalNesting;
644 uxPrevSchedulerSuspended = uxSchedulerSuspended;
646 /* this must only be called the first time we enter into a critical
647 * section, otherwise it could context switch in the middle of a
648 * critical section. */
649 configASSERT( uxPrevCriticalNesting + uxPrevSchedulerSuspended == 1U );
651 uxSchedulerSuspended = 0U;
653 if( uxPrevCriticalNesting > 0U )
655 pxThisTCB->uxCriticalNesting = 0U;
656 portRELEASE_ISR_LOCK();
657 portRELEASE_TASK_LOCK();
661 /* uxPrevSchedulerSuspended must be 1 */
662 portRELEASE_TASK_LOCK();
665 portMEMORY_BARRIER();
666 configASSERT( pxThisTCB->xTaskRunState == taskTASK_YIELDING );
668 portENABLE_INTERRUPTS();
670 /* Enabling interrupts should cause this core to immediately
671 * service the pending interrupt and yield. If the run state is still
672 * yielding here then that is a problem. */
673 configASSERT( pxThisTCB->xTaskRunState != taskTASK_YIELDING );
675 portDISABLE_INTERRUPTS();
678 pxCurrentTCB->uxCriticalNesting = uxPrevCriticalNesting;
679 uxSchedulerSuspended = uxPrevSchedulerSuspended;
681 if( uxPrevCriticalNesting == 0U )
683 /* uxPrevSchedulerSuspended must be 1 */
684 configASSERT( uxPrevSchedulerSuspended != ( UBaseType_t ) pdFALSE );
685 portRELEASE_ISR_LOCK();
691 /*-----------------------------------------------------------*/
693 static void prvYieldCore( BaseType_t xCoreID )
695 /* This must be called from a critical section and
696 * xCoreID must be valid. */
698 if( portCHECK_IF_IN_ISR() && ( xCoreID == portGET_CORE_ID() ) )
700 xYieldPendings[ xCoreID ] = pdTRUE;
702 else if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_YIELDING )
704 if( xCoreID == portGET_CORE_ID() )
706 xYieldPendings[ xCoreID ] = pdTRUE;
710 portYIELD_CORE( xCoreID );
711 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 ( 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 ( 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.
1004 * @todo See if we can schedule it on any of the cores where it is not excluded from. */
1007 uxCoreMap &= ( ( 1 << configNUM_CORES ) - 1 );
1009 while( uxCoreMap != 0 )
1011 int uxCore = 31UL - ( uint32_t ) __builtin_clz( uxCoreMap );
1013 xassert( taskVALID_CORE_ID( uxCore ) );
1015 uxCoreMap &= ~( 1 << uxCore );
1017 BaseType_t xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority - pxCurrentTCBs[ uxCore ]->xIsIdle;
1019 if( ( xTaskPriority < xLowestPriority ) && ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ]->xTaskRunState ) != pdFALSE ) && ( xYieldPendings[ uxCore ] == pdFALSE ) )
1021 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1022 if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1025 xLowestPriority = xTaskPriority;
1026 xLowestPriorityCore = uxCore;
1031 if( taskVALID_CORE_ID( xLowestPriorityCore ) )
1033 prvYieldCore( xLowestPriorityCore );
1036 #endif /* if ( configUSE_CORE_AFFINITY == 1 ) */
1037 #endif /* if ( configNUM_CORES > 1 ) */
1042 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1044 static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
1046 UBaseType_t uxTopPriority;
1048 /* Find the highest priority list that contains ready tasks. */
1049 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority );
1050 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 );
1051 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) );
1054 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
1055 /*-----------------------------------------------------------*/
1057 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1059 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1060 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1061 const uint32_t ulStackDepth,
1062 void * const pvParameters,
1063 UBaseType_t uxPriority,
1064 StackType_t * const puxStackBuffer,
1065 StaticTask_t * const pxTaskBuffer )
1068 TaskHandle_t xReturn;
1070 configASSERT( puxStackBuffer != NULL );
1071 configASSERT( pxTaskBuffer != NULL );
1073 #if ( configASSERT_DEFINED == 1 )
1075 /* Sanity check that the size of the structure used to declare a
1076 * variable of type StaticTask_t equals the size of the real task
1078 volatile size_t xSize = sizeof( StaticTask_t );
1079 configASSERT( xSize == sizeof( TCB_t ) );
1080 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1082 #endif /* configASSERT_DEFINED */
1084 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1086 /* The memory used for the task's TCB and stack are passed into this
1087 * function - use them. */
1088 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. */
1089 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1091 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1093 /* Tasks can be created statically or dynamically, so note this
1094 * task was created statically in case the task is later deleted. */
1095 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1097 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1099 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1100 prvAddNewTaskToReadyList( pxNewTCB );
1110 #endif /* SUPPORT_STATIC_ALLOCATION */
1111 /*-----------------------------------------------------------*/
1113 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1115 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1116 TaskHandle_t * pxCreatedTask )
1119 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1121 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1122 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1124 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1126 /* Allocate space for the TCB. Where the memory comes from depends
1127 * on the implementation of the port malloc function and whether or
1128 * not static allocation is being used. */
1129 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1131 /* Store the stack location in the TCB. */
1132 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1134 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1136 /* Tasks can be created statically or dynamically, so note this
1137 * task was created statically in case the task is later deleted. */
1138 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1140 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1142 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1143 pxTaskDefinition->pcName,
1144 ( uint32_t ) pxTaskDefinition->usStackDepth,
1145 pxTaskDefinition->pvParameters,
1146 pxTaskDefinition->uxPriority,
1147 pxCreatedTask, pxNewTCB,
1148 pxTaskDefinition->xRegions );
1150 prvAddNewTaskToReadyList( pxNewTCB );
1157 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1158 /*-----------------------------------------------------------*/
1160 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1162 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1163 TaskHandle_t * pxCreatedTask )
1166 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1168 configASSERT( pxTaskDefinition->puxStackBuffer );
1170 if( pxTaskDefinition->puxStackBuffer != NULL )
1172 /* Allocate space for the TCB. Where the memory comes from depends
1173 * on the implementation of the port malloc function and whether or
1174 * not static allocation is being used. */
1175 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1177 if( pxNewTCB != NULL )
1179 /* Store the stack location in the TCB. */
1180 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1182 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1184 /* Tasks can be created statically or dynamically, so note
1185 * this task had a statically allocated stack in case it is
1186 * later deleted. The TCB was allocated dynamically. */
1187 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1189 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1191 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1192 pxTaskDefinition->pcName,
1193 ( uint32_t ) pxTaskDefinition->usStackDepth,
1194 pxTaskDefinition->pvParameters,
1195 pxTaskDefinition->uxPriority,
1196 pxCreatedTask, pxNewTCB,
1197 pxTaskDefinition->xRegions );
1199 prvAddNewTaskToReadyList( pxNewTCB );
1207 #endif /* portUSING_MPU_WRAPPERS */
1208 /*-----------------------------------------------------------*/
1210 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1212 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1213 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1214 const configSTACK_DEPTH_TYPE usStackDepth,
1215 void * const pvParameters,
1216 UBaseType_t uxPriority,
1217 TaskHandle_t * const pxCreatedTask )
1222 /* If the stack grows down then allocate the stack then the TCB so the stack
1223 * does not grow into the TCB. Likewise if the stack grows up then allocate
1224 * the TCB then the stack. */
1225 #if ( portSTACK_GROWTH > 0 )
1227 /* Allocate space for the TCB. Where the memory comes from depends on
1228 * the implementation of the port malloc function and whether or not static
1229 * allocation is being used. */
1230 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1232 if( pxNewTCB != NULL )
1234 /* Allocate space for the stack used by the task being created.
1235 * The base of the stack memory stored in the TCB so the task can
1236 * be deleted later if required. */
1237 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1239 if( pxNewTCB->pxStack == NULL )
1241 /* Could not allocate the stack. Delete the allocated TCB. */
1242 vPortFree( pxNewTCB );
1247 #else /* portSTACK_GROWTH */
1249 StackType_t * pxStack;
1251 /* Allocate space for the stack used by the task being created. */
1252 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. */
1254 if( pxStack != NULL )
1256 /* Allocate space for the TCB. */
1257 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. */
1259 if( pxNewTCB != NULL )
1261 /* Store the stack location in the TCB. */
1262 pxNewTCB->pxStack = pxStack;
1266 /* The stack cannot be used as the TCB was not created. Free
1268 vPortFreeStack( pxStack );
1276 #endif /* portSTACK_GROWTH */
1278 if( pxNewTCB != NULL )
1280 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1282 /* Tasks can be created statically or dynamically, so note this
1283 * task was created dynamically in case it is later deleted. */
1284 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1286 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1288 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1289 prvAddNewTaskToReadyList( pxNewTCB );
1294 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1300 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1301 /*-----------------------------------------------------------*/
1303 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1304 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1305 const uint32_t ulStackDepth,
1306 void * const pvParameters,
1307 UBaseType_t uxPriority,
1308 TaskHandle_t * const pxCreatedTask,
1310 const MemoryRegion_t * const xRegions )
1312 StackType_t * pxTopOfStack;
1315 #if ( portUSING_MPU_WRAPPERS == 1 )
1316 /* Should the task be created in privileged mode? */
1317 BaseType_t xRunPrivileged;
1319 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1321 xRunPrivileged = pdTRUE;
1325 xRunPrivileged = pdFALSE;
1327 uxPriority &= ~portPRIVILEGE_BIT;
1328 #endif /* portUSING_MPU_WRAPPERS == 1 */
1330 /* Avoid dependency on memset() if it is not required. */
1331 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1333 /* Fill the stack with a known value to assist debugging. */
1334 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1336 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1338 /* Calculate the top of stack address. This depends on whether the stack
1339 * grows from high memory to low (as per the 80x86) or vice versa.
1340 * portSTACK_GROWTH is used to make the result positive or negative as required
1342 #if ( portSTACK_GROWTH < 0 )
1344 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1345 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(). */
1347 /* Check the alignment of the calculated top of stack is correct. */
1348 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1350 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1352 /* Also record the stack's high address, which may assist
1354 pxNewTCB->pxEndOfStack = pxTopOfStack;
1356 #endif /* configRECORD_STACK_HIGH_ADDRESS */
1358 #else /* portSTACK_GROWTH */
1360 pxTopOfStack = pxNewTCB->pxStack;
1362 /* Check the alignment of the stack buffer is correct. */
1363 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1365 /* The other extreme of the stack space is required if stack checking is
1367 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1369 #endif /* portSTACK_GROWTH */
1371 /* Store the task name in the TCB. */
1372 if( pcName != NULL )
1374 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1376 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1378 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1379 * configMAX_TASK_NAME_LEN characters just in case the memory after the
1380 * string is not accessible (extremely unlikely). */
1381 if( pcName[ x ] == ( char ) 0x00 )
1387 mtCOVERAGE_TEST_MARKER();
1391 /* Ensure the name string is terminated in the case that the string length
1392 * was greater or equal to configMAX_TASK_NAME_LEN. */
1393 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1397 /* The task has not been given a name, so just ensure there is a NULL
1398 * terminator when it is read out. */
1399 pxNewTCB->pcTaskName[ 0 ] = 0x00;
1402 /* This is used as an array index so must ensure it's not too large. First
1403 * remove the privilege bit if one is present. */
1404 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1406 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1410 mtCOVERAGE_TEST_MARKER();
1413 pxNewTCB->uxPriority = uxPriority;
1414 #if ( configUSE_MUTEXES == 1 )
1416 pxNewTCB->uxBasePriority = uxPriority;
1417 pxNewTCB->uxMutexesHeld = 0;
1419 #endif /* configUSE_MUTEXES */
1421 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1422 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1424 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
1425 * back to the containing TCB from a generic item in a list. */
1426 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1428 /* Event lists are always in priority order. */
1429 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. */
1430 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1432 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
1434 pxNewTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
1436 #endif /* portCRITICAL_NESTING_IN_TCB */
1438 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
1440 pxNewTCB->pxTaskTag = NULL;
1442 #endif /* configUSE_APPLICATION_TASK_TAG */
1444 #if ( configGENERATE_RUN_TIME_STATS == 1 )
1446 pxNewTCB->ulRunTimeCounter = 0UL;
1448 #endif /* configGENERATE_RUN_TIME_STATS */
1450 #if ( portUSING_MPU_WRAPPERS == 1 )
1452 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1456 /* Avoid compiler warning about unreferenced parameter. */
1461 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
1463 memset( ( void * ) &( pxNewTCB->pvThreadLocalStoragePointers[ 0 ] ), 0x00, sizeof( pxNewTCB->pvThreadLocalStoragePointers ) );
1467 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1469 memset( ( void * ) &( pxNewTCB->ulNotifiedValue[ 0 ] ), 0x00, sizeof( pxNewTCB->ulNotifiedValue ) );
1470 memset( ( void * ) &( pxNewTCB->ucNotifyState[ 0 ] ), 0x00, sizeof( pxNewTCB->ucNotifyState ) );
1474 #if ( configUSE_NEWLIB_REENTRANT == 1 )
1476 /* Initialise this task's Newlib reent structure.
1477 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
1478 * for additional information. */
1479 _REENT_INIT_PTR( ( &( pxNewTCB->xNewLib_reent ) ) );
1483 #if ( INCLUDE_xTaskAbortDelay == 1 )
1485 pxNewTCB->ucDelayAborted = pdFALSE;
1489 #if ( configNUM_CORES > 1 )
1490 #if ( configUSE_CORE_AFFINITY == 1 )
1492 pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1496 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1498 pxNewTCB->xPreemptionDisable = 0;
1502 /* Initialize the TCB stack to look as if the task was already running,
1503 * but had been interrupted by the scheduler. The return address is set
1504 * to the start of the task function. Once the stack has been initialised
1505 * the top of stack variable is updated. */
1506 #if ( portUSING_MPU_WRAPPERS == 1 )
1508 /* If the port has capability to detect stack overflow,
1509 * pass the stack end address to the stack initialization
1510 * function as well. */
1511 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1513 #if ( portSTACK_GROWTH < 0 )
1515 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
1517 #else /* portSTACK_GROWTH */
1519 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1521 #endif /* portSTACK_GROWTH */
1523 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1525 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1527 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1529 #else /* portUSING_MPU_WRAPPERS */
1531 /* If the port has capability to detect stack overflow,
1532 * pass the stack end address to the stack initialization
1533 * function as well. */
1534 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1536 #if ( portSTACK_GROWTH < 0 )
1538 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1540 #else /* portSTACK_GROWTH */
1542 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1544 #endif /* portSTACK_GROWTH */
1546 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1548 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1550 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1552 #endif /* portUSING_MPU_WRAPPERS */
1554 /* Initialize to not running */
1555 pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1557 /* Is this an idle task? */
1558 if(pxTaskCode == prvIdleTask)
1560 pxNewTCB->xIsIdle = pdTRUE;
1562 #if ( configNUM_CORES > 1 )
1563 else if( pxTaskCode == prvMinimalIdleTask )
1565 pxNewTCB->xIsIdle = pdTRUE;
1570 pxNewTCB->xIsIdle = pdFALSE;
1573 if( pxCreatedTask != NULL )
1575 /* Pass the handle out in an anonymous way. The handle can be used to
1576 * change the created task's priority, delete the created task, etc.*/
1577 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1581 mtCOVERAGE_TEST_MARKER();
1584 /*-----------------------------------------------------------*/
1586 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1588 /* Ensure interrupts don't access the task lists while the lists are being
1590 taskENTER_CRITICAL();
1592 uxCurrentNumberOfTasks++;
1594 if( xSchedulerRunning == pdFALSE )
1596 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1598 /* This is the first task to be created so do the preliminary
1599 * initialisation required. We will not recover if this call
1600 * fails, but we will report the failure. */
1601 prvInitialiseTaskLists();
1605 mtCOVERAGE_TEST_MARKER();
1608 if( pxNewTCB->xIsIdle != pdFALSE )
1612 /* Check if a core is free. */
1613 for( xCoreID = ( UBaseType_t ) 0; xCoreID < ( UBaseType_t ) configNUM_CORES; xCoreID++ )
1615 if( pxCurrentTCBs[ xCoreID ] == NULL )
1617 pxNewTCB->xTaskRunState = xCoreID;
1619 /* This section of code pins the idle tasks to cores.
1620 #if ( configUSE_CORE_AFFINITY == 1 )
1622 * pxNewTCB->uxCoreAffinityMask = ( 1 << xCoreID );
1626 pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1634 mtCOVERAGE_TEST_MARKER();
1639 #if ( configUSE_TRACE_FACILITY == 1 )
1641 /* Add a counter into the TCB for tracing only. */
1642 pxNewTCB->uxTCBNumber = uxTaskNumber;
1644 #endif /* configUSE_TRACE_FACILITY */
1645 traceTASK_CREATE( pxNewTCB );
1647 prvAddTaskToReadyList( pxNewTCB );
1649 portSETUP_TCB( pxNewTCB );
1651 if( xSchedulerRunning != pdFALSE )
1653 /* If the created task is of a higher priority than another
1654 * currently running task and preemption is on then it should
1656 #if ( configUSE_PREEMPTION == 1 )
1657 prvYieldForTask( pxNewTCB, pdFALSE );
1662 mtCOVERAGE_TEST_MARKER();
1665 taskEXIT_CRITICAL();
1667 /*-----------------------------------------------------------*/
1669 #if ( INCLUDE_vTaskDelete == 1 )
1671 void vTaskDelete( TaskHandle_t xTaskToDelete )
1674 TaskRunning_t xTaskRunningOnCore;
1676 taskENTER_CRITICAL();
1678 /* If null is passed in here then it is the calling task that is
1680 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1682 xTaskRunningOnCore = pxTCB->xTaskRunState;
1684 /* Remove task from the ready/delayed list. */
1685 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1687 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1691 mtCOVERAGE_TEST_MARKER();
1694 /* Is the task waiting on an event also? */
1695 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1697 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1701 mtCOVERAGE_TEST_MARKER();
1704 /* Increment the uxTaskNumber also so kernel aware debuggers can
1705 * detect that the task lists need re-generating. This is done before
1706 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1710 /* If the task is running (or yielding), we must add it to the
1711 * termination list so that an idle task can delete it when it is
1712 * no longer running. */
1713 if( xTaskRunningOnCore != taskTASK_NOT_RUNNING )
1715 /* A running task is being deleted. This cannot complete within the
1716 * task itself, as a context switch to another task is required.
1717 * Place the task in the termination list. The idle task will
1718 * check the termination list and free up any memory allocated by
1719 * the scheduler for the TCB and stack of the deleted task. */
1720 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1722 /* Increment the ucTasksDeleted variable so the idle task knows
1723 * there is a task that has been deleted and that it should therefore
1724 * check the xTasksWaitingTermination list. */
1725 ++uxDeletedTasksWaitingCleanUp;
1727 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1728 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1729 traceTASK_DELETE( pxTCB );
1731 /* The pre-delete hook is primarily for the Windows simulator,
1732 * in which Windows specific clean up operations are performed,
1733 * after which it is not possible to yield away from this task -
1734 * hence xYieldPending is used to latch that a context switch is
1736 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPendings[ pxTCB->xTaskRunState ] );
1740 --uxCurrentNumberOfTasks;
1741 traceTASK_DELETE( pxTCB );
1742 prvDeleteTCB( pxTCB );
1744 /* Reset the next expected unblock time in case it referred to
1745 * the task that has just been deleted. */
1746 prvResetNextTaskUnblockTime();
1749 /* Force a reschedule if the task that has just been deleted was running. */
1750 if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( xTaskRunningOnCore ) ) )
1754 xCoreID = portGET_CORE_ID();
1756 if( xTaskRunningOnCore == xCoreID )
1758 configASSERT( uxSchedulerSuspended == 0 );
1759 vTaskYieldWithinAPI();
1763 prvYieldCore( xTaskRunningOnCore );
1767 taskEXIT_CRITICAL();
1770 #endif /* INCLUDE_vTaskDelete */
1771 /*-----------------------------------------------------------*/
1773 #if ( INCLUDE_xTaskDelayUntil == 1 )
1775 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
1776 const TickType_t xTimeIncrement )
1778 TickType_t xTimeToWake;
1779 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
1781 configASSERT( pxPreviousWakeTime );
1782 configASSERT( ( xTimeIncrement > 0U ) );
1786 configASSERT( uxSchedulerSuspended == 1 );
1788 /* Minor optimisation. The tick count cannot change in this
1790 const TickType_t xConstTickCount = xTickCount;
1792 /* Generate the tick time at which the task wants to wake. */
1793 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
1795 if( xConstTickCount < *pxPreviousWakeTime )
1797 /* The tick count has overflowed since this function was
1798 * lasted called. In this case the only time we should ever
1799 * actually delay is if the wake time has also overflowed,
1800 * and the wake time is greater than the tick time. When this
1801 * is the case it is as if neither time had overflowed. */
1802 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1804 xShouldDelay = pdTRUE;
1808 mtCOVERAGE_TEST_MARKER();
1813 /* The tick time has not overflowed. In this case we will
1814 * delay if either the wake time has overflowed, and/or the
1815 * tick time is less than the wake time. */
1816 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1818 xShouldDelay = pdTRUE;
1822 mtCOVERAGE_TEST_MARKER();
1826 /* Update the wake time ready for the next call. */
1827 *pxPreviousWakeTime = xTimeToWake;
1829 if( xShouldDelay != pdFALSE )
1831 traceTASK_DELAY_UNTIL( xTimeToWake );
1833 /* prvAddCurrentTaskToDelayedList() needs the block time, not
1834 * the time to wake, so subtract the current tick count. */
1835 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
1839 mtCOVERAGE_TEST_MARKER();
1842 xAlreadyYielded = xTaskResumeAll();
1844 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1845 * have put ourselves to sleep. */
1846 if( xAlreadyYielded == pdFALSE )
1848 vTaskYieldWithinAPI();
1852 mtCOVERAGE_TEST_MARKER();
1855 return xShouldDelay;
1858 #endif /* INCLUDE_xTaskDelayUntil */
1859 /*-----------------------------------------------------------*/
1861 #if ( INCLUDE_vTaskDelay == 1 )
1863 void vTaskDelay( const TickType_t xTicksToDelay )
1865 BaseType_t xAlreadyYielded = pdFALSE;
1867 /* A delay time of zero just forces a reschedule. */
1868 if( xTicksToDelay > ( TickType_t ) 0U )
1872 configASSERT( uxSchedulerSuspended == 1 );
1875 /* A task that is removed from the event list while the
1876 * scheduler is suspended will not get placed in the ready
1877 * list or removed from the blocked list until the scheduler
1880 * This task cannot be in an event list as it is the currently
1881 * executing task. */
1882 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
1884 xAlreadyYielded = xTaskResumeAll();
1888 mtCOVERAGE_TEST_MARKER();
1891 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1892 * have put ourselves to sleep. */
1893 if( xAlreadyYielded == pdFALSE )
1895 vTaskYieldWithinAPI();
1899 mtCOVERAGE_TEST_MARKER();
1903 #endif /* INCLUDE_vTaskDelay */
1904 /*-----------------------------------------------------------*/
1906 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
1908 eTaskState eTaskGetState( TaskHandle_t xTask )
1911 List_t const * pxStateList, * pxDelayedList, * pxOverflowedDelayedList;
1912 const TCB_t * const pxTCB = xTask;
1914 configASSERT( pxTCB );
1916 taskENTER_CRITICAL();
1918 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
1919 pxDelayedList = pxDelayedTaskList;
1920 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
1922 taskEXIT_CRITICAL();
1924 if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
1926 /* The task being queried is referenced from one of the Blocked
1931 #if ( INCLUDE_vTaskSuspend == 1 )
1932 else if( pxStateList == &xSuspendedTaskList )
1934 /* The task being queried is referenced from the suspended
1935 * list. Is it genuinely suspended or is it blocked
1937 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
1939 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1943 /* The task does not appear on the event list item of
1944 * and of the RTOS objects, but could still be in the
1945 * blocked state if it is waiting on its notification
1946 * rather than waiting on an object. If not, is
1948 eReturn = eSuspended;
1950 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1952 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1959 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1961 eReturn = eSuspended;
1963 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1970 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
1972 #if ( INCLUDE_vTaskDelete == 1 )
1973 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
1975 /* The task being queried is referenced from the deleted
1976 * tasks list, or it is not referenced from any lists at
1982 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
1984 /* If the task is not in any other state, it must be in the
1985 * Ready (including pending ready) state. */
1986 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
1988 /* Is it actively running on a core? */
1998 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2000 #endif /* INCLUDE_eTaskGetState */
2001 /*-----------------------------------------------------------*/
2003 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2005 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2007 TCB_t const * pxTCB;
2008 UBaseType_t uxReturn;
2010 taskENTER_CRITICAL();
2012 /* If null is passed in here then it is the priority of the task
2013 * that called uxTaskPriorityGet() that is being queried. */
2014 pxTCB = prvGetTCBFromHandle( xTask );
2015 uxReturn = pxTCB->uxPriority;
2017 taskEXIT_CRITICAL();
2022 #endif /* INCLUDE_uxTaskPriorityGet */
2023 /*-----------------------------------------------------------*/
2025 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2027 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2029 TCB_t const * pxTCB;
2030 UBaseType_t uxReturn, uxSavedInterruptState;
2032 /* RTOS ports that support interrupt nesting have the concept of a
2033 * maximum system call (or maximum API call) interrupt priority.
2034 * Interrupts that are above the maximum system call priority are keep
2035 * permanently enabled, even when the RTOS kernel is in a critical section,
2036 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2037 * is defined in FreeRTOSConfig.h then
2038 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2039 * failure if a FreeRTOS API function is called from an interrupt that has
2040 * been assigned a priority above the configured maximum system call
2041 * priority. Only FreeRTOS functions that end in FromISR can be called
2042 * from interrupts that have been assigned a priority at or (logically)
2043 * below the maximum system call interrupt priority. FreeRTOS maintains a
2044 * separate interrupt safe API to ensure interrupt entry is as fast and as
2045 * simple as possible. More information (albeit Cortex-M specific) is
2046 * provided on the following link:
2047 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2048 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2050 uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
2052 /* If null is passed in here then it is the priority of the calling
2053 * task that is being queried. */
2054 pxTCB = prvGetTCBFromHandle( xTask );
2055 uxReturn = pxTCB->uxPriority;
2057 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
2062 #endif /* INCLUDE_uxTaskPriorityGet */
2063 /*-----------------------------------------------------------*/
2065 #if ( INCLUDE_vTaskPrioritySet == 1 )
2067 void vTaskPrioritySet( TaskHandle_t xTask,
2068 UBaseType_t uxNewPriority )
2071 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2072 BaseType_t xYieldRequired = pdFALSE;
2073 BaseType_t xYieldForTask = pdFALSE;
2076 configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
2078 /* Ensure the new priority is valid. */
2079 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2081 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2085 mtCOVERAGE_TEST_MARKER();
2088 taskENTER_CRITICAL();
2090 /* If null is passed in here then it is the priority of the calling
2091 * task that is being changed. */
2092 pxTCB = prvGetTCBFromHandle( xTask );
2094 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2096 #if ( configUSE_MUTEXES == 1 )
2098 uxCurrentBasePriority = pxTCB->uxBasePriority;
2102 uxCurrentBasePriority = pxTCB->uxPriority;
2106 if( uxCurrentBasePriority != uxNewPriority )
2108 /* The priority change may have readied a task of higher
2109 * priority than a running task. */
2110 if( uxNewPriority > uxCurrentBasePriority )
2112 /* The priority of a task is being raised so
2113 * perform a yield for this task later. */
2114 xYieldForTask = pdTRUE;
2116 else if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2118 /* Setting the priority of a running task down means
2119 * there may now be another task of higher priority that
2120 * is ready to execute. */
2121 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2122 if( pxTCB->xPreemptionDisable == pdFALSE )
2125 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2126 xYieldRequired = pdTRUE;
2131 /* Setting the priority of any other task down does not
2132 * require a yield as the running task must be above the
2133 * new priority of the task being modified. */
2136 /* Remember the ready list the task might be referenced from
2137 * before its uxPriority member is changed so the
2138 * taskRESET_READY_PRIORITY() macro can function correctly. */
2139 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2141 #if ( configUSE_MUTEXES == 1 )
2143 /* Only change the priority being used if the task is not
2144 * currently using an inherited priority. */
2145 if( pxTCB->uxBasePriority == pxTCB->uxPriority )
2147 pxTCB->uxPriority = uxNewPriority;
2151 mtCOVERAGE_TEST_MARKER();
2154 /* The base priority gets set whatever. */
2155 pxTCB->uxBasePriority = uxNewPriority;
2157 #else /* if ( configUSE_MUTEXES == 1 ) */
2159 pxTCB->uxPriority = uxNewPriority;
2161 #endif /* if ( configUSE_MUTEXES == 1 ) */
2163 /* Only reset the event list item value if the value is not
2164 * being used for anything else. */
2165 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2167 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. */
2171 mtCOVERAGE_TEST_MARKER();
2174 /* If the task is in the blocked or suspended list we need do
2175 * nothing more than change its priority variable. However, if
2176 * the task is in a ready list it needs to be removed and placed
2177 * in the list appropriate to its new priority. */
2178 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2180 /* The task is currently in its ready list - remove before
2181 * adding it to its new ready list. As we are in a critical
2182 * section we can do this even if the scheduler is suspended. */
2183 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2185 /* It is known that the task is in its ready list so
2186 * there is no need to check again and the port level
2187 * reset macro can be called directly. */
2188 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2192 mtCOVERAGE_TEST_MARKER();
2195 prvAddTaskToReadyList( pxTCB );
2199 /* It's possible that xYieldForTask was already set to pdTRUE because
2200 * its priority is being raised. However, since it is not in a ready list
2201 * we don't actually need to yield for it. */
2202 xYieldForTask = pdFALSE;
2205 #if ( configUSE_PREEMPTION == 1 )
2206 if( xYieldRequired != pdFALSE )
2208 prvYieldCore( xCoreID );
2210 else if( xYieldForTask != pdFALSE )
2212 prvYieldForTask( pxTCB, pdTRUE );
2216 mtCOVERAGE_TEST_MARKER();
2218 #endif /* if ( configUSE_PREEMPTION == 1 ) */
2220 /* Remove compiler warning about unused variables when the port
2221 * optimised task selection is not being used. */
2222 ( void ) uxPriorityUsedOnEntry;
2225 taskEXIT_CRITICAL();
2228 #endif /* INCLUDE_vTaskPrioritySet */
2229 /*-----------------------------------------------------------*/
2231 #if ( configNUM_CORES > 1 )
2232 #if ( configUSE_CORE_AFFINITY == 1 )
2234 void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2235 UBaseType_t uxCoreAffinityMask )
2240 taskENTER_CRITICAL();
2242 pxTCB = prvGetTCBFromHandle( xTask );
2244 pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2246 if( xSchedulerRunning != pdFALSE )
2248 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2250 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2252 if( ( uxCoreAffinityMask & ( 1 << xCoreID ) ) != 0 )
2254 prvYieldCore( xCoreID );
2259 taskEXIT_CRITICAL();
2262 #endif /* configUSE_CORE_AFFINITY */
2263 #endif /* if ( configNUM_CORES > 1 ) */
2264 /*-----------------------------------------------------------*/
2266 #if ( configNUM_CORES > 1 )
2267 #if ( configUSE_CORE_AFFINITY == 1 )
2269 UBaseType_t vTaskCoreAffinityGet( const TaskHandle_t xTask )
2272 UBaseType_t uxCoreAffinityMask;
2274 taskENTER_CRITICAL();
2276 pxTCB = prvGetTCBFromHandle( xTask );
2277 uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2279 taskEXIT_CRITICAL();
2281 return uxCoreAffinityMask;
2284 #endif /* configUSE_CORE_AFFINITY */
2285 #endif /* if ( configNUM_CORES > 1 ) */
2287 /*-----------------------------------------------------------*/
2289 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2291 void vTaskPreemptionDisable( const TaskHandle_t xTask )
2295 taskENTER_CRITICAL();
2297 pxTCB = prvGetTCBFromHandle( xTask );
2299 pxTCB->xPreemptionDisable = pdTRUE;
2301 taskEXIT_CRITICAL();
2304 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2305 /*-----------------------------------------------------------*/
2307 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2309 void vTaskPreemptionEnable( const TaskHandle_t xTask )
2314 taskENTER_CRITICAL();
2316 pxTCB = prvGetTCBFromHandle( xTask );
2318 pxTCB->xPreemptionDisable = pdFALSE;
2320 if( xSchedulerRunning != pdFALSE )
2322 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
2324 xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2325 prvYieldCore( xCoreID );
2329 taskEXIT_CRITICAL();
2332 #endif /* configUSE_TASK_PREEMPTION_DISABLE */
2333 /*-----------------------------------------------------------*/
2335 #if ( INCLUDE_vTaskSuspend == 1 )
2337 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2340 TaskRunning_t xTaskRunningOnCore;
2342 taskENTER_CRITICAL();
2344 /* If null is passed in here then it is the running task that is
2345 * being suspended. */
2346 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2348 traceTASK_SUSPEND( pxTCB );
2350 xTaskRunningOnCore = pxTCB->xTaskRunState;
2352 /* Remove task from the ready/delayed list and place in the
2353 * suspended list. */
2354 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2356 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2360 mtCOVERAGE_TEST_MARKER();
2363 /* Is the task waiting on an event also? */
2364 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2366 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2370 mtCOVERAGE_TEST_MARKER();
2373 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2375 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2379 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2381 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2383 /* The task was blocked to wait for a notification, but is
2384 * now suspended, so no notification was received. */
2385 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2389 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2391 if( xSchedulerRunning != pdFALSE )
2393 /* Reset the next expected unblock time in case it referred to the
2394 * task that is now in the Suspended state. */
2395 prvResetNextTaskUnblockTime();
2399 mtCOVERAGE_TEST_MARKER();
2402 if( taskTASK_IS_RUNNING( xTaskRunningOnCore ) )
2404 if( xSchedulerRunning != pdFALSE )
2406 if( xTaskRunningOnCore == portGET_CORE_ID() )
2408 /* The current task has just been suspended. */
2409 configASSERT( uxSchedulerSuspended == 0 );
2410 vTaskYieldWithinAPI();
2414 prvYieldCore( xTaskRunningOnCore );
2417 taskEXIT_CRITICAL();
2421 taskEXIT_CRITICAL();
2423 configASSERT( pxTCB == pxCurrentTCBs[ xTaskRunningOnCore ] );
2425 /* The scheduler is not running, but the task that was pointed
2426 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
2427 * must be adjusted to point to a different task. */
2428 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
2430 /* No other tasks are ready, so set the core's TCB back to
2431 * NULL so when the next task is created the core's TCB will
2432 * be able to be set to point to it no matter what its relative
2434 pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2435 pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2439 /* Attempt to switch in a new task. This could fail since the idle tasks
2440 * haven't been created yet. If it does then set the core's TCB back to
2442 if( prvSelectHighestPriorityTask( xTaskRunningOnCore ) == pdFALSE )
2444 pxTCB->xTaskRunState = taskTASK_NOT_RUNNING;
2445 pxCurrentTCBs[ xTaskRunningOnCore ] = NULL;
2452 taskEXIT_CRITICAL();
2454 } /* taskEXIT_CRITICAL() - already exited in one of three cases above */
2457 #endif /* INCLUDE_vTaskSuspend */
2458 /*-----------------------------------------------------------*/
2460 #if ( INCLUDE_vTaskSuspend == 1 )
2462 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
2464 BaseType_t xReturn = pdFALSE;
2465 const TCB_t * const pxTCB = xTask;
2467 /* Accesses xPendingReadyList so must be called from a critical section. */
2469 /* It does not make sense to check if the calling task is suspended. */
2470 configASSERT( xTask );
2472 /* Is the task being resumed actually in the suspended list? */
2473 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
2475 /* Has the task already been resumed from within an ISR? */
2476 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
2478 /* Is it in the suspended list because it is in the Suspended
2479 * state, or because is is blocked with no timeout? */
2480 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
2486 mtCOVERAGE_TEST_MARKER();
2491 mtCOVERAGE_TEST_MARKER();
2496 mtCOVERAGE_TEST_MARKER();
2500 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2502 #endif /* INCLUDE_vTaskSuspend */
2503 /*-----------------------------------------------------------*/
2505 #if ( INCLUDE_vTaskSuspend == 1 )
2507 void vTaskResume( TaskHandle_t xTaskToResume )
2509 TCB_t * const pxTCB = xTaskToResume;
2511 /* It does not make sense to resume the calling task. */
2512 configASSERT( xTaskToResume );
2514 /* The parameter cannot be NULL as it is impossible to resume the
2515 * currently executing task. It is also impossible to resume a task
2516 * that is actively running on another core but it is too dangerous
2517 * to check their run state here. Safer to get into a critical section
2518 * and check if it is actually suspended or not below. */
2521 taskENTER_CRITICAL();
2523 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2525 traceTASK_RESUME( pxTCB );
2527 /* The ready list can be accessed even if the scheduler is
2528 * suspended because this is inside a critical section. */
2529 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2530 prvAddTaskToReadyList( pxTCB );
2532 /* A higher priority task may have just been resumed. */
2533 #if ( configUSE_PREEMPTION == 1 )
2535 prvYieldForTask( pxTCB, pdTRUE );
2541 mtCOVERAGE_TEST_MARKER();
2544 taskEXIT_CRITICAL();
2548 mtCOVERAGE_TEST_MARKER();
2552 #endif /* INCLUDE_vTaskSuspend */
2554 /*-----------------------------------------------------------*/
2556 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
2558 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
2560 BaseType_t xYieldRequired = pdFALSE;
2561 TCB_t * const pxTCB = xTaskToResume;
2562 UBaseType_t uxSavedInterruptStatus;
2564 configASSERT( xTaskToResume );
2566 /* RTOS ports that support interrupt nesting have the concept of a
2567 * maximum system call (or maximum API call) interrupt priority.
2568 * Interrupts that are above the maximum system call priority are keep
2569 * permanently enabled, even when the RTOS kernel is in a critical section,
2570 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
2571 * is defined in FreeRTOSConfig.h then
2572 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2573 * failure if a FreeRTOS API function is called from an interrupt that has
2574 * been assigned a priority above the configured maximum system call
2575 * priority. Only FreeRTOS functions that end in FromISR can be called
2576 * from interrupts that have been assigned a priority at or (logically)
2577 * below the maximum system call interrupt priority. FreeRTOS maintains a
2578 * separate interrupt safe API to ensure interrupt entry is as fast and as
2579 * simple as possible. More information (albeit Cortex-M specific) is
2580 * provided on the following link:
2581 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2582 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2584 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2586 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
2588 traceTASK_RESUME_FROM_ISR( pxTCB );
2590 /* Check the ready lists can be accessed. */
2591 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2593 /* Ready lists can be accessed so move the task from the
2594 * suspended list to the ready list directly. */
2596 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2597 prvAddTaskToReadyList( pxTCB );
2601 /* The delayed or ready lists cannot be accessed so the task
2602 * is held in the pending ready list until the scheduler is
2604 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
2607 #if ( configUSE_PREEMPTION == 1 )
2608 prvYieldForTask( pxTCB, pdTRUE );
2610 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
2612 xYieldRequired = pdTRUE;
2618 mtCOVERAGE_TEST_MARKER();
2621 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2623 return xYieldRequired;
2626 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
2627 /*-----------------------------------------------------------*/
2629 static BaseType_t prvCreateIdleTasks( void )
2631 BaseType_t xReturn = pdPASS;
2633 char cIdleName[ configMAX_TASK_NAME_LEN ];
2635 /* Add each idle task at the lowest priority. */
2636 for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUM_CORES; xCoreID++ )
2640 if( xReturn == pdFAIL )
2646 mtCOVERAGE_TEST_MARKER();
2649 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
2651 cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
2653 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
2654 * configMAX_TASK_NAME_LEN characters just in case the memory after the
2655 * string is not accessible (extremely unlikely). */
2656 if( cIdleName[ x ] == ( char ) 0x00 )
2662 mtCOVERAGE_TEST_MARKER();
2666 /* Append the idle task number to the end of the name if there is space */
2667 if( x < configMAX_TASK_NAME_LEN )
2669 cIdleName[ x++ ] = xCoreID + '0';
2671 /* And append a null character if there is space */
2672 if( x < configMAX_TASK_NAME_LEN )
2674 cIdleName[ x ] = '\0';
2678 mtCOVERAGE_TEST_MARKER();
2683 mtCOVERAGE_TEST_MARKER();
2686 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
2690 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
2691 StackType_t * pxIdleTaskStackBuffer = NULL;
2692 uint32_t ulIdleTaskStackSize;
2694 /* The Idle task is created using user provided RAM - obtain the
2695 * address of the RAM then create the idle task. */
2696 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
2697 xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
2699 ulIdleTaskStackSize,
2700 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
2701 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2702 pxIdleTaskStackBuffer,
2703 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2705 #if ( configNUM_CORES > 1 )
2708 static StaticTask_t xIdleTCBBuffers[ configNUM_CORES - 1 ];
2709 static StackType_t xIdleTaskStackBuffers[ configNUM_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
2711 xIdleTaskHandle[ xCoreID ] = xTaskCreateStatic( prvMinimalIdleTask,
2713 configMINIMAL_STACK_SIZE,
2714 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
2715 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2716 xIdleTaskStackBuffers[ xCoreID - 1 ],
2717 &xIdleTCBBuffers[ xCoreID - 1 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2719 #endif /* if ( configNUM_CORES > 1 ) */
2721 if( xIdleTaskHandle[ xCoreID ] != NULL )
2730 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
2734 /* The Idle task is being created using dynamically allocated RAM. */
2735 xReturn = xTaskCreate( prvIdleTask,
2737 configMINIMAL_STACK_SIZE,
2739 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2740 &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2743 #if ( configNUM_CORES > 1 )
2746 xReturn = xTaskCreate( prvMinimalIdleTask,
2748 configMINIMAL_STACK_SIZE,
2750 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2751 &xIdleTaskHandle[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2755 #endif /* configSUPPORT_STATIC_ALLOCATION */
2760 void vTaskStartScheduler( void )
2764 #if ( configUSE_TIMERS == 1 )
2766 xReturn = xTimerCreateTimerTask();
2768 #endif /* configUSE_TIMERS */
2770 xReturn = prvCreateIdleTasks();
2772 if( xReturn == pdPASS )
2774 /* freertos_tasks_c_additions_init() should only be called if the user
2775 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
2776 * the only macro called by the function. */
2777 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
2779 freertos_tasks_c_additions_init();
2783 /* Interrupts are turned off here, to ensure a tick does not occur
2784 * before or during the call to xPortStartScheduler(). The stacks of
2785 * the created tasks contain a status word with interrupts switched on
2786 * so interrupts will automatically get re-enabled when the first task
2788 portDISABLE_INTERRUPTS();
2790 #if ( configUSE_NEWLIB_REENTRANT == 1 )
2792 /* Switch Newlib's _impure_ptr variable to point to the _reent
2793 * structure specific to the task that will run first.
2794 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
2795 * for additional information. */
2796 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
2798 #endif /* configUSE_NEWLIB_REENTRANT */
2800 xNextTaskUnblockTime = portMAX_DELAY;
2801 xSchedulerRunning = pdTRUE;
2802 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
2804 /* If configGENERATE_RUN_TIME_STATS is defined then the following
2805 * macro must be defined to configure the timer/counter used to generate
2806 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
2807 * is set to 0 and the following line fails to build then ensure you do not
2808 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
2809 * FreeRTOSConfig.h file. */
2810 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
2812 traceTASK_SWITCHED_IN();
2814 /* Setting up the timer tick is hardware specific and thus in the
2815 * portable interface. */
2816 if( xPortStartScheduler() != pdFALSE )
2818 /* Should not reach here as if the scheduler is running the
2819 * function will not return. */
2823 /* Should only reach here if a task calls xTaskEndScheduler(). */
2828 /* This line will only be reached if the kernel could not be started,
2829 * because there was not enough FreeRTOS heap to create the idle task
2830 * or the timer task. */
2831 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
2834 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
2835 * meaning xIdleTaskHandle is not used anywhere else. */
2836 ( void ) xIdleTaskHandle;
2838 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
2839 * from getting optimized out as it is no longer used by the kernel. */
2840 ( void ) uxTopUsedPriority;
2842 /*-----------------------------------------------------------*/
2844 void vTaskEndScheduler( void )
2846 /* Stop the scheduler interrupts and call the portable scheduler end
2847 * routine so the original ISRs can be restored if necessary. The port
2848 * layer must ensure interrupts enable bit is left in the correct state. */
2849 portDISABLE_INTERRUPTS();
2850 xSchedulerRunning = pdFALSE;
2851 vPortEndScheduler();
2853 /*----------------------------------------------------------*/
2855 void vTaskSuspendAll( void )
2857 UBaseType_t ulState;
2859 /* This must only be called from within a task */
2860 portASSERT_IF_IN_ISR();
2862 if( xSchedulerRunning != pdFALSE )
2864 /* writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
2865 * We must disable interrupts before we grab the locks in the event that this task is
2866 * interrupted and switches context before incrementing uxSchedulerSuspended.
2867 * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
2868 * uxSchedulerSuspended since that will prevent context switches. */
2869 ulState = portDISABLE_INTERRUPTS();
2871 /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
2872 * do not otherwise exhibit real time behaviour. */
2873 portSOFTWARE_BARRIER();
2875 portGET_TASK_LOCK();
2878 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
2879 * is used to allow calls to vTaskSuspendAll() to nest. */
2880 ++uxSchedulerSuspended;
2881 portRELEASE_ISR_LOCK();
2883 if( ( uxSchedulerSuspended == 1U ) && ( pxCurrentTCB->uxCriticalNesting == 0U ) )
2885 prvCheckForRunStateChange();
2888 portRESTORE_INTERRUPTS( ulState );
2892 mtCOVERAGE_TEST_MARKER();
2895 /*----------------------------------------------------------*/
2897 #if ( configUSE_TICKLESS_IDLE != 0 )
2899 static TickType_t prvGetExpectedIdleTime( void )
2902 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
2904 /* uxHigherPriorityReadyTasks takes care of the case where
2905 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
2906 * task that are in the Ready state, even though the idle task is
2908 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
2910 if( uxTopReadyPriority > tskIDLE_PRIORITY )
2912 uxHigherPriorityReadyTasks = pdTRUE;
2917 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
2919 /* When port optimised task selection is used the uxTopReadyPriority
2920 * variable is used as a bit map. If bits other than the least
2921 * significant bit are set then there are tasks that have a priority
2922 * above the idle priority that are in the Ready state. This takes
2923 * care of the case where the co-operative scheduler is in use. */
2924 if( uxTopReadyPriority > uxLeastSignificantBit )
2926 uxHigherPriorityReadyTasks = pdTRUE;
2929 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
2931 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
2935 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
2937 /* There are other idle priority tasks in the ready state. If
2938 * time slicing is used then the very next tick interrupt must be
2942 else if( uxHigherPriorityReadyTasks != pdFALSE )
2944 /* There are tasks in the Ready state that have a priority above the
2945 * idle priority. This path can only be reached if
2946 * configUSE_PREEMPTION is 0. */
2951 xReturn = xNextTaskUnblockTime - xTickCount;
2957 #endif /* configUSE_TICKLESS_IDLE */
2958 /*----------------------------------------------------------*/
2960 BaseType_t xTaskResumeAll( void )
2962 TCB_t * pxTCB = NULL;
2963 BaseType_t xAlreadyYielded = pdFALSE;
2965 if( xSchedulerRunning != pdFALSE )
2967 /* It is possible that an ISR caused a task to be removed from an event
2968 * list while the scheduler was suspended. If this was the case then the
2969 * removed task will have been added to the xPendingReadyList. Once the
2970 * scheduler has been resumed it is safe to move all the pending ready
2971 * tasks from this list into their appropriate ready list. */
2972 taskENTER_CRITICAL();
2976 xCoreID = portGET_CORE_ID();
2978 /* If uxSchedulerSuspended is zero then this function does not match a
2979 * previous call to vTaskSuspendAll(). */
2980 configASSERT( uxSchedulerSuspended );
2982 --uxSchedulerSuspended;
2983 portRELEASE_TASK_LOCK();
2985 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2987 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
2989 /* Move any readied tasks from the pending list into the
2990 * appropriate ready list. */
2991 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
2993 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. */
2994 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2995 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2996 prvAddTaskToReadyList( pxTCB );
2998 /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
2999 * If the current core yielded then vTaskSwitchContext() has already been called
3000 * which sets xYieldPendings for the current core to pdTRUE. */
3005 /* A task was unblocked while the scheduler was suspended,
3006 * which may have prevented the next unblock time from being
3007 * re-calculated, in which case re-calculate it now. Mainly
3008 * important for low power tickless implementations, where
3009 * this can prevent an unnecessary exit from low power
3011 prvResetNextTaskUnblockTime();
3014 /* If any ticks occurred while the scheduler was suspended then
3015 * they should be processed now. This ensures the tick count does
3016 * not slip, and that any delayed tasks are resumed at the correct
3019 * It should be safe to call xTaskIncrementTick here from any core
3020 * since we are in a critical section and xTaskIncrementTick itself
3021 * protects itself within a critical section. Suspending the scheduler
3022 * from any core causes xTaskIncrementTick to increment uxPendedCounts.*/
3024 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3026 if( xPendedCounts > ( TickType_t ) 0U )
3030 if( xTaskIncrementTick() != pdFALSE )
3032 /* other cores are interrupted from
3033 * within xTaskIncrementTick(). */
3034 xYieldPendings[ xCoreID ] = pdTRUE;
3038 mtCOVERAGE_TEST_MARKER();
3042 } while( xPendedCounts > ( TickType_t ) 0U );
3048 mtCOVERAGE_TEST_MARKER();
3052 if( xYieldPendings[ xCoreID ] != pdFALSE )
3054 /* If xYieldPendings is true then taskEXIT_CRITICAL()
3055 * will yield, so make sure we return true to let the
3056 * caller know a yield has already happened. */
3057 xAlreadyYielded = pdTRUE;
3063 mtCOVERAGE_TEST_MARKER();
3066 taskEXIT_CRITICAL();
3070 mtCOVERAGE_TEST_MARKER();
3073 return xAlreadyYielded;
3075 /*-----------------------------------------------------------*/
3077 TickType_t xTaskGetTickCount( void )
3081 /* Critical section required if running on a 16 bit processor. */
3082 portTICK_TYPE_ENTER_CRITICAL();
3084 xTicks = xTickCount;
3086 portTICK_TYPE_EXIT_CRITICAL();
3090 /*-----------------------------------------------------------*/
3092 TickType_t xTaskGetTickCountFromISR( void )
3095 UBaseType_t uxSavedInterruptStatus;
3097 /* RTOS ports that support interrupt nesting have the concept of a maximum
3098 * system call (or maximum API call) interrupt priority. Interrupts that are
3099 * above the maximum system call priority are kept permanently enabled, even
3100 * when the RTOS kernel is in a critical section, but cannot make any calls to
3101 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
3102 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3103 * failure if a FreeRTOS API function is called from an interrupt that has been
3104 * assigned a priority above the configured maximum system call priority.
3105 * Only FreeRTOS functions that end in FromISR can be called from interrupts
3106 * that have been assigned a priority at or (logically) below the maximum
3107 * system call interrupt priority. FreeRTOS maintains a separate interrupt
3108 * safe API to ensure interrupt entry is as fast and as simple as possible.
3109 * More information (albeit Cortex-M specific) is provided on the following
3110 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3111 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3113 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
3115 xReturn = xTickCount;
3117 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3121 /*-----------------------------------------------------------*/
3123 UBaseType_t uxTaskGetNumberOfTasks( void )
3125 /* A critical section is not required because the variables are of type
3127 return uxCurrentNumberOfTasks;
3129 /*-----------------------------------------------------------*/
3131 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3135 /* If null is passed in here then the name of the calling task is being
3137 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3138 configASSERT( pxTCB );
3139 return &( pxTCB->pcTaskName[ 0 ] );
3141 /*-----------------------------------------------------------*/
3143 #if ( INCLUDE_xTaskGetHandle == 1 )
3145 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3146 const char pcNameToQuery[] )
3148 TCB_t * pxNextTCB, * pxFirstTCB, * pxReturn = NULL;
3151 BaseType_t xBreakLoop;
3153 /* This function is called with the scheduler suspended. */
3155 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3157 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. */
3161 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. */
3163 /* Check each character in the name looking for a match or
3165 xBreakLoop = pdFALSE;
3167 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3169 cNextChar = pxNextTCB->pcTaskName[ x ];
3171 if( cNextChar != pcNameToQuery[ x ] )
3173 /* Characters didn't match. */
3174 xBreakLoop = pdTRUE;
3176 else if( cNextChar == ( char ) 0x00 )
3178 /* Both strings terminated, a match must have been
3180 pxReturn = pxNextTCB;
3181 xBreakLoop = pdTRUE;
3185 mtCOVERAGE_TEST_MARKER();
3188 if( xBreakLoop != pdFALSE )
3194 if( pxReturn != NULL )
3196 /* The handle has been found. */
3199 } while( pxNextTCB != pxFirstTCB );
3203 mtCOVERAGE_TEST_MARKER();
3209 #endif /* INCLUDE_xTaskGetHandle */
3210 /*-----------------------------------------------------------*/
3212 #if ( INCLUDE_xTaskGetHandle == 1 )
3214 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3216 UBaseType_t uxQueue = configMAX_PRIORITIES;
3219 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
3220 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
3224 /* Search the ready lists. */
3228 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
3232 /* Found the handle. */
3235 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3237 /* Search the delayed lists. */
3240 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
3245 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
3248 #if ( INCLUDE_vTaskSuspend == 1 )
3252 /* Search the suspended list. */
3253 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
3258 #if ( INCLUDE_vTaskDelete == 1 )
3262 /* Search the deleted list. */
3263 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
3268 ( void ) xTaskResumeAll();
3273 #endif /* INCLUDE_xTaskGetHandle */
3274 /*-----------------------------------------------------------*/
3276 #if ( configUSE_TRACE_FACILITY == 1 )
3278 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
3279 const UBaseType_t uxArraySize,
3280 uint32_t * const pulTotalRunTime )
3282 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
3286 /* Is there a space in the array for each task in the system? */
3287 if( uxArraySize >= uxCurrentNumberOfTasks )
3289 /* Fill in an TaskStatus_t structure with information on each
3290 * task in the Ready state. */
3294 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
3295 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3297 /* Fill in an TaskStatus_t structure with information on each
3298 * task in the Blocked state. */
3299 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
3300 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
3302 #if ( INCLUDE_vTaskDelete == 1 )
3304 /* Fill in an TaskStatus_t structure with information on
3305 * each task that has been deleted but not yet cleaned up. */
3306 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
3310 #if ( INCLUDE_vTaskSuspend == 1 )
3312 /* Fill in an TaskStatus_t structure with information on
3313 * each task in the Suspended state. */
3314 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
3318 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3320 if( pulTotalRunTime != NULL )
3322 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3323 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
3325 *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3329 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3331 if( pulTotalRunTime != NULL )
3333 *pulTotalRunTime = 0;
3336 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
3340 mtCOVERAGE_TEST_MARKER();
3343 ( void ) xTaskResumeAll();
3348 #endif /* configUSE_TRACE_FACILITY */
3349 /*----------------------------------------------------------*/
3351 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
3353 TaskHandle_t * xTaskGetIdleTaskHandle( void )
3355 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
3356 * started, then xIdleTaskHandle will be NULL. */
3357 configASSERT( ( xIdleTaskHandle != NULL ) );
3358 return &( xIdleTaskHandle[ 0 ] );
3361 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
3362 /*----------------------------------------------------------*/
3364 /* This conditional compilation should use inequality to 0, not equality to 1.
3365 * This is to ensure vTaskStepTick() is available when user defined low power mode
3366 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
3368 #if ( configUSE_TICKLESS_IDLE != 0 )
3370 void vTaskStepTick( const TickType_t xTicksToJump )
3372 /* Correct the tick count value after a period during which the tick
3373 * was suppressed. Note this does *not* call the tick hook function for
3374 * each stepped tick. */
3375 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
3376 xTickCount += xTicksToJump;
3377 traceINCREASE_TICK_COUNT( xTicksToJump );
3380 #endif /* configUSE_TICKLESS_IDLE */
3381 /*----------------------------------------------------------*/
3383 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
3385 BaseType_t xYieldOccurred;
3387 /* Must not be called with the scheduler suspended as the implementation
3388 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
3389 configASSERT( uxSchedulerSuspended == 0 );
3391 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
3392 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
3394 xPendedTicks += xTicksToCatchUp;
3395 xYieldOccurred = xTaskResumeAll();
3397 return xYieldOccurred;
3399 /*----------------------------------------------------------*/
3401 #if ( INCLUDE_xTaskAbortDelay == 1 )
3403 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
3405 TCB_t * pxTCB = xTask;
3408 configASSERT( pxTCB );
3412 /* A task can only be prematurely removed from the Blocked state if
3413 * it is actually in the Blocked state. */
3414 if( eTaskGetState( xTask ) == eBlocked )
3418 /* Remove the reference to the task from the blocked list. An
3419 * interrupt won't touch the xStateListItem because the
3420 * scheduler is suspended. */
3421 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3423 /* Is the task waiting on an event also? If so remove it from
3424 * the event list too. Interrupts can touch the event list item,
3425 * even though the scheduler is suspended, so a critical section
3427 taskENTER_CRITICAL();
3429 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3431 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3433 /* This lets the task know it was forcibly removed from the
3434 * blocked state so it should not re-evaluate its block time and
3435 * then block again. */
3436 pxTCB->ucDelayAborted = pdTRUE;
3440 mtCOVERAGE_TEST_MARKER();
3443 taskEXIT_CRITICAL();
3445 /* Place the unblocked task into the appropriate ready list. */
3446 prvAddTaskToReadyList( pxTCB );
3448 /* A task being unblocked cannot cause an immediate context
3449 * switch if preemption is turned off. */
3450 #if ( configUSE_PREEMPTION == 1 )
3452 taskENTER_CRITICAL();
3454 prvYieldForTask( pxTCB, pdFALSE );
3456 taskEXIT_CRITICAL();
3458 #endif /* configUSE_PREEMPTION */
3465 ( void ) xTaskResumeAll();
3470 #endif /* INCLUDE_xTaskAbortDelay */
3471 /*----------------------------------------------------------*/
3473 BaseType_t xTaskIncrementTick( void )
3476 TickType_t xItemValue;
3477 BaseType_t xSwitchRequired = pdFALSE;
3479 #if ( configUSE_PREEMPTION == 1 )
3481 BaseType_t xCoreYieldList[ configNUM_CORES ] = { pdFALSE };
3482 #endif /* configUSE_PREEMPTION */
3484 taskENTER_CRITICAL();
3486 /* Called by the portable layer each time a tick interrupt occurs.
3487 * Increments the tick then checks to see if the new tick value will cause any
3488 * tasks to be unblocked. */
3489 traceTASK_INCREMENT_TICK( xTickCount );
3491 /* Tick increment should occur on every kernel timer event. Core 0 has the
3492 * responsibility to increment the tick, or increment the pended ticks if the
3493 * scheduler is suspended. If pended ticks is greater than zero, the core that
3494 * calls xTaskResumeAll has the responsibility to increment the tick. */
3495 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3497 /* Minor optimisation. The tick count cannot change in this
3499 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
3501 /* Increment the RTOS tick, switching the delayed and overflowed
3502 * delayed lists if it wraps to 0. */
3503 xTickCount = xConstTickCount;
3505 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
3507 taskSWITCH_DELAYED_LISTS();
3511 mtCOVERAGE_TEST_MARKER();
3514 /* See if this tick has made a timeout expire. Tasks are stored in
3515 * the queue in the order of their wake time - meaning once one task
3516 * has been found whose block time has not expired there is no need to
3517 * look any further down the list. */
3518 if( xConstTickCount >= xNextTaskUnblockTime )
3522 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
3524 /* The delayed list is empty. Set xNextTaskUnblockTime
3525 * to the maximum possible value so it is extremely
3527 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
3528 * next time through. */
3529 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
3534 /* The delayed list is not empty, get the value of the
3535 * item at the head of the delayed list. This is the time
3536 * at which the task at the head of the delayed list must
3537 * be removed from the Blocked state. */
3538 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. */
3539 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
3541 if( xConstTickCount < xItemValue )
3543 /* It is not time to unblock this item yet, but the
3544 * item value is the time at which the task at the head
3545 * of the blocked list must be removed from the Blocked
3546 * state - so record the item value in
3547 * xNextTaskUnblockTime. */
3548 xNextTaskUnblockTime = xItemValue;
3549 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
3553 mtCOVERAGE_TEST_MARKER();
3556 /* It is time to remove the item from the Blocked state. */
3557 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3559 /* Is the task waiting on an event also? If so remove
3560 * it from the event list. */
3561 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3563 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
3567 mtCOVERAGE_TEST_MARKER();
3570 /* Place the unblocked task into the appropriate ready
3572 prvAddTaskToReadyList( pxTCB );
3574 /* A task being unblocked cannot cause an immediate
3575 * context switch if preemption is turned off. */
3576 #if ( configUSE_PREEMPTION == 1 )
3578 prvYieldForTask( pxTCB, pdTRUE );
3580 #endif /* configUSE_PREEMPTION */
3585 /* Tasks of equal priority to the currently running task will share
3586 * processing time (time slice) if preemption is on, and the application
3587 * writer has not explicitly turned time slicing off. */
3588 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
3590 /* TODO: If there are fewer "non-IDLE" READY tasks than cores, do not
3591 * force a context switch that would just shuffle tasks around cores */
3592 /* TODO: There are certainly better ways of doing this that would reduce
3593 * the number of interrupts and also potentially help prevent tasks from
3594 * moving between cores as often. This, however, works for now. */
3595 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3597 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ x ]->uxPriority ] ) ) > ( UBaseType_t ) 1 )
3599 xCoreYieldList[ x ] = pdTRUE;
3603 mtCOVERAGE_TEST_MARKER();
3607 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
3609 #if ( configUSE_TICK_HOOK == 1 )
3611 /* Guard against the tick hook being called when the pended tick
3612 * count is being unwound (when the scheduler is being unlocked). */
3613 if( xPendedTicks == ( TickType_t ) 0 )
3615 vApplicationTickHook();
3619 mtCOVERAGE_TEST_MARKER();
3622 #endif /* configUSE_TICK_HOOK */
3624 #if ( configUSE_PREEMPTION == 1 )
3626 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3628 if( xYieldPendings[ x ] != pdFALSE )
3630 xCoreYieldList[ x ] = pdTRUE;
3634 mtCOVERAGE_TEST_MARKER();
3638 #endif /* configUSE_PREEMPTION */
3640 #if ( configUSE_PREEMPTION == 1 )
3644 xCoreID = portGET_CORE_ID();
3646 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configNUM_CORES; x++ )
3648 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
3649 if( pxCurrentTCBs[ x ]->xPreemptionDisable == pdFALSE )
3652 if( xCoreYieldList[ x ] != pdFALSE )
3656 xSwitchRequired = pdTRUE;
3665 mtCOVERAGE_TEST_MARKER();
3670 #endif /* configUSE_PREEMPTION */
3676 /* The tick hook gets called at regular intervals, even if the
3677 * scheduler is locked. */
3678 #if ( configUSE_TICK_HOOK == 1 )
3680 vApplicationTickHook();
3685 taskEXIT_CRITICAL();
3687 return xSwitchRequired;
3689 /*-----------------------------------------------------------*/
3691 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3693 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
3694 TaskHookFunction_t pxHookFunction )
3698 /* If xTask is NULL then it is the task hook of the calling task that is
3702 xTCB = ( TCB_t * ) pxCurrentTCB;
3709 /* Save the hook function in the TCB. A critical section is required as
3710 * the value can be accessed from an interrupt. */
3711 taskENTER_CRITICAL();
3713 xTCB->pxTaskTag = pxHookFunction;
3715 taskEXIT_CRITICAL();
3718 #endif /* configUSE_APPLICATION_TASK_TAG */
3719 /*-----------------------------------------------------------*/
3721 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3723 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
3726 TaskHookFunction_t xReturn;
3728 /* If xTask is NULL then set the calling task's hook. */
3729 pxTCB = prvGetTCBFromHandle( xTask );
3731 /* Save the hook function in the TCB. A critical section is required as
3732 * the value can be accessed from an interrupt. */
3733 taskENTER_CRITICAL();
3735 xReturn = pxTCB->pxTaskTag;
3737 taskEXIT_CRITICAL();
3742 #endif /* configUSE_APPLICATION_TASK_TAG */
3743 /*-----------------------------------------------------------*/
3745 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3747 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
3750 TaskHookFunction_t xReturn;
3751 UBaseType_t uxSavedInterruptStatus;
3753 /* If xTask is NULL then set the calling task's hook. */
3754 pxTCB = prvGetTCBFromHandle( xTask );
3756 /* Save the hook function in the TCB. A critical section is required as
3757 * the value can be accessed from an interrupt. */
3758 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
3760 xReturn = pxTCB->pxTaskTag;
3762 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3767 #endif /* configUSE_APPLICATION_TASK_TAG */
3768 /*-----------------------------------------------------------*/
3770 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
3772 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
3773 void * pvParameter )
3778 /* If xTask is NULL then we are calling our own task hook. */
3781 xTCB = pxCurrentTCB;
3788 if( xTCB->pxTaskTag != NULL )
3790 xReturn = xTCB->pxTaskTag( pvParameter );
3800 #endif /* configUSE_APPLICATION_TASK_TAG */
3801 /*-----------------------------------------------------------*/
3803 void vTaskSwitchContext( BaseType_t xCoreID )
3805 /* Acquire both locks:
3806 * - The ISR lock protects the ready list from simultaneous access by
3807 * both other ISRs and tasks.
3808 * - We also take the task lock to pause here in case another core has
3809 * suspended the scheduler. We don't want to simply set xYieldPending
3810 * and move on if another core suspended the scheduler. We should only
3811 * do that if the current core has suspended the scheduler. */
3813 portGET_TASK_LOCK(); /* Must always acquire the task lock first */
3816 /* vTaskSwitchContext() must never be called from within a critical section.
3817 * This is not necessarily true for vanilla FreeRTOS, but it is for this SMP port. */
3818 configASSERT( pxCurrentTCB->uxCriticalNesting == 0 );
3820 if( uxSchedulerSuspended != ( UBaseType_t ) pdFALSE )
3822 /* The scheduler is currently suspended - do not allow a context
3824 xYieldPendings[ xCoreID ] = pdTRUE;
3828 xYieldPendings[ xCoreID ] = pdFALSE;
3829 traceTASK_SWITCHED_OUT();
3831 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3833 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3834 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
3836 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3839 /* Add the amount of time the task has been running to the
3840 * accumulated time so far. The time the task started running was
3841 * stored in ulTaskSwitchedInTime. Note that there is no overflow
3842 * protection here so count values are only valid until the timer
3843 * overflows. The guard against negative values is to protect
3844 * against suspect run time stat counter implementations - which
3845 * are provided by the application, not the kernel. */
3846 if( ulTotalRunTime > ulTaskSwitchedInTime )
3848 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime );
3852 mtCOVERAGE_TEST_MARKER();
3855 ulTaskSwitchedInTime = ulTotalRunTime;
3857 #endif /* configGENERATE_RUN_TIME_STATS */
3859 /* Check for stack overflow, if configured. */
3860 taskCHECK_FOR_STACK_OVERFLOW();
3862 /* Before the currently running task is switched out, save its errno. */
3863 #if ( configUSE_POSIX_ERRNO == 1 )
3865 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
3869 /* Select a new task to run using either the generic C or port
3870 * optimised asm code. */
3871 ( void ) prvSelectHighestPriorityTask( xCoreID );
3872 traceTASK_SWITCHED_IN();
3874 /* After the new task is switched in, update the global errno. */
3875 #if ( configUSE_POSIX_ERRNO == 1 )
3877 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
3881 #if ( configUSE_NEWLIB_REENTRANT == 1 )
3883 /* Switch Newlib's _impure_ptr variable to point to the _reent
3884 * structure specific to this task.
3885 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
3886 * for additional information. */
3887 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
3889 #endif /* configUSE_NEWLIB_REENTRANT */
3892 portRELEASE_ISR_LOCK();
3893 portRELEASE_TASK_LOCK();
3895 /*-----------------------------------------------------------*/
3897 void vTaskPlaceOnEventList( List_t * const pxEventList,
3898 const TickType_t xTicksToWait )
3900 configASSERT( pxEventList );
3902 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
3903 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
3905 /* Place the event list item of the TCB in the appropriate event list.
3906 * This is placed in the list in priority order so the highest priority task
3907 * is the first to be woken by the event. The queue that contains the event
3908 * list is locked, preventing simultaneous access from interrupts. */
3909 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3911 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3913 /*-----------------------------------------------------------*/
3915 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
3916 const TickType_t xItemValue,
3917 const TickType_t xTicksToWait )
3919 configASSERT( pxEventList );
3921 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3922 * the event groups implementation. */
3923 configASSERT( uxSchedulerSuspended != 0 );
3925 /* Store the item value in the event list item. It is safe to access the
3926 * event list item here as interrupts won't access the event list item of a
3927 * task that is not in the Blocked state. */
3928 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
3930 /* Place the event list item of the TCB at the end of the appropriate event
3931 * list. It is safe to access the event list here because it is part of an
3932 * event group implementation - and interrupts don't access event groups
3933 * directly (instead they access them indirectly by pending function calls to
3934 * the task level). */
3935 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3937 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3939 /*-----------------------------------------------------------*/
3941 #if ( configUSE_TIMERS == 1 )
3943 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
3944 TickType_t xTicksToWait,
3945 const BaseType_t xWaitIndefinitely )
3947 configASSERT( pxEventList );
3949 /* This function should not be called by application code hence the
3950 * 'Restricted' in its name. It is not part of the public API. It is
3951 * designed for use by kernel code, and has special calling requirements -
3952 * it should be called with the scheduler suspended. */
3955 /* Place the event list item of the TCB in the appropriate event list.
3956 * In this case it is assume that this is the only task that is going to
3957 * be waiting on this event list, so the faster vListInsertEnd() function
3958 * can be used in place of vListInsert. */
3959 vListInsertEnd( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3961 /* If the task should block indefinitely then set the block time to a
3962 * value that will be recognised as an indefinite delay inside the
3963 * prvAddCurrentTaskToDelayedList() function. */
3964 if( xWaitIndefinitely != pdFALSE )
3966 xTicksToWait = portMAX_DELAY;
3969 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
3970 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
3973 #endif /* configUSE_TIMERS */
3974 /*-----------------------------------------------------------*/
3976 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
3978 TCB_t * pxUnblockedTCB;
3981 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
3982 * called from a critical section within an ISR. */
3984 /* The event list is sorted in priority order, so the first in the list can
3985 * be removed as it is known to be the highest priority. Remove the TCB from
3986 * the delayed list, and add it to the ready list.
3988 * If an event is for a queue that is locked then this function will never
3989 * get called - the lock count on the queue will get modified instead. This
3990 * means exclusive access to the event list is guaranteed here.
3992 * This function assumes that a check has already been made to ensure that
3993 * pxEventList is not empty. */
3994 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. */
3995 configASSERT( pxUnblockedTCB );
3996 ( void ) uxListRemove( &( pxUnblockedTCB->xEventListItem ) );
3998 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4000 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
4001 prvAddTaskToReadyList( pxUnblockedTCB );
4003 #if ( configUSE_TICKLESS_IDLE != 0 )
4005 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4006 * might be set to the blocked task's time out time. If the task is
4007 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4008 * normally left unchanged, because it is automatically reset to a new
4009 * value when the tick count equals xNextTaskUnblockTime. However if
4010 * tickless idling is used it might be more important to enter sleep mode
4011 * at the earliest possible time - so reset xNextTaskUnblockTime here to
4012 * ensure it is updated at the earliest possible time. */
4013 prvResetNextTaskUnblockTime();
4019 /* The delayed and ready lists cannot be accessed, so hold this task
4020 * pending until the scheduler is resumed. */
4021 vListInsertEnd( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
4025 #if ( configUSE_PREEMPTION == 1 )
4026 prvYieldForTask( pxUnblockedTCB, pdFALSE );
4028 if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
4036 /*-----------------------------------------------------------*/
4038 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
4039 const TickType_t xItemValue )
4041 TCB_t * pxUnblockedTCB;
4043 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
4044 * the event flags implementation. */
4045 configASSERT( uxSchedulerSuspended != pdFALSE );
4047 /* Store the new item value in the event list. */
4048 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4050 /* Remove the event list form the event flag. Interrupts do not access
4052 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. */
4053 configASSERT( pxUnblockedTCB );
4054 ( void ) uxListRemove( pxEventListItem );
4056 #if ( configUSE_TICKLESS_IDLE != 0 )
4058 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
4059 * might be set to the blocked task's time out time. If the task is
4060 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
4061 * normally left unchanged, because it is automatically reset to a new
4062 * value when the tick count equals xNextTaskUnblockTime. However if
4063 * tickless idling is used it might be more important to enter sleep mode
4064 * at the earliest possible time - so reset xNextTaskUnblockTime here to
4065 * ensure it is updated at the earliest possible time. */
4066 prvResetNextTaskUnblockTime();
4070 /* Remove the task from the delayed list and add it to the ready list. The
4071 * scheduler is suspended so interrupts will not be accessing the ready
4073 ( void ) uxListRemove( &( pxUnblockedTCB->xStateListItem ) );
4074 prvAddTaskToReadyList( pxUnblockedTCB );
4076 #if ( configUSE_PREEMPTION == 1 )
4077 taskENTER_CRITICAL();
4079 prvYieldForTask( pxUnblockedTCB, pdFALSE );
4081 taskEXIT_CRITICAL();
4084 /*-----------------------------------------------------------*/
4086 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
4088 configASSERT( pxTimeOut );
4089 taskENTER_CRITICAL();
4091 pxTimeOut->xOverflowCount = xNumOfOverflows;
4092 pxTimeOut->xTimeOnEntering = xTickCount;
4094 taskEXIT_CRITICAL();
4096 /*-----------------------------------------------------------*/
4098 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
4100 /* For internal use only as it does not use a critical section. */
4101 pxTimeOut->xOverflowCount = xNumOfOverflows;
4102 pxTimeOut->xTimeOnEntering = xTickCount;
4104 /*-----------------------------------------------------------*/
4106 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
4107 TickType_t * const pxTicksToWait )
4111 configASSERT( pxTimeOut );
4112 configASSERT( pxTicksToWait );
4114 taskENTER_CRITICAL();
4116 /* Minor optimisation. The tick count cannot change in this block. */
4117 const TickType_t xConstTickCount = xTickCount;
4118 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
4120 #if ( INCLUDE_xTaskAbortDelay == 1 )
4121 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
4123 /* The delay was aborted, which is not the same as a time out,
4124 * but has the same result. */
4125 pxCurrentTCB->ucDelayAborted = pdFALSE;
4131 #if ( INCLUDE_vTaskSuspend == 1 )
4132 if( *pxTicksToWait == portMAX_DELAY )
4134 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
4135 * specified is the maximum block time then the task should block
4136 * indefinitely, and therefore never time out. */
4142 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
4144 /* The tick count is greater than the time at which
4145 * vTaskSetTimeout() was called, but has also overflowed since
4146 * vTaskSetTimeOut() was called. It must have wrapped all the way
4147 * around and gone past again. This passed since vTaskSetTimeout()
4150 *pxTicksToWait = ( TickType_t ) 0;
4152 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
4154 /* Not a genuine timeout. Adjust parameters for time remaining. */
4155 *pxTicksToWait -= xElapsedTime;
4156 vTaskInternalSetTimeOutState( pxTimeOut );
4161 *pxTicksToWait = ( TickType_t ) 0;
4165 taskEXIT_CRITICAL();
4169 /*-----------------------------------------------------------*/
4171 void vTaskMissedYield( void )
4173 /* Must be called from within a critical section */
4174 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
4176 /*-----------------------------------------------------------*/
4178 #if ( configUSE_TRACE_FACILITY == 1 )
4180 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
4182 UBaseType_t uxReturn;
4183 TCB_t const * pxTCB;
4188 uxReturn = pxTCB->uxTaskNumber;
4198 #endif /* configUSE_TRACE_FACILITY */
4199 /*-----------------------------------------------------------*/
4201 #if ( configUSE_TRACE_FACILITY == 1 )
4203 void vTaskSetTaskNumber( TaskHandle_t xTask,
4204 const UBaseType_t uxHandle )
4211 pxTCB->uxTaskNumber = uxHandle;
4215 #endif /* configUSE_TRACE_FACILITY */
4218 * -----------------------------------------------------------
4219 * The MinimalIdle task.
4220 * ----------------------------------------------------------
4222 * The minimal idle task is used for all the additional Cores in a SMP system.
4223 * There must be only 1 idle task and the rest are minimal idle tasks.
4225 * @todo additional conditional compiles to remove this function.
4228 #if ( configNUM_CORES > 1 )
4229 static portTASK_FUNCTION( prvMinimalIdleTask, pvParameters )
4234 #if ( configUSE_PREEMPTION == 0 )
4236 /* If we are not using preemption we keep forcing a task switch to
4237 * see if any other task has become available. If we are using
4238 * preemption we don't need to do this as any task becoming available
4239 * will automatically get the processor anyway. */
4242 #endif /* configUSE_PREEMPTION */
4244 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4246 /* When using preemption tasks of equal priority will be
4247 * timesliced. If a task that is sharing the idle priority is ready
4248 * to run then the idle task should yield before the end of the
4251 * A critical region is not required here as we are just reading from
4252 * the list, and an occasional incorrect value will not matter. If
4253 * the ready list at the idle priority contains one more task than the
4254 * number of idle tasks, which is equal to the configured numbers of cores
4255 * then a task other than the idle task is ready to execute. */
4256 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4262 mtCOVERAGE_TEST_MARKER();
4265 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4268 #endif /* if ( configNUM_CORES > 1 ) */
4271 * -----------------------------------------------------------
4273 * ----------------------------------------------------------
4277 static portTASK_FUNCTION( prvIdleTask, pvParameters )
4279 /* Stop warnings. */
4280 ( void ) pvParameters;
4282 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
4283 * SCHEDULER IS STARTED. **/
4285 /* In case a task that has a secure context deletes itself, in which case
4286 * the idle task is responsible for deleting the task's secure context, if
4288 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
4290 /* All cores start up in the idle task. This initial yield gets the application
4296 /* See if any tasks have deleted themselves - if so then the idle task
4297 * is responsible for freeing the deleted task's TCB and stack. */
4298 prvCheckTasksWaitingTermination();
4300 #if ( configUSE_PREEMPTION == 0 )
4302 /* If we are not using preemption we keep forcing a task switch to
4303 * see if any other task has become available. If we are using
4304 * preemption we don't need to do this as any task becoming available
4305 * will automatically get the processor anyway. */
4308 #endif /* configUSE_PREEMPTION */
4310 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
4312 /* When using preemption tasks of equal priority will be
4313 * timesliced. If a task that is sharing the idle priority is ready
4314 * to run then the idle task should yield before the end of the
4317 * A critical region is not required here as we are just reading from
4318 * the list, and an occasional incorrect value will not matter. If
4319 * the ready list at the idle priority contains one more task than the
4320 * number of idle tasks, which is equal to the configured numbers of cores
4321 * then a task other than the idle task is ready to execute. */
4322 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUM_CORES )
4328 mtCOVERAGE_TEST_MARKER();
4331 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
4333 #if ( configUSE_IDLE_HOOK == 1 )
4335 extern void vApplicationIdleHook( void );
4337 /* Call the user defined function from within the idle task. This
4338 * allows the application designer to add background functionality
4339 * without the overhead of a separate task.
4340 * NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
4341 * CALL A FUNCTION THAT MIGHT BLOCK. */
4342 vApplicationIdleHook();
4344 #endif /* configUSE_IDLE_HOOK */
4346 /* This conditional compilation should use inequality to 0, not equality
4347 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
4348 * user defined low power mode implementations require
4349 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
4350 #if ( configUSE_TICKLESS_IDLE != 0 )
4352 TickType_t xExpectedIdleTime;
4354 /* It is not desirable to suspend then resume the scheduler on
4355 * each iteration of the idle task. Therefore, a preliminary
4356 * test of the expected idle time is performed without the
4357 * scheduler suspended. The result here is not necessarily
4359 xExpectedIdleTime = prvGetExpectedIdleTime();
4361 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4365 /* Now the scheduler is suspended, the expected idle
4366 * time can be sampled again, and this time its value can
4368 configASSERT( xNextTaskUnblockTime >= xTickCount );
4369 xExpectedIdleTime = prvGetExpectedIdleTime();
4371 /* Define the following macro to set xExpectedIdleTime to 0
4372 * if the application does not want
4373 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
4374 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
4376 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
4378 traceLOW_POWER_IDLE_BEGIN();
4379 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
4380 traceLOW_POWER_IDLE_END();
4384 mtCOVERAGE_TEST_MARKER();
4387 ( void ) xTaskResumeAll();
4391 mtCOVERAGE_TEST_MARKER();
4394 #endif /* configUSE_TICKLESS_IDLE */
4397 /*-----------------------------------------------------------*/
4399 #if ( configUSE_TICKLESS_IDLE != 0 )
4401 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
4403 /* The idle task exists in addition to the application tasks. */
4404 const UBaseType_t uxNonApplicationTasks = 1;
4405 eSleepModeStatus eReturn = eStandardSleep;
4407 /* This function must be called from a critical section. */
4409 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
4411 /* A task was made ready while the scheduler was suspended. */
4412 eReturn = eAbortSleep;
4414 else if( xYieldPending != pdFALSE )
4416 /* A yield was pended while the scheduler was suspended. */
4417 eReturn = eAbortSleep;
4419 else if( xPendedTicks != 0 )
4421 /* A tick interrupt has already occurred but was held pending
4422 * because the scheduler is suspended. */
4423 eReturn = eAbortSleep;
4427 /* If all the tasks are in the suspended list (which might mean they
4428 * have an infinite block time rather than actually being suspended)
4429 * then it is safe to turn all clocks off and just wait for external
4431 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
4433 eReturn = eNoTasksWaitingTimeout;
4437 mtCOVERAGE_TEST_MARKER();
4444 #endif /* configUSE_TICKLESS_IDLE */
4445 /*-----------------------------------------------------------*/
4447 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4449 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
4455 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4457 pxTCB = prvGetTCBFromHandle( xTaskToSet );
4458 configASSERT( pxTCB != NULL );
4459 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
4463 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4464 /*-----------------------------------------------------------*/
4466 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
4468 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
4471 void * pvReturn = NULL;
4474 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
4476 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
4477 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
4487 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
4488 /*-----------------------------------------------------------*/
4490 #if ( portUSING_MPU_WRAPPERS == 1 )
4492 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
4493 const MemoryRegion_t * const xRegions )
4497 /* If null is passed in here then we are modifying the MPU settings of
4498 * the calling task. */
4499 pxTCB = prvGetTCBFromHandle( xTaskToModify );
4501 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
4504 #endif /* portUSING_MPU_WRAPPERS */
4505 /*-----------------------------------------------------------*/
4507 static void prvInitialiseTaskLists( void )
4509 UBaseType_t uxPriority;
4511 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
4513 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
4516 vListInitialise( &xDelayedTaskList1 );
4517 vListInitialise( &xDelayedTaskList2 );
4518 vListInitialise( &xPendingReadyList );
4520 #if ( INCLUDE_vTaskDelete == 1 )
4522 vListInitialise( &xTasksWaitingTermination );
4524 #endif /* INCLUDE_vTaskDelete */
4526 #if ( INCLUDE_vTaskSuspend == 1 )
4528 vListInitialise( &xSuspendedTaskList );
4530 #endif /* INCLUDE_vTaskSuspend */
4532 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
4534 pxDelayedTaskList = &xDelayedTaskList1;
4535 pxOverflowDelayedTaskList = &xDelayedTaskList2;
4537 /*-----------------------------------------------------------*/
4539 static void prvCheckTasksWaitingTermination( void )
4541 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
4543 #if ( INCLUDE_vTaskDelete == 1 )
4547 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
4548 * being called too often in the idle task. */
4549 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4551 taskENTER_CRITICAL();
4553 /* Since we are SMP, multiple idles can be running simultaneously
4554 * and we need to check that other idles did not cleanup while we were
4555 * waiting to enter the critical section */
4556 if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
4558 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. */
4560 if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
4562 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4563 --uxCurrentNumberOfTasks;
4564 --uxDeletedTasksWaitingCleanUp;
4565 prvDeleteTCB( pxTCB );
4569 /* The TCB to be deleted still has not yet been switched out
4570 * by the scheduler, so we will just exit this loop early and
4571 * try again next time. */
4572 taskEXIT_CRITICAL();
4577 taskEXIT_CRITICAL();
4580 #endif /* INCLUDE_vTaskDelete */
4582 /*-----------------------------------------------------------*/
4584 #if ( configUSE_TRACE_FACILITY == 1 )
4586 void vTaskGetInfo( TaskHandle_t xTask,
4587 TaskStatus_t * pxTaskStatus,
4588 BaseType_t xGetFreeStackSpace,
4593 /* xTask is NULL then get the state of the calling task. */
4594 pxTCB = prvGetTCBFromHandle( xTask );
4596 pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
4597 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
4598 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
4599 pxTaskStatus->pxStackBase = pxTCB->pxStack;
4600 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
4602 #if ( configUSE_MUTEXES == 1 )
4604 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
4608 pxTaskStatus->uxBasePriority = 0;
4612 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4614 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
4618 pxTaskStatus->ulRunTimeCounter = 0;
4622 /* Obtaining the task state is a little fiddly, so is only done if the
4623 * value of eState passed into this function is eInvalid - otherwise the
4624 * state is just set to whatever is passed in. */
4625 if( eState != eInvalid )
4627 if( taskTASK_IS_RUNNING( pxTCB->xTaskRunState ) )
4629 pxTaskStatus->eCurrentState = eRunning;
4633 pxTaskStatus->eCurrentState = eState;
4635 #if ( INCLUDE_vTaskSuspend == 1 )
4637 /* If the task is in the suspended list then there is a
4638 * chance it is actually just blocked indefinitely - so really
4639 * it should be reported as being in the Blocked state. */
4640 if( eState == eSuspended )
4644 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4646 pxTaskStatus->eCurrentState = eBlocked;
4649 ( void ) xTaskResumeAll();
4652 #endif /* INCLUDE_vTaskSuspend */
4657 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
4660 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
4661 * parameter is provided to allow it to be skipped. */
4662 if( xGetFreeStackSpace != pdFALSE )
4664 #if ( portSTACK_GROWTH > 0 )
4666 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
4670 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
4676 pxTaskStatus->usStackHighWaterMark = 0;
4680 #endif /* configUSE_TRACE_FACILITY */
4681 /*-----------------------------------------------------------*/
4683 #if ( configUSE_TRACE_FACILITY == 1 )
4685 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
4689 configLIST_VOLATILE TCB_t * pxNextTCB, * pxFirstTCB;
4690 UBaseType_t uxTask = 0;
4692 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
4694 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. */
4696 /* Populate an TaskStatus_t structure within the
4697 * pxTaskStatusArray array for each task that is referenced from
4698 * pxList. See the definition of TaskStatus_t in task.h for the
4699 * meaning of each TaskStatus_t structure member. */
4702 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. */
4703 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
4705 } while( pxNextTCB != pxFirstTCB );
4709 mtCOVERAGE_TEST_MARKER();
4715 #endif /* configUSE_TRACE_FACILITY */
4716 /*-----------------------------------------------------------*/
4718 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
4720 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
4722 uint32_t ulCount = 0U;
4724 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
4726 pucStackByte -= portSTACK_GROWTH;
4730 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
4732 return ( configSTACK_DEPTH_TYPE ) ulCount;
4735 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
4736 /*-----------------------------------------------------------*/
4738 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
4740 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
4741 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
4742 * user to determine the return type. It gets around the problem of the value
4743 * overflowing on 8-bit types without breaking backward compatibility for
4744 * applications that expect an 8-bit return type. */
4745 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
4748 uint8_t * pucEndOfStack;
4749 configSTACK_DEPTH_TYPE uxReturn;
4751 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
4752 * the same except for their return type. Using configSTACK_DEPTH_TYPE
4753 * allows the user to determine the return type. It gets around the
4754 * problem of the value overflowing on 8-bit types without breaking
4755 * backward compatibility for applications that expect an 8-bit return
4758 pxTCB = prvGetTCBFromHandle( xTask );
4760 #if portSTACK_GROWTH < 0
4762 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4766 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4770 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
4775 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
4776 /*-----------------------------------------------------------*/
4778 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
4780 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
4783 uint8_t * pucEndOfStack;
4784 UBaseType_t uxReturn;
4786 pxTCB = prvGetTCBFromHandle( xTask );
4788 #if portSTACK_GROWTH < 0
4790 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
4794 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
4798 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
4803 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
4804 /*-----------------------------------------------------------*/
4806 #if ( INCLUDE_vTaskDelete == 1 )
4808 static void prvDeleteTCB( TCB_t * pxTCB )
4810 /* This call is required specifically for the TriCore port. It must be
4811 * above the vPortFree() calls. The call is also used by ports/demos that
4812 * want to allocate and clean RAM statically. */
4813 portCLEAN_UP_TCB( pxTCB );
4815 /* Free up the memory allocated by the scheduler for the task. It is up
4816 * to the task to free any memory allocated at the application level.
4817 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
4818 * for additional information. */
4819 #if ( configUSE_NEWLIB_REENTRANT == 1 )
4821 _reclaim_reent( &( pxTCB->xNewLib_reent ) );
4823 #endif /* configUSE_NEWLIB_REENTRANT */
4825 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
4827 /* The task can only have been allocated dynamically - free both
4828 * the stack and TCB. */
4829 vPortFreeStack( pxTCB->pxStack );
4832 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
4834 /* The task could have been allocated statically or dynamically, so
4835 * check what was statically allocated before trying to free the
4837 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
4839 /* Both the stack and TCB were allocated dynamically, so both
4841 vPortFreeStack( pxTCB->pxStack );
4844 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4846 /* Only the stack was statically allocated, so the TCB is the
4847 * only memory that must be freed. */
4852 /* Neither the stack nor the TCB were allocated dynamically, so
4853 * nothing needs to be freed. */
4854 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
4855 mtCOVERAGE_TEST_MARKER();
4858 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
4861 #endif /* INCLUDE_vTaskDelete */
4862 /*-----------------------------------------------------------*/
4864 static void prvResetNextTaskUnblockTime( void )
4866 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4868 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
4869 * the maximum possible value so it is extremely unlikely that the
4870 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
4871 * there is an item in the delayed list. */
4872 xNextTaskUnblockTime = portMAX_DELAY;
4876 /* The new current delayed list is not empty, get the value of
4877 * the item at the head of the delayed list. This is the time at
4878 * which the task at the head of the delayed list should be removed
4879 * from the Blocked state. */
4880 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
4883 /*-----------------------------------------------------------*/
4885 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
4887 TaskHandle_t xTaskGetCurrentTaskHandle( void )
4889 TaskHandle_t xReturn;
4892 ulState = portDISABLE_INTERRUPTS();
4893 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
4894 portRESTORE_INTERRUPTS( ulState );
4899 TaskHandle_t xTaskGetCurrentTaskHandleCPU( UBaseType_t xCoreID )
4901 TaskHandle_t xReturn = NULL;
4903 if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
4905 xReturn = pxCurrentTCBs[ xCoreID ];
4911 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
4912 /*-----------------------------------------------------------*/
4914 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
4916 BaseType_t xTaskGetSchedulerState( void )
4920 if( xSchedulerRunning == pdFALSE )
4922 xReturn = taskSCHEDULER_NOT_STARTED;
4926 taskENTER_CRITICAL();
4928 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4930 xReturn = taskSCHEDULER_RUNNING;
4934 xReturn = taskSCHEDULER_SUSPENDED;
4937 taskEXIT_CRITICAL();
4943 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
4944 /*-----------------------------------------------------------*/
4946 #if ( configUSE_MUTEXES == 1 )
4948 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
4950 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
4951 BaseType_t xReturn = pdFALSE;
4953 /* If the mutex was given back by an interrupt while the queue was
4954 * locked then the mutex holder might now be NULL. _RB_ Is this still
4955 * needed as interrupts can no longer use mutexes? */
4956 if( pxMutexHolder != NULL )
4958 /* If the holder of the mutex has a priority below the priority of
4959 * the task attempting to obtain the mutex then it will temporarily
4960 * inherit the priority of the task attempting to obtain the mutex. */
4961 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
4963 /* Adjust the mutex holder state to account for its new
4964 * priority. Only reset the event list item value if the value is
4965 * not being used for anything else. */
4966 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4968 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. */
4972 mtCOVERAGE_TEST_MARKER();
4975 /* If the task being modified is in the ready state it will need
4976 * to be moved into a new list. */
4977 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
4979 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4981 /* It is known that the task is in its ready list so
4982 * there is no need to check again and the port level
4983 * reset macro can be called directly. */
4984 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
4988 mtCOVERAGE_TEST_MARKER();
4991 /* Inherit the priority before being moved into the new list. */
4992 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4993 prvAddTaskToReadyList( pxMutexHolderTCB );
4997 /* Just inherit the priority. */
4998 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
5001 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
5003 /* Inheritance occurred. */
5008 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
5010 /* The base priority of the mutex holder is lower than the
5011 * priority of the task attempting to take the mutex, but the
5012 * current priority of the mutex holder is not lower than the
5013 * priority of the task attempting to take the mutex.
5014 * Therefore the mutex holder must have already inherited a
5015 * priority, but inheritance would have occurred if that had
5016 * not been the case. */
5021 mtCOVERAGE_TEST_MARKER();
5027 mtCOVERAGE_TEST_MARKER();
5033 #endif /* configUSE_MUTEXES */
5034 /*-----------------------------------------------------------*/
5036 #if ( configUSE_MUTEXES == 1 )
5038 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
5040 TCB_t * const pxTCB = pxMutexHolder;
5041 BaseType_t xReturn = pdFALSE;
5043 if( pxMutexHolder != NULL )
5045 /* A task can only have an inherited priority if it holds the mutex.
5046 * If the mutex is held by a task then it cannot be given from an
5047 * interrupt, and if a mutex is given by the holding task then it must
5048 * be the running state task. */
5049 configASSERT( pxTCB == pxCurrentTCB );
5050 configASSERT( pxTCB->uxMutexesHeld );
5051 ( pxTCB->uxMutexesHeld )--;
5053 /* Has the holder of the mutex inherited the priority of another
5055 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
5057 /* Only disinherit if no other mutexes are held. */
5058 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
5060 /* A task can only have an inherited priority if it holds
5061 * the mutex. If the mutex is held by a task then it cannot be
5062 * given from an interrupt, and if a mutex is given by the
5063 * holding task then it must be the running state task. Remove
5064 * the holding task from the ready list. */
5065 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5067 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5071 mtCOVERAGE_TEST_MARKER();
5074 /* Disinherit the priority before adding the task into the
5075 * new ready list. */
5076 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
5077 pxTCB->uxPriority = pxTCB->uxBasePriority;
5079 /* Reset the event list item value. It cannot be in use for
5080 * any other purpose if this task is running, and it must be
5081 * running to give back the mutex. */
5082 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. */
5083 prvAddTaskToReadyList( pxTCB );
5085 /* Return true to indicate that a context switch is required.
5086 * This is only actually required in the corner case whereby
5087 * multiple mutexes were held and the mutexes were given back
5088 * in an order different to that in which they were taken.
5089 * If a context switch did not occur when the first mutex was
5090 * returned, even if a task was waiting on it, then a context
5091 * switch should occur when the last mutex is returned whether
5092 * a task is waiting on it or not. */
5097 mtCOVERAGE_TEST_MARKER();
5102 mtCOVERAGE_TEST_MARKER();
5107 mtCOVERAGE_TEST_MARKER();
5113 #endif /* configUSE_MUTEXES */
5114 /*-----------------------------------------------------------*/
5116 #if ( configUSE_MUTEXES == 1 )
5118 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
5119 UBaseType_t uxHighestPriorityWaitingTask )
5121 TCB_t * const pxTCB = pxMutexHolder;
5122 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
5123 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
5125 if( pxMutexHolder != NULL )
5127 /* If pxMutexHolder is not NULL then the holder must hold at least
5129 configASSERT( pxTCB->uxMutexesHeld );
5131 /* Determine the priority to which the priority of the task that
5132 * holds the mutex should be set. This will be the greater of the
5133 * holding task's base priority and the priority of the highest
5134 * priority task that is waiting to obtain the mutex. */
5135 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
5137 uxPriorityToUse = uxHighestPriorityWaitingTask;
5141 uxPriorityToUse = pxTCB->uxBasePriority;
5144 /* Does the priority need to change? */
5145 if( pxTCB->uxPriority != uxPriorityToUse )
5147 /* Only disinherit if no other mutexes are held. This is a
5148 * simplification in the priority inheritance implementation. If
5149 * the task that holds the mutex is also holding other mutexes then
5150 * the other mutexes may have caused the priority inheritance. */
5151 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
5153 /* If a task has timed out because it already holds the
5154 * mutex it was trying to obtain then it cannot of inherited
5155 * its own priority. */
5156 configASSERT( pxTCB != pxCurrentTCB );
5158 /* Disinherit the priority, remembering the previous
5159 * priority to facilitate determining the subject task's
5161 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
5162 uxPriorityUsedOnEntry = pxTCB->uxPriority;
5163 pxTCB->uxPriority = uxPriorityToUse;
5165 /* Only reset the event list item value if the value is not
5166 * being used for anything else. */
5167 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
5169 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. */
5173 mtCOVERAGE_TEST_MARKER();
5176 /* If the running task is not the task that holds the mutex
5177 * then the task that holds the mutex could be in either the
5178 * Ready, Blocked or Suspended states. Only remove the task
5179 * from its current state list if it is in the Ready state as
5180 * the task's priority is going to change and there is one
5181 * Ready list per priority. */
5182 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
5184 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5186 /* It is known that the task is in its ready list so
5187 * there is no need to check again and the port level
5188 * reset macro can be called directly. */
5189 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
5193 mtCOVERAGE_TEST_MARKER();
5196 prvAddTaskToReadyList( pxTCB );
5200 mtCOVERAGE_TEST_MARKER();
5205 mtCOVERAGE_TEST_MARKER();
5210 mtCOVERAGE_TEST_MARKER();
5215 mtCOVERAGE_TEST_MARKER();
5219 #endif /* configUSE_MUTEXES */
5220 /*-----------------------------------------------------------*/
5223 * If not in a critical section then yield immediately.
5224 * Otherwise set xYieldPending to true to wait to
5225 * yield until exiting the critical section.
5227 void vTaskYieldWithinAPI( void )
5229 if( pxCurrentTCB->uxCriticalNesting == 0U )
5235 xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5238 /*-----------------------------------------------------------*/
5240 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5242 void vTaskEnterCritical( void )
5244 portDISABLE_INTERRUPTS();
5246 if( xSchedulerRunning != pdFALSE )
5248 if( pxCurrentTCB->uxCriticalNesting == 0U )
5250 if( portCHECK_IF_IN_ISR() == pdFALSE )
5252 portGET_TASK_LOCK();
5258 ( pxCurrentTCB->uxCriticalNesting )++;
5260 /* This should now be interrupt safe. The only time there would be
5261 * a problem is if this is called before a context switch and
5262 * vTaskExitCritical() is called after pxCurrentTCB changes. Therefore
5263 * this should not be used within vTaskSwitchContext(). */
5265 if( ( uxSchedulerSuspended == 0U ) && ( pxCurrentTCB->uxCriticalNesting == 1U ) )
5267 prvCheckForRunStateChange();
5272 mtCOVERAGE_TEST_MARKER();
5276 #endif /* portCRITICAL_NESTING_IN_TCB */
5277 /*-----------------------------------------------------------*/
5279 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
5281 void vTaskExitCritical( void )
5283 if( xSchedulerRunning != pdFALSE )
5285 /* If pxCurrentTCB->uxCriticalNesting is zero then this function
5286 * does not match a previous call to vTaskEnterCritical(). */
5287 configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
5289 if( pxCurrentTCB->uxCriticalNesting > 0U )
5291 ( pxCurrentTCB->uxCriticalNesting )--;
5293 if( pxCurrentTCB->uxCriticalNesting == 0U )
5295 portRELEASE_ISR_LOCK();
5297 if( portCHECK_IF_IN_ISR() == pdFALSE )
5299 portRELEASE_TASK_LOCK();
5300 portENABLE_INTERRUPTS();
5302 /* When a task yields in a critical section it just sets
5303 * xYieldPending to true. So now that we have exited the
5304 * critical section check if xYieldPending is true, and
5306 if( xYieldPending != pdFALSE )
5313 /* In an ISR we don't hold the task lock and don't
5314 * need to yield. Yield will happen if necessary when
5315 * the application ISR calls portEND_SWITCHING_ISR() */
5316 mtCOVERAGE_TEST_MARKER();
5321 mtCOVERAGE_TEST_MARKER();
5326 mtCOVERAGE_TEST_MARKER();
5331 mtCOVERAGE_TEST_MARKER();
5335 #endif /* portCRITICAL_NESTING_IN_TCB */
5336 /*-----------------------------------------------------------*/
5338 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
5340 static char * prvWriteNameToBuffer( char * pcBuffer,
5341 const char * pcTaskName )
5345 /* Start by copying the entire string. */
5346 strcpy( pcBuffer, pcTaskName );
5348 /* Pad the end of the string with spaces to ensure columns line up when
5350 for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
5352 pcBuffer[ x ] = ' ';
5356 pcBuffer[ x ] = ( char ) 0x00;
5358 /* Return the new end of string. */
5359 return &( pcBuffer[ x ] );
5362 #endif /* ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
5363 /*-----------------------------------------------------------*/
5365 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5367 void vTaskList( char * pcWriteBuffer )
5369 TaskStatus_t * pxTaskStatusArray;
5370 UBaseType_t uxArraySize, x;
5376 * This function is provided for convenience only, and is used by many
5377 * of the demo applications. Do not consider it to be part of the
5380 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
5381 * uxTaskGetSystemState() output into a human readable table that
5382 * displays task: names, states, priority, stack usage and task number.
5383 * Stack usage specified as the number of unused StackType_t words stack can hold
5384 * on top of stack - not the number of bytes.
5386 * vTaskList() has a dependency on the sprintf() C library function that
5387 * might bloat the code size, use a lot of stack, and provide different
5388 * results on different platforms. An alternative, tiny, third party,
5389 * and limited functionality implementation of sprintf() is provided in
5390 * many of the FreeRTOS/Demo sub-directories in a file called
5391 * printf-stdarg.c (note printf-stdarg.c does not provide a full
5392 * snprintf() implementation!).
5394 * It is recommended that production systems call uxTaskGetSystemState()
5395 * directly to get access to raw stats data, rather than indirectly
5396 * through a call to vTaskList().
5400 /* Make sure the write buffer does not contain a string. */
5401 *pcWriteBuffer = ( char ) 0x00;
5403 /* Take a snapshot of the number of tasks in case it changes while this
5404 * function is executing. */
5405 uxArraySize = uxCurrentNumberOfTasks;
5407 /* Allocate an array index for each task. NOTE! if
5408 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5409 * equate to NULL. */
5410 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. */
5412 if( pxTaskStatusArray != NULL )
5414 /* Generate the (binary) data. */
5415 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
5417 /* Create a human readable table from the binary data. */
5418 for( x = 0; x < uxArraySize; x++ )
5420 switch( pxTaskStatusArray[ x ].eCurrentState )
5423 cStatus = tskRUNNING_CHAR;
5427 cStatus = tskREADY_CHAR;
5431 cStatus = tskBLOCKED_CHAR;
5435 cStatus = tskSUSPENDED_CHAR;
5439 cStatus = tskDELETED_CHAR;
5442 case eInvalid: /* Fall through. */
5443 default: /* Should not get here, but it is included
5444 * to prevent static checking errors. */
5445 cStatus = ( char ) 0x00;
5449 /* Write the task name to the string, padding with spaces so it
5450 * can be printed in tabular form more easily. */
5451 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5453 /* Write the rest of the string. */
5454 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. */
5455 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. */
5458 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
5459 * is 0 then vPortFree() will be #defined to nothing. */
5460 vPortFree( pxTaskStatusArray );
5464 mtCOVERAGE_TEST_MARKER();
5468 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
5469 /*----------------------------------------------------------*/
5471 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
5473 void vTaskGetRunTimeStats( char * pcWriteBuffer )
5475 TaskStatus_t * pxTaskStatusArray;
5476 UBaseType_t uxArraySize, x;
5477 uint32_t ulTotalTime, ulStatsAsPercentage;
5479 #if ( configUSE_TRACE_FACILITY != 1 )
5481 #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats().
5488 * This function is provided for convenience only, and is used by many
5489 * of the demo applications. Do not consider it to be part of the
5492 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
5493 * of the uxTaskGetSystemState() output into a human readable table that
5494 * displays the amount of time each task has spent in the Running state
5495 * in both absolute and percentage terms.
5497 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
5498 * function that might bloat the code size, use a lot of stack, and
5499 * provide different results on different platforms. An alternative,
5500 * tiny, third party, and limited functionality implementation of
5501 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
5502 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
5503 * a full snprintf() implementation!).
5505 * It is recommended that production systems call uxTaskGetSystemState()
5506 * directly to get access to raw stats data, rather than indirectly
5507 * through a call to vTaskGetRunTimeStats().
5510 /* Make sure the write buffer does not contain a string. */
5511 *pcWriteBuffer = ( char ) 0x00;
5513 /* Take a snapshot of the number of tasks in case it changes while this
5514 * function is executing. */
5515 uxArraySize = uxCurrentNumberOfTasks;
5517 /* Allocate an array index for each task. NOTE! If
5518 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
5519 * equate to NULL. */
5520 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. */
5522 if( pxTaskStatusArray != NULL )
5524 /* Generate the (binary) data. */
5525 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
5527 /* For percentage calculations. */
5528 ulTotalTime /= 100UL;
5530 /* Avoid divide by zero errors. */
5531 if( ulTotalTime > 0UL )
5533 /* Create a human readable table from the binary data. */
5534 for( x = 0; x < uxArraySize; x++ )
5536 /* What percentage of the total run time has the task used?
5537 * This will always be rounded down to the nearest integer.
5538 * ulTotalRunTimeDiv100 has already been divided by 100. */
5539 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
5541 /* Write the task name to the string, padding with
5542 * spaces so it can be printed in tabular form more
5544 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
5546 if( ulStatsAsPercentage > 0UL )
5548 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5550 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
5554 /* sizeof( int ) == sizeof( long ) so a smaller
5555 * printf() library can be used. */
5556 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. */
5562 /* If the percentage is zero here then the task has
5563 * consumed less than 1% of the total run time. */
5564 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
5566 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
5570 /* sizeof( int ) == sizeof( long ) so a smaller
5571 * printf() library can be used. */
5572 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. */
5577 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. */
5582 mtCOVERAGE_TEST_MARKER();
5585 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
5586 * is 0 then vPortFree() will be #defined to nothing. */
5587 vPortFree( pxTaskStatusArray );
5591 mtCOVERAGE_TEST_MARKER();
5595 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
5596 /*-----------------------------------------------------------*/
5598 TickType_t uxTaskResetEventItemValue( void )
5600 TickType_t uxReturn;
5602 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
5604 /* Reset the event list item to its normal value - so it can be used with
5605 * queues and semaphores. */
5606 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. */
5610 /*-----------------------------------------------------------*/
5612 #if ( configUSE_MUTEXES == 1 )
5614 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
5616 /* If xSemaphoreCreateMutex() is called before any tasks have been created
5617 * then pxCurrentTCB will be NULL. */
5618 if( pxCurrentTCB != NULL )
5620 ( pxCurrentTCB->uxMutexesHeld )++;
5623 return pxCurrentTCB;
5626 #endif /* configUSE_MUTEXES */
5627 /*-----------------------------------------------------------*/
5629 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5631 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
5632 BaseType_t xClearCountOnExit,
5633 TickType_t xTicksToWait )
5637 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5639 taskENTER_CRITICAL();
5641 /* Only block if the notification count is not already non-zero. */
5642 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
5644 /* Mark this task as waiting for a notification. */
5645 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5647 if( xTicksToWait > ( TickType_t ) 0 )
5649 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5650 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
5652 /* All ports are written to allow a yield in a critical
5653 * section (some will yield immediately, others wait until the
5654 * critical section exits) - but it is not something that
5655 * application code should ever do. */
5656 vTaskYieldWithinAPI();
5660 mtCOVERAGE_TEST_MARKER();
5665 mtCOVERAGE_TEST_MARKER();
5668 taskEXIT_CRITICAL();
5670 taskENTER_CRITICAL();
5672 traceTASK_NOTIFY_TAKE( uxIndexToWait );
5673 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5675 if( ulReturn != 0UL )
5677 if( xClearCountOnExit != pdFALSE )
5679 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
5683 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
5688 mtCOVERAGE_TEST_MARKER();
5691 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5693 taskEXIT_CRITICAL();
5698 #endif /* configUSE_TASK_NOTIFICATIONS */
5699 /*-----------------------------------------------------------*/
5701 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5703 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
5704 uint32_t ulBitsToClearOnEntry,
5705 uint32_t ulBitsToClearOnExit,
5706 uint32_t * pulNotificationValue,
5707 TickType_t xTicksToWait )
5711 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5713 taskENTER_CRITICAL();
5715 /* Only block if a notification is not already pending. */
5716 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5718 /* Clear bits in the task's notification value as bits may get
5719 * set by the notifying task or interrupt. This can be used to
5720 * clear the value to zero. */
5721 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
5723 /* Mark this task as waiting for a notification. */
5724 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
5726 if( xTicksToWait > ( TickType_t ) 0 )
5728 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
5729 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
5731 /* All ports are written to allow a yield in a critical
5732 * section (some will yield immediately, others wait until the
5733 * critical section exits) - but it is not something that
5734 * application code should ever do. */
5735 vTaskYieldWithinAPI();
5739 mtCOVERAGE_TEST_MARKER();
5744 mtCOVERAGE_TEST_MARKER();
5747 taskEXIT_CRITICAL();
5749 taskENTER_CRITICAL();
5751 traceTASK_NOTIFY_WAIT( uxIndexToWait );
5753 if( pulNotificationValue != NULL )
5755 /* Output the current notification value, which may or may not
5757 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
5760 /* If ucNotifyValue is set then either the task never entered the
5761 * blocked state (because a notification was already pending) or the
5762 * task unblocked because of a notification. Otherwise the task
5763 * unblocked because of a timeout. */
5764 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
5766 /* A notification was not received. */
5771 /* A notification was already pending or a notification was
5772 * received while the task was waiting. */
5773 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
5777 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
5779 taskEXIT_CRITICAL();
5784 #endif /* configUSE_TASK_NOTIFICATIONS */
5785 /*-----------------------------------------------------------*/
5787 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5789 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
5790 UBaseType_t uxIndexToNotify,
5792 eNotifyAction eAction,
5793 uint32_t * pulPreviousNotificationValue )
5796 BaseType_t xReturn = pdPASS;
5797 uint8_t ucOriginalNotifyState;
5799 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5800 configASSERT( xTaskToNotify );
5801 pxTCB = xTaskToNotify;
5803 taskENTER_CRITICAL();
5805 if( pulPreviousNotificationValue != NULL )
5807 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5810 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5812 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5817 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5821 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5824 case eSetValueWithOverwrite:
5825 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5828 case eSetValueWithoutOverwrite:
5830 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5832 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5836 /* The value could not be written to the task. */
5844 /* The task is being notified without its notify value being
5850 /* Should not get here if all enums are handled.
5851 * Artificially force an assert by testing a value the
5852 * compiler can't assume is const. */
5853 configASSERT( xTickCount == ( TickType_t ) 0 );
5858 traceTASK_NOTIFY( uxIndexToNotify );
5860 /* If the task is in the blocked state specifically to wait for a
5861 * notification then unblock it now. */
5862 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5864 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5865 prvAddTaskToReadyList( pxTCB );
5867 /* The task should not have been on an event list. */
5868 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5870 #if ( configUSE_TICKLESS_IDLE != 0 )
5872 /* If a task is blocked waiting for a notification then
5873 * xNextTaskUnblockTime might be set to the blocked task's time
5874 * out time. If the task is unblocked for a reason other than
5875 * a timeout xNextTaskUnblockTime is normally left unchanged,
5876 * because it will automatically get reset to a new value when
5877 * the tick count equals xNextTaskUnblockTime. However if
5878 * tickless idling is used it might be more important to enter
5879 * sleep mode at the earliest possible time - so reset
5880 * xNextTaskUnblockTime here to ensure it is updated at the
5881 * earliest possible time. */
5882 prvResetNextTaskUnblockTime();
5886 #if ( configUSE_PREEMPTION == 1 )
5888 prvYieldForTask( pxTCB, pdFALSE );
5894 mtCOVERAGE_TEST_MARKER();
5897 taskEXIT_CRITICAL();
5902 #endif /* configUSE_TASK_NOTIFICATIONS */
5903 /*-----------------------------------------------------------*/
5905 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5907 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
5908 UBaseType_t uxIndexToNotify,
5910 eNotifyAction eAction,
5911 uint32_t * pulPreviousNotificationValue,
5912 BaseType_t * pxHigherPriorityTaskWoken )
5915 uint8_t ucOriginalNotifyState;
5916 BaseType_t xReturn = pdPASS;
5917 UBaseType_t uxSavedInterruptStatus;
5919 configASSERT( xTaskToNotify );
5920 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5922 /* RTOS ports that support interrupt nesting have the concept of a
5923 * maximum system call (or maximum API call) interrupt priority.
5924 * Interrupts that are above the maximum system call priority are keep
5925 * permanently enabled, even when the RTOS kernel is in a critical section,
5926 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
5927 * is defined in FreeRTOSConfig.h then
5928 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
5929 * failure if a FreeRTOS API function is called from an interrupt that has
5930 * been assigned a priority above the configured maximum system call
5931 * priority. Only FreeRTOS functions that end in FromISR can be called
5932 * from interrupts that have been assigned a priority at or (logically)
5933 * below the maximum system call interrupt priority. FreeRTOS maintains a
5934 * separate interrupt safe API to ensure interrupt entry is as fast and as
5935 * simple as possible. More information (albeit Cortex-M specific) is
5936 * provided on the following link:
5937 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5938 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
5940 pxTCB = xTaskToNotify;
5942 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5944 if( pulPreviousNotificationValue != NULL )
5946 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5949 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5950 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5955 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5959 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5962 case eSetValueWithOverwrite:
5963 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5966 case eSetValueWithoutOverwrite:
5968 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5970 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5974 /* The value could not be written to the task. */
5982 /* The task is being notified without its notify value being
5988 /* Should not get here if all enums are handled.
5989 * Artificially force an assert by testing a value the
5990 * compiler can't assume is const. */
5991 configASSERT( xTickCount == ( TickType_t ) 0 );
5995 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
5997 /* If the task is in the blocked state specifically to wait for a
5998 * notification then unblock it now. */
5999 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
6001 /* The task should not have been on an event list. */
6002 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
6004 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
6006 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6007 prvAddTaskToReadyList( pxTCB );
6011 /* The delayed and ready lists cannot be accessed, so hold
6012 * this task pending until the scheduler is resumed. */
6013 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
6016 #if ( configUSE_PREEMPTION == 1 )
6017 prvYieldForTask( pxTCB, pdFALSE );
6019 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6021 if( pxHigherPriorityTaskWoken != NULL )
6023 *pxHigherPriorityTaskWoken = pdTRUE;
6029 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6034 #endif /* configUSE_TASK_NOTIFICATIONS */
6035 /*-----------------------------------------------------------*/
6037 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6039 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
6040 UBaseType_t uxIndexToNotify,
6041 BaseType_t * pxHigherPriorityTaskWoken )
6044 uint8_t ucOriginalNotifyState;
6045 UBaseType_t uxSavedInterruptStatus;
6047 configASSERT( xTaskToNotify );
6048 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6050 /* RTOS ports that support interrupt nesting have the concept of a
6051 * maximum system call (or maximum API call) interrupt priority.
6052 * Interrupts that are above the maximum system call priority are keep
6053 * permanently enabled, even when the RTOS kernel is in a critical section,
6054 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
6055 * is defined in FreeRTOSConfig.h then
6056 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
6057 * failure if a FreeRTOS API function is called from an interrupt that has
6058 * been assigned a priority above the configured maximum system call
6059 * priority. Only FreeRTOS functions that end in FromISR can be called
6060 * from interrupts that have been assigned a priority at or (logically)
6061 * below the maximum system call interrupt priority. FreeRTOS maintains a
6062 * separate interrupt safe API to ensure interrupt entry is as fast and as
6063 * simple as possible. More information (albeit Cortex-M specific) is
6064 * provided on the following link:
6065 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
6066 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
6068 pxTCB = xTaskToNotify;
6070 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6072 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
6073 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
6075 /* 'Giving' is equivalent to incrementing a count in a counting
6077 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
6079 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
6081 /* If the task is in the blocked state specifically to wait for a
6082 * notification then unblock it now. */
6083 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
6085 /* The task should not have been on an event list. */
6086 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
6088 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
6090 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
6091 prvAddTaskToReadyList( pxTCB );
6095 /* The delayed and ready lists cannot be accessed, so hold
6096 * this task pending until the scheduler is resumed. */
6097 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
6100 #if ( configUSE_PREEMPTION == 1 )
6101 prvYieldForTask( pxTCB, pdFALSE );
6103 if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
6105 if( pxHigherPriorityTaskWoken != NULL )
6107 *pxHigherPriorityTaskWoken = pdTRUE;
6113 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6116 #endif /* configUSE_TASK_NOTIFICATIONS */
6117 /*-----------------------------------------------------------*/
6119 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6121 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
6122 UBaseType_t uxIndexToClear )
6127 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
6129 /* If null is passed in here then it is the calling task that is having
6130 * its notification state cleared. */
6131 pxTCB = prvGetTCBFromHandle( xTask );
6133 taskENTER_CRITICAL();
6135 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
6137 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
6145 taskEXIT_CRITICAL();
6150 #endif /* configUSE_TASK_NOTIFICATIONS */
6151 /*-----------------------------------------------------------*/
6153 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
6155 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
6156 UBaseType_t uxIndexToClear,
6157 uint32_t ulBitsToClear )
6162 /* If null is passed in here then it is the calling task that is having
6163 * its notification state cleared. */
6164 pxTCB = prvGetTCBFromHandle( xTask );
6166 taskENTER_CRITICAL();
6168 /* Return the notification as it was before the bits were cleared,
6169 * then clear the bit mask. */
6170 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
6171 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
6173 taskEXIT_CRITICAL();
6178 #endif /* configUSE_TASK_NOTIFICATIONS */
6179 /*-----------------------------------------------------------*/
6181 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
6183 uint32_t ulTaskGetIdleRunTimeCounter( void )
6185 uint32_t ulReturn = 0;
6187 for( BaseType_t i = 0; i < configNUM_CORES; i++ )
6189 ulReturn += xIdleTaskHandle[ i ]->ulRunTimeCounter;
6195 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
6196 /*-----------------------------------------------------------*/
6198 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
6199 const BaseType_t xCanBlockIndefinitely )
6201 TickType_t xTimeToWake;
6202 const TickType_t xConstTickCount = xTickCount;
6204 #if ( INCLUDE_xTaskAbortDelay == 1 )
6206 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
6207 * reset to pdFALSE so it can be detected as having been set to pdTRUE
6208 * when the task leaves the Blocked state. */
6209 pxCurrentTCB->ucDelayAborted = pdFALSE;
6213 /* Remove the task from the ready list before adding it to the blocked list
6214 * as the same list item is used for both lists. */
6215 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6217 /* The current task must be in a ready list, so there is no need to
6218 * check, and the port reset macro can be called directly. */
6219 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. */
6223 mtCOVERAGE_TEST_MARKER();
6226 #if ( INCLUDE_vTaskSuspend == 1 )
6228 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
6230 /* Add the task to the suspended task list instead of a delayed task
6231 * list to ensure it is not woken by a timing event. It will block
6233 vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
6237 /* Calculate the time at which the task should be woken if the event
6238 * does not occur. This may overflow but this doesn't matter, the
6239 * kernel will manage it correctly. */
6240 xTimeToWake = xConstTickCount + xTicksToWait;
6242 /* The list item will be inserted in wake time order. */
6243 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6245 if( xTimeToWake < xConstTickCount )
6247 /* Wake time has overflowed. Place this item in the overflow
6249 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6253 /* The wake time has not overflowed, so the current block list
6255 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6257 /* If the task entering the blocked state was placed at the
6258 * head of the list of blocked tasks then xNextTaskUnblockTime
6259 * needs to be updated too. */
6260 if( xTimeToWake < xNextTaskUnblockTime )
6262 xNextTaskUnblockTime = xTimeToWake;
6266 mtCOVERAGE_TEST_MARKER();
6271 #else /* INCLUDE_vTaskSuspend */
6273 /* Calculate the time at which the task should be woken if the event
6274 * does not occur. This may overflow but this doesn't matter, the kernel
6275 * will manage it correctly. */
6276 xTimeToWake = xConstTickCount + xTicksToWait;
6278 /* The list item will be inserted in wake time order. */
6279 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
6281 if( xTimeToWake < xConstTickCount )
6283 /* Wake time has overflowed. Place this item in the overflow list. */
6284 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6288 /* The wake time has not overflowed, so the current block list is used. */
6289 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
6291 /* If the task entering the blocked state was placed at the head of the
6292 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
6294 if( xTimeToWake < xNextTaskUnblockTime )
6296 xNextTaskUnblockTime = xTimeToWake;
6300 mtCOVERAGE_TEST_MARKER();
6304 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
6305 ( void ) xCanBlockIndefinitely;
6307 #endif /* INCLUDE_vTaskSuspend */
6310 /* Code below here allows additional code to be inserted into this source file,
6311 * especially where access to file scope functions and data is needed (for example
6312 * when performing module tests). */
6314 #ifdef FREERTOS_MODULE_TEST
6315 #include "tasks_test_access_functions.h"
6319 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
6321 #include "freertos_tasks_c_additions.h"
6323 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
6324 static void freertos_tasks_c_additions_init( void )
6326 FREERTOS_TASKS_C_ADDITIONS_INIT();
6330 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */