2 * FreeRTOS Kernel V10.4.6
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /* Standard includes. */
33 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
34 * all the API functions to use the MPU wrappers. That should only be done when
35 * task.h is included from an application file. */
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
38 /* FreeRTOS includes. */
42 #include "stack_macros.h"
44 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
45 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
46 * for the header files above, but not in this file, in order to generate the
47 * correct privileged Vs unprivileged linkage and placement. */
48 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
50 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
51 * functions but without including stdio.h here. */
52 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
54 /* At the bottom of this file are two optional functions that can be used
55 * to generate human readable text from the raw data generated by the
56 * uxTaskGetSystemState() function. Note the formatting functions are provided
57 * for convenience only, and are NOT considered part of the kernel. */
59 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
61 #if ( configUSE_PREEMPTION == 0 )
63 /* If the cooperative scheduler is being used then a yield should not be
64 * performed just because a higher priority task has been woken. */
65 #define taskYIELD_IF_USING_PREEMPTION()
67 #define taskYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
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 taskSELECT_HIGHEST_PRIORITY_TASK() \
138 UBaseType_t uxTopPriority = uxTopReadyPriority; \
140 /* Find the highest priority queue that contains ready tasks. */ \
141 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
143 configASSERT( uxTopPriority ); \
147 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
148 * the same priority get an equal share of the processor time. */ \
149 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
150 uxTopReadyPriority = uxTopPriority; \
151 } /* taskSELECT_HIGHEST_PRIORITY_TASK */
153 /*-----------------------------------------------------------*/
155 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
156 * they are only required when a port optimised method of task selection is
158 #define taskRESET_READY_PRIORITY( uxPriority )
159 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
161 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
163 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
164 * performed in a way that is tailored to the particular microcontroller
165 * architecture being used. */
167 /* A port optimised version is provided. Call the port defined macros. */
168 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( uxPriority, uxTopReadyPriority )
170 /*-----------------------------------------------------------*/
172 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
174 UBaseType_t uxTopPriority; \
176 /* Find the highest priority list that contains ready tasks. */ \
177 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
178 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
179 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
180 } /* taskSELECT_HIGHEST_PRIORITY_TASK() */
182 /*-----------------------------------------------------------*/
184 /* A port optimised version is provided, call it only if the TCB being reset
185 * is being referenced from a ready list. If it is referenced from a delayed
186 * or suspended list then it won't be in a ready list. */
187 #define taskRESET_READY_PRIORITY( uxPriority ) \
189 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
191 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
195 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
197 /*-----------------------------------------------------------*/
199 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
200 * count overflows. */
201 #define taskSWITCH_DELAYED_LISTS() \
205 /* The delayed tasks list should be empty when the lists are switched. */ \
206 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
208 pxTemp = pxDelayedTaskList; \
209 pxDelayedTaskList = pxOverflowDelayedTaskList; \
210 pxOverflowDelayedTaskList = pxTemp; \
212 prvResetNextTaskUnblockTime(); \
215 /*-----------------------------------------------------------*/
218 * Place the task represented by pxTCB into the appropriate ready list for
219 * the task. It is inserted at the end of the list.
221 #define prvAddTaskToReadyList( pxTCB ) \
222 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
223 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
224 listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
225 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB )
226 /*-----------------------------------------------------------*/
229 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
230 * where NULL is used to indicate that the handle of the currently executing
231 * task should be used in place of the parameter. This macro simply checks to
232 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
234 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
236 /* The item value of the event list item is normally used to hold the priority
237 * of the task to which it belongs (coded to allow it to be held in reverse
238 * priority order). However, it is occasionally borrowed for other purposes. It
239 * is important its value is not updated due to a task priority change while it is
240 * being used for another purpose. The following bit definition is used to inform
241 * the scheduler that the value should not be changed - in which case it is the
242 * responsibility of whichever module is using the value to ensure it gets set back
243 * to its original value when it is released. */
244 #if ( configUSE_16_BIT_TICKS == 1 )
245 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U
247 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
251 * Task control block. A task control block (TCB) is allocated for each task,
252 * and stores task state information, including a pointer to the task's context
253 * (the task's run time environment, including register values)
255 typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
257 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. */
259 #if ( portUSING_MPU_WRAPPERS == 1 )
260 xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
263 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 ). */
264 ListItem_t xEventListItem; /*< Used to reference a task from an event list. */
265 UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */
266 StackType_t * pxStack; /*< Points to the start of the stack. */
267 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. */
269 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
270 StackType_t * pxEndOfStack; /*< Points to the highest valid address for the stack. */
273 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
274 UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
277 #if ( configUSE_TRACE_FACILITY == 1 )
278 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. */
279 UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
282 #if ( configUSE_MUTEXES == 1 )
283 UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
284 UBaseType_t uxMutexesHeld;
287 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
288 TaskHookFunction_t pxTaskTag;
291 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
292 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
295 #if ( configGENERATE_RUN_TIME_STATS == 1 )
296 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
299 #if ( configUSE_NEWLIB_REENTRANT == 1 )
301 /* Allocate a Newlib reent structure that is specific to this task.
302 * Note Newlib support has been included by popular demand, but is not
303 * used by the FreeRTOS maintainers themselves. FreeRTOS is not
304 * responsible for resulting newlib operation. User must be familiar with
305 * newlib and must provide system-wide implementations of the necessary
306 * stubs. Be warned that (at the time of writing) the current newlib design
307 * implements a system-wide malloc() that must be provided with locks.
309 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
310 * for additional information. */
311 struct _reent xNewLib_reent;
314 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
315 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
316 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
319 /* See the comments in FreeRTOS.h with the definition of
320 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
321 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
322 uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
325 #if ( INCLUDE_xTaskAbortDelay == 1 )
326 uint8_t ucDelayAborted;
329 #if ( configUSE_POSIX_ERRNO == 1 )
334 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
335 * below to enable the use of older kernel aware debuggers. */
336 typedef tskTCB TCB_t;
338 /*lint -save -e956 A manual analysis and inspection has been used to determine
339 * which static variables must be declared volatile. */
340 PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
342 /* Lists for ready and blocked tasks. --------------------
343 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
344 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
345 * the static qualifier. */
346 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */
347 PRIVILEGED_DATA static List_t xDelayedTaskList1; /*< Delayed tasks. */
348 PRIVILEGED_DATA static List_t xDelayedTaskList2; /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
349 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /*< Points to the delayed task list currently being used. */
350 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. */
351 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. */
353 #if ( INCLUDE_vTaskDelete == 1 )
355 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /*< Tasks that have been deleted - but their memory not yet freed. */
356 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
360 #if ( INCLUDE_vTaskSuspend == 1 )
362 PRIVILEGED_DATA static List_t xSuspendedTaskList; /*< Tasks that are currently suspended. */
366 /* Global POSIX errno. Its value is changed upon context switching to match
367 * the errno of the currently running task. */
368 #if ( configUSE_POSIX_ERRNO == 1 )
369 int FreeRTOS_errno = 0;
372 /* Other file private variables. --------------------------------*/
373 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
374 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
375 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
376 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
377 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
378 PRIVILEGED_DATA static volatile BaseType_t xYieldPending = pdFALSE;
379 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
380 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
381 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
382 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandle = NULL; /*< Holds the handle of the idle task. The idle task is created automatically when the scheduler is started. */
384 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
385 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
386 * to determine the number of priority lists to read back from the remote target. */
387 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
389 /* Context switches are held pending while the scheduler is suspended. Also,
390 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
391 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
392 * If an interrupt needs to unblock a task while the scheduler is suspended then it
393 * moves the task's event list item into the xPendingReadyList, ready for the
394 * kernel to move the task from the pending ready list into the real ready list
395 * when the scheduler is unsuspended. The pending ready list itself can only be
396 * accessed from a critical section. */
397 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) pdFALSE;
399 #if ( configGENERATE_RUN_TIME_STATS == 1 )
401 /* Do not move these variables to function scope as doing so prevents the
402 * code working with debuggers that need to remove the static qualifier. */
403 PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime = 0UL; /*< Holds the value of a timer/counter the last time a task was switched in. */
404 PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime = 0UL; /*< Holds the total amount of execution time as defined by the run time counter clock. */
410 /*-----------------------------------------------------------*/
412 /* File private functions. --------------------------------*/
415 * Utility task that simply returns pdTRUE if the task referenced by xTask is
416 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
417 * is in any other state.
419 #if ( INCLUDE_vTaskSuspend == 1 )
421 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
423 #endif /* INCLUDE_vTaskSuspend */
426 * Utility to ready all the lists used by the scheduler. This is called
427 * automatically upon the creation of the first task.
429 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
432 * The idle task, which as all tasks is implemented as a never ending loop.
433 * The idle task is automatically created and added to the ready lists upon
434 * creation of the first user task.
436 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
437 * language extensions. The equivalent prototype for this function is:
439 * void prvIdleTask( void *pvParameters );
442 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
445 * Utility to free all memory allocated by the scheduler to hold a TCB,
446 * including the stack pointed to by the TCB.
448 * This does not free memory allocated by the task itself (i.e. memory
449 * allocated by calls to pvPortMalloc from within the tasks application code).
451 #if ( INCLUDE_vTaskDelete == 1 )
453 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
458 * Used only by the idle task. This checks to see if anything has been placed
459 * in the list of tasks waiting to be deleted. If so the task is cleaned up
460 * and its TCB deleted.
462 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
465 * The currently executing task is entering the Blocked state. Add the task to
466 * either the current or the overflow delayed task list.
468 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
469 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
472 * Fills an TaskStatus_t structure with information on each task that is
473 * referenced from the pxList list (which may be a ready list, a delayed list,
474 * a suspended list, etc.).
476 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
477 * NORMAL APPLICATION CODE.
479 #if ( configUSE_TRACE_FACILITY == 1 )
481 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
483 eTaskState eState ) PRIVILEGED_FUNCTION;
488 * Searches pxList for a task with name pcNameToQuery - returning a handle to
489 * the task if it is found, or NULL if the task is not found.
491 #if ( INCLUDE_xTaskGetHandle == 1 )
493 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
494 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
499 * When a task is created, the stack of the task is filled with a known value.
500 * This function determines the 'high water mark' of the task stack by
501 * determining how much of the stack remains at the original preset value.
503 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
505 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
510 * Return the amount of time, in ticks, that will pass before the kernel will
511 * next move a task from the Blocked state to the Running state.
513 * This conditional compilation should use inequality to 0, not equality to 1.
514 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
515 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
516 * set to a value other than 1.
518 #if ( configUSE_TICKLESS_IDLE != 0 )
520 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
525 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
526 * will exit the Blocked state.
528 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
530 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
533 * Helper function used to pad task names with spaces when printing out
534 * human readable tables of task information.
536 static char * prvWriteNameToBuffer( char * pcBuffer,
537 const char * pcTaskName ) PRIVILEGED_FUNCTION;
542 * Called after a Task_t structure has been allocated either statically or
543 * dynamically to fill in the structure's members.
545 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
546 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
547 const uint32_t ulStackDepth,
548 void * const pvParameters,
549 UBaseType_t uxPriority,
550 TaskHandle_t * const pxCreatedTask,
552 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
555 * Called after a new task has been created and initialised to place the task
556 * under the control of the scheduler.
558 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
561 * freertos_tasks_c_additions_init() should only be called if the user definable
562 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
563 * called by the function.
565 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
567 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
571 /*-----------------------------------------------------------*/
573 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
575 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
576 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
577 const uint32_t ulStackDepth,
578 void * const pvParameters,
579 UBaseType_t uxPriority,
580 StackType_t * const puxStackBuffer,
581 StaticTask_t * const pxTaskBuffer )
584 TaskHandle_t xReturn;
586 configASSERT( puxStackBuffer != NULL );
587 configASSERT( pxTaskBuffer != NULL );
589 #if ( configASSERT_DEFINED == 1 )
591 /* Sanity check that the size of the structure used to declare a
592 * variable of type StaticTask_t equals the size of the real task
594 volatile size_t xSize = sizeof( StaticTask_t );
595 configASSERT( xSize == sizeof( TCB_t ) );
596 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
598 #endif /* configASSERT_DEFINED */
600 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
602 /* The memory used for the task's TCB and stack are passed into this
603 * function - use them. */
604 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. */
605 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
607 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
609 /* Tasks can be created statically or dynamically, so note this
610 * task was created statically in case the task is later deleted. */
611 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
613 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
615 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
616 prvAddNewTaskToReadyList( pxNewTCB );
626 #endif /* SUPPORT_STATIC_ALLOCATION */
627 /*-----------------------------------------------------------*/
629 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
631 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
632 TaskHandle_t * pxCreatedTask )
635 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
637 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
638 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
640 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
642 /* Allocate space for the TCB. Where the memory comes from depends
643 * on the implementation of the port malloc function and whether or
644 * not static allocation is being used. */
645 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
647 /* Store the stack location in the TCB. */
648 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
650 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
652 /* Tasks can be created statically or dynamically, so note this
653 * task was created statically in case the task is later deleted. */
654 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
656 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
658 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
659 pxTaskDefinition->pcName,
660 ( uint32_t ) pxTaskDefinition->usStackDepth,
661 pxTaskDefinition->pvParameters,
662 pxTaskDefinition->uxPriority,
663 pxCreatedTask, pxNewTCB,
664 pxTaskDefinition->xRegions );
666 prvAddNewTaskToReadyList( pxNewTCB );
673 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
674 /*-----------------------------------------------------------*/
676 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
678 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
679 TaskHandle_t * pxCreatedTask )
682 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
684 configASSERT( pxTaskDefinition->puxStackBuffer );
686 if( pxTaskDefinition->puxStackBuffer != NULL )
688 /* Allocate space for the TCB. Where the memory comes from depends
689 * on the implementation of the port malloc function and whether or
690 * not static allocation is being used. */
691 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
693 if( pxNewTCB != NULL )
695 /* Store the stack location in the TCB. */
696 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
698 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
700 /* Tasks can be created statically or dynamically, so note
701 * this task had a statically allocated stack in case it is
702 * later deleted. The TCB was allocated dynamically. */
703 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
705 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
707 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
708 pxTaskDefinition->pcName,
709 ( uint32_t ) pxTaskDefinition->usStackDepth,
710 pxTaskDefinition->pvParameters,
711 pxTaskDefinition->uxPriority,
712 pxCreatedTask, pxNewTCB,
713 pxTaskDefinition->xRegions );
715 prvAddNewTaskToReadyList( pxNewTCB );
723 #endif /* portUSING_MPU_WRAPPERS */
724 /*-----------------------------------------------------------*/
726 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
728 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
729 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
730 const configSTACK_DEPTH_TYPE usStackDepth,
731 void * const pvParameters,
732 UBaseType_t uxPriority,
733 TaskHandle_t * const pxCreatedTask )
738 /* If the stack grows down then allocate the stack then the TCB so the stack
739 * does not grow into the TCB. Likewise if the stack grows up then allocate
740 * the TCB then the stack. */
741 #if ( portSTACK_GROWTH > 0 )
743 /* Allocate space for the TCB. Where the memory comes from depends on
744 * the implementation of the port malloc function and whether or not static
745 * allocation is being used. */
746 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
748 if( pxNewTCB != NULL )
750 /* Allocate space for the stack used by the task being created.
751 * The base of the stack memory stored in the TCB so the task can
752 * be deleted later if required. */
753 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
755 if( pxNewTCB->pxStack == NULL )
757 /* Could not allocate the stack. Delete the allocated TCB. */
758 vPortFree( pxNewTCB );
763 #else /* portSTACK_GROWTH */
765 StackType_t * pxStack;
767 /* Allocate space for the stack used by the task being created. */
768 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. */
770 if( pxStack != NULL )
772 /* Allocate space for the TCB. */
773 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. */
775 if( pxNewTCB != NULL )
777 /* Store the stack location in the TCB. */
778 pxNewTCB->pxStack = pxStack;
782 /* The stack cannot be used as the TCB was not created. Free
784 vPortFreeStack( pxStack );
792 #endif /* portSTACK_GROWTH */
794 if( pxNewTCB != NULL )
796 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
798 /* Tasks can be created statically or dynamically, so note this
799 * task was created dynamically in case it is later deleted. */
800 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
802 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
804 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
805 prvAddNewTaskToReadyList( pxNewTCB );
810 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
816 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
817 /*-----------------------------------------------------------*/
819 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
820 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
821 const uint32_t ulStackDepth,
822 void * const pvParameters,
823 UBaseType_t uxPriority,
824 TaskHandle_t * const pxCreatedTask,
826 const MemoryRegion_t * const xRegions )
828 StackType_t * pxTopOfStack;
831 #if ( portUSING_MPU_WRAPPERS == 1 )
832 /* Should the task be created in privileged mode? */
833 BaseType_t xRunPrivileged;
835 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
837 xRunPrivileged = pdTRUE;
841 xRunPrivileged = pdFALSE;
843 uxPriority &= ~portPRIVILEGE_BIT;
844 #endif /* portUSING_MPU_WRAPPERS == 1 */
846 /* Avoid dependency on memset() if it is not required. */
847 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
849 /* Fill the stack with a known value to assist debugging. */
850 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
852 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
854 /* Calculate the top of stack address. This depends on whether the stack
855 * grows from high memory to low (as per the 80x86) or vice versa.
856 * portSTACK_GROWTH is used to make the result positive or negative as required
858 #if ( portSTACK_GROWTH < 0 )
860 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
861 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(). */
863 /* Check the alignment of the calculated top of stack is correct. */
864 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
866 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
868 /* Also record the stack's high address, which may assist
870 pxNewTCB->pxEndOfStack = pxTopOfStack;
872 #endif /* configRECORD_STACK_HIGH_ADDRESS */
874 #else /* portSTACK_GROWTH */
876 pxTopOfStack = pxNewTCB->pxStack;
878 /* Check the alignment of the stack buffer is correct. */
879 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
881 /* The other extreme of the stack space is required if stack checking is
883 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
885 #endif /* portSTACK_GROWTH */
887 /* Store the task name in the TCB. */
890 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
892 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
894 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
895 * configMAX_TASK_NAME_LEN characters just in case the memory after the
896 * string is not accessible (extremely unlikely). */
897 if( pcName[ x ] == ( char ) 0x00 )
903 mtCOVERAGE_TEST_MARKER();
907 /* Ensure the name string is terminated in the case that the string length
908 * was greater or equal to configMAX_TASK_NAME_LEN. */
909 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
913 /* The task has not been given a name, so just ensure there is a NULL
914 * terminator when it is read out. */
915 pxNewTCB->pcTaskName[ 0 ] = 0x00;
918 /* This is used as an array index so must ensure it's not too large. */
919 configASSERT( uxPriority < configMAX_PRIORITIES );
921 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
923 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
927 mtCOVERAGE_TEST_MARKER();
930 pxNewTCB->uxPriority = uxPriority;
931 #if ( configUSE_MUTEXES == 1 )
933 pxNewTCB->uxBasePriority = uxPriority;
934 pxNewTCB->uxMutexesHeld = 0;
936 #endif /* configUSE_MUTEXES */
938 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
939 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
941 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
942 * back to the containing TCB from a generic item in a list. */
943 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
945 /* Event lists are always in priority order. */
946 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. */
947 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
949 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
951 pxNewTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
953 #endif /* portCRITICAL_NESTING_IN_TCB */
955 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
957 pxNewTCB->pxTaskTag = NULL;
959 #endif /* configUSE_APPLICATION_TASK_TAG */
961 #if ( configGENERATE_RUN_TIME_STATS == 1 )
963 pxNewTCB->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
965 #endif /* configGENERATE_RUN_TIME_STATS */
967 #if ( portUSING_MPU_WRAPPERS == 1 )
969 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
973 /* Avoid compiler warning about unreferenced parameter. */
978 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
980 memset( ( void * ) &( pxNewTCB->pvThreadLocalStoragePointers[ 0 ] ), 0x00, sizeof( pxNewTCB->pvThreadLocalStoragePointers ) );
984 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
986 memset( ( void * ) &( pxNewTCB->ulNotifiedValue[ 0 ] ), 0x00, sizeof( pxNewTCB->ulNotifiedValue ) );
987 memset( ( void * ) &( pxNewTCB->ucNotifyState[ 0 ] ), 0x00, sizeof( pxNewTCB->ucNotifyState ) );
991 #if ( configUSE_NEWLIB_REENTRANT == 1 )
993 /* Initialise this task's Newlib reent structure.
994 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
995 * for additional information. */
996 _REENT_INIT_PTR( ( &( pxNewTCB->xNewLib_reent ) ) );
1000 #if ( INCLUDE_xTaskAbortDelay == 1 )
1002 pxNewTCB->ucDelayAborted = pdFALSE;
1006 /* Initialize the TCB stack to look as if the task was already running,
1007 * but had been interrupted by the scheduler. The return address is set
1008 * to the start of the task function. Once the stack has been initialised
1009 * the top of stack variable is updated. */
1010 #if ( portUSING_MPU_WRAPPERS == 1 )
1012 /* If the port has capability to detect stack overflow,
1013 * pass the stack end address to the stack initialization
1014 * function as well. */
1015 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1017 #if ( portSTACK_GROWTH < 0 )
1019 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
1021 #else /* portSTACK_GROWTH */
1023 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1025 #endif /* portSTACK_GROWTH */
1027 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1029 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1031 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1033 #else /* portUSING_MPU_WRAPPERS */
1035 /* If the port has capability to detect stack overflow,
1036 * pass the stack end address to the stack initialization
1037 * function as well. */
1038 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1040 #if ( portSTACK_GROWTH < 0 )
1042 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1044 #else /* portSTACK_GROWTH */
1046 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1048 #endif /* portSTACK_GROWTH */
1050 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1052 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1054 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1056 #endif /* portUSING_MPU_WRAPPERS */
1058 if( pxCreatedTask != NULL )
1060 /* Pass the handle out in an anonymous way. The handle can be used to
1061 * change the created task's priority, delete the created task, etc.*/
1062 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1066 mtCOVERAGE_TEST_MARKER();
1069 /*-----------------------------------------------------------*/
1071 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1073 /* Ensure interrupts don't access the task lists while the lists are being
1075 taskENTER_CRITICAL();
1077 uxCurrentNumberOfTasks++;
1079 if( pxCurrentTCB == NULL )
1081 /* There are no other tasks, or all the other tasks are in
1082 * the suspended state - make this the current task. */
1083 pxCurrentTCB = pxNewTCB;
1085 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1087 /* This is the first task to be created so do the preliminary
1088 * initialisation required. We will not recover if this call
1089 * fails, but we will report the failure. */
1090 prvInitialiseTaskLists();
1094 mtCOVERAGE_TEST_MARKER();
1099 /* If the scheduler is not already running, make this task the
1100 * current task if it is the highest priority task to be created
1102 if( xSchedulerRunning == pdFALSE )
1104 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
1106 pxCurrentTCB = pxNewTCB;
1110 mtCOVERAGE_TEST_MARKER();
1115 mtCOVERAGE_TEST_MARKER();
1121 #if ( configUSE_TRACE_FACILITY == 1 )
1123 /* Add a counter into the TCB for tracing only. */
1124 pxNewTCB->uxTCBNumber = uxTaskNumber;
1126 #endif /* configUSE_TRACE_FACILITY */
1127 traceTASK_CREATE( pxNewTCB );
1129 prvAddTaskToReadyList( pxNewTCB );
1131 portSETUP_TCB( pxNewTCB );
1133 taskEXIT_CRITICAL();
1135 if( xSchedulerRunning != pdFALSE )
1137 /* If the created task is of a higher priority than the current task
1138 * then it should run now. */
1139 if( pxCurrentTCB->uxPriority < pxNewTCB->uxPriority )
1141 taskYIELD_IF_USING_PREEMPTION();
1145 mtCOVERAGE_TEST_MARKER();
1150 mtCOVERAGE_TEST_MARKER();
1153 /*-----------------------------------------------------------*/
1155 #if ( INCLUDE_vTaskDelete == 1 )
1157 void vTaskDelete( TaskHandle_t xTaskToDelete )
1161 taskENTER_CRITICAL();
1163 /* If null is passed in here then it is the calling task that is
1165 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1167 /* Remove task from the ready/delayed list. */
1168 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1170 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1174 mtCOVERAGE_TEST_MARKER();
1177 /* Is the task waiting on an event also? */
1178 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1180 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1184 mtCOVERAGE_TEST_MARKER();
1187 /* Increment the uxTaskNumber also so kernel aware debuggers can
1188 * detect that the task lists need re-generating. This is done before
1189 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1193 if( pxTCB == pxCurrentTCB )
1195 /* A task is deleting itself. This cannot complete within the
1196 * task itself, as a context switch to another task is required.
1197 * Place the task in the termination list. The idle task will
1198 * check the termination list and free up any memory allocated by
1199 * the scheduler for the TCB and stack of the deleted task. */
1200 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
1202 /* Increment the ucTasksDeleted variable so the idle task knows
1203 * there is a task that has been deleted and that it should therefore
1204 * check the xTasksWaitingTermination list. */
1205 ++uxDeletedTasksWaitingCleanUp;
1207 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1208 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1209 traceTASK_DELETE( pxTCB );
1211 /* The pre-delete hook is primarily for the Windows simulator,
1212 * in which Windows specific clean up operations are performed,
1213 * after which it is not possible to yield away from this task -
1214 * hence xYieldPending is used to latch that a context switch is
1216 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPending );
1220 --uxCurrentNumberOfTasks;
1221 traceTASK_DELETE( pxTCB );
1223 /* Reset the next expected unblock time in case it referred to
1224 * the task that has just been deleted. */
1225 prvResetNextTaskUnblockTime();
1228 taskEXIT_CRITICAL();
1230 /* If the task is not deleting itself, call prvDeleteTCB from outside of
1231 * critical section. If a task deletes itself, prvDeleteTCB is called
1232 * from prvCheckTasksWaitingTermination which is called from Idle task. */
1233 if( pxTCB != pxCurrentTCB )
1235 prvDeleteTCB( pxTCB );
1238 /* Force a reschedule if it is the currently running task that has just
1240 if( xSchedulerRunning != pdFALSE )
1242 if( pxTCB == pxCurrentTCB )
1244 configASSERT( uxSchedulerSuspended == 0 );
1245 portYIELD_WITHIN_API();
1249 mtCOVERAGE_TEST_MARKER();
1254 #endif /* INCLUDE_vTaskDelete */
1255 /*-----------------------------------------------------------*/
1257 #if ( INCLUDE_xTaskDelayUntil == 1 )
1259 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
1260 const TickType_t xTimeIncrement )
1262 TickType_t xTimeToWake;
1263 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
1265 configASSERT( pxPreviousWakeTime );
1266 configASSERT( ( xTimeIncrement > 0U ) );
1267 configASSERT( uxSchedulerSuspended == 0 );
1271 /* Minor optimisation. The tick count cannot change in this
1273 const TickType_t xConstTickCount = xTickCount;
1275 /* Generate the tick time at which the task wants to wake. */
1276 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
1278 if( xConstTickCount < *pxPreviousWakeTime )
1280 /* The tick count has overflowed since this function was
1281 * lasted called. In this case the only time we should ever
1282 * actually delay is if the wake time has also overflowed,
1283 * and the wake time is greater than the tick time. When this
1284 * is the case it is as if neither time had overflowed. */
1285 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1287 xShouldDelay = pdTRUE;
1291 mtCOVERAGE_TEST_MARKER();
1296 /* The tick time has not overflowed. In this case we will
1297 * delay if either the wake time has overflowed, and/or the
1298 * tick time is less than the wake time. */
1299 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1301 xShouldDelay = pdTRUE;
1305 mtCOVERAGE_TEST_MARKER();
1309 /* Update the wake time ready for the next call. */
1310 *pxPreviousWakeTime = xTimeToWake;
1312 if( xShouldDelay != pdFALSE )
1314 traceTASK_DELAY_UNTIL( xTimeToWake );
1316 /* prvAddCurrentTaskToDelayedList() needs the block time, not
1317 * the time to wake, so subtract the current tick count. */
1318 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
1322 mtCOVERAGE_TEST_MARKER();
1325 xAlreadyYielded = xTaskResumeAll();
1327 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1328 * have put ourselves to sleep. */
1329 if( xAlreadyYielded == pdFALSE )
1331 portYIELD_WITHIN_API();
1335 mtCOVERAGE_TEST_MARKER();
1338 return xShouldDelay;
1341 #endif /* INCLUDE_xTaskDelayUntil */
1342 /*-----------------------------------------------------------*/
1344 #if ( INCLUDE_vTaskDelay == 1 )
1346 void vTaskDelay( const TickType_t xTicksToDelay )
1348 BaseType_t xAlreadyYielded = pdFALSE;
1350 /* A delay time of zero just forces a reschedule. */
1351 if( xTicksToDelay > ( TickType_t ) 0U )
1353 configASSERT( uxSchedulerSuspended == 0 );
1358 /* A task that is removed from the event list while the
1359 * scheduler is suspended will not get placed in the ready
1360 * list or removed from the blocked list until the scheduler
1363 * This task cannot be in an event list as it is the currently
1364 * executing task. */
1365 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
1367 xAlreadyYielded = xTaskResumeAll();
1371 mtCOVERAGE_TEST_MARKER();
1374 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1375 * have put ourselves to sleep. */
1376 if( xAlreadyYielded == pdFALSE )
1378 portYIELD_WITHIN_API();
1382 mtCOVERAGE_TEST_MARKER();
1386 #endif /* INCLUDE_vTaskDelay */
1387 /*-----------------------------------------------------------*/
1389 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
1391 eTaskState eTaskGetState( TaskHandle_t xTask )
1394 List_t const * pxStateList, * pxDelayedList, * pxOverflowedDelayedList;
1395 const TCB_t * const pxTCB = xTask;
1397 configASSERT( pxTCB );
1399 if( pxTCB == pxCurrentTCB )
1401 /* The task calling this function is querying its own state. */
1406 taskENTER_CRITICAL();
1408 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
1409 pxDelayedList = pxDelayedTaskList;
1410 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
1412 taskEXIT_CRITICAL();
1414 if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
1416 /* The task being queried is referenced from one of the Blocked
1421 #if ( INCLUDE_vTaskSuspend == 1 )
1422 else if( pxStateList == &xSuspendedTaskList )
1424 /* The task being queried is referenced from the suspended
1425 * list. Is it genuinely suspended or is it blocked
1427 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
1429 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1433 /* The task does not appear on the event list item of
1434 * and of the RTOS objects, but could still be in the
1435 * blocked state if it is waiting on its notification
1436 * rather than waiting on an object. If not, is
1438 eReturn = eSuspended;
1440 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1442 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1449 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1451 eReturn = eSuspended;
1453 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1460 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
1462 #if ( INCLUDE_vTaskDelete == 1 )
1463 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
1465 /* The task being queried is referenced from the deleted
1466 * tasks list, or it is not referenced from any lists at
1472 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
1474 /* If the task is not in any other state, it must be in the
1475 * Ready (including pending ready) state. */
1481 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
1483 #endif /* INCLUDE_eTaskGetState */
1484 /*-----------------------------------------------------------*/
1486 #if ( INCLUDE_uxTaskPriorityGet == 1 )
1488 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
1490 TCB_t const * pxTCB;
1491 UBaseType_t uxReturn;
1493 taskENTER_CRITICAL();
1495 /* If null is passed in here then it is the priority of the task
1496 * that called uxTaskPriorityGet() that is being queried. */
1497 pxTCB = prvGetTCBFromHandle( xTask );
1498 uxReturn = pxTCB->uxPriority;
1500 taskEXIT_CRITICAL();
1505 #endif /* INCLUDE_uxTaskPriorityGet */
1506 /*-----------------------------------------------------------*/
1508 #if ( INCLUDE_uxTaskPriorityGet == 1 )
1510 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
1512 TCB_t const * pxTCB;
1513 UBaseType_t uxReturn, uxSavedInterruptState;
1515 /* RTOS ports that support interrupt nesting have the concept of a
1516 * maximum system call (or maximum API call) interrupt priority.
1517 * Interrupts that are above the maximum system call priority are keep
1518 * permanently enabled, even when the RTOS kernel is in a critical section,
1519 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
1520 * is defined in FreeRTOSConfig.h then
1521 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1522 * failure if a FreeRTOS API function is called from an interrupt that has
1523 * been assigned a priority above the configured maximum system call
1524 * priority. Only FreeRTOS functions that end in FromISR can be called
1525 * from interrupts that have been assigned a priority at or (logically)
1526 * below the maximum system call interrupt priority. FreeRTOS maintains a
1527 * separate interrupt safe API to ensure interrupt entry is as fast and as
1528 * simple as possible. More information (albeit Cortex-M specific) is
1529 * provided on the following link:
1530 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1531 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1533 uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
1535 /* If null is passed in here then it is the priority of the calling
1536 * task that is being queried. */
1537 pxTCB = prvGetTCBFromHandle( xTask );
1538 uxReturn = pxTCB->uxPriority;
1540 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
1545 #endif /* INCLUDE_uxTaskPriorityGet */
1546 /*-----------------------------------------------------------*/
1548 #if ( INCLUDE_vTaskPrioritySet == 1 )
1550 void vTaskPrioritySet( TaskHandle_t xTask,
1551 UBaseType_t uxNewPriority )
1554 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
1555 BaseType_t xYieldRequired = pdFALSE;
1557 configASSERT( uxNewPriority < configMAX_PRIORITIES );
1559 /* Ensure the new priority is valid. */
1560 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1562 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1566 mtCOVERAGE_TEST_MARKER();
1569 taskENTER_CRITICAL();
1571 /* If null is passed in here then it is the priority of the calling
1572 * task that is being changed. */
1573 pxTCB = prvGetTCBFromHandle( xTask );
1575 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
1577 #if ( configUSE_MUTEXES == 1 )
1579 uxCurrentBasePriority = pxTCB->uxBasePriority;
1583 uxCurrentBasePriority = pxTCB->uxPriority;
1587 if( uxCurrentBasePriority != uxNewPriority )
1589 /* The priority change may have readied a task of higher
1590 * priority than the calling task. */
1591 if( uxNewPriority > uxCurrentBasePriority )
1593 if( pxTCB != pxCurrentTCB )
1595 /* The priority of a task other than the currently
1596 * running task is being raised. Is the priority being
1597 * raised above that of the running task? */
1598 if( uxNewPriority >= pxCurrentTCB->uxPriority )
1600 xYieldRequired = pdTRUE;
1604 mtCOVERAGE_TEST_MARKER();
1609 /* The priority of the running task is being raised,
1610 * but the running task must already be the highest
1611 * priority task able to run so no yield is required. */
1614 else if( pxTCB == pxCurrentTCB )
1616 /* Setting the priority of the running task down means
1617 * there may now be another task of higher priority that
1618 * is ready to execute. */
1619 xYieldRequired = pdTRUE;
1623 /* Setting the priority of any other task down does not
1624 * require a yield as the running task must be above the
1625 * new priority of the task being modified. */
1628 /* Remember the ready list the task might be referenced from
1629 * before its uxPriority member is changed so the
1630 * taskRESET_READY_PRIORITY() macro can function correctly. */
1631 uxPriorityUsedOnEntry = pxTCB->uxPriority;
1633 #if ( configUSE_MUTEXES == 1 )
1635 /* Only change the priority being used if the task is not
1636 * currently using an inherited priority. */
1637 if( pxTCB->uxBasePriority == pxTCB->uxPriority )
1639 pxTCB->uxPriority = uxNewPriority;
1643 mtCOVERAGE_TEST_MARKER();
1646 /* The base priority gets set whatever. */
1647 pxTCB->uxBasePriority = uxNewPriority;
1649 #else /* if ( configUSE_MUTEXES == 1 ) */
1651 pxTCB->uxPriority = uxNewPriority;
1653 #endif /* if ( configUSE_MUTEXES == 1 ) */
1655 /* Only reset the event list item value if the value is not
1656 * being used for anything else. */
1657 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
1659 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. */
1663 mtCOVERAGE_TEST_MARKER();
1666 /* If the task is in the blocked or suspended list we need do
1667 * nothing more than change its priority variable. However, if
1668 * the task is in a ready list it needs to be removed and placed
1669 * in the list appropriate to its new priority. */
1670 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
1672 /* The task is currently in its ready list - remove before
1673 * adding it to its new ready list. As we are in a critical
1674 * section we can do this even if the scheduler is suspended. */
1675 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1677 /* It is known that the task is in its ready list so
1678 * there is no need to check again and the port level
1679 * reset macro can be called directly. */
1680 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
1684 mtCOVERAGE_TEST_MARKER();
1687 prvAddTaskToReadyList( pxTCB );
1691 mtCOVERAGE_TEST_MARKER();
1694 if( xYieldRequired != pdFALSE )
1696 taskYIELD_IF_USING_PREEMPTION();
1700 mtCOVERAGE_TEST_MARKER();
1703 /* Remove compiler warning about unused variables when the port
1704 * optimised task selection is not being used. */
1705 ( void ) uxPriorityUsedOnEntry;
1708 taskEXIT_CRITICAL();
1711 #endif /* INCLUDE_vTaskPrioritySet */
1712 /*-----------------------------------------------------------*/
1714 #if ( INCLUDE_vTaskSuspend == 1 )
1716 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
1720 taskENTER_CRITICAL();
1722 /* If null is passed in here then it is the running task that is
1723 * being suspended. */
1724 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
1726 traceTASK_SUSPEND( pxTCB );
1728 /* Remove task from the ready/delayed list and place in the
1729 * suspended list. */
1730 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1732 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1736 mtCOVERAGE_TEST_MARKER();
1739 /* Is the task waiting on an event also? */
1740 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1742 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1746 mtCOVERAGE_TEST_MARKER();
1749 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
1751 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1755 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1757 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1759 /* The task was blocked to wait for a notification, but is
1760 * now suspended, so no notification was received. */
1761 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
1765 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1767 taskEXIT_CRITICAL();
1769 if( xSchedulerRunning != pdFALSE )
1771 /* Reset the next expected unblock time in case it referred to the
1772 * task that is now in the Suspended state. */
1773 taskENTER_CRITICAL();
1775 prvResetNextTaskUnblockTime();
1777 taskEXIT_CRITICAL();
1781 mtCOVERAGE_TEST_MARKER();
1784 if( pxTCB == pxCurrentTCB )
1786 if( xSchedulerRunning != pdFALSE )
1788 /* The current task has just been suspended. */
1789 configASSERT( uxSchedulerSuspended == 0 );
1790 portYIELD_WITHIN_API();
1794 /* The scheduler is not running, but the task that was pointed
1795 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
1796 * must be adjusted to point to a different task. */
1797 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
1799 /* No other tasks are ready, so set pxCurrentTCB back to
1800 * NULL so when the next task is created pxCurrentTCB will
1801 * be set to point to it no matter what its relative priority
1803 pxCurrentTCB = NULL;
1807 vTaskSwitchContext();
1813 mtCOVERAGE_TEST_MARKER();
1817 #endif /* INCLUDE_vTaskSuspend */
1818 /*-----------------------------------------------------------*/
1820 #if ( INCLUDE_vTaskSuspend == 1 )
1822 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
1824 BaseType_t xReturn = pdFALSE;
1825 const TCB_t * const pxTCB = xTask;
1827 /* Accesses xPendingReadyList so must be called from a critical
1830 /* It does not make sense to check if the calling task is suspended. */
1831 configASSERT( xTask );
1833 /* Is the task being resumed actually in the suspended list? */
1834 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
1836 /* Has the task already been resumed from within an ISR? */
1837 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
1839 /* Is it in the suspended list because it is in the Suspended
1840 * state, or because is is blocked with no timeout? */
1841 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
1847 mtCOVERAGE_TEST_MARKER();
1852 mtCOVERAGE_TEST_MARKER();
1857 mtCOVERAGE_TEST_MARKER();
1861 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
1863 #endif /* INCLUDE_vTaskSuspend */
1864 /*-----------------------------------------------------------*/
1866 #if ( INCLUDE_vTaskSuspend == 1 )
1868 void vTaskResume( TaskHandle_t xTaskToResume )
1870 TCB_t * const pxTCB = xTaskToResume;
1872 /* It does not make sense to resume the calling task. */
1873 configASSERT( xTaskToResume );
1875 /* The parameter cannot be NULL as it is impossible to resume the
1876 * currently executing task. */
1877 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
1879 taskENTER_CRITICAL();
1881 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
1883 traceTASK_RESUME( pxTCB );
1885 /* The ready list can be accessed even if the scheduler is
1886 * suspended because this is inside a critical section. */
1887 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
1888 prvAddTaskToReadyList( pxTCB );
1890 /* A higher priority task may have just been resumed. */
1891 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
1893 /* This yield may not cause the task just resumed to run,
1894 * but will leave the lists in the correct state for the
1896 taskYIELD_IF_USING_PREEMPTION();
1900 mtCOVERAGE_TEST_MARKER();
1905 mtCOVERAGE_TEST_MARKER();
1908 taskEXIT_CRITICAL();
1912 mtCOVERAGE_TEST_MARKER();
1916 #endif /* INCLUDE_vTaskSuspend */
1918 /*-----------------------------------------------------------*/
1920 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
1922 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
1924 BaseType_t xYieldRequired = pdFALSE;
1925 TCB_t * const pxTCB = xTaskToResume;
1926 UBaseType_t uxSavedInterruptStatus;
1928 configASSERT( xTaskToResume );
1930 /* RTOS ports that support interrupt nesting have the concept of a
1931 * maximum system call (or maximum API call) interrupt priority.
1932 * Interrupts that are above the maximum system call priority are keep
1933 * permanently enabled, even when the RTOS kernel is in a critical section,
1934 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
1935 * is defined in FreeRTOSConfig.h then
1936 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1937 * failure if a FreeRTOS API function is called from an interrupt that has
1938 * been assigned a priority above the configured maximum system call
1939 * priority. Only FreeRTOS functions that end in FromISR can be called
1940 * from interrupts that have been assigned a priority at or (logically)
1941 * below the maximum system call interrupt priority. FreeRTOS maintains a
1942 * separate interrupt safe API to ensure interrupt entry is as fast and as
1943 * simple as possible. More information (albeit Cortex-M specific) is
1944 * provided on the following link:
1945 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1946 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
1948 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1950 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
1952 traceTASK_RESUME_FROM_ISR( pxTCB );
1954 /* Check the ready lists can be accessed. */
1955 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
1957 /* Ready lists can be accessed so move the task from the
1958 * suspended list to the ready list directly. */
1959 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
1961 xYieldRequired = pdTRUE;
1963 /* Mark that a yield is pending in case the user is not
1964 * using the return value to initiate a context switch
1965 * from the ISR using portYIELD_FROM_ISR. */
1966 xYieldPending = pdTRUE;
1970 mtCOVERAGE_TEST_MARKER();
1973 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
1974 prvAddTaskToReadyList( pxTCB );
1978 /* The delayed or ready lists cannot be accessed so the task
1979 * is held in the pending ready list until the scheduler is
1981 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
1986 mtCOVERAGE_TEST_MARKER();
1989 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1991 return xYieldRequired;
1994 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
1995 /*-----------------------------------------------------------*/
1997 void vTaskStartScheduler( void )
2001 /* Add the idle task at the lowest priority. */
2002 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
2004 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
2005 StackType_t * pxIdleTaskStackBuffer = NULL;
2006 uint32_t ulIdleTaskStackSize;
2008 /* The Idle task is created using user provided RAM - obtain the
2009 * address of the RAM then create the idle task. */
2010 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
2011 xIdleTaskHandle = xTaskCreateStatic( prvIdleTask,
2012 configIDLE_TASK_NAME,
2013 ulIdleTaskStackSize,
2014 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
2015 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2016 pxIdleTaskStackBuffer,
2017 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2019 if( xIdleTaskHandle != NULL )
2028 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
2030 /* The Idle task is being created using dynamically allocated RAM. */
2031 xReturn = xTaskCreate( prvIdleTask,
2032 configIDLE_TASK_NAME,
2033 configMINIMAL_STACK_SIZE,
2035 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
2036 &xIdleTaskHandle ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2038 #endif /* configSUPPORT_STATIC_ALLOCATION */
2040 #if ( configUSE_TIMERS == 1 )
2042 if( xReturn == pdPASS )
2044 xReturn = xTimerCreateTimerTask();
2048 mtCOVERAGE_TEST_MARKER();
2051 #endif /* configUSE_TIMERS */
2053 if( xReturn == pdPASS )
2055 /* freertos_tasks_c_additions_init() should only be called if the user
2056 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
2057 * the only macro called by the function. */
2058 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
2060 freertos_tasks_c_additions_init();
2064 /* Interrupts are turned off here, to ensure a tick does not occur
2065 * before or during the call to xPortStartScheduler(). The stacks of
2066 * the created tasks contain a status word with interrupts switched on
2067 * so interrupts will automatically get re-enabled when the first task
2069 portDISABLE_INTERRUPTS();
2071 #if ( configUSE_NEWLIB_REENTRANT == 1 )
2073 /* Switch Newlib's _impure_ptr variable to point to the _reent
2074 * structure specific to the task that will run first.
2075 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
2076 * for additional information. */
2077 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
2079 #endif /* configUSE_NEWLIB_REENTRANT */
2081 xNextTaskUnblockTime = portMAX_DELAY;
2082 xSchedulerRunning = pdTRUE;
2083 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
2085 /* If configGENERATE_RUN_TIME_STATS is defined then the following
2086 * macro must be defined to configure the timer/counter used to generate
2087 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
2088 * is set to 0 and the following line fails to build then ensure you do not
2089 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
2090 * FreeRTOSConfig.h file. */
2091 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
2093 traceTASK_SWITCHED_IN();
2095 /* Setting up the timer tick is hardware specific and thus in the
2096 * portable interface. */
2097 if( xPortStartScheduler() != pdFALSE )
2099 /* Should not reach here as if the scheduler is running the
2100 * function will not return. */
2104 /* Should only reach here if a task calls xTaskEndScheduler(). */
2109 /* This line will only be reached if the kernel could not be started,
2110 * because there was not enough FreeRTOS heap to create the idle task
2111 * or the timer task. */
2112 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
2115 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
2116 * meaning xIdleTaskHandle is not used anywhere else. */
2117 ( void ) xIdleTaskHandle;
2119 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
2120 * from getting optimized out as it is no longer used by the kernel. */
2121 ( void ) uxTopUsedPriority;
2123 /*-----------------------------------------------------------*/
2125 void vTaskEndScheduler( void )
2127 /* Stop the scheduler interrupts and call the portable scheduler end
2128 * routine so the original ISRs can be restored if necessary. The port
2129 * layer must ensure interrupts enable bit is left in the correct state. */
2130 portDISABLE_INTERRUPTS();
2131 xSchedulerRunning = pdFALSE;
2132 vPortEndScheduler();
2134 /*----------------------------------------------------------*/
2136 void vTaskSuspendAll( void )
2138 /* A critical section is not required as the variable is of type
2139 * BaseType_t. Please read Richard Barry's reply in the following link to a
2140 * post in the FreeRTOS support forum before reporting this as a bug! -
2141 * https://goo.gl/wu4acr */
2143 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
2144 * do not otherwise exhibit real time behaviour. */
2145 portSOFTWARE_BARRIER();
2147 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
2148 * is used to allow calls to vTaskSuspendAll() to nest. */
2149 ++uxSchedulerSuspended;
2151 /* Enforces ordering for ports and optimised compilers that may otherwise place
2152 * the above increment elsewhere. */
2153 portMEMORY_BARRIER();
2155 /*----------------------------------------------------------*/
2157 #if ( configUSE_TICKLESS_IDLE != 0 )
2159 static TickType_t prvGetExpectedIdleTime( void )
2162 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
2164 /* uxHigherPriorityReadyTasks takes care of the case where
2165 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
2166 * task that are in the Ready state, even though the idle task is
2168 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
2170 if( uxTopReadyPriority > tskIDLE_PRIORITY )
2172 uxHigherPriorityReadyTasks = pdTRUE;
2177 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
2179 /* When port optimised task selection is used the uxTopReadyPriority
2180 * variable is used as a bit map. If bits other than the least
2181 * significant bit are set then there are tasks that have a priority
2182 * above the idle priority that are in the Ready state. This takes
2183 * care of the case where the co-operative scheduler is in use. */
2184 if( uxTopReadyPriority > uxLeastSignificantBit )
2186 uxHigherPriorityReadyTasks = pdTRUE;
2189 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
2191 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
2195 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
2197 /* There are other idle priority tasks in the ready state. If
2198 * time slicing is used then the very next tick interrupt must be
2202 else if( uxHigherPriorityReadyTasks != pdFALSE )
2204 /* There are tasks in the Ready state that have a priority above the
2205 * idle priority. This path can only be reached if
2206 * configUSE_PREEMPTION is 0. */
2211 xReturn = xNextTaskUnblockTime - xTickCount;
2217 #endif /* configUSE_TICKLESS_IDLE */
2218 /*----------------------------------------------------------*/
2220 BaseType_t xTaskResumeAll( void )
2222 TCB_t * pxTCB = NULL;
2223 BaseType_t xAlreadyYielded = pdFALSE;
2225 /* If uxSchedulerSuspended is zero then this function does not match a
2226 * previous call to vTaskSuspendAll(). */
2227 configASSERT( uxSchedulerSuspended );
2229 /* It is possible that an ISR caused a task to be removed from an event
2230 * list while the scheduler was suspended. If this was the case then the
2231 * removed task will have been added to the xPendingReadyList. Once the
2232 * scheduler has been resumed it is safe to move all the pending ready
2233 * tasks from this list into their appropriate ready list. */
2234 taskENTER_CRITICAL();
2236 --uxSchedulerSuspended;
2238 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2240 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
2242 /* Move any readied tasks from the pending list into the
2243 * appropriate ready list. */
2244 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
2246 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. */
2247 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
2248 portMEMORY_BARRIER();
2249 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
2250 prvAddTaskToReadyList( pxTCB );
2252 /* If the moved task has a priority higher than or equal to
2253 * the current task then a yield must be performed. */
2254 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
2256 xYieldPending = pdTRUE;
2260 mtCOVERAGE_TEST_MARKER();
2266 /* A task was unblocked while the scheduler was suspended,
2267 * which may have prevented the next unblock time from being
2268 * re-calculated, in which case re-calculate it now. Mainly
2269 * important for low power tickless implementations, where
2270 * this can prevent an unnecessary exit from low power
2272 prvResetNextTaskUnblockTime();
2275 /* If any ticks occurred while the scheduler was suspended then
2276 * they should be processed now. This ensures the tick count does
2277 * not slip, and that any delayed tasks are resumed at the correct
2280 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
2282 if( xPendedCounts > ( TickType_t ) 0U )
2286 if( xTaskIncrementTick() != pdFALSE )
2288 xYieldPending = pdTRUE;
2292 mtCOVERAGE_TEST_MARKER();
2296 } while( xPendedCounts > ( TickType_t ) 0U );
2302 mtCOVERAGE_TEST_MARKER();
2306 if( xYieldPending != pdFALSE )
2308 #if ( configUSE_PREEMPTION != 0 )
2310 xAlreadyYielded = pdTRUE;
2313 taskYIELD_IF_USING_PREEMPTION();
2317 mtCOVERAGE_TEST_MARKER();
2323 mtCOVERAGE_TEST_MARKER();
2326 taskEXIT_CRITICAL();
2328 return xAlreadyYielded;
2330 /*-----------------------------------------------------------*/
2332 TickType_t xTaskGetTickCount( void )
2336 /* Critical section required if running on a 16 bit processor. */
2337 portTICK_TYPE_ENTER_CRITICAL();
2339 xTicks = xTickCount;
2341 portTICK_TYPE_EXIT_CRITICAL();
2345 /*-----------------------------------------------------------*/
2347 TickType_t xTaskGetTickCountFromISR( void )
2350 UBaseType_t uxSavedInterruptStatus;
2352 /* RTOS ports that support interrupt nesting have the concept of a maximum
2353 * system call (or maximum API call) interrupt priority. Interrupts that are
2354 * above the maximum system call priority are kept permanently enabled, even
2355 * when the RTOS kernel is in a critical section, but cannot make any calls to
2356 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
2357 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2358 * failure if a FreeRTOS API function is called from an interrupt that has been
2359 * assigned a priority above the configured maximum system call priority.
2360 * Only FreeRTOS functions that end in FromISR can be called from interrupts
2361 * that have been assigned a priority at or (logically) below the maximum
2362 * system call interrupt priority. FreeRTOS maintains a separate interrupt
2363 * safe API to ensure interrupt entry is as fast and as simple as possible.
2364 * More information (albeit Cortex-M specific) is provided on the following
2365 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2366 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2368 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
2370 xReturn = xTickCount;
2372 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2376 /*-----------------------------------------------------------*/
2378 UBaseType_t uxTaskGetNumberOfTasks( void )
2380 /* A critical section is not required because the variables are of type
2382 return uxCurrentNumberOfTasks;
2384 /*-----------------------------------------------------------*/
2386 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2390 /* If null is passed in here then the name of the calling task is being
2392 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
2393 configASSERT( pxTCB );
2394 return &( pxTCB->pcTaskName[ 0 ] );
2396 /*-----------------------------------------------------------*/
2398 #if ( INCLUDE_xTaskGetHandle == 1 )
2400 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
2401 const char pcNameToQuery[] )
2403 TCB_t * pxNextTCB, * pxFirstTCB, * pxReturn = NULL;
2406 BaseType_t xBreakLoop;
2408 /* This function is called with the scheduler suspended. */
2410 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
2412 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. */
2416 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. */
2418 /* Check each character in the name looking for a match or
2420 xBreakLoop = pdFALSE;
2422 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
2424 cNextChar = pxNextTCB->pcTaskName[ x ];
2426 if( cNextChar != pcNameToQuery[ x ] )
2428 /* Characters didn't match. */
2429 xBreakLoop = pdTRUE;
2431 else if( cNextChar == ( char ) 0x00 )
2433 /* Both strings terminated, a match must have been
2435 pxReturn = pxNextTCB;
2436 xBreakLoop = pdTRUE;
2440 mtCOVERAGE_TEST_MARKER();
2443 if( xBreakLoop != pdFALSE )
2449 if( pxReturn != NULL )
2451 /* The handle has been found. */
2454 } while( pxNextTCB != pxFirstTCB );
2458 mtCOVERAGE_TEST_MARKER();
2464 #endif /* INCLUDE_xTaskGetHandle */
2465 /*-----------------------------------------------------------*/
2467 #if ( INCLUDE_xTaskGetHandle == 1 )
2469 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2471 UBaseType_t uxQueue = configMAX_PRIORITIES;
2474 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
2475 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
2479 /* Search the ready lists. */
2483 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
2487 /* Found the handle. */
2490 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2492 /* Search the delayed lists. */
2495 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
2500 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
2503 #if ( INCLUDE_vTaskSuspend == 1 )
2507 /* Search the suspended list. */
2508 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
2513 #if ( INCLUDE_vTaskDelete == 1 )
2517 /* Search the deleted list. */
2518 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
2523 ( void ) xTaskResumeAll();
2528 #endif /* INCLUDE_xTaskGetHandle */
2529 /*-----------------------------------------------------------*/
2531 #if ( configUSE_TRACE_FACILITY == 1 )
2533 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
2534 const UBaseType_t uxArraySize,
2535 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
2537 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
2541 /* Is there a space in the array for each task in the system? */
2542 if( uxArraySize >= uxCurrentNumberOfTasks )
2544 /* Fill in an TaskStatus_t structure with information on each
2545 * task in the Ready state. */
2549 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
2550 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2552 /* Fill in an TaskStatus_t structure with information on each
2553 * task in the Blocked state. */
2554 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
2555 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
2557 #if ( INCLUDE_vTaskDelete == 1 )
2559 /* Fill in an TaskStatus_t structure with information on
2560 * each task that has been deleted but not yet cleaned up. */
2561 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
2565 #if ( INCLUDE_vTaskSuspend == 1 )
2567 /* Fill in an TaskStatus_t structure with information on
2568 * each task in the Suspended state. */
2569 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
2573 #if ( configGENERATE_RUN_TIME_STATS == 1 )
2575 if( pulTotalRunTime != NULL )
2577 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
2578 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
2580 *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
2584 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
2586 if( pulTotalRunTime != NULL )
2588 *pulTotalRunTime = 0;
2591 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
2595 mtCOVERAGE_TEST_MARKER();
2598 ( void ) xTaskResumeAll();
2603 #endif /* configUSE_TRACE_FACILITY */
2604 /*----------------------------------------------------------*/
2606 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
2608 TaskHandle_t xTaskGetIdleTaskHandle( void )
2610 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
2611 * started, then xIdleTaskHandle will be NULL. */
2612 configASSERT( ( xIdleTaskHandle != NULL ) );
2613 return xIdleTaskHandle;
2616 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
2617 /*----------------------------------------------------------*/
2619 /* This conditional compilation should use inequality to 0, not equality to 1.
2620 * This is to ensure vTaskStepTick() is available when user defined low power mode
2621 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
2623 #if ( configUSE_TICKLESS_IDLE != 0 )
2625 void vTaskStepTick( const TickType_t xTicksToJump )
2627 /* Correct the tick count value after a period during which the tick
2628 * was suppressed. Note this does *not* call the tick hook function for
2629 * each stepped tick. */
2630 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
2631 xTickCount += xTicksToJump;
2632 traceINCREASE_TICK_COUNT( xTicksToJump );
2635 #endif /* configUSE_TICKLESS_IDLE */
2636 /*----------------------------------------------------------*/
2638 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
2640 BaseType_t xYieldOccurred;
2642 /* Must not be called with the scheduler suspended as the implementation
2643 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
2644 configASSERT( uxSchedulerSuspended == 0 );
2646 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
2647 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
2649 xPendedTicks += xTicksToCatchUp;
2650 xYieldOccurred = xTaskResumeAll();
2652 return xYieldOccurred;
2654 /*----------------------------------------------------------*/
2656 #if ( INCLUDE_xTaskAbortDelay == 1 )
2658 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
2660 TCB_t * pxTCB = xTask;
2663 configASSERT( pxTCB );
2667 /* A task can only be prematurely removed from the Blocked state if
2668 * it is actually in the Blocked state. */
2669 if( eTaskGetState( xTask ) == eBlocked )
2673 /* Remove the reference to the task from the blocked list. An
2674 * interrupt won't touch the xStateListItem because the
2675 * scheduler is suspended. */
2676 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
2678 /* Is the task waiting on an event also? If so remove it from
2679 * the event list too. Interrupts can touch the event list item,
2680 * even though the scheduler is suspended, so a critical section
2682 taskENTER_CRITICAL();
2684 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2686 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2688 /* This lets the task know it was forcibly removed from the
2689 * blocked state so it should not re-evaluate its block time and
2690 * then block again. */
2691 pxTCB->ucDelayAborted = pdTRUE;
2695 mtCOVERAGE_TEST_MARKER();
2698 taskEXIT_CRITICAL();
2700 /* Place the unblocked task into the appropriate ready list. */
2701 prvAddTaskToReadyList( pxTCB );
2703 /* A task being unblocked cannot cause an immediate context
2704 * switch if preemption is turned off. */
2705 #if ( configUSE_PREEMPTION == 1 )
2707 /* Preemption is on, but a context switch should only be
2708 * performed if the unblocked task has a priority that is
2709 * higher than the currently executing task. */
2710 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
2712 /* Pend the yield to be performed when the scheduler
2713 * is unsuspended. */
2714 xYieldPending = pdTRUE;
2718 mtCOVERAGE_TEST_MARKER();
2721 #endif /* configUSE_PREEMPTION */
2728 ( void ) xTaskResumeAll();
2733 #endif /* INCLUDE_xTaskAbortDelay */
2734 /*----------------------------------------------------------*/
2736 BaseType_t xTaskIncrementTick( void )
2739 TickType_t xItemValue;
2740 BaseType_t xSwitchRequired = pdFALSE;
2742 /* Called by the portable layer each time a tick interrupt occurs.
2743 * Increments the tick then checks to see if the new tick value will cause any
2744 * tasks to be unblocked. */
2745 traceTASK_INCREMENT_TICK( xTickCount );
2747 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2749 /* Minor optimisation. The tick count cannot change in this
2751 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
2753 /* Increment the RTOS tick, switching the delayed and overflowed
2754 * delayed lists if it wraps to 0. */
2755 xTickCount = xConstTickCount;
2757 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
2759 taskSWITCH_DELAYED_LISTS();
2763 mtCOVERAGE_TEST_MARKER();
2766 /* See if this tick has made a timeout expire. Tasks are stored in
2767 * the queue in the order of their wake time - meaning once one task
2768 * has been found whose block time has not expired there is no need to
2769 * look any further down the list. */
2770 if( xConstTickCount >= xNextTaskUnblockTime )
2774 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
2776 /* The delayed list is empty. Set xNextTaskUnblockTime
2777 * to the maximum possible value so it is extremely
2779 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
2780 * next time through. */
2781 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2786 /* The delayed list is not empty, get the value of the
2787 * item at the head of the delayed list. This is the time
2788 * at which the task at the head of the delayed list must
2789 * be removed from the Blocked state. */
2790 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. */
2791 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
2793 if( xConstTickCount < xItemValue )
2795 /* It is not time to unblock this item yet, but the
2796 * item value is the time at which the task at the head
2797 * of the blocked list must be removed from the Blocked
2798 * state - so record the item value in
2799 * xNextTaskUnblockTime. */
2800 xNextTaskUnblockTime = xItemValue;
2801 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
2805 mtCOVERAGE_TEST_MARKER();
2808 /* It is time to remove the item from the Blocked state. */
2809 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
2811 /* Is the task waiting on an event also? If so remove
2812 * it from the event list. */
2813 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2815 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
2819 mtCOVERAGE_TEST_MARKER();
2822 /* Place the unblocked task into the appropriate ready
2824 prvAddTaskToReadyList( pxTCB );
2826 /* A task being unblocked cannot cause an immediate
2827 * context switch if preemption is turned off. */
2828 #if ( configUSE_PREEMPTION == 1 )
2830 /* Preemption is on, but a context switch should
2831 * only be performed if the unblocked task has a
2832 * priority that is equal to or higher than the
2833 * currently executing task. */
2834 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
2836 xSwitchRequired = pdTRUE;
2840 mtCOVERAGE_TEST_MARKER();
2843 #endif /* configUSE_PREEMPTION */
2848 /* Tasks of equal priority to the currently running task will share
2849 * processing time (time slice) if preemption is on, and the application
2850 * writer has not explicitly turned time slicing off. */
2851 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
2853 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 )
2855 xSwitchRequired = pdTRUE;
2859 mtCOVERAGE_TEST_MARKER();
2862 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
2864 #if ( configUSE_TICK_HOOK == 1 )
2866 /* Guard against the tick hook being called when the pended tick
2867 * count is being unwound (when the scheduler is being unlocked). */
2868 if( xPendedTicks == ( TickType_t ) 0 )
2870 vApplicationTickHook();
2874 mtCOVERAGE_TEST_MARKER();
2877 #endif /* configUSE_TICK_HOOK */
2879 #if ( configUSE_PREEMPTION == 1 )
2881 if( xYieldPending != pdFALSE )
2883 xSwitchRequired = pdTRUE;
2887 mtCOVERAGE_TEST_MARKER();
2890 #endif /* configUSE_PREEMPTION */
2896 /* The tick hook gets called at regular intervals, even if the
2897 * scheduler is locked. */
2898 #if ( configUSE_TICK_HOOK == 1 )
2900 vApplicationTickHook();
2905 return xSwitchRequired;
2907 /*-----------------------------------------------------------*/
2909 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
2911 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
2912 TaskHookFunction_t pxHookFunction )
2916 /* If xTask is NULL then it is the task hook of the calling task that is
2920 xTCB = ( TCB_t * ) pxCurrentTCB;
2927 /* Save the hook function in the TCB. A critical section is required as
2928 * the value can be accessed from an interrupt. */
2929 taskENTER_CRITICAL();
2931 xTCB->pxTaskTag = pxHookFunction;
2933 taskEXIT_CRITICAL();
2936 #endif /* configUSE_APPLICATION_TASK_TAG */
2937 /*-----------------------------------------------------------*/
2939 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
2941 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
2944 TaskHookFunction_t xReturn;
2946 /* If xTask is NULL then set the calling task's hook. */
2947 pxTCB = prvGetTCBFromHandle( xTask );
2949 /* Save the hook function in the TCB. A critical section is required as
2950 * the value can be accessed from an interrupt. */
2951 taskENTER_CRITICAL();
2953 xReturn = pxTCB->pxTaskTag;
2955 taskEXIT_CRITICAL();
2960 #endif /* configUSE_APPLICATION_TASK_TAG */
2961 /*-----------------------------------------------------------*/
2963 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
2965 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
2968 TaskHookFunction_t xReturn;
2969 UBaseType_t uxSavedInterruptStatus;
2971 /* If xTask is NULL then set the calling task's hook. */
2972 pxTCB = prvGetTCBFromHandle( xTask );
2974 /* Save the hook function in the TCB. A critical section is required as
2975 * the value can be accessed from an interrupt. */
2976 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2978 xReturn = pxTCB->pxTaskTag;
2980 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
2985 #endif /* configUSE_APPLICATION_TASK_TAG */
2986 /*-----------------------------------------------------------*/
2988 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
2990 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
2991 void * pvParameter )
2996 /* If xTask is NULL then we are calling our own task hook. */
2999 xTCB = pxCurrentTCB;
3006 if( xTCB->pxTaskTag != NULL )
3008 xReturn = xTCB->pxTaskTag( pvParameter );
3018 #endif /* configUSE_APPLICATION_TASK_TAG */
3019 /*-----------------------------------------------------------*/
3021 void vTaskSwitchContext( void )
3023 if( uxSchedulerSuspended != ( UBaseType_t ) pdFALSE )
3025 /* The scheduler is currently suspended - do not allow a context
3027 xYieldPending = pdTRUE;
3031 xYieldPending = pdFALSE;
3032 traceTASK_SWITCHED_OUT();
3034 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3036 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3037 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
3039 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3042 /* Add the amount of time the task has been running to the
3043 * accumulated time so far. The time the task started running was
3044 * stored in ulTaskSwitchedInTime. Note that there is no overflow
3045 * protection here so count values are only valid until the timer
3046 * overflows. The guard against negative values is to protect
3047 * against suspect run time stat counter implementations - which
3048 * are provided by the application, not the kernel. */
3049 if( ulTotalRunTime > ulTaskSwitchedInTime )
3051 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime );
3055 mtCOVERAGE_TEST_MARKER();
3058 ulTaskSwitchedInTime = ulTotalRunTime;
3060 #endif /* configGENERATE_RUN_TIME_STATS */
3062 /* Check for stack overflow, if configured. */
3063 taskCHECK_FOR_STACK_OVERFLOW();
3065 /* Before the currently running task is switched out, save its errno. */
3066 #if ( configUSE_POSIX_ERRNO == 1 )
3068 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
3072 /* Select a new task to run using either the generic C or port
3073 * optimised asm code. */
3074 taskSELECT_HIGHEST_PRIORITY_TASK(); /*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. */
3075 traceTASK_SWITCHED_IN();
3077 /* After the new task is switched in, update the global errno. */
3078 #if ( configUSE_POSIX_ERRNO == 1 )
3080 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
3084 #if ( configUSE_NEWLIB_REENTRANT == 1 )
3086 /* Switch Newlib's _impure_ptr variable to point to the _reent
3087 * structure specific to this task.
3088 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
3089 * for additional information. */
3090 _impure_ptr = &( pxCurrentTCB->xNewLib_reent );
3092 #endif /* configUSE_NEWLIB_REENTRANT */
3095 /*-----------------------------------------------------------*/
3097 void vTaskPlaceOnEventList( List_t * const pxEventList,
3098 const TickType_t xTicksToWait )
3100 configASSERT( pxEventList );
3102 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
3103 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
3105 /* Place the event list item of the TCB in the appropriate event list.
3106 * This is placed in the list in priority order so the highest priority task
3107 * is the first to be woken by the event.
3109 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
3110 * Normally, the xItemValue of a TCB's ListItem_t members is:
3111 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
3112 * Therefore, the event list is sorted in descending priority order.
3114 * The queue that contains the event list is locked, preventing
3115 * simultaneous access from interrupts. */
3116 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3118 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3120 /*-----------------------------------------------------------*/
3122 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
3123 const TickType_t xItemValue,
3124 const TickType_t xTicksToWait )
3126 configASSERT( pxEventList );
3128 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3129 * the event groups implementation. */
3130 configASSERT( uxSchedulerSuspended != 0 );
3132 /* Store the item value in the event list item. It is safe to access the
3133 * event list item here as interrupts won't access the event list item of a
3134 * task that is not in the Blocked state. */
3135 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
3137 /* Place the event list item of the TCB at the end of the appropriate event
3138 * list. It is safe to access the event list here because it is part of an
3139 * event group implementation - and interrupts don't access event groups
3140 * directly (instead they access them indirectly by pending function calls to
3141 * the task level). */
3142 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3144 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
3146 /*-----------------------------------------------------------*/
3148 #if ( configUSE_TIMERS == 1 )
3150 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
3151 TickType_t xTicksToWait,
3152 const BaseType_t xWaitIndefinitely )
3154 configASSERT( pxEventList );
3156 /* This function should not be called by application code hence the
3157 * 'Restricted' in its name. It is not part of the public API. It is
3158 * designed for use by kernel code, and has special calling requirements -
3159 * it should be called with the scheduler suspended. */
3162 /* Place the event list item of the TCB in the appropriate event list.
3163 * In this case it is assume that this is the only task that is going to
3164 * be waiting on this event list, so the faster vListInsertEnd() function
3165 * can be used in place of vListInsert. */
3166 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
3168 /* If the task should block indefinitely then set the block time to a
3169 * value that will be recognised as an indefinite delay inside the
3170 * prvAddCurrentTaskToDelayedList() function. */
3171 if( xWaitIndefinitely != pdFALSE )
3173 xTicksToWait = portMAX_DELAY;
3176 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
3177 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
3180 #endif /* configUSE_TIMERS */
3181 /*-----------------------------------------------------------*/
3183 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
3185 TCB_t * pxUnblockedTCB;
3188 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
3189 * called from a critical section within an ISR. */
3191 /* The event list is sorted in priority order, so the first in the list can
3192 * be removed as it is known to be the highest priority. Remove the TCB from
3193 * the delayed list, and add it to the ready list.
3195 * If an event is for a queue that is locked then this function will never
3196 * get called - the lock count on the queue will get modified instead. This
3197 * means exclusive access to the event list is guaranteed here.
3199 * This function assumes that a check has already been made to ensure that
3200 * pxEventList is not empty. */
3201 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. */
3202 configASSERT( pxUnblockedTCB );
3203 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
3205 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3207 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
3208 prvAddTaskToReadyList( pxUnblockedTCB );
3210 #if ( configUSE_TICKLESS_IDLE != 0 )
3212 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3213 * might be set to the blocked task's time out time. If the task is
3214 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
3215 * normally left unchanged, because it is automatically reset to a new
3216 * value when the tick count equals xNextTaskUnblockTime. However if
3217 * tickless idling is used it might be more important to enter sleep mode
3218 * at the earliest possible time - so reset xNextTaskUnblockTime here to
3219 * ensure it is updated at the earliest possible time. */
3220 prvResetNextTaskUnblockTime();
3226 /* The delayed and ready lists cannot be accessed, so hold this task
3227 * pending until the scheduler is resumed. */
3228 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
3231 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
3233 /* Return true if the task removed from the event list has a higher
3234 * priority than the calling task. This allows the calling task to know if
3235 * it should force a context switch now. */
3238 /* Mark that a yield is pending in case the user is not using the
3239 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
3240 xYieldPending = pdTRUE;
3249 /*-----------------------------------------------------------*/
3251 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
3252 const TickType_t xItemValue )
3254 TCB_t * pxUnblockedTCB;
3256 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3257 * the event flags implementation. */
3258 configASSERT( uxSchedulerSuspended != pdFALSE );
3260 /* Store the new item value in the event list. */
3261 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
3263 /* Remove the event list form the event flag. Interrupts do not access
3265 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. */
3266 configASSERT( pxUnblockedTCB );
3267 listREMOVE_ITEM( pxEventListItem );
3269 #if ( configUSE_TICKLESS_IDLE != 0 )
3271 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3272 * might be set to the blocked task's time out time. If the task is
3273 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
3274 * normally left unchanged, because it is automatically reset to a new
3275 * value when the tick count equals xNextTaskUnblockTime. However if
3276 * tickless idling is used it might be more important to enter sleep mode
3277 * at the earliest possible time - so reset xNextTaskUnblockTime here to
3278 * ensure it is updated at the earliest possible time. */
3279 prvResetNextTaskUnblockTime();
3283 /* Remove the task from the delayed list and add it to the ready list. The
3284 * scheduler is suspended so interrupts will not be accessing the ready
3286 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
3287 prvAddTaskToReadyList( pxUnblockedTCB );
3289 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
3291 /* The unblocked task has a priority above that of the calling task, so
3292 * a context switch is required. This function is called with the
3293 * scheduler suspended so xYieldPending is set so the context switch
3294 * occurs immediately that the scheduler is resumed (unsuspended). */
3295 xYieldPending = pdTRUE;
3298 /*-----------------------------------------------------------*/
3300 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
3302 configASSERT( pxTimeOut );
3303 taskENTER_CRITICAL();
3305 pxTimeOut->xOverflowCount = xNumOfOverflows;
3306 pxTimeOut->xTimeOnEntering = xTickCount;
3308 taskEXIT_CRITICAL();
3310 /*-----------------------------------------------------------*/
3312 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
3314 /* For internal use only as it does not use a critical section. */
3315 pxTimeOut->xOverflowCount = xNumOfOverflows;
3316 pxTimeOut->xTimeOnEntering = xTickCount;
3318 /*-----------------------------------------------------------*/
3320 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
3321 TickType_t * const pxTicksToWait )
3325 configASSERT( pxTimeOut );
3326 configASSERT( pxTicksToWait );
3328 taskENTER_CRITICAL();
3330 /* Minor optimisation. The tick count cannot change in this block. */
3331 const TickType_t xConstTickCount = xTickCount;
3332 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
3334 #if ( INCLUDE_xTaskAbortDelay == 1 )
3335 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
3337 /* The delay was aborted, which is not the same as a time out,
3338 * but has the same result. */
3339 pxCurrentTCB->ucDelayAborted = pdFALSE;
3345 #if ( INCLUDE_vTaskSuspend == 1 )
3346 if( *pxTicksToWait == portMAX_DELAY )
3348 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
3349 * specified is the maximum block time then the task should block
3350 * indefinitely, and therefore never time out. */
3356 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
3358 /* The tick count is greater than the time at which
3359 * vTaskSetTimeout() was called, but has also overflowed since
3360 * vTaskSetTimeOut() was called. It must have wrapped all the way
3361 * around and gone past again. This passed since vTaskSetTimeout()
3364 *pxTicksToWait = ( TickType_t ) 0;
3366 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
3368 /* Not a genuine timeout. Adjust parameters for time remaining. */
3369 *pxTicksToWait -= xElapsedTime;
3370 vTaskInternalSetTimeOutState( pxTimeOut );
3375 *pxTicksToWait = ( TickType_t ) 0;
3379 taskEXIT_CRITICAL();
3383 /*-----------------------------------------------------------*/
3385 void vTaskMissedYield( void )
3387 xYieldPending = pdTRUE;
3389 /*-----------------------------------------------------------*/
3391 #if ( configUSE_TRACE_FACILITY == 1 )
3393 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
3395 UBaseType_t uxReturn;
3396 TCB_t const * pxTCB;
3401 uxReturn = pxTCB->uxTaskNumber;
3411 #endif /* configUSE_TRACE_FACILITY */
3412 /*-----------------------------------------------------------*/
3414 #if ( configUSE_TRACE_FACILITY == 1 )
3416 void vTaskSetTaskNumber( TaskHandle_t xTask,
3417 const UBaseType_t uxHandle )
3424 pxTCB->uxTaskNumber = uxHandle;
3428 #endif /* configUSE_TRACE_FACILITY */
3431 * -----------------------------------------------------------
3433 * ----------------------------------------------------------
3435 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
3436 * language extensions. The equivalent prototype for this function is:
3438 * void prvIdleTask( void *pvParameters );
3441 static portTASK_FUNCTION( prvIdleTask, pvParameters )
3443 /* Stop warnings. */
3444 ( void ) pvParameters;
3446 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
3447 * SCHEDULER IS STARTED. **/
3449 /* In case a task that has a secure context deletes itself, in which case
3450 * the idle task is responsible for deleting the task's secure context, if
3452 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
3456 /* See if any tasks have deleted themselves - if so then the idle task
3457 * is responsible for freeing the deleted task's TCB and stack. */
3458 prvCheckTasksWaitingTermination();
3460 #if ( configUSE_PREEMPTION == 0 )
3462 /* If we are not using preemption we keep forcing a task switch to
3463 * see if any other task has become available. If we are using
3464 * preemption we don't need to do this as any task becoming available
3465 * will automatically get the processor anyway. */
3468 #endif /* configUSE_PREEMPTION */
3470 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
3472 /* When using preemption tasks of equal priority will be
3473 * timesliced. If a task that is sharing the idle priority is ready
3474 * to run then the idle task should yield before the end of the
3477 * A critical region is not required here as we are just reading from
3478 * the list, and an occasional incorrect value will not matter. If
3479 * the ready list at the idle priority contains more than one task
3480 * then a task other than the idle task is ready to execute. */
3481 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) 1 )
3487 mtCOVERAGE_TEST_MARKER();
3490 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
3492 #if ( configUSE_IDLE_HOOK == 1 )
3494 extern void vApplicationIdleHook( void );
3496 /* Call the user defined function from within the idle task. This
3497 * allows the application designer to add background functionality
3498 * without the overhead of a separate task.
3499 * NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
3500 * CALL A FUNCTION THAT MIGHT BLOCK. */
3501 vApplicationIdleHook();
3503 #endif /* configUSE_IDLE_HOOK */
3505 /* This conditional compilation should use inequality to 0, not equality
3506 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
3507 * user defined low power mode implementations require
3508 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
3509 #if ( configUSE_TICKLESS_IDLE != 0 )
3511 TickType_t xExpectedIdleTime;
3513 /* It is not desirable to suspend then resume the scheduler on
3514 * each iteration of the idle task. Therefore, a preliminary
3515 * test of the expected idle time is performed without the
3516 * scheduler suspended. The result here is not necessarily
3518 xExpectedIdleTime = prvGetExpectedIdleTime();
3520 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
3524 /* Now the scheduler is suspended, the expected idle
3525 * time can be sampled again, and this time its value can
3527 configASSERT( xNextTaskUnblockTime >= xTickCount );
3528 xExpectedIdleTime = prvGetExpectedIdleTime();
3530 /* Define the following macro to set xExpectedIdleTime to 0
3531 * if the application does not want
3532 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
3533 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
3535 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
3537 traceLOW_POWER_IDLE_BEGIN();
3538 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
3539 traceLOW_POWER_IDLE_END();
3543 mtCOVERAGE_TEST_MARKER();
3546 ( void ) xTaskResumeAll();
3550 mtCOVERAGE_TEST_MARKER();
3553 #endif /* configUSE_TICKLESS_IDLE */
3556 /*-----------------------------------------------------------*/
3558 #if ( configUSE_TICKLESS_IDLE != 0 )
3560 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
3562 /* The idle task exists in addition to the application tasks. */
3563 const UBaseType_t uxNonApplicationTasks = 1;
3564 eSleepModeStatus eReturn = eStandardSleep;
3566 /* This function must be called from a critical section. */
3568 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
3570 /* A task was made ready while the scheduler was suspended. */
3571 eReturn = eAbortSleep;
3573 else if( xYieldPending != pdFALSE )
3575 /* A yield was pended while the scheduler was suspended. */
3576 eReturn = eAbortSleep;
3578 else if( xPendedTicks != 0 )
3580 /* A tick interrupt has already occurred but was held pending
3581 * because the scheduler is suspended. */
3582 eReturn = eAbortSleep;
3586 /* If all the tasks are in the suspended list (which might mean they
3587 * have an infinite block time rather than actually being suspended)
3588 * then it is safe to turn all clocks off and just wait for external
3590 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
3592 eReturn = eNoTasksWaitingTimeout;
3596 mtCOVERAGE_TEST_MARKER();
3603 #endif /* configUSE_TICKLESS_IDLE */
3604 /*-----------------------------------------------------------*/
3606 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
3608 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
3614 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
3616 pxTCB = prvGetTCBFromHandle( xTaskToSet );
3617 configASSERT( pxTCB != NULL );
3618 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
3622 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
3623 /*-----------------------------------------------------------*/
3625 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
3627 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
3630 void * pvReturn = NULL;
3633 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
3635 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3636 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
3646 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
3647 /*-----------------------------------------------------------*/
3649 #if ( portUSING_MPU_WRAPPERS == 1 )
3651 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
3652 const MemoryRegion_t * const xRegions )
3656 /* If null is passed in here then we are modifying the MPU settings of
3657 * the calling task. */
3658 pxTCB = prvGetTCBFromHandle( xTaskToModify );
3660 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
3663 #endif /* portUSING_MPU_WRAPPERS */
3664 /*-----------------------------------------------------------*/
3666 static void prvInitialiseTaskLists( void )
3668 UBaseType_t uxPriority;
3670 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
3672 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
3675 vListInitialise( &xDelayedTaskList1 );
3676 vListInitialise( &xDelayedTaskList2 );
3677 vListInitialise( &xPendingReadyList );
3679 #if ( INCLUDE_vTaskDelete == 1 )
3681 vListInitialise( &xTasksWaitingTermination );
3683 #endif /* INCLUDE_vTaskDelete */
3685 #if ( INCLUDE_vTaskSuspend == 1 )
3687 vListInitialise( &xSuspendedTaskList );
3689 #endif /* INCLUDE_vTaskSuspend */
3691 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
3693 pxDelayedTaskList = &xDelayedTaskList1;
3694 pxOverflowDelayedTaskList = &xDelayedTaskList2;
3696 /*-----------------------------------------------------------*/
3698 static void prvCheckTasksWaitingTermination( void )
3700 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
3702 #if ( INCLUDE_vTaskDelete == 1 )
3706 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
3707 * being called too often in the idle task. */
3708 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
3710 taskENTER_CRITICAL();
3712 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. */
3713 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3714 --uxCurrentNumberOfTasks;
3715 --uxDeletedTasksWaitingCleanUp;
3717 taskEXIT_CRITICAL();
3719 prvDeleteTCB( pxTCB );
3722 #endif /* INCLUDE_vTaskDelete */
3724 /*-----------------------------------------------------------*/
3726 #if ( configUSE_TRACE_FACILITY == 1 )
3728 void vTaskGetInfo( TaskHandle_t xTask,
3729 TaskStatus_t * pxTaskStatus,
3730 BaseType_t xGetFreeStackSpace,
3735 /* xTask is NULL then get the state of the calling task. */
3736 pxTCB = prvGetTCBFromHandle( xTask );
3738 pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
3739 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
3740 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
3741 pxTaskStatus->pxStackBase = pxTCB->pxStack;
3742 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
3744 #if ( configUSE_MUTEXES == 1 )
3746 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
3750 pxTaskStatus->uxBasePriority = 0;
3754 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3756 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
3760 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
3764 /* Obtaining the task state is a little fiddly, so is only done if the
3765 * value of eState passed into this function is eInvalid - otherwise the
3766 * state is just set to whatever is passed in. */
3767 if( eState != eInvalid )
3769 if( pxTCB == pxCurrentTCB )
3771 pxTaskStatus->eCurrentState = eRunning;
3775 pxTaskStatus->eCurrentState = eState;
3777 #if ( INCLUDE_vTaskSuspend == 1 )
3779 /* If the task is in the suspended list then there is a
3780 * chance it is actually just blocked indefinitely - so really
3781 * it should be reported as being in the Blocked state. */
3782 if( eState == eSuspended )
3786 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3788 pxTaskStatus->eCurrentState = eBlocked;
3791 ( void ) xTaskResumeAll();
3794 #endif /* INCLUDE_vTaskSuspend */
3799 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
3802 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
3803 * parameter is provided to allow it to be skipped. */
3804 if( xGetFreeStackSpace != pdFALSE )
3806 #if ( portSTACK_GROWTH > 0 )
3808 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
3812 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
3818 pxTaskStatus->usStackHighWaterMark = 0;
3822 #endif /* configUSE_TRACE_FACILITY */
3823 /*-----------------------------------------------------------*/
3825 #if ( configUSE_TRACE_FACILITY == 1 )
3827 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
3831 configLIST_VOLATILE TCB_t * pxNextTCB, * pxFirstTCB;
3832 UBaseType_t uxTask = 0;
3834 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3836 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. */
3838 /* Populate an TaskStatus_t structure within the
3839 * pxTaskStatusArray array for each task that is referenced from
3840 * pxList. See the definition of TaskStatus_t in task.h for the
3841 * meaning of each TaskStatus_t structure member. */
3844 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. */
3845 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
3847 } while( pxNextTCB != pxFirstTCB );
3851 mtCOVERAGE_TEST_MARKER();
3857 #endif /* configUSE_TRACE_FACILITY */
3858 /*-----------------------------------------------------------*/
3860 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
3862 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
3864 uint32_t ulCount = 0U;
3866 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
3868 pucStackByte -= portSTACK_GROWTH;
3872 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
3874 return ( configSTACK_DEPTH_TYPE ) ulCount;
3877 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
3878 /*-----------------------------------------------------------*/
3880 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
3882 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
3883 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
3884 * user to determine the return type. It gets around the problem of the value
3885 * overflowing on 8-bit types without breaking backward compatibility for
3886 * applications that expect an 8-bit return type. */
3887 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
3890 uint8_t * pucEndOfStack;
3891 configSTACK_DEPTH_TYPE uxReturn;
3893 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
3894 * the same except for their return type. Using configSTACK_DEPTH_TYPE
3895 * allows the user to determine the return type. It gets around the
3896 * problem of the value overflowing on 8-bit types without breaking
3897 * backward compatibility for applications that expect an 8-bit return
3900 pxTCB = prvGetTCBFromHandle( xTask );
3902 #if portSTACK_GROWTH < 0
3904 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
3908 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
3912 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
3917 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
3918 /*-----------------------------------------------------------*/
3920 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
3922 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
3925 uint8_t * pucEndOfStack;
3926 UBaseType_t uxReturn;
3928 pxTCB = prvGetTCBFromHandle( xTask );
3930 #if portSTACK_GROWTH < 0
3932 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
3936 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
3940 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
3945 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
3946 /*-----------------------------------------------------------*/
3948 #if ( INCLUDE_vTaskDelete == 1 )
3950 static void prvDeleteTCB( TCB_t * pxTCB )
3952 /* This call is required specifically for the TriCore port. It must be
3953 * above the vPortFree() calls. The call is also used by ports/demos that
3954 * want to allocate and clean RAM statically. */
3955 portCLEAN_UP_TCB( pxTCB );
3957 /* Free up the memory allocated by the scheduler for the task. It is up
3958 * to the task to free any memory allocated at the application level.
3959 * See the third party link http://www.nadler.com/embedded/newlibAndFreeRTOS.html
3960 * for additional information. */
3961 #if ( configUSE_NEWLIB_REENTRANT == 1 )
3963 _reclaim_reent( &( pxTCB->xNewLib_reent ) );
3965 #endif /* configUSE_NEWLIB_REENTRANT */
3967 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
3969 /* The task can only have been allocated dynamically - free both
3970 * the stack and TCB. */
3971 vPortFreeStack( pxTCB->pxStack );
3974 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
3976 /* The task could have been allocated statically or dynamically, so
3977 * check what was statically allocated before trying to free the
3979 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
3981 /* Both the stack and TCB were allocated dynamically, so both
3983 vPortFreeStack( pxTCB->pxStack );
3986 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
3988 /* Only the stack was statically allocated, so the TCB is the
3989 * only memory that must be freed. */
3994 /* Neither the stack nor the TCB were allocated dynamically, so
3995 * nothing needs to be freed. */
3996 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
3997 mtCOVERAGE_TEST_MARKER();
4000 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
4003 #endif /* INCLUDE_vTaskDelete */
4004 /*-----------------------------------------------------------*/
4006 static void prvResetNextTaskUnblockTime( void )
4008 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4010 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
4011 * the maximum possible value so it is extremely unlikely that the
4012 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
4013 * there is an item in the delayed list. */
4014 xNextTaskUnblockTime = portMAX_DELAY;
4018 /* The new current delayed list is not empty, get the value of
4019 * the item at the head of the delayed list. This is the time at
4020 * which the task at the head of the delayed list should be removed
4021 * from the Blocked state. */
4022 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
4025 /*-----------------------------------------------------------*/
4027 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
4029 TaskHandle_t xTaskGetCurrentTaskHandle( void )
4031 TaskHandle_t xReturn;
4033 /* A critical section is not required as this is not called from
4034 * an interrupt and the current TCB will always be the same for any
4035 * individual execution thread. */
4036 xReturn = pxCurrentTCB;
4041 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
4042 /*-----------------------------------------------------------*/
4044 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
4046 BaseType_t xTaskGetSchedulerState( void )
4050 if( xSchedulerRunning == pdFALSE )
4052 xReturn = taskSCHEDULER_NOT_STARTED;
4056 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4058 xReturn = taskSCHEDULER_RUNNING;
4062 xReturn = taskSCHEDULER_SUSPENDED;
4069 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
4070 /*-----------------------------------------------------------*/
4072 #if ( configUSE_MUTEXES == 1 )
4074 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
4076 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
4077 BaseType_t xReturn = pdFALSE;
4079 /* If the mutex was given back by an interrupt while the queue was
4080 * locked then the mutex holder might now be NULL. _RB_ Is this still
4081 * needed as interrupts can no longer use mutexes? */
4082 if( pxMutexHolder != NULL )
4084 /* If the holder of the mutex has a priority below the priority of
4085 * the task attempting to obtain the mutex then it will temporarily
4086 * inherit the priority of the task attempting to obtain the mutex. */
4087 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
4089 /* Adjust the mutex holder state to account for its new
4090 * priority. Only reset the event list item value if the value is
4091 * not being used for anything else. */
4092 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4094 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. */
4098 mtCOVERAGE_TEST_MARKER();
4101 /* If the task being modified is in the ready state it will need
4102 * to be moved into a new list. */
4103 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
4105 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4107 /* It is known that the task is in its ready list so
4108 * there is no need to check again and the port level
4109 * reset macro can be called directly. */
4110 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
4114 mtCOVERAGE_TEST_MARKER();
4117 /* Inherit the priority before being moved into the new list. */
4118 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4119 prvAddTaskToReadyList( pxMutexHolderTCB );
4123 /* Just inherit the priority. */
4124 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4127 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
4129 /* Inheritance occurred. */
4134 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
4136 /* The base priority of the mutex holder is lower than the
4137 * priority of the task attempting to take the mutex, but the
4138 * current priority of the mutex holder is not lower than the
4139 * priority of the task attempting to take the mutex.
4140 * Therefore the mutex holder must have already inherited a
4141 * priority, but inheritance would have occurred if that had
4142 * not been the case. */
4147 mtCOVERAGE_TEST_MARKER();
4153 mtCOVERAGE_TEST_MARKER();
4159 #endif /* configUSE_MUTEXES */
4160 /*-----------------------------------------------------------*/
4162 #if ( configUSE_MUTEXES == 1 )
4164 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
4166 TCB_t * const pxTCB = pxMutexHolder;
4167 BaseType_t xReturn = pdFALSE;
4169 if( pxMutexHolder != NULL )
4171 /* A task can only have an inherited priority if it holds the mutex.
4172 * If the mutex is held by a task then it cannot be given from an
4173 * interrupt, and if a mutex is given by the holding task then it must
4174 * be the running state task. */
4175 configASSERT( pxTCB == pxCurrentTCB );
4176 configASSERT( pxTCB->uxMutexesHeld );
4177 ( pxTCB->uxMutexesHeld )--;
4179 /* Has the holder of the mutex inherited the priority of another
4181 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
4183 /* Only disinherit if no other mutexes are held. */
4184 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
4186 /* A task can only have an inherited priority if it holds
4187 * the mutex. If the mutex is held by a task then it cannot be
4188 * given from an interrupt, and if a mutex is given by the
4189 * holding task then it must be the running state task. Remove
4190 * the holding task from the ready list. */
4191 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4193 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
4197 mtCOVERAGE_TEST_MARKER();
4200 /* Disinherit the priority before adding the task into the
4201 * new ready list. */
4202 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
4203 pxTCB->uxPriority = pxTCB->uxBasePriority;
4205 /* Reset the event list item value. It cannot be in use for
4206 * any other purpose if this task is running, and it must be
4207 * running to give back the mutex. */
4208 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. */
4209 prvAddTaskToReadyList( pxTCB );
4211 /* Return true to indicate that a context switch is required.
4212 * This is only actually required in the corner case whereby
4213 * multiple mutexes were held and the mutexes were given back
4214 * in an order different to that in which they were taken.
4215 * If a context switch did not occur when the first mutex was
4216 * returned, even if a task was waiting on it, then a context
4217 * switch should occur when the last mutex is returned whether
4218 * a task is waiting on it or not. */
4223 mtCOVERAGE_TEST_MARKER();
4228 mtCOVERAGE_TEST_MARKER();
4233 mtCOVERAGE_TEST_MARKER();
4239 #endif /* configUSE_MUTEXES */
4240 /*-----------------------------------------------------------*/
4242 #if ( configUSE_MUTEXES == 1 )
4244 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
4245 UBaseType_t uxHighestPriorityWaitingTask )
4247 TCB_t * const pxTCB = pxMutexHolder;
4248 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
4249 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
4251 if( pxMutexHolder != NULL )
4253 /* If pxMutexHolder is not NULL then the holder must hold at least
4255 configASSERT( pxTCB->uxMutexesHeld );
4257 /* Determine the priority to which the priority of the task that
4258 * holds the mutex should be set. This will be the greater of the
4259 * holding task's base priority and the priority of the highest
4260 * priority task that is waiting to obtain the mutex. */
4261 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
4263 uxPriorityToUse = uxHighestPriorityWaitingTask;
4267 uxPriorityToUse = pxTCB->uxBasePriority;
4270 /* Does the priority need to change? */
4271 if( pxTCB->uxPriority != uxPriorityToUse )
4273 /* Only disinherit if no other mutexes are held. This is a
4274 * simplification in the priority inheritance implementation. If
4275 * the task that holds the mutex is also holding other mutexes then
4276 * the other mutexes may have caused the priority inheritance. */
4277 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
4279 /* If a task has timed out because it already holds the
4280 * mutex it was trying to obtain then it cannot of inherited
4281 * its own priority. */
4282 configASSERT( pxTCB != pxCurrentTCB );
4284 /* Disinherit the priority, remembering the previous
4285 * priority to facilitate determining the subject task's
4287 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
4288 uxPriorityUsedOnEntry = pxTCB->uxPriority;
4289 pxTCB->uxPriority = uxPriorityToUse;
4291 /* Only reset the event list item value if the value is not
4292 * being used for anything else. */
4293 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4295 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. */
4299 mtCOVERAGE_TEST_MARKER();
4302 /* If the running task is not the task that holds the mutex
4303 * then the task that holds the mutex could be in either the
4304 * Ready, Blocked or Suspended states. Only remove the task
4305 * from its current state list if it is in the Ready state as
4306 * the task's priority is going to change and there is one
4307 * Ready list per priority. */
4308 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
4310 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4312 /* It is known that the task is in its ready list so
4313 * there is no need to check again and the port level
4314 * reset macro can be called directly. */
4315 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
4319 mtCOVERAGE_TEST_MARKER();
4322 prvAddTaskToReadyList( pxTCB );
4326 mtCOVERAGE_TEST_MARKER();
4331 mtCOVERAGE_TEST_MARKER();
4336 mtCOVERAGE_TEST_MARKER();
4341 mtCOVERAGE_TEST_MARKER();
4345 #endif /* configUSE_MUTEXES */
4346 /*-----------------------------------------------------------*/
4348 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
4350 void vTaskEnterCritical( void )
4352 portDISABLE_INTERRUPTS();
4354 if( xSchedulerRunning != pdFALSE )
4356 ( pxCurrentTCB->uxCriticalNesting )++;
4358 /* This is not the interrupt safe version of the enter critical
4359 * function so assert() if it is being called from an interrupt
4360 * context. Only API functions that end in "FromISR" can be used in an
4361 * interrupt. Only assert if the critical nesting count is 1 to
4362 * protect against recursive calls if the assert function also uses a
4363 * critical section. */
4364 if( pxCurrentTCB->uxCriticalNesting == 1 )
4366 portASSERT_IF_IN_ISR();
4371 mtCOVERAGE_TEST_MARKER();
4375 #endif /* portCRITICAL_NESTING_IN_TCB */
4376 /*-----------------------------------------------------------*/
4378 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
4380 void vTaskExitCritical( void )
4382 if( xSchedulerRunning != pdFALSE )
4384 if( pxCurrentTCB->uxCriticalNesting > 0U )
4386 ( pxCurrentTCB->uxCriticalNesting )--;
4388 if( pxCurrentTCB->uxCriticalNesting == 0U )
4390 portENABLE_INTERRUPTS();
4394 mtCOVERAGE_TEST_MARKER();
4399 mtCOVERAGE_TEST_MARKER();
4404 mtCOVERAGE_TEST_MARKER();
4408 #endif /* portCRITICAL_NESTING_IN_TCB */
4409 /*-----------------------------------------------------------*/
4411 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
4413 static char * prvWriteNameToBuffer( char * pcBuffer,
4414 const char * pcTaskName )
4418 /* Start by copying the entire string. */
4419 strcpy( pcBuffer, pcTaskName );
4421 /* Pad the end of the string with spaces to ensure columns line up when
4423 for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
4425 pcBuffer[ x ] = ' ';
4429 pcBuffer[ x ] = ( char ) 0x00;
4431 /* Return the new end of string. */
4432 return &( pcBuffer[ x ] );
4435 #endif /* ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
4436 /*-----------------------------------------------------------*/
4438 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
4440 void vTaskList( char * pcWriteBuffer )
4442 TaskStatus_t * pxTaskStatusArray;
4443 UBaseType_t uxArraySize, x;
4449 * This function is provided for convenience only, and is used by many
4450 * of the demo applications. Do not consider it to be part of the
4453 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
4454 * uxTaskGetSystemState() output into a human readable table that
4455 * displays task: names, states, priority, stack usage and task number.
4456 * Stack usage specified as the number of unused StackType_t words stack can hold
4457 * on top of stack - not the number of bytes.
4459 * vTaskList() has a dependency on the sprintf() C library function that
4460 * might bloat the code size, use a lot of stack, and provide different
4461 * results on different platforms. An alternative, tiny, third party,
4462 * and limited functionality implementation of sprintf() is provided in
4463 * many of the FreeRTOS/Demo sub-directories in a file called
4464 * printf-stdarg.c (note printf-stdarg.c does not provide a full
4465 * snprintf() implementation!).
4467 * It is recommended that production systems call uxTaskGetSystemState()
4468 * directly to get access to raw stats data, rather than indirectly
4469 * through a call to vTaskList().
4473 /* Make sure the write buffer does not contain a string. */
4474 *pcWriteBuffer = ( char ) 0x00;
4476 /* Take a snapshot of the number of tasks in case it changes while this
4477 * function is executing. */
4478 uxArraySize = uxCurrentNumberOfTasks;
4480 /* Allocate an array index for each task. NOTE! if
4481 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
4482 * equate to NULL. */
4483 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. */
4485 if( pxTaskStatusArray != NULL )
4487 /* Generate the (binary) data. */
4488 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
4490 /* Create a human readable table from the binary data. */
4491 for( x = 0; x < uxArraySize; x++ )
4493 switch( pxTaskStatusArray[ x ].eCurrentState )
4496 cStatus = tskRUNNING_CHAR;
4500 cStatus = tskREADY_CHAR;
4504 cStatus = tskBLOCKED_CHAR;
4508 cStatus = tskSUSPENDED_CHAR;
4512 cStatus = tskDELETED_CHAR;
4515 case eInvalid: /* Fall through. */
4516 default: /* Should not get here, but it is included
4517 * to prevent static checking errors. */
4518 cStatus = ( char ) 0x00;
4522 /* Write the task name to the string, padding with spaces so it
4523 * can be printed in tabular form more easily. */
4524 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
4526 /* Write the rest of the string. */
4527 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. */
4528 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. */
4531 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
4532 * is 0 then vPortFree() will be #defined to nothing. */
4533 vPortFree( pxTaskStatusArray );
4537 mtCOVERAGE_TEST_MARKER();
4541 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
4542 /*----------------------------------------------------------*/
4544 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
4546 void vTaskGetRunTimeStats( char * pcWriteBuffer )
4548 TaskStatus_t * pxTaskStatusArray;
4549 UBaseType_t uxArraySize, x;
4550 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
4552 #if ( configUSE_TRACE_FACILITY != 1 )
4554 #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats().
4561 * This function is provided for convenience only, and is used by many
4562 * of the demo applications. Do not consider it to be part of the
4565 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
4566 * of the uxTaskGetSystemState() output into a human readable table that
4567 * displays the amount of time each task has spent in the Running state
4568 * in both absolute and percentage terms.
4570 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
4571 * function that might bloat the code size, use a lot of stack, and
4572 * provide different results on different platforms. An alternative,
4573 * tiny, third party, and limited functionality implementation of
4574 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
4575 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
4576 * a full snprintf() implementation!).
4578 * It is recommended that production systems call uxTaskGetSystemState()
4579 * directly to get access to raw stats data, rather than indirectly
4580 * through a call to vTaskGetRunTimeStats().
4583 /* Make sure the write buffer does not contain a string. */
4584 *pcWriteBuffer = ( char ) 0x00;
4586 /* Take a snapshot of the number of tasks in case it changes while this
4587 * function is executing. */
4588 uxArraySize = uxCurrentNumberOfTasks;
4590 /* Allocate an array index for each task. NOTE! If
4591 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
4592 * equate to NULL. */
4593 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. */
4595 if( pxTaskStatusArray != NULL )
4597 /* Generate the (binary) data. */
4598 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
4600 /* For percentage calculations. */
4601 ulTotalTime /= 100UL;
4603 /* Avoid divide by zero errors. */
4604 if( ulTotalTime > 0UL )
4606 /* Create a human readable table from the binary data. */
4607 for( x = 0; x < uxArraySize; x++ )
4609 /* What percentage of the total run time has the task used?
4610 * This will always be rounded down to the nearest integer.
4611 * ulTotalRunTime has already been divided by 100. */
4612 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
4614 /* Write the task name to the string, padding with
4615 * spaces so it can be printed in tabular form more
4617 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
4619 if( ulStatsAsPercentage > 0UL )
4621 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
4623 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
4627 /* sizeof( int ) == sizeof( long ) so a smaller
4628 * printf() library can be used. */
4629 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. */
4635 /* If the percentage is zero here then the task has
4636 * consumed less than 1% of the total run time. */
4637 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
4639 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
4643 /* sizeof( int ) == sizeof( long ) so a smaller
4644 * printf() library can be used. */
4645 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. */
4650 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. */
4655 mtCOVERAGE_TEST_MARKER();
4658 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
4659 * is 0 then vPortFree() will be #defined to nothing. */
4660 vPortFree( pxTaskStatusArray );
4664 mtCOVERAGE_TEST_MARKER();
4668 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
4669 /*-----------------------------------------------------------*/
4671 TickType_t uxTaskResetEventItemValue( void )
4673 TickType_t uxReturn;
4675 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
4677 /* Reset the event list item to its normal value - so it can be used with
4678 * queues and semaphores. */
4679 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. */
4683 /*-----------------------------------------------------------*/
4685 #if ( configUSE_MUTEXES == 1 )
4687 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
4689 /* If xSemaphoreCreateMutex() is called before any tasks have been created
4690 * then pxCurrentTCB will be NULL. */
4691 if( pxCurrentTCB != NULL )
4693 ( pxCurrentTCB->uxMutexesHeld )++;
4696 return pxCurrentTCB;
4699 #endif /* configUSE_MUTEXES */
4700 /*-----------------------------------------------------------*/
4702 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
4704 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
4705 BaseType_t xClearCountOnExit,
4706 TickType_t xTicksToWait )
4710 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
4712 taskENTER_CRITICAL();
4714 /* Only block if the notification count is not already non-zero. */
4715 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
4717 /* Mark this task as waiting for a notification. */
4718 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
4720 if( xTicksToWait > ( TickType_t ) 0 )
4722 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4723 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
4725 /* All ports are written to allow a yield in a critical
4726 * section (some will yield immediately, others wait until the
4727 * critical section exits) - but it is not something that
4728 * application code should ever do. */
4729 portYIELD_WITHIN_API();
4733 mtCOVERAGE_TEST_MARKER();
4738 mtCOVERAGE_TEST_MARKER();
4741 taskEXIT_CRITICAL();
4743 taskENTER_CRITICAL();
4745 traceTASK_NOTIFY_TAKE( uxIndexToWait );
4746 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
4748 if( ulReturn != 0UL )
4750 if( xClearCountOnExit != pdFALSE )
4752 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
4756 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
4761 mtCOVERAGE_TEST_MARKER();
4764 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
4766 taskEXIT_CRITICAL();
4771 #endif /* configUSE_TASK_NOTIFICATIONS */
4772 /*-----------------------------------------------------------*/
4774 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
4776 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
4777 uint32_t ulBitsToClearOnEntry,
4778 uint32_t ulBitsToClearOnExit,
4779 uint32_t * pulNotificationValue,
4780 TickType_t xTicksToWait )
4784 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
4786 taskENTER_CRITICAL();
4788 /* Only block if a notification is not already pending. */
4789 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
4791 /* Clear bits in the task's notification value as bits may get
4792 * set by the notifying task or interrupt. This can be used to
4793 * clear the value to zero. */
4794 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
4796 /* Mark this task as waiting for a notification. */
4797 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
4799 if( xTicksToWait > ( TickType_t ) 0 )
4801 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4802 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
4804 /* All ports are written to allow a yield in a critical
4805 * section (some will yield immediately, others wait until the
4806 * critical section exits) - but it is not something that
4807 * application code should ever do. */
4808 portYIELD_WITHIN_API();
4812 mtCOVERAGE_TEST_MARKER();
4817 mtCOVERAGE_TEST_MARKER();
4820 taskEXIT_CRITICAL();
4822 taskENTER_CRITICAL();
4824 traceTASK_NOTIFY_WAIT( uxIndexToWait );
4826 if( pulNotificationValue != NULL )
4828 /* Output the current notification value, which may or may not
4830 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
4833 /* If ucNotifyValue is set then either the task never entered the
4834 * blocked state (because a notification was already pending) or the
4835 * task unblocked because of a notification. Otherwise the task
4836 * unblocked because of a timeout. */
4837 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
4839 /* A notification was not received. */
4844 /* A notification was already pending or a notification was
4845 * received while the task was waiting. */
4846 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
4850 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
4852 taskEXIT_CRITICAL();
4857 #endif /* configUSE_TASK_NOTIFICATIONS */
4858 /*-----------------------------------------------------------*/
4860 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
4862 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
4863 UBaseType_t uxIndexToNotify,
4865 eNotifyAction eAction,
4866 uint32_t * pulPreviousNotificationValue )
4869 BaseType_t xReturn = pdPASS;
4870 uint8_t ucOriginalNotifyState;
4872 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
4873 configASSERT( xTaskToNotify );
4874 pxTCB = xTaskToNotify;
4876 taskENTER_CRITICAL();
4878 if( pulPreviousNotificationValue != NULL )
4880 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
4883 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
4885 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
4890 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
4894 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
4897 case eSetValueWithOverwrite:
4898 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
4901 case eSetValueWithoutOverwrite:
4903 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
4905 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
4909 /* The value could not be written to the task. */
4917 /* The task is being notified without its notify value being
4923 /* Should not get here if all enums are handled.
4924 * Artificially force an assert by testing a value the
4925 * compiler can't assume is const. */
4926 configASSERT( xTickCount == ( TickType_t ) 0 );
4931 traceTASK_NOTIFY( uxIndexToNotify );
4933 /* If the task is in the blocked state specifically to wait for a
4934 * notification then unblock it now. */
4935 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
4937 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4938 prvAddTaskToReadyList( pxTCB );
4940 /* The task should not have been on an event list. */
4941 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
4943 #if ( configUSE_TICKLESS_IDLE != 0 )
4945 /* If a task is blocked waiting for a notification then
4946 * xNextTaskUnblockTime might be set to the blocked task's time
4947 * out time. If the task is unblocked for a reason other than
4948 * a timeout xNextTaskUnblockTime is normally left unchanged,
4949 * because it will automatically get reset to a new value when
4950 * the tick count equals xNextTaskUnblockTime. However if
4951 * tickless idling is used it might be more important to enter
4952 * sleep mode at the earliest possible time - so reset
4953 * xNextTaskUnblockTime here to ensure it is updated at the
4954 * earliest possible time. */
4955 prvResetNextTaskUnblockTime();
4959 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4961 /* The notified task has a priority above the currently
4962 * executing task so a yield is required. */
4963 taskYIELD_IF_USING_PREEMPTION();
4967 mtCOVERAGE_TEST_MARKER();
4972 mtCOVERAGE_TEST_MARKER();
4975 taskEXIT_CRITICAL();
4980 #endif /* configUSE_TASK_NOTIFICATIONS */
4981 /*-----------------------------------------------------------*/
4983 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
4985 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
4986 UBaseType_t uxIndexToNotify,
4988 eNotifyAction eAction,
4989 uint32_t * pulPreviousNotificationValue,
4990 BaseType_t * pxHigherPriorityTaskWoken )
4993 uint8_t ucOriginalNotifyState;
4994 BaseType_t xReturn = pdPASS;
4995 UBaseType_t uxSavedInterruptStatus;
4997 configASSERT( xTaskToNotify );
4998 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5000 /* RTOS ports that support interrupt nesting have the concept of a
5001 * maximum system call (or maximum API call) interrupt priority.
5002 * Interrupts that are above the maximum system call priority are keep
5003 * permanently enabled, even when the RTOS kernel is in a critical section,
5004 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
5005 * is defined in FreeRTOSConfig.h then
5006 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
5007 * failure if a FreeRTOS API function is called from an interrupt that has
5008 * been assigned a priority above the configured maximum system call
5009 * priority. Only FreeRTOS functions that end in FromISR can be called
5010 * from interrupts that have been assigned a priority at or (logically)
5011 * below the maximum system call interrupt priority. FreeRTOS maintains a
5012 * separate interrupt safe API to ensure interrupt entry is as fast and as
5013 * simple as possible. More information (albeit Cortex-M specific) is
5014 * provided on the following link:
5015 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5016 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
5018 pxTCB = xTaskToNotify;
5020 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5022 if( pulPreviousNotificationValue != NULL )
5024 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5027 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5028 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5033 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5037 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5040 case eSetValueWithOverwrite:
5041 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5044 case eSetValueWithoutOverwrite:
5046 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5048 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5052 /* The value could not be written to the task. */
5060 /* The task is being notified without its notify value being
5066 /* Should not get here if all enums are handled.
5067 * Artificially force an assert by testing a value the
5068 * compiler can't assume is const. */
5069 configASSERT( xTickCount == ( TickType_t ) 0 );
5073 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
5075 /* If the task is in the blocked state specifically to wait for a
5076 * notification then unblock it now. */
5077 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5079 /* The task should not have been on an event list. */
5080 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5082 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
5084 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
5085 prvAddTaskToReadyList( pxTCB );
5089 /* The delayed and ready lists cannot be accessed, so hold
5090 * this task pending until the scheduler is resumed. */
5091 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
5094 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
5096 /* The notified task has a priority above the currently
5097 * executing task so a yield is required. */
5098 if( pxHigherPriorityTaskWoken != NULL )
5100 *pxHigherPriorityTaskWoken = pdTRUE;
5103 /* Mark that a yield is pending in case the user is not
5104 * using the "xHigherPriorityTaskWoken" parameter to an ISR
5105 * safe FreeRTOS function. */
5106 xYieldPending = pdTRUE;
5110 mtCOVERAGE_TEST_MARKER();
5114 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
5119 #endif /* configUSE_TASK_NOTIFICATIONS */
5120 /*-----------------------------------------------------------*/
5122 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5124 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
5125 UBaseType_t uxIndexToNotify,
5126 BaseType_t * pxHigherPriorityTaskWoken )
5129 uint8_t ucOriginalNotifyState;
5130 UBaseType_t uxSavedInterruptStatus;
5132 configASSERT( xTaskToNotify );
5133 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5135 /* RTOS ports that support interrupt nesting have the concept of a
5136 * maximum system call (or maximum API call) interrupt priority.
5137 * Interrupts that are above the maximum system call priority are keep
5138 * permanently enabled, even when the RTOS kernel is in a critical section,
5139 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
5140 * is defined in FreeRTOSConfig.h then
5141 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
5142 * failure if a FreeRTOS API function is called from an interrupt that has
5143 * been assigned a priority above the configured maximum system call
5144 * priority. Only FreeRTOS functions that end in FromISR can be called
5145 * from interrupts that have been assigned a priority at or (logically)
5146 * below the maximum system call interrupt priority. FreeRTOS maintains a
5147 * separate interrupt safe API to ensure interrupt entry is as fast and as
5148 * simple as possible. More information (albeit Cortex-M specific) is
5149 * provided on the following link:
5150 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5151 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
5153 pxTCB = xTaskToNotify;
5155 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5157 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5158 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
5160 /* 'Giving' is equivalent to incrementing a count in a counting
5162 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5164 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
5166 /* If the task is in the blocked state specifically to wait for a
5167 * notification then unblock it now. */
5168 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5170 /* The task should not have been on an event list. */
5171 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5173 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
5175 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
5176 prvAddTaskToReadyList( pxTCB );
5180 /* The delayed and ready lists cannot be accessed, so hold
5181 * this task pending until the scheduler is resumed. */
5182 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
5185 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
5187 /* The notified task has a priority above the currently
5188 * executing task so a yield is required. */
5189 if( pxHigherPriorityTaskWoken != NULL )
5191 *pxHigherPriorityTaskWoken = pdTRUE;
5194 /* Mark that a yield is pending in case the user is not
5195 * using the "xHigherPriorityTaskWoken" parameter in an ISR
5196 * safe FreeRTOS function. */
5197 xYieldPending = pdTRUE;
5201 mtCOVERAGE_TEST_MARKER();
5205 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
5208 #endif /* configUSE_TASK_NOTIFICATIONS */
5209 /*-----------------------------------------------------------*/
5211 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5213 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
5214 UBaseType_t uxIndexToClear )
5219 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5221 /* If null is passed in here then it is the calling task that is having
5222 * its notification state cleared. */
5223 pxTCB = prvGetTCBFromHandle( xTask );
5225 taskENTER_CRITICAL();
5227 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
5229 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
5237 taskEXIT_CRITICAL();
5242 #endif /* configUSE_TASK_NOTIFICATIONS */
5243 /*-----------------------------------------------------------*/
5245 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
5247 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
5248 UBaseType_t uxIndexToClear,
5249 uint32_t ulBitsToClear )
5254 /* If null is passed in here then it is the calling task that is having
5255 * its notification state cleared. */
5256 pxTCB = prvGetTCBFromHandle( xTask );
5258 taskENTER_CRITICAL();
5260 /* Return the notification as it was before the bits were cleared,
5261 * then clear the bit mask. */
5262 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
5263 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
5265 taskEXIT_CRITICAL();
5270 #endif /* configUSE_TASK_NOTIFICATIONS */
5271 /*-----------------------------------------------------------*/
5273 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
5275 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
5277 return xIdleTaskHandle->ulRunTimeCounter;
5281 /*-----------------------------------------------------------*/
5283 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
5285 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
5287 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
5289 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE();
5291 /* For percentage calculations. */
5292 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
5294 /* Avoid divide by zero errors. */
5295 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
5297 ulReturn = xIdleTaskHandle->ulRunTimeCounter / ulTotalTime;
5307 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
5308 /*-----------------------------------------------------------*/
5310 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
5311 const BaseType_t xCanBlockIndefinitely )
5313 TickType_t xTimeToWake;
5314 const TickType_t xConstTickCount = xTickCount;
5316 #if ( INCLUDE_xTaskAbortDelay == 1 )
5318 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
5319 * reset to pdFALSE so it can be detected as having been set to pdTRUE
5320 * when the task leaves the Blocked state. */
5321 pxCurrentTCB->ucDelayAborted = pdFALSE;
5325 /* Remove the task from the ready list before adding it to the blocked list
5326 * as the same list item is used for both lists. */
5327 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5329 /* The current task must be in a ready list, so there is no need to
5330 * check, and the port reset macro can be called directly. */
5331 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. */
5335 mtCOVERAGE_TEST_MARKER();
5338 #if ( INCLUDE_vTaskSuspend == 1 )
5340 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
5342 /* Add the task to the suspended task list instead of a delayed task
5343 * list to ensure it is not woken by a timing event. It will block
5345 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
5349 /* Calculate the time at which the task should be woken if the event
5350 * does not occur. This may overflow but this doesn't matter, the
5351 * kernel will manage it correctly. */
5352 xTimeToWake = xConstTickCount + xTicksToWait;
5354 /* The list item will be inserted in wake time order. */
5355 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
5357 if( xTimeToWake < xConstTickCount )
5359 /* Wake time has overflowed. Place this item in the overflow
5361 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5365 /* The wake time has not overflowed, so the current block list
5367 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5369 /* If the task entering the blocked state was placed at the
5370 * head of the list of blocked tasks then xNextTaskUnblockTime
5371 * needs to be updated too. */
5372 if( xTimeToWake < xNextTaskUnblockTime )
5374 xNextTaskUnblockTime = xTimeToWake;
5378 mtCOVERAGE_TEST_MARKER();
5383 #else /* INCLUDE_vTaskSuspend */
5385 /* Calculate the time at which the task should be woken if the event
5386 * does not occur. This may overflow but this doesn't matter, the kernel
5387 * will manage it correctly. */
5388 xTimeToWake = xConstTickCount + xTicksToWait;
5390 /* The list item will be inserted in wake time order. */
5391 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
5393 if( xTimeToWake < xConstTickCount )
5395 /* Wake time has overflowed. Place this item in the overflow list. */
5396 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5400 /* The wake time has not overflowed, so the current block list is used. */
5401 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5403 /* If the task entering the blocked state was placed at the head of the
5404 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
5406 if( xTimeToWake < xNextTaskUnblockTime )
5408 xNextTaskUnblockTime = xTimeToWake;
5412 mtCOVERAGE_TEST_MARKER();
5416 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
5417 ( void ) xCanBlockIndefinitely;
5419 #endif /* INCLUDE_vTaskSuspend */
5422 /* Code below here allows additional code to be inserted into this source file,
5423 * especially where access to file scope functions and data is needed (for example
5424 * when performing module tests). */
5426 #ifdef FREERTOS_MODULE_TEST
5427 #include "tasks_test_access_functions.h"
5431 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
5433 #include "freertos_tasks_c_additions.h"
5435 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
5436 static void freertos_tasks_c_additions_init( void )
5438 FREERTOS_TASKS_C_ADDITIONS_INIT();
5442 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */